危化品全生命周期管理后端
“djh”
2025-03-04 48bdd6d0b6c13c99dfc8ec5801d70a056d9db315
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
package com.gkhy.hazmat.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.constant.Constant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.EntryStateEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.enums.WarehouseRecordEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.service.RedisService;
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.HzEntryTransferVO;
import com.gkhy.hazmat.system.domain.vo.TabooDisVo;
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzEntryRecordService;
import com.gkhy.hazmat.system.service.HzHazmatService;
import com.gkhy.hazmat.system.service.TaBooComparisonService;
import com.gkhy.hazmat.system.service.TabooWarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 系统配置表 服务实现类
 * </p>
 *
 * @author hh
 * @since 2025-02-13 08:39:55
 */
@Service
public class TaBooWarningServiceImpl extends ServiceImpl<HzTabooWarningMapper, HzTabooWarning> implements TabooWarningService {
 
    @Autowired
    private RedisService redisService;
    @Autowired
    private HzHazmatService hazmatService;
    @Autowired
    private HzHazmatBasicMapper hazmatBasicMapper;
    @Autowired
    private HzHazmatMapper hazmatMapper;
    @Autowired
    private HzWarehouseRecordMapper warehouseRecordMapper;
    @Autowired
    private HzEntryRecordService entryRecordService;
 
 
    @Override
    public CommonPage selectHzTabooWarningPageList(HzTabooWarning hzTabooWarning) {
        PageUtils.startPage();
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        hzTabooWarning.setCompanyId(currentUser.getCompanyId());
        List<HzTabooWarning> hzTabooWarnings = baseMapper.selectHzTabooWarningList(hzTabooWarning);
        return CommonPage.restPage(hzTabooWarnings);
    }
 
