package com.sifang.modules.sunshine.SystemSetting.dao;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectInstallWay;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectSetting;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hongzq
 */
@Repository
public class ProjectInstallDao extends BaseDao<CrmProjectInstallWay> {

    public List<Record> selectProjectInstalls(LayuiPage<Record> page){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select id, name, status, create_time ")
                .line("from crm_project_install_way")
                .line("where status = '0'");
        builder.line("order by create_time desc")
                .limit(page.getPage(), page.getLimit());
        return Db.find(builder.toString());
    }

    public Long countProjectInstalls(){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(*) ")
                .line("from crm_project_install_way")
                .line("where status = '0'");
        return Db.queryLong(builder.toString());
    }

    public Long countExistsName(String name, String excludeId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select count(*) ")
                .line("from crm_project_install_way")
                .line("where status='0'")
                .line("and name=? ", name);
        if(StrKit.notBlank(excludeId)){
            builder.line("and id != ?", excludeId);
        }
        return Db.queryLong(builder.build(), builder.params());
    }


    /**
     * 获取所有的安装方式
    */
    public List<Record> findProjectInstalls(String id){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select id, name, status, create_time ")
                .line("from crm_project_install_way")
                .line("where status = '0'");
            if(StringUtil.isNotBlank(id)){
                builder.line(" and id <> ?",id);
            }
        builder.line("order by create_time desc");

        return Db.find(builder.toString(),builder.params());
    }

}
