From 06af2ffa26ed44bb2a8a60c0618f8d0490fdd6ed Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: 星期三, 05 三月 2025 08:38:22 +0800
Subject: [PATCH] 用量统计页面及导出 修改

---
 hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatBasicServiceImpl.java |  158 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 158 insertions(+), 0 deletions(-)

diff --git a/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatBasicServiceImpl.java b/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatBasicServiceImpl.java
index 6e67893..5a41154 100644
--- a/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatBasicServiceImpl.java
+++ b/hazmat-system/src/main/java/com/gkhy/hazmat/system/service/impl/HzHazmatBasicServiceImpl.java
@@ -1,18 +1,34 @@
 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;
 
@@ -26,6 +42,10 @@
  */
 @Service
 public class HzHazmatBasicServiceImpl extends ServiceImpl<HzHazmatBasicMapper, HzHazmatBasic> implements HzHazmatBasicService {
+
+
+    @Autowired
+    private HzSecientificMapper secientificMapper;
 
     @Override
     public CommonPage selectHazmatBasicList(HzHazmatBasic hazmatBasic) {
@@ -59,6 +79,11 @@
             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);
+
         int row = baseMapper.insert(hazmatBasic);
         if (row < 1) {
             throw new ApiException("新增危化品基础信息失败");
@@ -114,4 +139,137 @@
         }
         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.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");
+        }
+
+
+    }
 }

--
Gitblit v1.9.2