package com.sifang.modules.supply.warehouse.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.warehouse.entity.CrmSupplyInventoryOrder;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
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 InventoryOrderDao extends BaseDao<CrmSupplyInventoryOrder> {

    public CrmSupplyInventoryOrder getByProcessInstanceId(String processInstanceId){
        return getByHql("from CrmSupplyInventoryOrder where processInstanceId =:p1 ", new Parameter(processInstanceId));
    }

    public List<Record> listInventoryOrder(LayuiPage page, Map<String, String> map, User user) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select o.*, date_format(o.create_time, '%Y-%m-%d %H:%i:%s') as create_time,");
        sql.line("date_format(o.inventory_time, '%Y-%m-%d %H:%i:%s') as inventory_time,");
        sql.line("date_format(o.audit_time, '%Y-%m-%d %H:%i:%s') as audit_time,");
        sql.line("(select count(*) from crm_supply_inventory_order_detail d where d.inventory_id = o.id and d.status = '0') as count_details  ");
        if(StringUtil.equalsAny(user.getUserType(),User.USER_TYPE_KJ,User.USER_TYPE_ADVISOR)){
            sql.line(" ,(CASE WHEN o.status!='3' THEN '审核中' ELSE o.task_name END) AS taskName ");
        }else{
            sql.line(",o.task_name AS taskName ");
        }
        sql.line(" ,(case when  k.`ASSIGNEE_`='"+user.getId()+"' then 1 else 0 end) as audit ");
        sql.line(" ,(case when FIND_IN_SET(?,o.operate_user_ids) then 1 else 0 end ) as lookAuth ",user.getId());
        sql.line("from crm_supply_inventory_order o ");
        sql.line(" LEFT JOIN `act_ru_task` k ON o.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='10' and k.ASSIGNEE_ =? ",user.getId());
        sql.line("where o.status <> '4' ");
        appendInventoryOrderCondition(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 List<Record> inintStock(){
        SqlBuilder sql = new SqlBuilder();
        sql.line("select s.stocks , l.id materialId, s.material_code materialCode");
        sql.line("from crm_supply_material l, crm_material_stocks s ");
        sql.line("where l.code =s.material_code and l.status ='1'  ");
        return Db.find(sql.build());
    }

    public long countInventoryOrder(Map<String, String> map,User user) {
        SqlBuilder sql = new SqlBuilder();
        sql.line("select count(*) from crm_supply_inventory_order o ");
        sql.line(" LEFT JOIN `act_ru_task` k ON o.`process_instance_id`=k.PROC_INST_ID_ AND k.FORM_KEY_='10' ");
        sql.line(" where o.status <> '4' ");
        appendInventoryOrderCondition(sql, map);
        return Db.queryLong(sql.build(), sql.params());
    }

    public void appendInventoryOrderCondition(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("status"))) {
            sql.inst(" and o.status in ", map.get("status").split(","));
        }
        if (StringUtil.checkParamsValid(map.get("inventoryType"))) {
            sql.line(" and o.inventory_type = ?", map.get("inventoryType"));
        }
        if (StringUtil.checkParamsValid(map.get("warehouseId"))) {
            sql.line(" and o.warehouse_id = ?", map.get("warehouseId"));
        }
        if (StringUtil.checkParamsValid(map.get("inventoryName"))) {
            sql.line(" and o.inventory_name like concat('%', ?, '%')", map.get("inventoryName"));
        }
        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("inventoryObjectId"))) {
            if (!"a".equals(map.get("inventoryObjectId"))) {
                sql.line(" and o.inventory_object_id = ?", map.get("inventoryObjectId"));
            }
        }
    }

    public Record findById(String id) {
        return Db.findFirst("select *, date_format(create_time, '%Y-%m-%d %H:%i:%s') as create_time," +
                "date_format(audit_time, '%Y-%m-%d %H:%i:%s') as audit_time from crm_supply_inventory_order where id = ?", id);
    }

}
