危化品全生命周期管理后端
“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
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
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.constant.UserConstant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.HazmatKindEnum;
import com.gkhy.hazmat.common.enums.HazmatPackageEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.excel.HazmatBasicExcelData;
import com.gkhy.hazmat.common.excel.HazmatBasicExcelDataListener;
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.common.utils.StringUtils;
import com.gkhy.hazmat.system.domain.HzHazmatBasic;
import com.gkhy.hazmat.system.domain.vo.HzSecientificVo;
import com.gkhy.hazmat.system.mapper.HzHazmatBasicMapper;
import com.gkhy.hazmat.system.mapper.HzSecientificMapper;
import com.gkhy.hazmat.system.service.HzHazmatBasicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * <p>
 * 危化品基础数据表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-05 14:41:40
 */
@Service
public class HzHazmatBasicServiceImpl extends ServiceImpl<HzHazmatBasicMapper, HzHazmatBasic> implements HzHazmatBasicService {
 
 
    @Autowired
    private HzSecientificMapper secientificMapper;
 
    @Override
    public CommonPage selectHazmatBasicList(HzHazmatBasic hazmatBasic) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && !currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())) {
            hazmatBasic.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzHazmatBasic> basicList = baseMapper.selectHazmatBasicList(hazmatBasic);
        return CommonPage.restPage(basicList);
    }
 
    @Override
    public HzHazmatBasic selectHazmatBasicById(Long hazmatBasicId) {
        HzHazmatBasic hazmatBasic = baseMapper.selectById(hazmatBasicId);
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            return hazmatBasic;
        } else if (!hazmatBasic.getCompanyId().equals(currentUser.getCompanyId())) {
            throw new ApiException("无权限查看其它企业数据");
        }
        return hazmatBasic;
    }
 
    @Override
    public int insertHazmatBasic(HzHazmatBasic hazmatBasic) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        hazmatBasic.setCreateBy(currentUser.getUsername());
        hazmatBasic.setCompanyId(currentUser.getCompanyId());
        if (!checkProductSnUnique(hazmatBasic)) {
            throw new ApiException("产品编号已存在");
        }
        checkUserAllowed(null,currentUser);
 
        HzSecientificVo hzSecientificVo = secientificMapper.selectBySecientificName(hazmatBasic.getName());
        hazmatBasic.setPeculiarityType(hzSecientificVo!=null? hzSecientificVo.getPeculiarityType() : null);
        hazmatBasic.setPeculiarityNumber(hzSecientificVo!=null? hzSecientificVo.getPeculiarityNumber() : 0);
        hazmatBasic.setSecientificId(hzSecientificVo!=null? hzSecientificVo.getId() : null);
 
        int row = baseMapper.insert(hazmatBasic);
        if (row < 1) {
            throw new ApiException("新增危化品基础信息失败");
        }
        return row;
    }
 
    @Override
    public int updateHazmatBasic(HzHazmatBasic hazmatBasic) {
        if (!checkProductSnUnique(hazmatBasic)) {
            throw new ApiException("产品编号已存在");
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(hazmatBasic,currentUser);
        hazmatBasic.setUpdateBy(currentUser.getUsername());
        HzSecientificVo hzSecientificVo = secientificMapper.selectBySecientificName(hazmatBasic.getName());
        hazmatBasic.setPeculiarityType(hzSecientificVo!=null? hzSecientificVo.getPeculiarityType() : null);
        hazmatBasic.setPeculiarityNumber(hzSecientificVo!=null? hzSecientificVo.getPeculiarityNumber() : 0);
        hazmatBasic.setSecientificId(hzSecientificVo!=null? hzSecientificVo.getId() : null);
        int row=baseMapper.updateById(hazmatBasic);
        if(row<1){
            throw new ApiException("更新危化品基础信息失败");
        }
        return row;
    }
 
    public void checkUserAllowed(HzHazmatBasic hazmatBasic,SysUser user) {
        if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            throw new ApiException("管理员不能操作");
        }
        if(hazmatBasic!=null){
            if(!Objects.equals(user.getCompanyId(), hazmatBasic.getCompanyId())){
                throw new ApiException("无权限操作其他企业数据");
            }
        }
    }
 
 
    @Override
    public int deleteHazmatBasicById(Long hazmatBasicId) {
        HzHazmatBasic hazmatBasic=baseMapper.selectById(hazmatBasicId);
        if(hazmatBasic==null){
            throw new ApiException("危化品基础信息不存在");
        }
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(hazmatBasic,currentUser);
        baseMapper.deleteHazmatBasicById(hazmatBasicId);
        return 0;
    }
 
    @Override
    public boolean checkProductSnUnique(HzHazmatBasic hazmatBasic) {
        Long hazmatBasicId=hazmatBasic.getId()==null?-1L:hazmatBasic.getId();
        HzHazmatBasic hb= baseMapper.checkProductSnUnique(hazmatBasic.getProductSn(),hazmatBasic.getCompanyId());
        if(hb!=null&&hb.getId().longValue()!=hazmatBasicId.longValue()){
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer importExcel(MultipartFile file) throws IOException {
        if(ObjectUtil.isEmpty(file)){
            throw new ApiException("上传对象不能为空");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        checkUserAllowed(null,currentUser);
        List<HazmatBasicExcelData> hazmatExcelDataList = EasyExcel.read(file.getInputStream(), HazmatBasicExcelData.class, new HazmatBasicExcelDataListener()).sheet().doReadSync();
        List<HzHazmatBasic> hazmatBasicList=new ArrayList<>();
 
        for(HazmatBasicExcelData hazmatBasicExcelData:hazmatExcelDataList){
            validateData(hazmatBasicExcelData);
            if (!checkProductSnUnique(new HzHazmatBasic().setProductSn(hazmatBasicExcelData.getProductSn()).setCompanyId(currentUser.getCompanyId()))) {
                throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"产品编号已存在");
            }
            HzHazmatBasic hazmatBasic=new HzHazmatBasic();
            BeanUtils.copyProperties(hazmatBasicExcelData,hazmatBasic,new String[]{"kind","minPackage"});
            Integer kind= HazmatKindEnum.getCodeByInfo(hazmatBasicExcelData.getKind());
            if(kind==null){
                throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"种类填写不正确");
            }
            Integer minPackage= HazmatPackageEnum.getCodeByInfo(hazmatBasicExcelData.getMinPackage());
            if(minPackage==null){
                throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"最小包装类型填写不正确");
            }
 
            HzSecientificVo hzSecientificVo = secientificMapper.selectBySecientificName(hazmatBasic.getName());
            hazmatBasic.setPeculiarityType(hzSecientificVo!=null? hzSecientificVo.getPeculiarityType() : "");
            hazmatBasic.setPeculiarityNumber(hzSecientificVo!=null? hzSecientificVo.getPeculiarityNumber() : 0);
            hazmatBasic.setSecientificId(hzSecientificVo!=null? hzSecientificVo.getId() : null);
 
            hazmatBasic.setKind(kind);
            hazmatBasic.setMinPackage(minPackage);
            hazmatBasic.setCompanyId(currentUser.getCompanyId());
            hazmatBasic.setCreateBy(currentUser.getUsername());
            hazmatBasicList.add(hazmatBasic);
        }
        if(!hazmatBasicList.isEmpty()){
            if(hazmatBasicList.size()>100){
                int pageSize=100;
                while (true){
                    List<HzHazmatBasic> hazmatBasics=hazmatBasicList.subList(0, Math.min(hazmatBasicList.size(), pageSize));
                    saveBatch(hazmatBasics);
                    if(hazmatBasics.size()<pageSize){
                        break;
                    }
                    hazmatBasicList=hazmatBasicList.subList(pageSize,hazmatBasicList.size());
                    if(hazmatBasicList.isEmpty()){
                        break;
                    }
                }
            }else{
                saveBatch(hazmatBasicList);
            }
        }
        return hazmatBasicList.size();
    }
 
 
    public void validateData(HazmatBasicExcelData hazmatBasicExcelData){
        if(StringUtils.isBlank(hazmatBasicExcelData.getName())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"名称为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getProductSn())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"产品编码为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getKind())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"种类为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getCas())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"CAS为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getHazmatType())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"试剂类型为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getHazmatCharacter())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"危险性质为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getSupplier())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"供应商为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getManufacturer())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"厂家为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getHazmatFormat())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"规格为空");
        }
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getMetering())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"包装数量为空");
        }
        if(StringUtils.isBlank(hazmatBasicExcelData.getUnit())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"包装单位为空");
        }
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getPrice())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"含税价格为空");
        }
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getMinPackage())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"最小包装类型为空");
        }
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getSafeNum())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"安全库存为空");
        }
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getThreshold())){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"超期阀值为空");
        }
 
        if(ObjectUtil.isEmpty(hazmatBasicExcelData.getMaxEntry())||hazmatBasicExcelData.getMaxEntry()<1){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"单次录入最大数量为空或者小于0");
        }
 
        if(hazmatBasicExcelData.getMetering().compareTo(BigDecimal.ZERO)<=0){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"包装数量小于0");
        }
 
        if(hazmatBasicExcelData.getPrice().compareTo(BigDecimal.ZERO)<=0){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"含税价格小于0");
        }
 
        if(ObjectUtil.isNotEmpty(hazmatBasicExcelData.getPerBox()) && hazmatBasicExcelData.getPerBox()<=0){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"每箱数量小于0");
        }
 
        if(hazmatBasicExcelData.getSafeNum()<0){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"安全库存小于0");
        }
 
        if(hazmatBasicExcelData.getThreshold()<0){
            throw new ApiException("序号"+hazmatBasicExcelData.getIndex()+"超期阀值小于0");
        }
 
 
    }
}