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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderContract;
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;

@Repository
public class CrmTraderContractDao extends BaseDao<CrmTraderContract> {

    public CrmTraderContract getByContractId(String contractId){
        return getByHql("FROM CrmTraderContract WHERE contractId = :p1", new Parameter(contractId));
    }

    public List<Record> listDate(LayuiPage<Record> page, Map<String, String> map){

        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT c.id,c.trader_id,c.status,c.contract_number,c.start_time,c.end_time ")
                .line(",c.sign_finish_time,c.contract_url,c.contract_id,c.contract_signed_path ")
                .line(",c.create_time,c.create_name,c.update_time,c.update_name,t.name AS traderName")
                .line("FROM `crm_trader_contract` c ")
                .line("LEFT JOIN `crm_trader` t ON c.`trader_id`=t.id ")
                .line("WHERE c.status ");
        appendCondition(builder, map);
        builder.line("order by c.create_time desc");
        if (page != null) {
            builder.limit(page.getPage(), page.getLimit());
            return Db.find(builder.build(), builder.params());
        }
        return Db.find(builder.build(), builder.params());
    }

    public Long countList(Map<String, String> map){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" SELECT COUNT(c.id) FROM `crm_trader_contract` c  ");
        sqlBuilder.line(" LEFT JOIN `crm_trader` t  ON c.`trader_id`=t.id ");
        sqlBuilder.line(" WHERE c.status <>'0' ");
        appendCondition(sqlBuilder, map);
        return Db.queryLong(sqlBuilder.build(),sqlBuilder.params());
    }

    public void appendCondition(SqlBuilder builder,Map<String, String> map){
        if (StringUtil.checkParamsValid(map.get("traderName"))) {
            builder.like("and t.name like ? ", map.get("traderName").trim());
        }
        if(StringUtil.checkParamsValid(map.get("status"))){
              builder.line(" and c.status = ? ",map.get("status"));
        }
    }

    public Long getSameContractNumber(String contractNumber,String id){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" select count(id) from crm_trader_contract t ");
        sqlBuilder.line("  WHERE t.status ='4' and t.contract_number=? ",contractNumber);
        if(StringUtil.isNotBlank(contractNumber)){
            sqlBuilder.line(" and t.id<> ?",id);
        }
        return Db.queryLong(sqlBuilder.build(),sqlBuilder.params());
    }
}
