cy
2023-11-24 a5e195c4d1cf661ec0f1c03517ce3b5436b7e5b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.consum.base.core.service;
 
import cn.hutool.core.convert.Convert;
import com.consum.base.service.BaseGoodsModelsServiceImpl;
import com.consum.base.service.BaseWarehouseServiceImpl;
import com.consum.base.service.LWhWarningServiceImpl;
import com.consum.model.po.BaseWarehouse;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.WhWarning;
import com.walker.infrastructure.utils.NumberGenerator;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
 
/**
 * @ClassName LWhWarningCoreServiceImpl
 * @Author cy
 * @Date 2023/11/24
 * @Description
 * @Version 1.0
 **/
@Service
public class LWhWarningCoreServiceImpl {
 
    @Resource
    private LWhWarningServiceImpl lWhWarningService;
    @Resource
    private BaseWarehouseServiceImpl baseWarehouseService;
    @Resource
    private BaseGoodsModelsServiceImpl baseGoodsModelsService;
 
    /**
     * 当库存变动时调用该方法
     *
     * @param warehouseType
     * @param warehouseId
     * @param baseModelIdList
     */
    public void updateKuCun(Short warehouseType, Long warehouseId, List<Long> baseModelIdList, FinSysTenantUser sysInfo, Long dealTime) {
        if (baseModelIdList == null) {
            baseModelIdList = new ArrayList<>();
        }
        // 获取到达到阈值的物品
        List<Map<String, Object>> kuCunNotifyList = lWhWarningService.checkKuCun(warehouseType, warehouseId, baseModelIdList);
        //
//        log.info("没有达到阈值的物品");
        if (CollectionUtils.isEmpty(kuCunNotifyList)) {
            kuCunNormalModel(warehouseType, warehouseId, baseModelIdList);
            return;
        }
        // 将数据插入库存预警【WH_WARNING】
        BaseWarehouse baseWarehouses = baseWarehouseService.get(new BaseWarehouse(warehouseId));
 
        List<Long> kuCunNotifyModelIdList = new ArrayList<>(kuCunNotifyList.size());
        List<WhWarning> warningList = new ArrayList<>(kuCunNotifyList.size());
 
        for (Map<String, Object> notifyKuCun : kuCunNotifyList) {
            Long baseGoodsModelsId = Convert.toLong(notifyKuCun.get("baseGoodsModelsId"));
            kuCunNotifyModelIdList.add(baseGoodsModelsId);
            Integer lowerLimit = Convert.toInt(notifyKuCun.get("lowerLimit"));
            Integer upperLimit = Convert.toInt(notifyKuCun.get("upperLimit"));
            Integer warningType = Convert.toInt(notifyKuCun.get("warningType"));
            Integer goodsNum = Convert.toInt(notifyKuCun.get("goodsNum"));
 
            WhWarning whWarning = new WhWarning();
            List<Map<String, Object>> modelInfoList = baseGoodsModelsService.queryGoodsModelInfo(baseGoodsModelsId);
            Map<String, Object> modelInfo = modelInfoList.get(0);
 
            whWarning.setId(NumberGenerator.getLongSequenceNumber());
            whWarning.setWarehouseType(0);
            whWarning.setBaseWarehouseId(warehouseId);
            whWarning.setBaseWarehouseName(baseWarehouses.getWarehouseName());
            whWarning.setBaseGoodsTemplateId(Convert.toLong(modelInfo.get("goodsId")));
            whWarning.setBaseGoodsTemplateName(Convert.toStr(modelInfo.get("goodsName")));
            whWarning.setBaseGoodsModelsId(baseGoodsModelsId);
            whWarning.setBaseGoodsModelsName((String) modelInfo.get("modelName"));
            whWarning.setGoodsType(2);
            whWarning.setWarningType(warningType);
            whWarning.setUpperLimit(upperLimit);
            whWarning.setLowerLimit(lowerLimit);
            whWarning.setWarehouseCount(goodsNum);
            whWarning.setWarningTime(dealTime);
            whWarning.setStates(1);
            whWarning.setOperatorId(sysInfo.getId());
            whWarning.setOperatorName(sysInfo.getUserName());
            whWarning.setDealTime(dealTime);
            whWarning.setAgencyId(Convert.toLong(sysInfo.getTenantId()));
            whWarning.setAgencyName(sysInfo.getTenantName());
            warningList.add(whWarning);
        }
 
        //修改以前的预警状态
        baseModelIdList.removeAll(kuCunNotifyModelIdList);
        kuCunNormalModel(warehouseType, warehouseId, baseModelIdList);
 
        //批量插入预警
        lWhWarningService.insertBatch(warningList);
 
    }
 
    private String KU_CUN_NORMAL_MODEL = "update WH_WARNING set STATES=2 where WAREHOUSE_TYPE=? and BASE_WAREHOUSE_ID=?";
 
    private int kuCunNormalModel(Short warehouseType, Long warehouseId, List<Long> baseModelIdList) {
        if (CollectionUtils.isEmpty(baseModelIdList)) {
            return 1;
        }
        ArrayList<Object> params = new ArrayList<>();
        params.add(warehouseType);
        params.add(warehouseId);
 
        StringBuilder sql = new StringBuilder(KU_CUN_NORMAL_MODEL);
        if (!org.springframework.util.CollectionUtils.isEmpty(baseModelIdList)) {
            sql.append(" and BASE_GOODS_MODELS_ID in(?)");
            params.add(baseModelIdList);
        }
        return lWhWarningService.update(sql.toString(), params.toArray());
    }
}