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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmElectricSet;
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.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ElectricSetDao extends BaseDao<CrmElectricSet> {


    public Record getElectricMaterielSetDetail(String id){
        return Db.findFirst(" SELECT * FROM `crm_electric_set` WHERE id=? ",id);
    }

    public List<Record> findAllElectric() {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select * from crm_electric_set s where status = '0' and s.type in ('1','2','3','4') order by s.type, CONVERT(s.model, SIGNED)");
        return Db.find(builder.toString());
    }

    public List<Record> findAllInverter() {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select * from crm_electric_set s where status = '0' and s.type = '1' GROUP BY s.model order by CONVERT(s.model, SIGNED)");
        return Db.find(builder.toString());
    }
    public List<Record> findAllInverter2() {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select * from crm_electric_set s where status = '0' and s.type = '1' GROUP BY s.model,s.material_model order by CONVERT(s.model, SIGNED)");
        return Db.find(builder.toString());
    }

    public List<Record> findAllInverterGF() {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select s.id,s.code,s.type,s.`name`,s.model,s.unit,s.`status`,s.remarks,s.create_by,s.create_time,s.update_by,s.update_time,concat(IFNULL(s.material_model,''),'|',s.model) as material_model from crm_electric_set s where status = '0' and s.type = '1' GROUP BY s.model order by CONVERT(s.model, SIGNED)");
        return Db.find(builder.toString());
    }

    public List<Record> findAllInverterGF2() {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select s.id,s.code,s.type,s.`name`,s.model,s.unit,s.`status`,s.remarks,s.create_by,s.create_time,s.update_by,s.update_time,concat(IFNULL(s.material_model,''),'|',s.model) as material_model from crm_electric_set s where status = '0' and s.type = '1' GROUP BY s.model,s.material_model order by CONVERT(s.model, SIGNED)");
        return Db.find(builder.toString());
    }

    public List<Record> findByIds(List<String> ids) {
        SqlBuilder builder = new SqlBuilder();
        builder.inst("select * from crm_electric_set where id in ", ids);
        return Db.find(builder.toString(), builder.params());
    }

    public CrmElectricSet getMaterialSet(String materialType,String model,String materialModel) {
        if (StringUtil.isBlank(materialModel)) {
            return getByHql(" from CrmElectricSet where type=:p1 and model=:p2 and material_model is null and status='0' ",
                    new Parameter(materialType, model));
        } else {
            return getByHql(" from CrmElectricSet where type=:p1 and model=:p2 and material_model=:p3 and status='0' ",
                    new Parameter(materialType, model, materialModel));
        }
    }

    public long checkMaterialSet(String materialType,String model,String materialModel) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COUNT(m.id) ");
        sb.append("FROM `crm_electric_set` m ");
        sb.append("WHERE m.status='0' AND m.type=? AND m.model=? ");
        if (StringUtil.isNotBlank(materialModel)) {
            sb.append(" and m.material_model = '" + materialModel + "' ");
        }
        else {
            sb.append(" and m.material_model is null");
        }
        return Db.queryLong(sb.toString(),materialType, model);
    }
}
