cy
2023-11-21 2ffe8ae42a3350588fa9235f22592e5ed6e51fe7
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.consum.base.controller;
 
import cn.hutool.core.convert.Convert;
import com.consum.base.BaseController;
import com.consum.base.core.utils.CommonUtil;
import com.consum.base.pojo.WhWarningConfigParam;
import com.consum.base.pojo.query.WhWarningConfigQry;
import com.consum.base.service.BaseWarehouseServiceImpl;
import com.consum.base.service.LWhWarningConfigServiceImpl;
import com.consum.model.po.BaseWarehouse;
import com.consum.model.po.FinSysTenantUser;
import com.consum.model.po.WhWarningConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.NumberGenerator;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @ClassName LWhWarningConfigController
 * @Author cy
 * @Date 2023/11/20
 * @Description
 * @Version 1.0
 **/
@RestController
@RequestMapping("/pc/warehouse/warningConfig")
@Api(value = "库存阀值", tags = "库存阀值")
public class LWhWarningConfigController extends BaseController {
 
    @Resource
    private LWhWarningConfigServiceImpl lWhWarningConfigService;
    @Resource
    private BaseWarehouseServiceImpl baseWarehouseService;
    private ObjectMapper mapper = new ObjectMapper();
 
    /**
     * @Description 新增
     */
    @PostMapping("/add")
    public ResponseValue add() throws Exception {
        WhWarningConfigParam param = CommonUtil.getObjFromReqBody(WhWarningConfigParam.class);
        String modelConfigStr = param.getModelConfigStr();
        Long baseWarehouseId = param.getBaseWarehouseId();
        Long baseGoodsTemplateId = param.getBaseGoodsTemplateId();
        if (param.getWarehouseType() == null || baseWarehouseId == null || baseGoodsTemplateId == null || StringUtils.isEmpty(modelConfigStr)) {
            return ResponseValue.error("缺少必要参数");
        }
        /*当前登录人只能看到自己机构下的列表*/
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            return ResponseValue.error("登录用户信息不存在");
        }
        String tenantId = sysInfo.getTenantId();
        BaseWarehouse baseWarehouse = baseWarehouseService.getById(baseWarehouseId);
        if (baseWarehouse == null) {
            return ResponseValue.error("仓库不存在!");
        }
        Long warehouseAgencyId = baseWarehouse.getAgencyId();
        if (warehouseAgencyId == null || !warehouseAgencyId.toString().startsWith(tenantId)) {
            return ResponseValue.error("操作失败!");
        }
 
        List<Map<String, Object>> list = mapper.readValue(modelConfigStr, List.class);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseValue.error("缺少必要参数");
        }
        WhWarningConfig queryIsExist = new WhWarningConfig();
        queryIsExist.setWarehouseType(0);
        queryIsExist.setBaseGoodsModelsId(baseWarehouseId);
        ArrayList<WhWarningConfig> configArrayList = new ArrayList<>(list.size());
        for (Map<String, Object> item : list) {
            Long baseGoodsModelsId = Convert.toLong(item.get("baseGoodsModelsId"));
            queryIsExist.setBaseGoodsModelsId(baseGoodsModelsId);
            List<WhWarningConfig> containWarningConfig = lWhWarningConfigService.select(queryIsExist);
            if (!CollectionUtils.isEmpty(containWarningConfig)) {
                return ResponseValue.error("记录已存在!");
            }
            Integer lowerLimit = Convert.toInt(item.get("lowerLimit"));
            Integer upperLimit = Convert.toInt(item.get("upperLimit"));
            if (lowerLimit == null && lowerLimit == null) {
                return ResponseValue.error("库存上限、下限不能全为空!");
            }
            if (upperLimit != null && upperLimit < 0) {
                return ResponseValue.error("库存限制不能小于0!");
            }
            if (lowerLimit != null && lowerLimit < 0) {
                return ResponseValue.error("库存限制不能小于0!");
            }
            if (upperLimit != null && lowerLimit != null && upperLimit < lowerLimit) {
                return ResponseValue.error("库存上限不能小于下限!");
            }
            WhWarningConfig whWarningConfig = new WhWarningConfig();
            whWarningConfig.setId(NumberGenerator.getLongSequenceNumber());
            whWarningConfig.setWarehouseType(0);
            whWarningConfig.setBaseWarehouseId(baseWarehouseId);
            whWarningConfig.setBaseGoodsTemplateId(baseGoodsTemplateId);
            whWarningConfig.setBaseGoodsModelsId(baseGoodsModelsId);
            whWarningConfig.setUpperLimit(upperLimit);
            whWarningConfig.setLowerLimit(lowerLimit);
            if (whWarningConfig.getBaseGoodsModelsId() == null) {
                whWarningConfig.setGoodsType(1);
            } else {
                whWarningConfig.setGoodsType(2);
            }
            configArrayList.add(whWarningConfig);
        }
 
        int flag = lWhWarningConfigService.insertBatch(configArrayList);
        return flag > 0 ? ResponseValue.success(1) : ResponseValue.error("操作失败!");
    }
 
    /**
     * @Description 修改
     */
    @PostMapping("/upd")
    public ResponseValue upd() {
        WhWarningConfig param = CommonUtil.getObjFromReqBody(WhWarningConfig.class);
        if (param.getId() == null || (param.getUpperLimit() == null && param.getLowerLimit() == null)) {
            return ResponseValue.error("库存上限、下限不能全为空!");
        }
        if (param.getUpperLimit() != null && param.getLowerLimit() != null && param.getUpperLimit() < param.getLowerLimit()) {
            return ResponseValue.error("库存上限不能小于下限!");
        }
 
        // 更新
        int flag = lWhWarningConfigService.save(param);
        return flag > 0 ? ResponseValue.success(1) : ResponseValue.error("操作失败!");
    }
 
 
    @DeleteMapping("del")
    public ResponseValue delById() {
        WhWarningConfig param = CommonUtil.getObjFromReqBody(WhWarningConfig.class);
        if (param.getId() == null) {
            return ResponseValue.error("id为空");
        }
        int num = baseWarehouseService.delete(param);
        return num > 0 ? ResponseValue.success(1) : ResponseValue.error("删除失败!");
    }
 
    @GetMapping("/getList")
    public ResponseValue getConfigList() {
        WhWarningConfigQry param = CommonUtil.getObjFromReq(WhWarningConfigQry.class);
        /*当前登录人只能看到自己机构下的列表*/
        FinSysTenantUser sysInfo = this.getSysInfo();
        String tenantId = sysInfo.getTenantId();
        Long paramAgencyId = param.getAgencyId();
        if (paramAgencyId == null || !paramAgencyId.toString().startsWith(tenantId)) {
            param.setAgencyId(Long.valueOf(tenantId));
        }
        GenericPager<Map<String, Object>> genericPager = lWhWarningConfigService.getConfigListWithPage(param);
        return ResponseValue.success(genericPager);
    }
 
    @GetMapping("/getById")
    public ResponseValue getById() {
        WhWarningConfig param = CommonUtil.getObjFromReq(WhWarningConfig.class);
        WhWarningConfig whWarningConfig = lWhWarningConfigService.get(param);
        return ResponseValue.success(whWarningConfig);
    }
 
    @GetMapping("/getConfigList")
    public ResponseValue getConfigList(WhWarningConfig param) {
        if (param.getBaseWarehouseId() == null || param.getWarehouseType() == null || (param.getBaseGoodsModelsId() == null || param.getBaseGoodsTemplateId() == null)) {
            return ResponseValue.error("缺少必要参数");
        }
        return ResponseValue.success(lWhWarningConfigService.select(param));
    }
}