package com.sifang.modules.completionrectification.dao;

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

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

@Repository
public class RectificationDao extends BaseDao<CrmRectificationInfo> {

    public List<Record> getRectificationOrderList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT r.id,r.rectification_no,r.rectification_status,r.create_time,r.completion_time,COUNT(d.id) recount, ")
                .line(" c.completion_no,c.power_station_count,c.project_company_name,c.entry_id,c.create_trader_name ")
                .line(" FROM crm_rectification_info r ")
                .line(" LEFT JOIN crm_rectification_info_detail d ON d.rectification_no = r.rectification_no ")
                .line(" LEFT JOIN crm_completion_info c ON r.completion_no = c.completion_no ")
                .line(" WHERE 1 =1  ");
        appendCondition(builder, map);
        builder.line(" GROUP BY d.rectification_no 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 countRectificationOrderList(Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT count(DISTINCT  d.rectification_no) ")
                .line(" FROM crm_rectification_info r ")
                .line(" LEFT JOIN crm_rectification_info_detail d ON d.rectification_no = r.rectification_no ")
                .line(" LEFT JOIN crm_completion_info c ON r.completion_no = c.completion_no ")
                .line(" WHERE 1 =1  ");
        appendCondition(builder, map);
        return Db.queryLong(builder.build(), builder.params());
    }

    private void appendCondition(SqlBuilder builder, Map<String, String> map) {
        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 r.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 r.completion_time >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            builder.line("and r.completion_time <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }
        if (StringUtil.checkParamsValid(map.get("rectificationStatus"))) {
            builder.line("AND r.rectification_status=?", map.get("rectificationStatus"));
        }
    }

    public CrmRectificationInfo findRectificationById(String id) {
        return getByHql(" from CrmRectificationInfo where id =:p1", new Parameter(id));
    }

    public CrmRectificationInfo findRectificationByRectificationNo(String rectificationNo) {
        return getByHql(" from CrmRectificationInfo where rectification_no =:p1", new Parameter(rectificationNo));
    }

    public List<Record> getRectificationOrderStationList(String[] inputPieceIds) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT e.id,e.power_station_code,e.project_company_name,CONCAT(e.province,e.area,e.tenant_name,ROUND(e.design_capacity / 1000,2),'KW光伏电站') AS stationName,a.input_piece_id ");
        sql.append(" FROM crm_customer_entry e ");
        sql.append(" LEFT JOIN crm_customer_incoming_application a ON a.entry_id = e.id  ");
        sql.append(" WHERE 1 = 1  ");
        sql.append(" AND a.input_piece_id IN (" + StringUtil.joinInSql(inputPieceIds) + ") ");
        return Db.find(sql.toString());
    }

    public Record getRectificationOrderDetail(String id) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT r.id,r.rectification_no,r.rectification_status,r.create_time,r.completion_time,COUNT(d.id) recount, ")
                .line(" r.completion_time,r.completion_no")
                .line(" FROM crm_rectification_info r ")
                .line(" LEFT JOIN crm_rectification_info_detail d ON d.rectification_no = r.rectification_no ")
                .line(" WHERE r.id = "+"'"+id+"'");
        return Db.findFirst(builder.toString());
    }

}