package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CustomerEntryComponent;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class CustomerEntryComponentDao extends BaseDao<CustomerEntryComponent> {

    /**
     * 获取电站电器材料
     */
    public List<Record> findByEntryId(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.project_id,c.id, c.entry_id, c.specs,c.item_type, c.item_id, c.num, e.model, concat(ifnull(e.material_model,''),'|',e.model) as material_model");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ?", entryId);
        builder.line(" order by c.item_type, CONVERT(e.model, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }
    //方案里面的逆变器配置有2条一模一样的记录 导致物料使用时 小程序识别出问题，根据这种情况后面调整小程序的电站详情接口，合并同物料类型同规格同型号的记录
    public List<Record> findByEntryId2ForSmallPrograms(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.project_id,c.id, c.entry_id, c.specs,c.item_type, c.item_id, SUM(c.num) as num, e.model, concat(ifnull(e.material_model,''),'|',e.model) as material_model");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ?", entryId);
        builder.line(" GROUP BY c.item_type,e.model,material_model   order by c.item_type, CONVERT(e.model, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }
    public List<Record> findByEntryIdByProjectId(String entryId,String projectId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.project_id,c.id, c.entry_id, c.specs,c.item_type, c.item_id, c.num, e.model, concat(ifnull(e.material_model,''),'|',e.model) as material_model");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ? and project_id = ? ", entryId,projectId);
        builder.line(" order by c.item_type, CONVERT(e.model, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }
    //方案里面的逆变器配置有2条一模一样的记录 导致物料使用时 小程序识别出问题，根据这种情况后面调整小程序的电站详情接口，合并同物料类型同规格同型号的记录
    public List<Record> findByEntryIdByProjectIdForSmallPrograms(String entryId,String projectId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.project_id,c.id, c.entry_id, c.specs,c.item_type, c.item_id, SUM(c.num) as num, e.model, concat(ifnull(e.material_model,''),'|',e.model) as material_model");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ? and project_id = ? ", entryId,projectId);
        builder.line("  GROUP BY c.item_type,e.model,material_model   order by c.item_type, CONVERT(e.model, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }

    public List<Record> findByEntryId2(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.id, c.entry_id, c.item_type, c.item_id, c.num, e.model,c.material_model ");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ?", entryId);
        builder.line("and c.item_type = '3' limit 1");
        return Db.find(builder.toString(), builder.params());
    }
    /**
     * 获取电站电器材料
     */
    public Record findZJByEntryId(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.id, c.entry_id, c.specs,c.item_type, c.item_id, c.num ");
        builder.line("from crm_customer_entry_component c ");
        builder.line("where c.status = '0' and c.item_type = '3' and c.entry_id = ?", entryId);
        builder.line("Limit 1 ");
        return Db.findFirst(builder.toString(), builder.params());
    }

    public List<Record> findByEntryId3(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.id, c.entry_id, c.item_type, c.item_id, c.num, c.specs, e.material_model, c.component_shape, c.battery_param, e.unit, e.model,c.project_id ");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ?", entryId);
        return Db.find(builder.toString(), builder.params());
    }

    public List<Record> findByEntryId4(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.num as zjNum, e.model as zjModel,e.code as zjCode ");
        builder.line("from crm_customer_entry_component c left join crm_electric_set e");
        builder.line("on c.item_id = e.id where c.status = '0' and c.entry_id = ?", entryId);
        builder.line("and c.item_type = '3' limit 1");
        return Db.find(builder.toString(), builder.params());
    }

    /**
     * 查询汇总多个电站下单物料信息
     */
    public List<Record> findByEntryIds(List<String> ids) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT any_value(ec.item_id) as item_id, SUM(ec.num) AS num, any_value(es.type) as type, any_value(es.model) as model ");
        sb.line("FROM crm_customer_entry_component ec");
        sb.line("INNER JOIN crm_electric_set es ON es.id = ec.item_id AND es.status = '0'");
        sb.line("WHERE ec.status = '0'");
        sb.inst("AND ec.entry_id IN ", ids);
        sb.line("GROUP BY ec.item_id");
        return Db.find(sb.build(), sb.params());
    }

    /**
     * 电站整改后重新下单，获取上一次下单时数据
     *
     * @param entryId 电站id
     * @param itemId 组件id
     * @return int
     */
    public int findOldNumByEntryIdAndItemId(String entryId, String itemId,Date oldDate) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.num");
        builder.line("from crm_customer_entry_component c");
        builder.line("where c.entry_id = ?", entryId);
        builder.line("and c.item_id = ?", itemId);
        builder.line("and c.status = '1' and c.create_time=?",oldDate);
        builder.line("order by c.create_time asc limit 1");
        Record rd = Db.findFirst(builder.toString(), builder.params());
        if(rd== null){
            return  0;
        }
        return rd.getInt("num");
    }

    public Date findOldDate(String entryId){
        return  Db.queryDate("SELECT create_time FROM  `crm_customer_entry_component` WHERE entry_id =? ORDER BY create_time ASC LIMIT 1",entryId);
    }

    public List<Record> findModelByEntryId(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.id, c.entry_id, c.item_type, c.item_id, c.num, c.specs as model, c.material_model");
        builder.line("from crm_customer_entry_component c ");
        builder.line("where c.status = '0' and c.entry_id = ?", entryId);
        builder.line(" order by c.item_type, CONVERT(c.specs, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }
    public List<Record> findModelByEntryProjectId(String entryId,String projectId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select c.id, c.entry_id, c.item_type, c.item_id, c.num, c.specs as model, c.material_model");
        builder.line("from crm_customer_entry_component c ");
        builder.line("where c.status = '0' and c.entry_id = ?", entryId);
        builder.line(" AND c.project_id = ? ",projectId);
        builder.line(" order by c.item_type, CONVERT(c.specs, SIGNED) ");
        return Db.find(builder.toString(), builder.params());
    }
}
