package com.sifang.modules.insurance.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.insurance.entity.CrmInsuranceList;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author DQChen
 * @date 2024-01-18 9:52
 */
@Repository
public class InsuranceListDao extends BaseDao<CrmInsuranceList> {


	/**
	 * 保险公司列表
	 */
	public List<Record> list(Map<String, String> queryParams, LayuiPage page) {
		SqlBuilder builder = new SqlBuilder();
		builder.line("select id,name,type,company_id,company_name,status,create_time")
				.line(",(select name from crm_project_company_settings where id=a.policyholder) as policyholder")
				.line(",(select name from crm_partner where id=a.insurant) as insurant")
				.line("from crm_insurance_list a")
				.line("where status in('0','1')")
				.line("order by create_time asc");
		builder.limit(page.getPage(), page.getLimit());
		return Db.find(builder.build(), builder.params());
	}

	/**
	 * 保险公司数量
	 */
	public Long countList(Map<String, String> queryParams) {
		SqlBuilder builder = new SqlBuilder();
		builder.line("select count(1)")
				.line("from crm_insurance_list")
				.line("where status in('0','1')");
		return Db.queryLong(builder.build(), builder.params());
	}

	public List<Record> insuranceList(){
		StringBuilder sb = new StringBuilder();
		sb.append("select a.id,a.name,a.type,a.company_name,b.name as policyholder,c.name as insurant");
		sb.append(" from crm_insurance_list a");
		sb.append(" left join crm_project_company_settings b on a.policyholder=b.id");
		sb.append(" left join crm_partner c on a.insurant=c.id");
		sb.append(" where a.status='0' order by a.create_time desc");
		return Db.find(sb.toString());
	}

	//修改
	public List<Record> insuranceLists(String id){
		StringBuilder sf = new StringBuilder();
		sf.append("select a.id,a.name,a.type,a.company_name,b.name as policyholder,c.name as insurant");
		sf.append(" from crm_insurance_list a");
		sf.append(" left join crm_project_company_settings b on a.policyholder=b.id");
		sf.append(" left join crm_partner c on a.insurant=c.id");
		sf.append(" where a.status='0'  and b.id = ?  order by a.create_time desc");
		return Db.find(sf.toString(), id);

	}


	public Record selectDetailById(String id) {
		SqlBuilder builder = new SqlBuilder();
		builder.line("select id,name,type,company_id,company_name,status,create_time")
				.line(",(select name from crm_project_company_settings where id=a.policyholder) as policyholder")
				.line(",(select name from crm_partner where id=a.insurant) as insurant")
				.line("from crm_insurance_list a")
				.line("where status in('0','1') and id=?", id);
		return Db.findFirst(builder.build(), builder.params());
	}


	public long queryCountSame(String policyholder, String type,String name, String id) {
		SqlBuilder sb = new SqlBuilder();
		sb.line("select count(1) from crm_insurance_list");
	//	sb.line("where status in('0','1') and policyholder=? and type=? and name=?", policyholder, type, name);
		sb.line("where status in('0','1') and policyholder=? and name=?", policyholder, name);
		if (StringUtil.isNotBlank(id)) {
			sb.line(" and id !=?", id);
		}
		return Db.queryLong(sb.build(), sb.params());
	}

	public CrmInsuranceList findInsuranceByName(String name) {
		return getByHql(" from CrmInsuranceList where name =:p1", new Parameter(name));
	}

}
