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.CrmSupplyWarehouse;
import com.sifang.modules.sys.common.persistence.BaseDao;
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 CrmSupplyWarehouseDao extends BaseDao<CrmSupplyWarehouse> {

    public List<Record> getList(LayuiPage page, Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" SELECT w.* ");
        sb.append(" FROM `crm_supply_warehouse` w  ");
        sb.append(" WHERE w.status<>'2'  ");
        sb.append(getCondition(map));
        sb.append(" ORDER BY w.create_time DESC  ");
        sb.append(" limit " + page.getLimit() + " offset " + (page.getPage() - 1) * page.getLimit());
        return Db.find(sb.toString());
    }

    /**
     * 判断外部编码是否唯一
     * @param qqCode
     * @param id
     * @return
     */

    public boolean checkQqCode(String qqCode,String id){
        SqlBuilder sql = new SqlBuilder();
        sql.line(" SELECT COUNT(*) FROM crm_supply_warehouse WHERE qq_code= ? AND status <>'2'",qqCode);
        if (StringUtil.isNotBlank(id)){
            sql.line(" and id <> ?",id);
        }
       return Db.queryLong(sql.build(),sql.params())>0;
    }


    public Long countList(Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        sb.append(" select count(w.id) ");
        sb.append(" FROM `crm_supply_warehouse` w  ");
        sb.append(" WHERE w.status<>'2' ");
        sb.append(getCondition(map));
        return Db.queryLong(sb.toString());
    }

    public String getCondition(Map<String, String> map){
        StringBuffer sb=new StringBuffer();
        if(StringUtil.checkParamsValid(map.get("ascriptionId"))){
            sb.append(" and  w.ascription_id ='"+map.get("ascriptionId") +"' ");
        }
        if (StringUtil.checkParamsValid(map.get("name"))) {
            sb.append(" and  w.name like '%"+map.get("name") +"%' ");
        }
        if (StringUtil.checkParamsValid(map.get("type"))) {
            sb.append(" and  w.type ='"+map.get("type") +"' ");
        }
        if (StringUtil.checkParamsValid(map.get("traderId"))) {
            sb.append(" and  w.ascription_id ='"+map.get("traderId") +"' ");
        }
        if (StringUtil.checkParamsValid(map.get("supplyId"))) {
            sb.append(" and  w.ascription_id ='"+map.get("supplyId") +"' ");
        }
        if (StringUtil.checkParamsValid(map.get("personInCharge"))) {
            sb.append(" and  w.person_in_charge like '%"+map.get("personInCharge") +"%' ");
        }
        if (StringUtil.checkParamsValid(map.get("mobile"))) {
            sb.append(" and  w.mobile like '%"+map.get("mobile") +"%' ");
        }
        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            sb.append(" and w.create_time >= '" + map.get("createTimeSta") + " 00:00:00' ");
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            sb.append(" and w.create_time <= '" + map.get("createTimeEnd") + " 23:59:59' ");
        }

        return sb.toString();
    }

    public static String getWatehouseName(String warehouseId){
       return Db.queryStr("  SELECT NAME FROM `crm_supply_warehouse` WHERE id=? ",warehouseId);
    }

    public List<Record> findByType(String type) {
        return Db.find("select * from crm_supply_warehouse where status = '0' and type = ? order by create_time desc", type);
    }

    public List<Record> findByTypeAndAscriptionId(String type, String ascriptionId) {
        return Db.find("select * from crm_supply_warehouse where status = '0' and type = ? and ascription_id = ? order by create_time desc", type, ascriptionId);
    }
}
