“djh”
2 days ago aad364bb323a1eaa0389ee5c6389bdc0ea7ed526
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.api.CommonResult;
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.HzHazmat;
import com.gkhy.hazmat.system.domain.HzHazmatBasic;
import com.gkhy.hazmat.system.domain.HzHazmatFlow;
import com.gkhy.hazmat.system.domain.HzWarehouseRecord;
import com.gkhy.hazmat.system.domain.dto.ChemicalLedger;
import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper;
import com.gkhy.hazmat.system.mapper.HzHazmatFlowMapper;
import com.gkhy.hazmat.system.mapper.HzHazmatMapper;
import com.gkhy.hazmat.system.mapper.HzWarehouseRecordMapper;
import com.gkhy.hazmat.system.service.HzHazmatFlowService;
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.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 危化品流向表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-05 14:41:40
 */
@Service
public class HzHazmatFlowServiceImpl extends ServiceImpl<HzHazmatFlowMapper, HzHazmatFlow> implements HzHazmatFlowService {
    @Autowired
    private HzHazmatMapper hazmatMapper;
 
    @Autowired
    private HzHazmatBasicMapper hazmatBasicMapper;
 
    @Autowired
    private HzWarehouseRecordMapper warehouseRecordMapper;
 
    @Override
    public CommonPage selectHazmatFlowList(HzHazmatFlow hazmatFlow) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
//        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
//            IdTableNameHandler.setCurrentId(hazmatFlow.getCompanyId());
//        }else {
//
//            //设置分表id
//            //todo
//            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
//        }
        hazmatFlow.setCompanyId(currentUser.getCompanyId());
        PageUtils.startPage();
        List<HzHazmatFlow> flowList = baseMapper.selectHazmatFlowList(hazmatFlow);
//        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(flowList);
    }
 
    @Override
    public HzHazmatFlow selectHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
//        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow = baseMapper.selectById(hazmatFlowId);
//        IdTableNameHandler.removeCurrentId();
        if (!hazmatFlow.getCompanyId().equals(currentUser.getCompanyId())) {
            throw new ApiException("无权限查看其它企业数据");
        }
        return hazmatFlow;
    }
 
    @Override
    public int insertHazmatFlow(HzHazmatFlow hazmatFlow) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        hazmatFlow.setCreateBy(currentUser.getUsername());
        hazmatFlow.setCompanyId(currentUser.getCompanyId());
        checkUserAllowed(null,currentUser);
        //设置分表id
//        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        int row = baseMapper.insert(hazmatFlow);
//        IdTableNameHandler.removeCurrentId();
        if (row < 1) {
            throw new ApiException("新增危化品流向失败");
        }
        return row;
    }
 
    @Override
    public int deleteHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
//        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow=baseMapper.selectById(hazmatFlowId);
//        IdTableNameHandler.removeCurrentId();
        if(hazmatFlow==null){
            throw new ApiException("流向信息不存在");
        }
        checkUserAllowed(hazmatFlow,currentUser);
 
        baseMapper.deleteById(hazmatFlow);
        return 0;
    }
 
    @Override
    public List<HzHazmatFlow> selectAllHazmatFlowById(Long hazmatFlowId) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        //设置分表id