    @Override
    public List<HzTabooWarning> selectHzTabooWarningList(HzTabooWarning tabooWarning) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        tabooWarning.setCompanyId(currentUser.getCompanyId());
        tabooWarning.setState(0);
        return baseMapper.selectHzTabooWarningList(tabooWarning);
    }
 
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void doTransfer(HzEntryTransferVO entryTransferVO) {
        HzTabooWarning hzTabooWarning = baseMapper.selectById(entryTransferVO.getId());
        if (hzTabooWarning == null){
            throw new ApiException("相忌报警信息不存在");
        }
        Long entryId = hzTabooWarning.getEntryId();
 
        HzEntryRecord entryRecord=entryRecordService.getById(entryId);
        if (entryRecord == null){
            throw new ApiException("入库信息不存在");
        }
        if (!entryRecord.getState().toString().equals(EntryStateEnum.ENTER.getCode().toString())){
            throw new ApiException("入库状态不正确");
        }
        if (entryRecord.getCupboardId().equals(entryTransferVO.getCupboardId())){
            throw new ApiException("不能转入原存储柜");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(entryRecord,currentUser);
        HzHazmatBasic hazmatBasic=hazmatBasicMapper.selectById(entryRecord.getBasicId());
        if(hazmatBasic==null){
            throw new ApiException("危化品基础数据不存在");
        }
 
        //获取当前仓库库存
        //设置分表id
        IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
 
 
        int countOut = hazmatMapper.selectHazmatCountOfWarehouse(entryRecord.getWarehouseId(), hazmatBasic.getId(), currentUser.getCompanyId(), entryRecord.getCupboardId());
 
        //新增危化品变动记录转出
        HzWarehouseRecord warehouseRecordOut = new HzWarehouseRecord()
                .setWarehouseId(entryRecord.getWarehouseId())
                .setCupboardId(entryRecord.getCupboardId())
                .setBasicId(hazmatBasic.getId())
                .setNum(entryRecord.getNum())
                .setCompanyId(currentUser.getCompanyId())
                .setCreateId(currentUser.getId())
                .setRemaining(countOut - entryRecord.getNum())
                .setState(WarehouseRecordEnum.TRANSFEROUT.getCode());
        warehouseRecordMapper.insert(warehouseRecordOut);
 
 
        int count = hazmatMapper.selectHazmatCountOfWarehouse(entryTransferVO.getWarehouseId(), hazmatBasic.getId(), currentUser.getCompanyId(), entryTransferVO.getCupboardId());
        //新增危化品变动记录转入
        HzWarehouseRecord warehouseRecord = new HzWarehouseRecord()
                .setWarehouseId(entryTransferVO.getWarehouseId())
                .setCupboardId(entryTransferVO.getCupboardId())
                .setBasicId(hazmatBasic.getId())
                .setNum(entryRecord.getNum())
                .setCompanyId(currentUser.getCompanyId())
                .setCreateId(currentUser.getId())
                .setRemaining(entryRecord.getNum() + count)
                .setState(WarehouseRecordEnum.TRANSFERIN.getCode());
        warehouseRecordMapper.insert(warehouseRecord);
 
        // 校验生成相忌数据
        Map<String, Long>  redata = (Map<String, Long>)redisService.get(Constant.TABOO_UNIQUE_KEY);
        if (!redata.isEmpty()){
            // 查询对应仓库和柜子的物品信息获取品类 获取相冲相弱相吸相异
            List<TabooDisVo> collectData =
                    hazmatMapper.selectHazmatWarehouseCheck(entryTransferVO.getWarehouseId(), currentUser.getCompanyId(), entryTransferVO.getCupboardId());
            if (!collectData.isEmpty() && collectData.size() > 0) {
                for (TabooDisVo collectDatum : collectData) {
                    String key1 = collectDatum.getSpNum() + "_" + hazmatBasic.getPeculiarityNumber();
                    String key2 = hazmatBasic.getPeculiarityNumber() + "_" + collectDatum.getSpNum();
 
                    if (redata.containsKey(key1) || redata.containsKey(key2)) {
                        // 记录数据
                        Long l = redata.get(key1);
                        Long l1 = l == null ? redata.get(key2) : l;
                        HzTabooWarning tabooWarning = new HzTabooWarning()
                                .setWarningType(l1)
                                .setWarehouseId(entryTransferVO.getWarehouseId())
                                .setCupboardId(entryTransferVO.getCupboardId())
                                .setEntryId(entryRecord.getId())
                                .setCompanyId(currentUser.getCompanyId())
                                .setTabooBasicId(collectDatum.getId()).setCreateBy(currentUser.getUsername());
                        baseMapper.insert(tabooWarning);
                        break;
                    }
                }
            }
 
        }else {
            throw new ApiException("危化品相忌信息不存在");
        }
        //更新危化品信息
        hazmatService.update(new LambdaUpdateWrapper<HzHazmat>().eq(HzHazmat::getEntryId,entryRecord.getId())
                .set(HzHazmat::getWarehouseId,entryTransferVO.getWarehouseId())
                .set(HzHazmat::getCupboardId,entryTransferVO.getCupboardId())
                .set(HzHazmat::getUpdateBy,currentUser.getUsername()));
 
        // }
        //更新入库记录状态
        entryRecordService.update(new LambdaUpdateWrapper<HzEntryRecord>()
                .eq(HzEntryRecord::getId,entryRecord.getId()).set(HzEntryRecord::getWarehouseId,entryTransferVO.getWarehouseId())
                .set(HzEntryRecord::getCupboardId,entryTransferVO.getCupboardId())
                .set(HzEntryRecord::getUpdateBy,currentUser.getUsername()));
        IdTableNameHandler.removeCurrentId();
 
        //处理数据本身
 
         baseMapper.updateById(new HzTabooWarning().setState(1).
                 setUpdateBy(currentUser.getUsername()).setReCupboardId(entryTransferVO.getCupboardId()).setReWarehouseId(entryTransferVO.getWarehouseId())
                 .setId(entryTransferVO.getId()));
 
    }
    public void checkUserAllowed(HzEntryRecord entryRecord,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(entryRecord!=null){
            if(!Objects.equals(user.getCompanyId(), entryRecord.getCompanyId())){
                throw new ApiException("无权限操作其他企业数据");
            }
        }
    }
 
}