diff --git a/laws-modules/src/main/java/com/jero/modules/projectLibrary/controller/LawsProjectLibraryController.java b/laws-modules/src/main/java/com/jero/modules/projectLibrary/controller/LawsProjectLibraryController.java index 23d9bdd8..f8fbcc5b 100644 --- a/laws-modules/src/main/java/com/jero/modules/projectLibrary/controller/LawsProjectLibraryController.java +++ b/laws-modules/src/main/java/com/jero/modules/projectLibrary/controller/LawsProjectLibraryController.java @@ -1,21 +1,15 @@ package com.jero.modules.projectLibrary.controller; -import cn.hutool.core.io.IoUtil; -import cn.hutool.poi.excel.ExcelUtil; -import cn.hutool.poi.excel.ExcelWriter; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.jero.common.api.vo.Result; +import com.jero.common.api.vo.ResultCommon; import com.jero.common.aspect.annotation.AutoLog; import com.jero.common.aspect.annotation.DictPoint; import com.jero.common.constant.CommonConstant; import com.jero.common.constant.enums.YesOrNoEnum; import com.jero.common.exception.JeroBootException; import com.jero.common.system.base.controller.JeroController; -import com.jero.common.system.query.QueryGenerator; -import com.jero.common.util.MessageUtils; -import com.jero.common.api.vo.ResultCommon; import com.jero.modules.projectLibrary.entity.LawsAssess; import com.jero.modules.projectLibrary.entity.LawsAssessResults; import com.jero.modules.projectLibrary.entity.LawsProjectLibrary; @@ -23,13 +17,13 @@ import com.jero.modules.projectLibrary.service.ILawsAssessResultsService; import com.jero.modules.projectLibrary.service.ILawsAssessService; import com.jero.modules.projectLibrary.service.ILawsProjectLibraryService; import com.jero.modules.projectLibrary.vo.RiskOverviewVo; -import com.jero.modules.system.entity.SysUser; import com.jero.modules.system.service.ISysDictService; import com.jero.modules.system.service.ISysUserService; import com.jero.modules.system.vo.IdMapBody; import com.jero.modules.system.vo.IdsMapBody; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.formula.functions.T; @@ -37,15 +31,14 @@ import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.OutputStream; -import java.net.URLEncoder; +import java.io.IOException; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; @@ -209,4 +202,12 @@ public class LawsProjectLibraryController extends JeroController * 通过id查询 */ LawsProjectLibrary queryById(String id); + + /** + * 导入 + */ + void importPMSExcel(MultipartFile file, HttpServletResponse response) throws IOException; + } diff --git a/laws-modules/src/main/java/com/jero/modules/projectLibrary/service/impl/LawsProjectLibraryServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/projectLibrary/service/impl/LawsProjectLibraryServiceImpl.java index d94f4ac1..dc131f19 100644 --- a/laws-modules/src/main/java/com/jero/modules/projectLibrary/service/impl/LawsProjectLibraryServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/projectLibrary/service/impl/LawsProjectLibraryServiceImpl.java @@ -3,185 +3,304 @@ package com.jero.modules.projectLibrary.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jero.common.api.vo.ResultCommon; import com.jero.common.constant.CommonConstant; +import com.jero.common.exception.JeroBootException; +import com.jero.common.system.query.QueryGenerator; import com.jero.common.system.vo.LoginUser; +import com.jero.common.util.MessageUtils; import com.jero.modules.laws.common.util.CurrentUserUtil; import com.jero.modules.projectLibrary.common.AssessCommon; import com.jero.modules.projectLibrary.entity.LawsAssess; import com.jero.modules.projectLibrary.entity.LawsEvaluationNotMet; import com.jero.modules.projectLibrary.entity.LawsProjectLibrary; +import com.jero.modules.projectLibrary.entity.LawsProjectLibraryPMS; import com.jero.modules.projectLibrary.mapper.LawsProjectLibraryMapper; import com.jero.modules.projectLibrary.service.ILawsAssessService; import com.jero.modules.projectLibrary.service.ILawsEvaluationNotMetService; import com.jero.modules.projectLibrary.service.ILawsProjectLibraryService; import com.jero.modules.system.entity.SysUser; import com.jero.modules.system.service.ISysUserService; +import lombok.Cleanup; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; +import org.apache.poi.ss.usermodel.Workbook; +import org.jeecgframework.poi.excel.ExcelExportUtil; +import org.jeecgframework.poi.excel.ExcelImportUtil; +import org.jeecgframework.poi.excel.entity.ExportParams; +import org.jeecgframework.poi.excel.entity.ImportParams; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import com.jero.common.exception.JeroBootException; -import java.util.List; -import java.util.Date; -import java.util.Objects; - -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.jero.common.system.query.QueryGenerator; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; /** * @Description: 项目库 * @Author: jero-boot - * @Date: 2023-09-11 + * @Date: 2023-09-11 * @Version: V1.0 */ +@Slf4j @Service @Transactional(rollbackFor = JeroBootException.class) public class LawsProjectLibraryServiceImpl extends ServiceImpl implements ILawsProjectLibraryService { - @Resource - private ISysUserService sysUserService; - @Resource - private ILawsAssessService lawsAssessService; - @Resource - private ILawsEvaluationNotMetService notMetService; + @Resource + private ISysUserService sysUserService; + @Resource + private ILawsAssessService lawsAssessService; + @Resource + private ILawsEvaluationNotMetService notMetService; + /** * 分页查询 */ @Override public IPage queryPage(LawsProjectLibrary lawsProjectLibrary, Integer pageNo, Integer pageSize, - HttpServletRequest req) { + HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap()); Page page = new Page<>(pageNo, pageSize); - Page libraryPage = page(page, queryWrapper); - return libraryPage; + Page libraryPage = page(page, queryWrapper); + return libraryPage; } - @Override - public List queryList(LawsProjectLibrary lawsProjectLibrary, HttpServletRequest req) { - QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap()); - List list = list(queryWrapper); - return list; - } + @Override + public List queryList(LawsProjectLibrary lawsProjectLibrary, HttpServletRequest req) { + QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap()); + List list = list(queryWrapper); + return list; + } - /** - * 保存 - */ - @Override + public LawsProjectLibrary queryByWorkNumber(String WorkNumber){ + LawsProjectLibrary lawsProjectLibrary = new LawsProjectLibrary(); + lawsProjectLibrary.setWorkNumber(WorkNumber); + QueryWrapper queryWrapper = new QueryWrapper<>(lawsProjectLibrary); + List list = list(queryWrapper); + if(!list.isEmpty()){ + return list.get(0); + } + return null; + } + + /** + * 保存 + */ + @Override public void add(LawsProjectLibrary lawsProjectLibrary) { save(lawsProjectLibrary); } - /** - * 更新 - */ - @Override + /** + * 更新 + */ + @Override public void editById(LawsProjectLibrary lawsProjectLibrary) { - LawsProjectLibrary projectLibrary = getById(lawsProjectLibrary.getId()); - checkPermission(projectLibrary); - LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); - updateWrapper.eq(LawsProjectLibrary::getId,lawsProjectLibrary.getId()); - updateWrapper.set(LawsProjectLibrary::getProjectName,lawsProjectLibrary.getProjectName()); - updateWrapper.set(LawsProjectLibrary::getWorkNumber,lawsProjectLibrary.getWorkNumber()); - updateWrapper.set(LawsProjectLibrary::getProjectLeader,lawsProjectLibrary.getProjectLeader()); - updateWrapper.set(LawsProjectLibrary::getProjectHealth,lawsProjectLibrary.getProjectHealth()); - updateWrapper.set(LawsProjectLibrary::getProjectLevel,lawsProjectLibrary.getProjectLevel()); - this.update(updateWrapper); + LawsProjectLibrary projectLibrary = getById(lawsProjectLibrary.getId()); + checkPermission(projectLibrary); + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.eq(LawsProjectLibrary::getId, lawsProjectLibrary.getId()); + updateWrapper.set(LawsProjectLibrary::getProjectName, lawsProjectLibrary.getProjectName()); + updateWrapper.set(LawsProjectLibrary::getWorkNumber, lawsProjectLibrary.getWorkNumber()); + updateWrapper.set(LawsProjectLibrary::getProjectLeader, lawsProjectLibrary.getProjectLeader()); + updateWrapper.set(LawsProjectLibrary::getProjectHealth, lawsProjectLibrary.getProjectHealth()); + updateWrapper.set(LawsProjectLibrary::getProjectLevel, lawsProjectLibrary.getProjectLevel()); + this.update(updateWrapper); } - /** - * 校验权限 - * @param projectLibrary - */ - @Override - public void checkPermission(LawsProjectLibrary projectLibrary){ - LoginUser loginUser = CurrentUserUtil.getLoginUser(); - if (!loginUser.getIsAdmin()&& !Objects.equals(loginUser.getUsername(),projectLibrary.getCreateBy())){ - throw new JeroBootException(ResultCommon.NO_PERMISSIONS_PLEASE_SELECT); - } - } - /** - * 通过id删除 - */ - @Override + /** + * 校验权限 + * + * @param projectLibrary + */ + @Override + public void checkPermission(LawsProjectLibrary projectLibrary) { + LoginUser loginUser = CurrentUserUtil.getLoginUser(); + if (!loginUser.getIsAdmin() && !Objects.equals(loginUser.getUsername(), projectLibrary.getCreateBy())) { + throw new JeroBootException(ResultCommon.NO_PERMISSIONS_PLEASE_SELECT); + } + } + + /** + * 通过id删除 + */ + @Override public void deleteById(String id) { - LawsProjectLibrary projectLibrary = getById(id); - checkPermission(projectLibrary); - List allByProjectIdNotDeleted = lawsAssessService.getAllByProjectIdNotDeleted(id); - for (LawsAssess lawsAssess : allByProjectIdNotDeleted) { - String processStatus = lawsAssess.getProcessStatus(); - if (AssessCommon.UNDER_EVALUATION.equals(processStatus)){ - throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR2); - } - //未符合项中有整改中、审批中的数据时,不允许删除 - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId()); - queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId()); - List notMetList = notMetService.list(queryWrapper); - for (LawsEvaluationNotMet notMet : notMetList) { - String state = notMet.getState(); - if (AssessCommon.RECTIFICATION.equals(state)|| - AssessCommon.UNDER_APPROVAL.equals(state)){ - throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR); - } - } - } - removeById(id); + LawsProjectLibrary projectLibrary = getById(id); + checkPermission(projectLibrary); + List allByProjectIdNotDeleted = lawsAssessService.getAllByProjectIdNotDeleted(id); + for (LawsAssess lawsAssess : allByProjectIdNotDeleted) { + String processStatus = lawsAssess.getProcessStatus(); + if (AssessCommon.UNDER_EVALUATION.equals(processStatus)) { + throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR2); + } + //未符合项中有整改中、审批中的数据时,不允许删除 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId()); + queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId()); + List notMetList = notMetService.list(queryWrapper); + for (LawsEvaluationNotMet notMet : notMetList) { + String state = notMet.getState(); + if (AssessCommon.RECTIFICATION.equals(state) || + AssessCommon.UNDER_APPROVAL.equals(state)) { + throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR); + } + } + } + removeById(id); } - /** - * 批量删除 - */ - @Override + /** + * 批量删除 + */ + @Override public void deleteByIds(List ids) { - LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); - wrapper.in(LawsProjectLibrary::getId, ids); - List projectLibraryList = this.list(wrapper); - for (LawsProjectLibrary projectLibrary : projectLibraryList) { - checkPermission(projectLibrary); - String id = projectLibrary.getId(); - List allByProjectIdNotDeleted = lawsAssessService.getAllByProjectIdNotDeleted(id); - for (LawsAssess lawsAssess : allByProjectIdNotDeleted) { - String processStatus = lawsAssess.getProcessStatus(); - if (AssessCommon.UNDER_EVALUATION.equals(processStatus)){ - throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR2); - } - //未符合项中有整改中、审批中的数据时,不允许删除 - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId()); - queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId()); - List notMetList = notMetService.list(queryWrapper); - for (LawsEvaluationNotMet notMet : notMetList) { - String state = notMet.getState(); - if (AssessCommon.RECTIFICATION.equals(state)|| - AssessCommon.UNDER_APPROVAL.equals(state)){ - throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR); - } - } - } - } + LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); + wrapper.in(LawsProjectLibrary::getId, ids); + List projectLibraryList = this.list(wrapper); + for (LawsProjectLibrary projectLibrary : projectLibraryList) { + checkPermission(projectLibrary); + String id = projectLibrary.getId(); + List allByProjectIdNotDeleted = lawsAssessService.getAllByProjectIdNotDeleted(id); + for (LawsAssess lawsAssess : allByProjectIdNotDeleted) { + String processStatus = lawsAssess.getProcessStatus(); + if (AssessCommon.UNDER_EVALUATION.equals(processStatus)) { + throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR2); + } + //未符合项中有整改中、审批中的数据时,不允许删除 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId()); + queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId()); + List notMetList = notMetService.list(queryWrapper); + for (LawsEvaluationNotMet notMet : notMetList) { + String state = notMet.getState(); + if (AssessCommon.RECTIFICATION.equals(state) || + AssessCommon.UNDER_APPROVAL.equals(state)) { + throw new JeroBootException(ResultCommon.ASSESS_DELETE_ERROR); + } + } + } + } - wrapper.set(LawsProjectLibrary::getDelFlag, CommonConstant.DEL_FLAG_1.toString()); - update(wrapper); + wrapper.set(LawsProjectLibrary::getDelFlag, CommonConstant.DEL_FLAG_1.toString()); + update(wrapper); } - /** - * 通过id查询 - */ - @Override + /** + * 通过id查询 + */ + @Override public LawsProjectLibrary queryById(String id) { - LawsProjectLibrary library = getById(id); - String projectLeader = library.getProjectLeader(); - if (StringUtils.isNotBlank(projectLeader)){ - SysUser user = sysUserService.getById(projectLeader); - library.setNameWorkNo(user.calcNameWorkNo()); - } + LawsProjectLibrary library = getById(id); + String projectLeader = library.getProjectLeader(); + if (StringUtils.isNotBlank(projectLeader)) { + SysUser user = sysUserService.getById(projectLeader); + library.setNameWorkNo(user.calcNameWorkNo()); + } return library; } + + public void exportWithExcel(HttpServletResponse response, List dtoList, String fileName) { + try { + // 设置导出参数 + ExportParams exportParams = new ExportParams(null, fileName); // 可以为标题添加更多的配置 + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, dtoList.get(0).getClass(), dtoList); + + // 设置响应头和内容类型 + response.setContentType("application/vnd.ms-excel"); + response.setHeader("Content-Disposition", "attachment; filename=" + fileName); + + // 写出到响应流 + workbook.write(response.getOutputStream()); + workbook.close(); + } catch (Exception e) { + log.error("导出excel异常", e); + throw new JeroBootException(MessageUtils.getMessage(ResultCommon.EXPORT_ERROR), e); + } + } + + @Override + public void importPMSExcel(MultipartFile file, HttpServletResponse response) throws IOException { + log.info("开始转换并导入PMS车型项目库文件"); + List errImportList = new ArrayList<>(); + // 获取当前时间戳(毫秒) + long startTime = System.currentTimeMillis(); + @Cleanup + InputStream inputStream = file.getInputStream(); + ImportParams params = new ImportParams(); + params.setTitleRows(0); + params.setNeedSave(true); + List pmsList = new ArrayList<>(); + try { + pmsList = ExcelImportUtil.importExcel(inputStream, LawsProjectLibraryPMS.class, params); + } catch (Exception e) { + log.error("导入失败: " + e.getMessage()); + } + // 区分数据 + log.info("获取数据成功,总数据条数:{}", pmsList.size()); + // 获取当前时间戳(毫秒) + long endTime = System.currentTimeMillis(); + // 计算执行时间(毫秒) + long executionTime = endTime - startTime; + log.info("数据转换执行时间:" + executionTime + " 毫秒"); + + for (LawsProjectLibraryPMS pms : pmsList) { + LawsProjectLibrary lpl = new LawsProjectLibrary(); + //负责人工号-》id + SysUser user = sysUserService.getUserByName(pms.getProjectLeaderNo()); + String projectLeader = null; + if (ObjectUtils.isNotEmpty(user)) { + projectLeader = user.getId(); + }else{ + pms.setErrorInfo("找不到项目负责人"); + log.error(pms.toString()); + errImportList.add(pms); + continue; + } + //项目级别-》后两位 + String projectLevel = null; + if (StringUtils.isNotEmpty(pms.getProjectLevel()) && StringUtils.isNotBlank(pms.getProjectLevel())) { + String str = pms.getProjectLevel(); + projectLevel = str.substring(Math.max(str.length() - 2, 0)); + } + + // 赋值 + lpl.setProjectSource("2"); + lpl.setDelFlag("0"); + lpl.setProjectName(pms.getProjectName()); + lpl.setWorkNumber(pms.getWorkNumber()); + lpl.setProjectLeader(projectLeader); + lpl.setProjectLevel(projectLevel); + + //查重 + LawsProjectLibrary queryItem = queryByWorkNumber(lpl.getWorkNumber()); + if(ObjectUtils.isNotEmpty(queryItem)){ + pms.setErrorInfo("工作令编号已存在"); + log.error(pms.toString()); + errImportList.add(pms); + continue; + } + //保存 + this.add(lpl); + } + if (!errImportList.isEmpty()) { + log.info("导入失败数据条数:{}", errImportList.size()); + this.exportWithExcel(response, errImportList, "LawsProjectLibraryPMS-ImportErrorData.xls"); + } + log.info("PMS车型项目库导入结束"); + + } + }