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 implements HzSecientificService { @Override public CommonPage selectSecientific(HzSecientific hzSecientific) { SysUser user = SecurityUtils.getLoginUser().getUser(); PageUtils.startPage(); List 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 = EasyExcel.read(file.getInputStream(), SecientificExcelData.class, new SecientificExcelDataListener()).sheet().doReadSync(); List 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 secientifics = hzSecientifics.subList(0, Math.min(hzSecientifics.size(), pageSize)); saveBatch(secientifics); if(secientifics.size()