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.CrmSupplyPurchaseOrderInspection;
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 CrmSupplyPurchaseOrderInspectionDao extends BaseDao<CrmSupplyPurchaseOrderInspection> {

    public List<Record> getList(LayuiPage<Record> page, Map<String, String> map,List<String> supplierIds){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line("  SELECT i.id,i.purchase_id,i.create_time,s.name as supplier_name,o.supplier_id,i.finish_time,i.status,i.warehouse_name,i.remark,o.spa_number,o.number,o.remark AS orderRemark,o.inspection_time ")
                .line(" FROM `crm_supply_purchase_order_inspection` i  ")
                .line("  LEFT JOIN `crm_supply_purchase_order` o ON i.purchase_id=o.id ")
                .line(" LEFT JOIN crm_supply_supplier s ON s.id=o.supplier_id ")
                .line("  WHERE i.status<>'0'  ")
                .line("  ");
        if(supplierIds!= null && supplierIds.size()>0){
            sqlBuilder.inst("and o.supplier_id in ",supplierIds);
        }
        appendCondition(map,sqlBuilder);
        sqlBuilder.line("order by i.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,List<String> supplierIds){
        SqlBuilder sqlBuilder=new SqlBuilder();
        sqlBuilder.line(" SELECT count(i.id) ")
                .line(" FROM `crm_supply_purchase_order_inspection` i  ")
                .line("  LEFT JOIN `crm_supply_purchase_order` o ON i.purchase_id=o.id ")
                .line(" WHERE i.status<>'0' ");
        if(supplierIds!= null &&supplierIds.size()>0){
            sqlBuilder.inst("and o.supplier_id in ",supplierIds);
        }
        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 o.supplier_id =? ", map.get("supplierId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("number"))){
             builder.line("and o.number like concat('%', ?, '%')", map.get("number").trim());
        }
        if(StringUtil.checkParamsValid(map.get("spaNumber"))){
             builder.line("and o.spa_number like concat('%', ?, '%')", map.get("spaNumber").trim());
        }
        if(StringUtil.checkParamsValid(map.get("orderRemark"))){
            builder.line("and o.remark like concat('%', ?, '%')", map.get("orderRemark").trim());
        }
        if(StringUtil.checkParamsValid(map.get("remark"))){
            builder.line("and o.remark like concat('%', ?, '%')", map.get("remark").trim());
        }
        if(StringUtil.checkParamsValid(map.get("warehouseId"))){
            builder.line("and i.warehouse_id =? ", map.get("warehouseId").trim());
        }
        if(StringUtil.checkParamsValid(map.get("status"))){
            builder.line("and i.status = ? ", map.get("status").trim());
        }
        if(StringUtil.checkParamsValid(map.get("examineRemark"))){
            builder.line("and i.remark like concat('%', ?, '%')", map.get("examineRemark").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeSta"))) {
            builder.line("and o.inspection_time >= concat(?, ' 00：00：00')", map.get("createTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("createTimeEnd"))) {
            builder.line("and o.inspection_time <= concat(?, ' 23：59：59')", map.get("createTimeEnd").trim());
        }
        if (StringUtil.checkParamsValid(map.get("confirmTimeSta"))) {
            builder.line("and i.create_time >= concat(?, ' 00：00：00')", map.get("confirmTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("confirmTimeEnd"))) {
            builder.line("and i.create_time <= concat(?, ' 23：59：59')", map.get("confirmTimeEnd").trim());
        }
        if (StringUtil.checkParamsValid(map.get("finishTimeSta"))) {
            builder.line("and i.finish_time >= concat(?, ' 00：00：00')", map.get("finishTimeSta").trim());
        }
        if (StringUtil.checkParamsValid(map.get("finishTimeEnd"))) {
            builder.line("and i.finish_time <= concat(?, ' 23：59：59')", map.get("finishTimeEnd").trim());
        }
    }
}
