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

import com.jfinal.kit.StrKit;
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.order.entity.CrmSupplyCheckPendingOrder;
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 CheckPendingOrderDao extends BaseDao<CrmSupplyCheckPendingOrder> {

    public List<Record> listPendingOrder(LayuiPage page, Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line(" select * from `crm_supply_check_pending_order` o where o.`status` <> '4'");
        appendQueryCondition(sql, map);
        sql.line(" order by o.`create_time` desc");
        if (StrKit.notNull(page)) {
            sql.limit(page.getPage(), page.getLimit());
        }
        return Db.find(sql.build(), sql.params());
    }

    public long countPendingOrder(Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select count(*) from `crm_supply_check_pending_order` o where o.`status` <> '4'");
        appendQueryCondition(sql, map);
        return Db.queryLong(sql.build(), sql.params());
    }

    public void appendQueryCondition(SqlBuilder sql, Map<String, String> map) {
        if (StringUtil.checkParamsValid(map.get("code"))) {
            sql.line(" and o.`code` = ?", map.get("code"));
        }
        if (StringUtil.checkParamsValid(map.get("receivingUnit"))) {
            sql.line(" and o.`receiving_unit` like concat('%', ?, '%')", map.get("receivingUnit"));
        }
        if (StringUtil.checkParamsValid(map.get("status"))) {
            sql.line(" and o.`status` = ?", map.get("status"));
        }
        if (StringUtil.checkParamsValid(map.get("forwardingUnit"))) {
            sql.line(" and o.`forwarding_unit` like concat('%', ?, '%')", map.get("forwardingUnit"));
        }
        if (StringUtil.checkParamsValid(map.get("deliverMethodId"))) {
            sql.line(" and o.`deliver_method_id` = ?", map.get("deliverMethodId"));
        }
        if (StringUtil.checkParamsValid(map.get("deliverRemarks"))) {
            sql.line(" and o.`deliver_remarks` like concat('%', ?, '%')", map.get("deliverRemarks"));
        }
        if (StringUtil.checkParamsValid(map.get("remarks"))) {
            sql.line(" and o.`remarks` like concat('%', ?, '%')", map.get("remarks"));
        }
        if (StringUtil.checkParamsValid(map.get("placeOrderTimeMin"))) {
            sql.line(" and o.`place_order_time` >= concat(?, ' 00:00:00')", map.get("placeOrderTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("placeOrderTimeMax"))) {
            sql.line(" and o.`place_order_time` <= concat(?, ' 23:59:59')", map.get("placeOrderTimeMax"));
        }
        if (StringUtil.checkParamsValid(map.get("deliverTimeMin"))) {
            sql.line(" and o.`deliver_time` >= concat(?, ' 00:00:00')", map.get("deliverTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("deliverTimeMax"))) {
            sql.line(" and o.`deliver_time` <= concat(?, ' 23:59:59')", map.get("deliverTimeMax"));
        }
        if (StringUtil.checkParamsValid(map.get("auditTimeMin"))) {
            sql.line(" and o.`audit_time` >= concat(?, ' 00:00:00')", map.get("auditTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("auditTimeMax"))) {
            sql.line(" and o.`audit_time` <= concat(?, ' 23:59:59')", map.get("auditTimeMax"));
        }
    }

    public Record statisticOrderForSupplyChainDsh() {
        return Db.findFirst("select count(*) as dsh from `crm_supply_check_pending_order` o where o.`status` = '1'");
    }
}
