危化品全生命周期管理后端
git
heheng
2025-03-06 07a6e79342c381d4d8c98a92a216c41dc9502a09
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
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.domain.entity.SysUser;
import com.gkhy.hazmat.common.excel.ProductBasicExcelData;
import com.gkhy.hazmat.common.excel.ProductBasicExcelDataListener;
import com.gkhy.hazmat.common.excel.SecientificExcelData;
import com.gkhy.hazmat.common.excel.SecientificExcelDataListener;
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.HzProductBasic;
import com.gkhy.hazmat.system.domain.HzSecientific;
import com.gkhy.hazmat.system.domain.vo.HzSecientificVo;
import com.gkhy.hazmat.system.mapper.HzSecientificMapper;
import com.gkhy.hazmat.system.service.HzSecientificService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class HzSecientificServiceImpl extends ServiceImpl<HzSecientificMapper, HzSecientific> implements HzSecientificService {
 
    @Override
    public CommonPage selectSecientific(HzSecientific hzSecientific) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        PageUtils.startPage();
        List<HzSecientificVo> hzSecientificVos = baseMapper.selectSecientificBatch(hzSecientific);
        return CommonPage.restPage(hzSecientificVos);
    }
 
    @Override
    public int insertSecientific(HzSecientific hzSecientific) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        hzSecientific.setCreateBy(user.getUsername());
        hzSecientific.setCreateTime(LocalDateTime.now());
        int insert = baseMapper.insert(hzSecientific);
        if (insert<0){
            throw new ApiException("新增失败");
        }
        return insert;
    }
 
    @Override
    public int updateSecientific(HzSecientific hzSecientific) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        hzSecientific.setUpdateBy(user.getUsername());
        hzSecientific.setUpdateTime(LocalDateTime.now());
        int row = baseMapper.updateById(hzSecientific);
        if (row<0){
            throw new ApiException("修改失败");
        }
        return row;
    }
 
    @Override
    public int deleteSecientific(Long secientificId) {
        int row = baseMapper.deleteById(secientificId);
        if (row<0){
            throw new ApiException("删除失败");
        }
        return row;
    }
 
    @Override
    public Integer importExcel(MultipartFile file) throws IOException {
        if(ObjectUtil.isEmpty(file)){
            throw new ApiException("上传对象不能为空");
        }
        List<SecientificExcelData> secientificExcelData = EasyExcel.read(file.getInputStream(), SecientificExcelData.class, new SecientificExcelDataListener()).sheet().doReadSync();
        List<HzSecientific> hzSecientifics = new ArrayList<>();
 
        SysUser user = SecurityUtils.getLoginUser().getUser();
 
        for (SecientificExcelData secientificExcelDatum : secientificExcelData) {
            checkData(secientificExcelDatum);
            HzSecientific hzSecientific = new HzSecientific();
            BeanUtils.copyProperties(secientificExcelDatum,hzSecientific);
            hzSecientific.setCreateBy(user.getUsername());
            hzSecientific.setCreateTime(LocalDateTime.now());
            hzSecientifics.add(hzSecientific);
        }
 
        if(!hzSecientifics.isEmpty()){
            if(hzSecientifics.size()>100){
                int pageSize=100;
                while (true){
                    List<HzSecientific> secientifics = hzSecientifics.subList(0, Math.min(hzSecientifics.size(), pageSize));
                    saveBatch(secientifics);
                    if(secientifics.size()<pageSize){
                        break;
                    }
                    hzSecientifics=hzSecientifics.subList(pageSize,hzSecientifics.size());
                    if(hzSecientifics.isEmpty()){
                        break;
                    }
                }
            }else{
                saveBatch(hzSecientifics);
            }
        }
        return hzSecientifics.size();
    }
 
    private void checkData(SecientificExcelData secientificExcelDatum) {
        if(StringUtils.isBlank(secientificExcelDatum.getSecientificName())){
            throw new ApiException("序号"+secientificExcelDatum.getIndex()+"品名为空"+"危险性类别:"+secientificExcelDatum.getRiskType());
        }
//        if(StringUtils.isBlank(secientificExcelDatum.getCas())){
//            throw new ApiException("序号"+secientificExcelDatum.getIndex()+"CAS码为空");
//        }
//        if(StringUtils.isBlank(secientificExcelDatum.getRiskType())){
//            throw new ApiException("序号"+secientificExcelDatum.getIndex()+"危险性类别为空");
//        }
//        if(secientificExcelDatum.getPeculiarityNumber() == null){
//            throw new ApiException("序号"+secientificExcelDatum.getIndex()+"危化品特性编码为空");
//        }
    }
}