package com.sifang.modules.taskdispatch.dao;

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

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

@Repository
public class CrmPriorReviewSettingDao extends BaseDao<CrmPriorReviewSetting> {

    public List<Record> getPriorReviewSettingList(LayuiPage<Record> page, Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT s.id,r.`name` AS traderName ,s.`status`,s.finally_update_time,s.trader_id, ")
                .line(" GROUP_CONCAT(IF(m.flag = '1', CONCAT( m.group_name, ' - ', e.`name` ), NULL)) AS reviewer1, ")
                .line(" GROUP_CONCAT(IF(m.flag = '2', CONCAT( m.group_name, ' - ', e.`name` ), NULL)) AS reviewer2, ")
                .line(" GROUP_CONCAT(IF(m.flag = '3', CONCAT( m.group_name, ' - ', e.`name` ), NULL)) AS reviewer3 ")
                .line(" FROM crm_prior_review_setting s ")
                .line(" LEFT JOIN crm_prior_review_setting_member m ON m.review_setting_id = s.id ")
                .line(" LEFT JOIN crm_trader r ON r.id = s.trader_id ")
                .line(" LEFT JOIN crm_platform_employee e ON e.id = m.employee_id ")
                .line(" WHERE 1 =1  ");
        appendCondition(builder, map);
        builder.line(" GROUP BY s.trader_id ORDER BY s.create_time ");
        builder.limit(page.getPage(), page.getLimit());
        List<Record> records = Db.find(builder.build(), builder.params());
        ItemDispatchUtil.setReviewFlag(records);
        return records;
    }

    public Long countPriorReviewSetting(Map<String, String> map) {
        SqlBuilder builder = new SqlBuilder();
        builder.line(" SELECT count(*) FROM (SELECT s.* ")
                .line(" FROM crm_prior_review_setting s ")
                .line(" LEFT JOIN crm_prior_review_setting_member m ON m.review_setting_id = s.id ")
                .line(" LEFT JOIN crm_trader r ON r.id = s.trader_id ")
                .line(" LEFT JOIN crm_platform_employee e ON e.id = m.employee_id ")
                .line(" WHERE 1 =1  ");
        appendCondition(builder, map);
        builder.line(" GROUP BY s.trader_id ) k ");
        return Db.queryLong(builder.build(), builder.params());
    }

    private void appendCondition(SqlBuilder builder, Map<String, String> map) {
        if (StringUtil.checkParamsValid(map.get("traderId"))) {
            builder.line("AND s.trader_id = ?", map.get("traderId"));
        }
        if (StringUtil.checkParamsValid(map.get("status"))) {
            builder.line("AND s.`status` = ?", map.get("status"));
        }
    }

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

    public void deleteById(String id) {
        StringBuffer sb = new StringBuffer();
        sb.append("DELETE FROM crm_prior_review_setting WHERE id = ? ");
        Db.update(sb.toString(), id);
    }

    public CrmPriorReviewSetting findReviewSettingByTraderIdStatus(String traderId) {
        return getByHql(" from CrmPriorReviewSetting where trader_id =:p1 and status ='0'", new Parameter(traderId));
    }

    public CrmPriorReviewSetting findReviewSettingByTraderId(String traderId) {
        return getByHql(" from CrmPriorReviewSetting where trader_id =:p1 ", new Parameter(traderId));
    }

}
