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.CrmSupplyMaterialBrand;
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 org.springframework.stereotype.Repository;

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

@Repository
public class CrmSupplyMaterialBrandDao extends BaseDao<CrmSupplyMaterialBrand> {

    public List<Record> materialBrandList(LayuiPage page, Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT m.*,t.name AS typeName FROM `crm_supply_material_brand` m ");
        sb.append(" LEFT JOIN crm_supply_material_type t ON t.id = m.type_id");
        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 materialBrandListCount(Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" select count(m.id) from `crm_supply_material_brand` 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 name, String id, String typeId) {
        SqlBuilder sb = new SqlBuilder();
        sb.line("SELECT COUNT(m.id)");
        sb.line("FROM crm_supply_material_brand m");
        sb.line("WHERE m.status = '1' AND m.name = ?", name);
        sb.line("AND m.type_id = ?", typeId);
        if (StringUtil.isNotBlank(id)) {
            sb.line("AND m.id <> ?", id);
        }
        return Db.queryLong(sb.build(), sb.params()) > 0;
    }

    public List<Record> getAllMaterialBrand(String typeId) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select m.id,m.name");
        sql.line("from crm_supply_material_brand m");
        sql.line("where m.status='1'");
        if (StringUtil.isNotBlank(typeId)) {
            sql.line("and m.type_id = ?", typeId);
        }
        return Db.find(sql.build(), sql.params());
    }

}
