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.ProductBasicExcelData;
|
import com.gkhy.hazmat.common.excel.ProductBasicExcelDataListener;
|
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.vo.HzSecientificVo;
|
import com.gkhy.hazmat.system.mapper.HzPeculiarityMapper;
|
import com.gkhy.hazmat.system.mapper.HzProductBasicMapper;
|
import com.gkhy.hazmat.system.mapper.HzSecientificMapper;
|
import com.gkhy.hazmat.system.service.HzProductBasicService;
|
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-06 16:03:53
|
*/
|
@Service
|
public class HzProductBasicServiceImpl extends ServiceImpl<HzProductBasicMapper, HzProductBasic> implements HzProductBasicService {
|
|
@Autowired
|
private HzSecientificMapper secientificMapper;
|
|
@Override
|
public CommonPage selectProductBasicList(HzProductBasic productBasic) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
productBasic.setCompanyId(currentUser.getCompanyId());
|
}
|
PageUtils.startPage();
|
List<HzProductBasic> basicList = baseMapper.selectProductBasicList(productBasic);
|
return CommonPage.restPage(basicList);
|
}
|
|
@Override
|
public HzProductBasic selectProductBasicById(Long productBasicId) {
|
HzProductBasic productBasic = baseMapper.selectById(productBasicId);
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
return productBasic;
|
} else if (!productBasic.getCompanyId().equals(currentUser.getCompanyId())) {
|
throw new ApiException("无权限查看其它企业数据");
|
}
|
return productBasic;
|
}
|
|
@Override
|
public int insertProductBasic(HzProductBasic productBasic) {
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
productBasic.setCreateBy(currentUser.getUsername());
|
productBasic.setCompanyId(currentUser.getCompanyId());
|
|
HzSecientificVo hzSecientificVo = secientificMapper.selectBySecientificName(productBasic.getName());
|
productBasic.setPeculiarityType(hzSecientificVo!=null? hzSecientificVo.getPeculiarityType() : null);
|
productBasic.setPeculiarityNumber(hzSecientificVo!=null ? hzSecientificVo.getPeculiarityNumber() : 0);
|
|
if (!checkProductSnUnique(productBasic)) {
|
throw new ApiException("产品编号已存在");
|
}
|
checkUserAllowed(null,currentUser);
|
int row = baseMapper.insert(productBasic);
|
if (row < 1) {
|
throw new ApiException("新增成品基础信息失败");
|
}
|
return row;
|
}
|
|
@Override
|
public int updateProductBasic(HzProductBasic productBasic) {
|
if (!checkProductSnUnique(productBasic)) {
|
throw new ApiException("产品编号已存在");
|
}
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(productBasic,currentUser);
|
productBasic.setUpdateBy(currentUser.getUsername());
|
int row=baseMapper.updateById(productBasic);
|
if(row<1){
|
throw new ApiException("更新成品基础信息失败");
|
}
|
return row;
|
}
|
|
public void checkUserAllowed(HzProductBasic productBasic,SysUser user) {
|
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
|
throw new ApiException("管理员不能操作");
|
}
|
if(productBasic!=null){
|
if(!Objects.equals(user.getCompanyId(), productBasic.getCompanyId())){
|
throw new ApiException("无权限操作其他企业数据");
|
}
|
}
|
}
|
|
@Override
|
public int deleteProductBasicById(Long productBasicId) {
|
HzProductBasic productBasic=baseMapper.selectById(productBasicId);
|
if(productBasic==null){
|
throw new ApiException("成品基础信息不存在");
|
}
|
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
|
checkUserAllowed(productBasic,currentUser);
|
baseMapper.deleteProductBasicById(productBasicId);
|
return 0;
|
}
|
|
@Override
|
public boolean checkProductSnUnique(HzProductBasic productBasic) {
|
Long productBasicId=productBasic.getId()==null?-1L:productBasic.getId();
|
HzProductBasic pb= baseMapper.checkProductSnUnique(productBasic.getProductSn(),productBasic.getCompanyId());
|
if(pb!=null&&pb.getId().longValue()!=productBasicId.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<ProductBasicExcelData> productExcelDataList = EasyExcel.read(file.getInputStream(), ProductBasicExcelData.class, new ProductBasicExcelDataListener()).sheet().doReadSync();
|
List<HzProductBasic> productBasicList=new ArrayList<>();
|
|
for(ProductBasicExcelData productBasicExcelData:productExcelDataList){
|
validateData(productBasicExcelData);
|
if (!checkProductSnUnique(new HzProductBasic().setProductSn(productBasicExcelData.getProductSn()).setCompanyId(currentUser.getCompanyId()))) {
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"产品编号已存在");
|
}
|
HzProductBasic productBasic=new HzProductBasic();
|
BeanUtils.copyProperties(productBasicExcelData,productBasic,new String[]{"kind","minPackage"});
|
Integer kind= HazmatKindEnum.getCodeByInfo(productBasicExcelData.getKind());
|
if(kind==null){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"种类填写不正确");
|
}
|
Integer minPackage= HazmatPackageEnum.getCodeByInfo(productBasicExcelData.getMinPackage());
|
if(minPackage==null){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"最小包装类型填写不正确");
|
}
|
HzSecientificVo hzSecientificVo = secientificMapper.selectBySecientificName(productBasic.getName());
|
productBasic.setPeculiarityType(hzSecientificVo!=null? hzSecientificVo.getPeculiarityType() : null);
|
productBasic.setPeculiarityNumber(hzSecientificVo!=null ? hzSecientificVo.getPeculiarityNumber() : 0);
|
productBasic.setKind(kind);
|
productBasic.setMinPackage(minPackage);
|
productBasic.setCompanyId(currentUser.getCompanyId());
|
productBasic.setCreateBy(currentUser.getUsername());
|
productBasicList.add(productBasic);
|
}
|
if(!productBasicList.isEmpty()){
|
if(productBasicList.size()>100){
|
int pageSize=100;
|
while (true){
|
List<HzProductBasic> hazmatBasics=productBasicList.subList(0, Math.min(productBasicList.size(), pageSize));
|
saveBatch(hazmatBasics);
|
if(hazmatBasics.size()<pageSize){
|
break;
|
}
|
productBasicList=productBasicList.subList(pageSize,productBasicList.size());
|
if(productBasicList.isEmpty()){
|
break;
|
}
|
}
|
}else{
|
saveBatch(productBasicList);
|
}
|
}
|
return productBasicList.size();
|
}
|
|
|
public void validateData(ProductBasicExcelData productBasicExcelData){
|
if(StringUtils.isBlank(productBasicExcelData.getName())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"名称为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getProductSn())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"产品编码为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getKind())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"种类为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getCas())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"CAS为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getProductType())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"试剂类型为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getProductCharacter())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"危险性质为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getSupplier())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"供应商为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getManufacturer())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"厂家为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getProductFormat())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"规格为空");
|
}
|
if(ObjectUtil.isEmpty(productBasicExcelData.getMetering())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"包装数量为空");
|
}
|
if(StringUtils.isBlank(productBasicExcelData.getUnit())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"包装单位为空");
|
}
|
if(ObjectUtil.isEmpty(productBasicExcelData.getPrice())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"含税价格为空");
|
}
|
if(ObjectUtil.isEmpty(productBasicExcelData.getMinPackage())){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"最小包装类型为空");
|
}
|
|
if(ObjectUtil.isEmpty(productBasicExcelData.getMaxEntry())||productBasicExcelData.getMaxEntry()<1){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"单次录入最大数量为空或者小于0");
|
}
|
|
if(productBasicExcelData.getMetering().compareTo(BigDecimal.ZERO)<=0){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"包装数量小于0");
|
}
|
|
if(productBasicExcelData.getPrice().compareTo(BigDecimal.ZERO)<=0){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"含税价格小于0");
|
}
|
|
if(ObjectUtil.isNotEmpty(productBasicExcelData.getPerBox()) && productBasicExcelData.getPerBox()<=0){
|
throw new ApiException("序号"+productBasicExcelData.getIndex()+"每箱数量小于0");
|
}
|
|
|
|
}
|
}
|