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.CrmSupplyMaterialUsedOrder;
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 MaterialOutStockOrderDao extends BaseDao<CrmSupplyMaterialUsedOrder> {

    public List<Record> listOutStockOrder(LayuiPage page, Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select *, date_format(o.create_time, '%Y-%m-%d %H:%i:%s') as create_time, ");
        sql.line("(select count(*) from crm_supply_material_used_order_detail d where d.main_id = o.id and d.status = '0') as count_details,");
        sql.line("(select sum(d.`amount`) from crm_supply_material_used_order_detail d where d.main_id = o.id and d.status = '0') as total_amount");
        sql.line("from crm_supply_material_used_order o where o.`status` = '0'");
        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 countOutStockOrder(Map<String, String> map) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select count(*) from crm_supply_material_used_order o where o.`status` = '0'");
        appendQueryCondition(sql, map);
        return Db.queryLong(sql.build(), sql.params());
    }

    public void appendQueryCondition(SqlBuilder sql, Map<String, String> map) {
        if (StringUtil.checkParamsValid(map.get("number"))) {
            sql.line(" and o.`number` like concat('%', ?, '%')", map.get("number"));
        }
        if (StringUtil.checkParamsValid(map.get("creator"))) {
            sql.line(" and o.`creator` like concat('%', ?, '%')", map.get("creator"));
        }
        if (StringUtil.checkParamsValid(map.get("warehouseId"))) {
            sql.line(" and o.`warehouse_id` = ?", map.get("warehouseId"));
        }
        if (StringUtil.checkParamsValid(map.get("remarks"))) {
            sql.line(" and o.`remarks` like concat('%', ?, '%')", map.get("remarks"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMin"))) {
            sql.line(" and o.`create_time` >= concat(?, ' 00:00:00')", map.get("createTimeMin"));
        }
        if (StringUtil.checkParamsValid(map.get("createTimeMax"))) {
            sql.line(" and o.`create_time` <= concat(?, ' 23:59:59')", map.get("createTimeMax"));
        }
        if (StringUtil.checkParamsValid(map.get("ascriptionId"))) {
            if (!"a".equals(map.get("ascriptionId"))) {
                sql.line(" and o.`ascription_id` = ?", map.get("ascriptionId"));
            }
        }
    }

    public Record findById(String id) {
        return Db.findFirst("select * from crm_supply_material_used_order where id = ?", id);
    }
}
