package com.sifang.modules.completionrectification.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.completionrectification.entity.CrmRectificationInfo;
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.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.SqlBuilder;
import com.sifang.modules.taskdispatch.util.ItemDispatchUtil;
import org.springframework.stereotype.Repository;

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

@Repository
public class CompletionDao extends BaseDao<CrmCompletionInfo> {

    public List<Record> getPowerStationByIds(String[] ids){
        List<Record> list = Db.find("SELECT power_station_code,project_name,create_trader_name,creator,project_company_name,status,filing_type FROM crm_customer_entry WHERE id IN("+ StringUtil.joinInSql(ids)+") ");
        return list;
    }

    public List<Record> getPowerStationByCode(String[] codes){
        List<Record> list = Db.find("SELECT power_station_code,project_name,create_trader_name,creator,project_company_name,status,filing_type FROM crm_customer_entry WHERE power_station_code IN("+ StringUtil.joinInSql(codes)+") ");
        return list;
    }

    public Record getCrmCompletionInfoByEntryId(String entryId){
        Record record = Db.findFirst("SELECT * FROM crm_completion_info WHERE entry_id LIKE '%" + entryId + "%' ");
        return record;
    }

    public Record getCrmCompletionInfoById(String id){
        Record record = Db.findFirst("SELECT * FROM crm_completion_info WHERE id = "+"'"+id+"'");
        return record;
    }

    public List<Record> getCompletionOrderList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT c.id,c.completion_no,c.power_station_count,c.project_company_name,c.create_time,c.entry_id, ")
                .line(" c.create_trader_name,concat(c.province, c.city, c.area, c.address) as address,c.completion_status, ")
                .line(" r.rectification_no,r.rectification_status ")
                .line(" FROM crm_completion_info c ")
                .line(" LEFT JOIN crm_rectification_info r ON r.completion_no = c.completion_no ")
                .line(" WHERE 1 =1  ");
        if (StringUtil.checkParamsValid(map.get("projectCompanyName"))) {
            builder.like("and c.project_company_name like ?", map.get("projectCompanyName"));
        }
        if (StringUtil.checkParamsValid(map.get("completionNo"))) {
            builder.like("and c.completion_no like ?", map.get("completionNo"));
        }
        if (StringUtil.checkParamsValid(map.get("rectificationNo"))) {
            builder.like("and c.rectification_no like ?", map.get("rectificationNo"));
        }
        if (StringUtil.checkParamsValid(map.get("traderId"))) {
            builder.line("and c.traderId=?", map.get("traderId"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            builder.line("and c.create_time >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and c.create_time <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }
        if (StringUtil.checkParamsValid(map.get("completionStatus"))) {
            builder.line("and c.completion_status=?", map.get("completionStatus"));
        }

        builder.line("order by c.create_time desc");
        builder.limit(page.getPage(), page.getLimit());
        List<Record> records = Db.find(builder.build(), builder.params());
        ItemDispatchUtil.setReviewFlag(records);
        return records;
    }

    public Long countCompletionOrderList(Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT count(1) ")
                .line(" FROM crm_completion_info c ")
                .line(" LEFT JOIN crm_rectification_info r ON r.completion_no = c.completion_no ")
                .line(" WHERE 1 =1  ");
        if (StringUtil.checkParamsValid(map.get("projectCompanyName"))) {
            builder.like("and c.project_company_name like ?", map.get("projectCompanyName"));
        }
        if (StringUtil.checkParamsValid(map.get("completionNo"))) {
            builder.like("and c.completion_no like ?", map.get("completionNo"));
        }
        if (StringUtil.checkParamsValid(map.get("rectificationNo"))) {
            builder.like("and c.rectification_no like ?", map.get("rectificationNo"));
        }
        if (StringUtil.checkParamsValid(map.get("traderId"))) {
            builder.line("and c.traderId=?", map.get("traderId"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            builder.line("and c.create_time >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and c.create_time <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }
        builder.line("order by c.create_time desc");
        return Db.queryLong(builder.build(), builder.params());
    }

    public CrmCompletionInfo findCompletionByCompletionNo(String completionNo) {
        return getByHql(" from CrmCompletionInfo where completion_no =:p1", new Parameter(completionNo));
    }

}