package com.sifang.modules.sunshine.SystemSetting.dao;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmPlanMaterial;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.stereotype.Repository;

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

/**
 * @author hongzq
 */
@Repository
public class PlanMaterialDao extends BaseDao<CrmPlanMaterial> {

    public List<Record> listMaterialByPlanId(String planId) {
        return Db.find("select * from crm_plan_material where plan_id=? order by sort", planId);
    }

    public List<Record> listMaterialByPlanIds(List<String> planIds) {
        SqlBuilder sql = new SqlBuilder();
        sql.inst("select * from crm_plan_material where plan_id in ",planIds);
        sql.line(" order by sort");
        return Db.find(sql.build(),sql.params());
    }

    public List<Record> listMatchedMaterialByPlanId(String entryId) {
        return Db.find("select code,any_value(name) as name,any_value(spec) as spec,any_value(unit) as unit,sum(amount) as amount from crm_plan_material_entry where entry_id=? GROUP BY CODE  order by any_value(sort)", entryId);
    }

    public int deleteByPlanId(String planId) {
        return Db.update("delete from crm_plan_material where plan_id=?", planId);
    }


    public List<Record> listMaterialByAssembly(String assembly,String num) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT b.* FROM crm_electric_plan a ");
        sql.line("LEFT JOIN crm_plan_material b ON a.id = b.plan_id");
        sql.line("WHERE a.assembly=?  AND a.STATUS='0'",assembly);
        sql.line(" AND a.assembly_min<=? and a.assembly_max>=? ",num,num);
        return Db.find(sql.build(),sql.params());
    }

    /**
     * 获取可以唯一匹配的电气方案
    */

    public Record findElectricPlan(String assembly,String num){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT a.* FROM crm_electric_plan a ");
        sql.line("WHERE a.assembly=?  AND a.STATUS='0'",assembly);
        sql.line(" AND a.assembly_min<=? and a.assembly_max>=? ",num,num);
        return Db.findFirst(sql.build(),sql.params());
    }

    /**
     * 获取所有逆变器或者并网箱
    */
    public Map<String,Record> findElectric(String type){
        Map<String,Record> map= Maps.newHashMap();
        List<Record> list = Db.find("SELECT * FROM crm_electric_set WHERE type=? AND status='0'",type);
        for(Record rd:list){
            map.put(rd.getStr("model"),rd);
        }
        return map;
    }

    /**
     * 获取匹配方案
    */
    public List<Record> findComposePlanByEntryId(String entryId ){
        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT a.*,b.attach2,b.attach,b.attach_name,b.attach_name2 FROM crm_compose_plan a ");
        sql.line(" LEFT JOIN crm_plan b ON b.id=a.plan_id");
        sql.line("WHERE a.customer_entry_id=?",entryId);
        return Db.find(sql.build(),sql.params());
    }

}