//        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        HzHazmatFlow hazmatFlow=getById(hazmatFlowId);
        if(hazmatFlow==null){
            throw new ApiException("流向不存在");
        }
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatFlow.getHazmatId());
//        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    @Override
    public List<HzHazmatFlow> selectAllHazmatFlowByCode(String code,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        if(!code.startsWith(CodePrexEnum.MATERIAL.getCode())){
            throw new ApiException("条码格式不正确");
        }
//        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
//            IdTableNameHandler.setCurrentId(companyId);
//        }else {
//            //设置分表id
//            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
//        }
        HzHazmat hazmat=hazmatMapper.selectHazmatByCode(code,currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())?companyId:currentUser.getCompanyId());
        if(hazmat==null){
            throw new ApiException("危化品条码数据不存在");
        }
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmat.getId());
//        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    @Override
    public CommonPage<HzHazmatFlow> selectAllHazmatFlowByUser() {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        HzHazmatFlow hazmat=new HzHazmatFlow()
                .setCreateId(currentUser.getId())
                .setCompanyId(currentUser.getCompanyId());
        //设置分表id
//        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
        PageUtils.startPage();
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByUser(hazmat);
//        IdTableNameHandler.removeCurrentId();
        return CommonPage.restPage(hazmatFlowList);
    }
 
    @Override
    public List<HzHazmatFlow> getAllHazmatFlowByHazmatId(Long hazmatId,Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
//        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
//            IdTableNameHandler.setCurrentId(companyId);
//        }else {
//            //设置分表id
//            IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
//        }
        HzHazmat hazmat=hazmatMapper.selectById(hazmatId);
        if(hazmat==null){
            throw new ApiException("危化品数据不存在");
        }
        checkUserAllowed(new HzHazmatFlow().setCompanyId(hazmat.getCompanyId()),currentUser);
        List<HzHazmatFlow> hazmatFlowList = baseMapper.selectAllHazmatFlowByHazmatId(hazmatId);
//        IdTableNameHandler.removeCurrentId();
        return hazmatFlowList;
    }
 
    @Override
    @Transactional
    public CommonResult pushFlowFromThirdParty(List<ChemicalLedger> flows) {
        List<HzHazmatFlow> hazmatFlowList = new ArrayList<>();
        List<String> errorMessages = new ArrayList<>();
        int successCount = 0;
        int failCount = 0;
        for (int i = 0; i < flows.size(); i++) {
            ChemicalLedger flowData = flows.get(i);
            try {
                HzHazmatFlow hazmatFlow = convertToHzHazmatFlow(flowData);
                HzHazmatBasic hzHazmatBasic = hazmatBasicMapper.selectByCas(flowData.getCas());
                hazmatFlow.setBasicId(hzHazmatBasic.getId());
                if (hazmatFlow.getBasicId() == null) {
                    errorMessages.add(String.format("第%d条数据:cas号没有对应基础信息", i + 1));
                    failCount++;
                    continue;
                }
 
                if (hazmatFlow.getState() == null) {
                    errorMessages.add(String.format("第%d条数据:操作状态不能为空", i + 1));
                    failCount++;
                    continue;
                }
                HzHazmat hzHazmat = hazmatMapper.selectById(hazmatFlow.getHazmatId());
                int count = hazmatMapper.selectHazmatCountOfWarehouse(hazmatFlow.getCabinetId(), hazmatFlow.getBasicId(),18L,hazmatFlow.getAuxiliaryCabinetId());
                if (hazmatFlow.getState()==3){
                    if (hzHazmat.getState()==2||hzHazmat.getState()==3||hzHazmat.getState()==4){
                        errorMessages.add(String.format("第%d条数据:危化品状态异常,不能出库", i + 1));
                    }else {
                        hzHazmat.setState(HazmatStatusEnum.USING.getCode());
                        hzHazmat.setUpdateBy(hazmatFlow.getCreateBy());
                        HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
                                .setWarehouseId(hzHazmat.getWarehouseId())
                                .setBasicId(hzHazmat.getBasicId())
                                .setCupboardId(hzHazmat.getCupboardId())
                                .setCreateBy(hazmatFlow.getCreateBy())
                                .setNum(-1)
                                .setState(OperateStatusEnum.USING.getCode())
                                .setCompanyId(18L)
                                .setRemaining(count-1);
                        warehouseRecordMapper.insert(warehouseRecord);
                    }
                    hazmatMapper.updateById(hzHazmat);
                }
                if (hazmatFlow.getState()==4){
                    HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
                            .setWarehouseId(hzHazmat.getWarehouseId())
                            .setBasicId(hzHazmat.getBasicId())
                            .setCupboardId(hzHazmat.getCupboardId())
                            .setCreateBy(hazmatFlow.getCreateBy())
                            .setNum(-1)
                            .setState(OperateStatusEnum.DISCARD.getCode())
                            .setCompanyId(18L)
                            .setRemaining(count-1);
                    warehouseRecordMapper.insert(warehouseRecord);
                    hzHazmat.setUpdateBy(hazmatFlow.getCreateBy());
                    if (hazmatFlow.getReasonId()==0||hazmatFlow.getReasonId()==3){
                        hzHazmat.setState(HazmatStatusEnum.USED.getCode());
                    }else {
                        hzHazmat.setState(HazmatStatusEnum.DISCARD.getCode());
                    }
                    hazmatMapper.updateById(hzHazmat);
                }
                if (hazmatFlow.getState()==2&&hzHazmat.getState()==HazmatStatusEnum.USING.getCode()){
                    HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
                            .setWarehouseId(hzHazmat.getWarehouseId())
                            .setBasicId(hzHazmat.getBasicId())
                            .setCupboardId(hzHazmat.getCupboardId())
                            .setCreateBy(hazmatFlow.getCreateBy())
                            .setNum(1)
                            .setState(OperateStatusEnum.RETURN.getCode())
                            .setCompanyId(18L)
                            .setRemaining(count+1);
                    warehouseRecordMapper.insert(warehouseRecord);
                    hzHazmat.setUpdateBy(hazmatFlow.getCreateBy());
                    hzHazmat.setState(HazmatStatusEnum.USEWAREHOUSEIN.getCode());
                    hazmatMapper.updateById(hzHazmat);
                }else{
                    errorMessages.add(String.format("第%d条数据:危化品状态异常,不能回库", i + 1));
                }
 
//                if (hazmatFlow.getNum() == null || hazmatFlow.getNum().compareTo(BigDecimal.ZERO) <= 0) {
//                    errorMessages.add(String.format("第%d条数据:数量必须大于0", i + 1));
//                    failCount++;
//                    continue;
//                }
 
                hazmatFlowList.add(hazmatFlow);
                successCount++;
 
            } catch (Exception e) {
                errorMessages.add(String.format("第%d条数据处理失败:%s", i + 1, e.getMessage()));
                failCount++;
            }
        }
 
            if (!hazmatFlowList.isEmpty()) {
                boolean saveResult = this.saveBatch(hazmatFlowList, 50);
                if (!saveResult) {
                    return CommonResult.failed("批量保存流向数据失败");
                }
            }
 
 
 
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("totalCount", flows.size());
        resultData.put("successCount", successCount);
        resultData.put("failCount", failCount);
        if (!errorMessages.isEmpty()) {
            resultData.put("errorMessages", errorMessages);
        }
 
        if (failCount > 0) {
            return CommonResult.success(resultData, "批量推送完成,部分数据失败");
        }
 
        return CommonResult.success(resultData, "批量推送成功");
    }
 
    private HzHazmatFlow convertToHzHazmatFlow(ChemicalLedger flowData) {
        HzHazmatFlow hazmatFlow = new HzHazmatFlow();
        hazmatFlow.setUuid(flowData.getId());
        hazmatFlow.setHazmatId(flowData.getHazmatId());
//        hazmatFlow.setBasicId(flowData.getBasicId());
        hazmatFlow.setState(flowData.getStorageType());
        hazmatFlow.setNum(BigDecimal.valueOf(flowData.getNum()));
        hazmatFlow.setCompanyId(18L);
//        hazmatFlow.setCreateId(flowData.getCreateId());
        hazmatFlow.setCabinetId(Long.valueOf(flowData.getCabinetId()));
        hazmatFlow.setCabinetName(flowData.getCabinetName());
        hazmatFlow.setAuxiliaryCabinetId(Long.valueOf(flowData.getAuxiliaryCabinetId()));
        hazmatFlow.setAuxiliaryCabinetName(flowData.getAuxiliaryCabinetName());
        hazmatFlow.setOwner(flowData.getOwner());
        hazmatFlow.setChemicalType(flowData.getChemicalType());
        hazmatFlow.setContainerNo(flowData.getContainerNo());
        hazmatFlow.setContainerUse(flowData.getContainerUse());
        hazmatFlow.setControlType(flowData.getControlType());
        hazmatFlow.setCreateBy(flowData.getCreateUserName());
        hazmatFlow.setUNameOne(flowData.getUNameOne());
        hazmatFlow.setFacelmgUrl(flowData.getFaceImgUrl());
        hazmatFlow.setUNameTwo(flowData.getUNameTwo());
        hazmatFlow.setFacelmgUrlTwo(flowData.getFaceImgUrlTwo());
        hazmatFlow.setHazchemType(flowData.getHazchemType());
        hazmatFlow.setOperate(flowData.getOperate());
        hazmatFlow.setOrgName(flowData.getOrgName());
        hazmatFlow.setReason(flowData.getReason());
        hazmatFlow.setReasonId(flowData.getReasonId());
        hazmatFlow.setRegulatory(flowData.getRegulatory());
        hazmatFlow.setRoomName(flowData.getRoomName());
        hazmatFlow.setSchoolName(flowData.getSchoolName());
        hazmatFlow.setUsername(flowData.getUsername());
        hazmatFlow.setWeight(flowData.getWeight());
        hazmatFlow.setWeightUnit(flowData.getWeightUnit());
        hazmatFlow.setConsumption(flowData.getConsumption());
        if (flowData.getCreateTime() != null) {
            hazmatFlow.setCreateTime(flowData.getCreateTime());
        }
        return hazmatFlow;
    }
 
 
    public void checkUserAllowed(HzHazmatFlow hazmatFlow,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(hazmatFlow!=null){
            if (!user.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
                if(!Objects.equals(user.getCompanyId(), hazmatFlow.getCompanyId())){
                    throw new ApiException("无权限操作其他企业数据");
                }
            }
        }
    }
}