危化品全生命周期管理后端
“djh”
2025-04-21 437f8e2b89a18363a1073fdbb3ab99bcd840a757
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.CodePrexEnum;
import com.gkhy.hazmat.common.enums.HazmatStatusEnum;
import com.gkhy.hazmat.common.enums.OperateStatusEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.system.domain.*;
import com.gkhy.hazmat.system.domain.vo.HzProductWarehouseVO;
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 成品表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-06 16:03:53
 */
@Service
public class HzProductServiceImpl extends ServiceImpl<HzProductMapper, HzProduct> implements HzProductService {
    @Autowired
    private HzProductFlowMapper productFlowMapper;
    @Autowired
    private HzProductWarehouseRecordMapper productWarehouseRecordMapper;
    @Autowired
    private HzProductBasicMapper productBasicMapper;
    @Autowired
    private HzWarehouseMapper warehouseMapper;
 
    @Override
    public CommonPage selectProductList(HzProduct product) {
        if(product.getWarehouseId()==null||product.getBasicId()==null){
            throw new ApiException("仓库id或者成品基础数据id不能为空");
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(product.getCompanyId());
        }else {
            //设置分表id
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
            product.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzProduct> productList = baseMapper.selectProductList(product);
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(productList);
    }
 
    @Override
    public HzProduct selectProductById(Long productId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProduct product = baseMapper.selectById(productId);
        IdTableNameHandler.removeCurrentId();
        checkUserAllowed(product,currentUser);
        return product;
    }
 
    @Override
    public int deleteProductById(Long productId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProduct product=baseMapper.selectById(productId);
        if(product==null){
            throw new ApiException("成品不存在");
        }
        checkUserAllowed(product,currentUser);
        baseMapper.deleteProductById(productId);
        IdTableNameHandler.removeCurrentId();
        return 0;
    }
 
    @Override
    public HzProduct selectProductByCode(String code) {
        if(!code.startsWith(CodePrexEnum.GOOD.getCode())){
            throw new ApiException("该条码格式不正确");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProduct product=baseMapper.selectProductByCode(code,currentUser.getCompanyId());
        IdTableNameHandler.removeCurrentId();
        if(product==null){
            throw new ApiException("该条码数据不存在");
        }
        return product;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void productSold(Long productId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProduct product=getById(productId);
        if(!product.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())){
            throw new ApiException("成品不在库中,不能进行销售操作");
        }
        checkUserAllowed(product,currentUser);
        //获取变动前仓库库存
        int count = baseMapper.selectProductCountOfWarehouse(product.getWarehouseId(), product.getBasicId(), currentUser.getCompanyId());
        product.setState(HazmatStatusEnum.SOLD.getCode());
        product.setUpdateBy(currentUser.getUsername());
        updateById(product);
        //生成流向
        HzProductFlow productFlow=new HzProductFlow();
        productFlow.setProductId(productId);
        productFlow.setBasicId(product.getBasicId());
        productFlow.setState(OperateStatusEnum.SOLD.getCode());
        productFlow.setCompanyId(currentUser.getCompanyId());
        productFlow.setNum(product.getRemaining().multiply(BigDecimal.valueOf(-1)));
        productFlow.setCreateId(currentUser.getId());
        productFlowMapper.insert(productFlow);
        //生成库存变动记录
        //新增危化品变动记录
        HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
                .setWarehouseId(product.getWarehouseId())
                .setCreateId(currentUser.getId())
                .setBasicId(product.getBasicId())
                .setNum(-1)
                .setState(OperateStatusEnum.SOLD.getCode())
                .setCompanyId(currentUser.getCompanyId())
                .setRemaining(count-1);
        productWarehouseRecordMapper.insert(warehouseRecord);
        IdTableNameHandler.removeCurrentId();
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void productDiscard(Long productId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzProduct product=getById(productId);
        if(!product.getState().equals(HazmatStatusEnum.WAREHOUSEIN.getCode())){
            throw new ApiException("危化品不在库中,不能进行作废操作");
        }
        checkUserAllowed(product,currentUser);
        //获取变动前仓库库存
        int count = baseMapper.selectProductCountOfWarehouse(product.getWarehouseId(), product.getBasicId(), currentUser.getCompanyId());
        product.setState(HazmatStatusEnum.DISCARD.getCode());
        product.setUpdateBy(currentUser.getUsername());
        updateById(product);
        //生成流向
        HzProductFlow productFlow=new HzProductFlow();
        productFlow.setProductId(product.getId());
        productFlow.setBasicId(product.getBasicId());
        productFlow.setState(OperateStatusEnum.DISCARD.getCode());
        productFlow.setCompanyId(currentUser.getCompanyId());
        productFlow.setNum(product.getRemaining().multiply(BigDecimal.valueOf(-1)));
        productFlow.setCreateId(currentUser.getId());
        productFlowMapper.insert(productFlow);
 
        //生成库存变动记录
        //新增危化品变动记录
        HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
                .setWarehouseId(product.getWarehouseId())
                .setBasicId(product.getBasicId())
                .setCreateId(currentUser.getId())
                .setNum(-1)
                .setState(OperateStatusEnum.DISCARD.getCode())
                .setCompanyId(currentUser.getCompanyId())
                .setRemaining(count-1);
        productWarehouseRecordMapper.insert(warehouseRecord);
        IdTableNameHandler.removeCurrentId();
    }
 
    @Override
    public void changeState(HzProduct product) {
        if(product.getId()==null||product.getCompanyId()==null||product.getState()==null){
            throw new ApiException("参数不正确");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(product,currentUser);
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
 
        HzProduct newProduct=new HzProduct().setId(product.getId()).setState(product.getState());
        newProduct.setUpdateBy(currentUser.getUsername());
        updateById(newProduct);
        IdTableNameHandler.removeCurrentId();
    }
 
    @Override
    public CommonPage selectProductGroupWarehouse(HzProduct product) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            IdTableNameHandler.setCurrentId(product.getCompanyId());
        }else {
            //设置分表id
            //todo
            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
            product.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzProductWarehouseVO> productList = baseMapper.selectProductGroupWareHouse(product);
        if(!productList.isEmpty()) {
            List<Long> warehouseIds = productList.stream().map(HzProductWarehouseVO::getWarehouseId).collect(Collectors.toList());
            List<Long> basicIds = productList.stream().map(HzProductWarehouseVO::getBasicId).collect(Collectors.toList());
            List<HzProductBasic> hazmatBasicList = productBasicMapper.selectProductBasicListByIds(basicIds);
            List<HzWarehouse> warehouseList = warehouseMapper.selectWarehouseListByIds(warehouseIds);
            Map<Long,HzProductBasic> productBasicMap=hazmatBasicList.stream().collect(Collectors.toMap(HzProductBasic::getId, item->item));
            Map<Long,HzWarehouse> warehouseMap=warehouseList.stream().collect(Collectors.toMap(HzWarehouse::getId, item->item));
            for(HzProductWarehouseVO productWarehouseVO:productList){
                HzProductBasic productBasic=productBasicMap.get(productWarehouseVO.getBasicId());
                HzWarehouse warehouse=warehouseMap.get(productWarehouseVO.getWarehouseId());
                if(productBasic!=null){
                    productWarehouseVO.setProductBasic(productBasic);
                }
                if(warehouse!=null){
                    productWarehouseVO.setWarehouseName(warehouse.getName());
                }
            }
        }
        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(productList);
    }
 
    public void checkUserAllowed(HzProduct product,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(product!=null){
            if(!Objects.equals(user.getCompanyId(), product.getCompanyId())){
                throw new ApiException("无权限操作其他企业数据");
            }
        }
    }
}