package com.sifang.modules.supply.setting.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.setting.entity.CrmSupplyMaterialUnit;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.stereotype.Repository;

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

@Repository
public class CrmSupplyMaterialUnitDao extends BaseDao<CrmSupplyMaterialUnit> {

    public List<Record> materialUnitList(LayuiPage page, Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT m.*,t.name as typeName FROM `crm_supply_material_unit` m ");
        sb.append(" left join crm_supply_material_type t on t.id=m.material_type ");
        sb.append(" WHERE m.status='1' ");
        sb.append(getCondtion(map));
        sb.append(" ORDER BY m.create_time DESC  ");
        sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sb.toString());
    }

    public Long materialUnitListCount(Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" select count(m.id) from `crm_supply_material_unit` m WHERE m.status='1' ");
        sb.append(getCondtion(map));
        return Db.queryLong(sb.toString());
    }

    public String getCondtion(Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        if (StringUtil.checkParamsValid(map.get("name"))) {
            sb.append(" and  m.name like '%"+map.get("name") +"%' ");
        }
        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            sb.append(" and m.create_time >= '" + map.get("createTimeSta") + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            sb.append(" and m.create_time <= '" + map.get("createTimeEnd") + " 23:59:59' ");
        }
        return sb.toString();
    }

    public Boolean checkName(String unit_code,String id){
        StringBuffer sb=new StringBuffer();
        sb.append("   SELECT COUNT(m.id) FROM `crm_supply_material_unit` m WHERE m.status='1' AND m.unit_code=? ");
        if(StringUtil.isNotBlank(id)){
            sb.append(" AND m.id<>'"+ id +"' ");
        }
        long count=Db.queryLong(sb.toString(),unit_code);
        return count>0?true:false;
    }

    // 获取新的单位编码
    public String newUnitCode(){
        StringBuffer sb=new StringBuffer();
        sb.append("   SELECT MAX( CAST(a.`unit_code` AS SIGNED INTEGER) ) AS 'unitCode' FROM crm_supply_material_unit a WHERE 1=1 ");
        Long maxValue = Db.queryLong(sb.toString());
        String newCode = "00001";
        if (maxValue != null) {
            newCode = String.valueOf((maxValue + 1));
        }
        newCode = String.format("%1$5s", newCode).replace(' ', '0');
        return newCode;
    }

    public List<Record> listMaterialUnit(String typeId){
        StringBuffer sb=new StringBuffer();
        sb.append("   SELECT * FROM `crm_supply_material_unit` m WHERE m.status='1' AND m.material_type=? ");
        return Db.find(sb.toString(),typeId);
    }
}
