package com.sifang.modules.supply.warehouse.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.warehouse.entity.CrmSupplyPurchaseOrder;
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 CrmSupplyPurchaseOrderDao extends BaseDao<CrmSupplyPurchaseOrder> {


    public List<Record> getList(LayuiPage<Record> page, Map<String, String> map){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" SELECT p.* ,s.name AS supplierName,w.name AS warehouseName ")
                  .line(" FROM `crm_supply_purchase_order` p  ")
                  .line(" LEFT JOIN `crm_supply_supplier` s ON s.id=p.supplier_id ")
                  .line(" LEFT JOIN `crm_supply_warehouse` w ON w.id=p.warehouse_id ")
                  .line(" WHERE p.status<>'0' ");
        appendCondition(map,sqlBuilder);
        sqlBuilder.line("order by p.create_time desc");
        if (page != null) {
            sqlBuilder.limit(page.getPage(), page.getLimit());
        }
        return Db.find(sqlBuilder.build(), sqlBuilder.params());
    }

    public Long countList(Map<String, String> map){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" SELECT count(p.id) ")
                .line(" FROM `crm_supply_purchase_order` p  ")
                .line(" LEFT JOIN `crm_supply_supplier` s ON s.id=p.supplier_id ")
                .line(" LEFT JOIN `crm_supply_warehouse` w ON w.id=p.warehouse_id ")
                .line(" WHERE p.status<>'0' ");
        appendCondition(map,sqlBuilder);
        return Db.queryLong(sqlBuilder.build(),sqlBuilder.params());
    }

    public void appendCondition(Map<String, String> map,SqlBuilder builder){
        if(StringUtil.checkParamsValid(map.get("supplierId"))){
            builder.line("and p.supplier_id =? and p.status<>'5' ", map.get("supplierId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("number"))){
           /* builder.line("and p.number like concat('%', ?, '%')", map.get("number").trim());*/
            builder.line("and p.number =? ", map.get("number").trim());
        }
        if(StringUtil.checkParamsValid(map.get("spaNumber"))){
           /* builder.line("and p.spa_number like concat('%', ?, '%')", map.get("spaNumber").trim());*/
            builder.line("and p.spa_number = ? ", map.get("spaNumber").trim());
        }
        if(StringUtil.checkParamsValid(map.get("creator"))){
            builder.line("and p.creator like concat('%', ?, '%')", map.get("creator").trim());
        }
        if(StringUtil.checkParamsValid(map.get("status"))){
            builder.line("and p.status = ? ", map.get("status").trim());
        }
        if(StringUtil.checkParamsValid(map.get("warehouseId"))){
            builder.line("and p.warehouse_id = ? ", map.get("warehouseId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("remark"))){
            builder.line("and p.remark like concat('%', ?, '%')", map.get("remark").trim());
        }
        if(StringUtil.checkParamsValid(map.get("supplierName"))){
            builder.line("and s.name like concat('%', ?, '%')", map.get("supplierName").trim());
        }
        if(StringUtil.checkParamsValid(map.get("examineRemark"))){
            builder.line("and p.examine_remark like concat('%', ?, '%')", map.get("examineRemark").trim());
        }

        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            builder.line("and p.create_time >= concat(?, ' 00：00：00')", map.get("createTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            builder.line("and p.create_time <= concat(?, ' 23：59：59')", map.get("createTimeEnd").trim());
        }


        if (StringUtil.checkParamsValid(map.get("confirmTimeSta"))) {
            builder.line("and p.confirm_time >= concat(?, ' 00：00：00')", map.get("confirmTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("confirmTimeEnd"))) {
            builder.line("and p.confirm_time <= concat(?, ' 23：59：59')", map.get("confirmTimeEnd").trim());
        }


        if (StringUtil.checkParamsValid(map.get("examineTimeSta"))) {
            builder.line("and p.inspection_time >= concat(?, ' 00：00：00')", map.get("examineTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("examineTimeEnd"))) {
            builder.line("and p.inspection_time <= concat(?, ' 23：59：59')", map.get("examineTimeEnd").trim());
        }

        if (StringUtil.checkParamsValid(map.get("finishTimeSta"))) {
            builder.line("and p.finish_time >= concat(?, ' 00：00：00')", map.get("finishTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("finishTimeEnd"))) {
            builder.line("and p.finish_time <= concat(?, ' 23：59：59')", map.get("finishTimeEnd").trim());
        }
    }

    public Boolean checkSpaNumber(String spaNumber,String id){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" SELECT COUNT(p.id) FROM `crm_supply_purchase_order` p WHERE p.status<>'0' AND p.spa_number=? ",spaNumber);
        if(StringUtil.isNotBlank(id)){
            sqlBuilder.line(" AND p.id<>? ",id);
        }
        Long count=Db.queryLong(sqlBuilder.build(),sqlBuilder.params());
        return count>0 ?true:false;
    }

    public Record statisticOrder(List<String> ascriptionId) {

        SqlBuilder sql = new SqlBuilder();
        sql.line("SELECT COUNT(CASE WHEN o.`status` = '1' THEN 0 END) AS dqr,");
        sql.line("COUNT(CASE WHEN o.`status` = '2' THEN 0 END) AS djy,");
        sql.line("COUNT(CASE WHEN o.`status` = '3' THEN 0 END) AS bfjy");
        sql.line("FROM crm_supply_purchase_order o WHERE 1=1");
        sql.inst("and o.`supplier_id` in ",ascriptionId);
        return Db.findFirst(sql.build(),sql.params());
    }
}
