package com.sifang.modules.contract.dao;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.contract.entity.TraderContractTemplate;
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.Date;
import java.util.List;
import java.util.Map;

@Repository
public class TraderContractTemplateDao extends BaseDao<TraderContractTemplate> {


  public List<Record> contractTemplateList(LayuiPage<Record> page, Map<String, String> map){
      SqlBuilder sf=new SqlBuilder();
      sf.line(" SELECT t.id,t.company_id,t.type,t.contract_name,t.doc_template_id,DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i:%s')AS createTime ,t.status ");
      sf.line(" ,r.name as patern,r.id as partner_id ,s.name as companyName,t.filing_type,t.position_page,t.position_across,t.position_vertical ,t.position_page2,t.position_across2,t.position_vertical2 FROM `crm_trader_contract_template` t  ");
      sf.line(" left join  crm_project_company_settings s on t.company_id=s.id ");
      sf.line(" left join  crm_partner r on t.patern=r.id ");
      sf.line(" WHERE 1=1 ");
      getConditionMap(map, sf);
      sf.line(" ORDER BY t.create_time DESC  ");
      if (page != null) {
          sf.line(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
      }
      List<Record> list = Db.find(sf.build(), sf.params());
      return list;
  }


  public long getContractTemplateCount(Map<String, String> map){
      SqlBuilder sf=new SqlBuilder();
      sf.line(" SELECT COUNT(t.id) FROM  `crm_trader_contract_template` t ");
      sf.line(" WHERE 1=1 ");
      getConditionMap(map, sf);
      return Db.queryLong(sf.build(), sf.params());
  }

    public void getConditionMap(Map<String, String> map, SqlBuilder sf) {
        if (StringUtil.isNotBlank(map.get("createTimeSta"))) {
            sf.line("AND t.create_time >= ?", map.get("createTimeSta") + " 00:00:00");
        }
        if (StringUtil.isNotBlank(map.get("createTimeEnd"))) {
            sf.line("AND t.create_time <= ?", map.get("createTimeEnd") + " 23:59:59");
        }
    }

    public long checkTemplate(String companyId,String type,String patern,String filingType){
      StringBuffer sb=new StringBuffer();
      sb.append(" SELECT COUNT(t.id) FROM `crm_trader_contract_template` t WHERE t.status='0' and t.type=? and t.patern=?");
      if(StringUtil.isNotBlank(companyId)){
          sb.append(" and t.company_id='"+companyId+"' ");
      }
      if(StringUtil.isNotBlank(filingType)){
        sb.append(" and t.filing_type='"+filingType+"' ");
      }
      return Db.queryLong(sb.toString(),type,patern);
    }

    public void deleteContractTemplate(String id){
      Db.update(" UPDATE `crm_trader_contract_template` SET status='1' WHERE id=? ",id);
    }

    /**
     * 获取项目公司模板
     */
    public TraderContractTemplate findOneByCompanyId(String companyId) {
        return getByHql("from TraderContractTemplate where status = '0' and company_id =:p1", new Parameter(companyId));
    }

    /**
     * 获取农户合同模板
     */
    public TraderContractTemplate getTemplateByPartnerId(String partnerId,String filingType) {
        return getByHql("from TraderContractTemplate where status = '0' and patern =:p1 and filing_type=:p2 ", new Parameter(partnerId,filingType));
    }


}
