package com.sifang.modules.completionrectification.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.entity.CrmRectificationInfoDetail;
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 RectificationInfoDetailDao extends BaseDao<CrmRectificationInfoDetail>  {

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

    public List<Record> getRectificationDetailByInputPieceId(String inputPieceId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * FROM crm_rectification_info_detail ");
        sql.append(" WHERE input_piece_id = "+"'"+inputPieceId+"'");
        return Db.find(sql.toString());
    }

    public List<Record> getRectificationDetailByRectificationNo(String rectificationNo) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT id FROM crm_rectification_info_detail ");
        sql.append(" WHERE rectification_no = "+"'"+rectificationNo+"'");
        return Db.find(sql.toString());
    }

    public List<Record> getRectificationResultList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT e.power_station_code,e.create_trader_name,d.input_piece_id,r.id,r.rectification_no,r.rectification_status,r.completion_no, ")
                .line(" r.create_time,r.fianl_update_time,r.completion_time,COUNT(d.id) recount,c.completion_status ")
                .line(" FROM crm_customer_entry e ")
                .line(" LEFT JOIN crm_customer_incoming_application a ON a.entry_id = e.id ")
                .line(" LEFT JOIN crm_rectification_info_detail d ON d.input_piece_id = a.input_piece_id ")
                .line(" LEFT JOIN crm_rectification_info r ON r.rectification_no = d.rectification_no  ")
                .line(" LEFT JOIN crm_completion_info c ON c.completion_no = r.completion_no ")
                .line(" WHERE 1 =1 ")
                .line(" AND a.input_piece_id IN (SELECT input_piece_id FROM crm_rectification_info_detail) ");
        appendCondition(builder, map);
        builder.line(" GROUP BY e.id ");
        builder.limit(page.getPage(), page.getLimit());
        List<Record> records = Db.find(builder.build(), builder.params());
        ItemDispatchUtil.setReviewFlag(records);
        return records;
    }

    public Long countRectificationResultList(Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT COUNT(*) FROM ( ")
                .line(" SELECT e.id FROM crm_customer_entry e ")
                .line(" LEFT JOIN crm_customer_incoming_application a ON a.entry_id = e.id  ")
                .line(" LEFT JOIN crm_rectification_info_detail d ON d.input_piece_id = a.input_piece_id  ")
                .line(" LEFT JOIN crm_rectification_info r ON r.rectification_no = d.rectification_no  ")
                .line(" LEFT JOIN crm_completion_info c ON c.completion_no = r.completion_no ")
                .line(" WHERE 1 =1  ")
                .line(" AND a.input_piece_id IN (SELECT input_piece_id FROM crm_rectification_info_detail ) ");
        appendCondition(builder, map);
        builder.line(" GROUP BY e.id)s ");
        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 e.create_trader_id=?", 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"));
        }

    }

}