PMS车型项目库导入

This commit is contained in:
Xia Zekun
2024-04-03 11:34:55 +08:00
committed by fengchenchen
parent 60ed3f3460
commit b24553177e
4 changed files with 307 additions and 134 deletions
@@ -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<LawsProjectLibr
return super.exportXls(request,lawsProjectLibrary,LawsProjectLibrary.class,"车型项目表");
}
@AutoLog(value = "车型项目库-PMS格式导入")
@ApiOperation(value="车型项目库-PMS格式导入", notes="车型项目库-PMS格式导入")
@PostMapping("/importPMSExcel")
public void importPMSExcel(@RequestParam("file") @ApiParam("文件") MultipartFile file,
HttpServletResponse response) throws IOException {
lawsProjectLibraryService.importPMSExcel(file, response);
}
}
@@ -0,0 +1,42 @@
package com.jero.modules.projectLibrary.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
/**
* @author: xzk
* @Date: 2024年4月2日 09:39:56
*/
@Data
public class LawsProjectLibraryPMS {
@Excel(name = "项目名称", width = 15, orderNum = "0")
private String projectName;
@Excel(name = "工作令编号", width = 15, orderNum = "1")
private String workNumber;
@Excel(name = "项目负责人", width = 15, orderNum = "2")
private String projectLeaderNo;
@Excel(name = "项目级别", width = 15, orderNum = "3")
private String projectLevel;
@Excel(name = "错误信息", width = 35, orderNum = "4")
@TableField(exist = false)
private String errorInfo;
/**
* 判断是否是空行
*
* @return 如果所有字段都为null或空字符串,返回true,否则返回false
*/
public boolean isEmptyRow() {
return projectName == null
&& workNumber == null
&& projectLeaderNo == null
&& projectLevel == null;
}
}
@@ -3,7 +3,12 @@ package com.jero.modules.projectLibrary.service;
import com.jero.modules.projectLibrary.entity.LawsProjectLibrary;
import com.baomidou.mybatisplus.extension.service.IService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
/**
@@ -47,4 +52,10 @@ public interface ILawsProjectLibraryService extends IService<LawsProjectLibrary>
* 通过id查询
*/
LawsProjectLibrary queryById(String id);
/**
* 导入
*/
void importPMSExcel(MultipartFile file, HttpServletResponse response) throws IOException;
}
@@ -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<LawsProjectLibraryMapper, LawsProjectLibrary> 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<LawsProjectLibrary> queryPage(LawsProjectLibrary lawsProjectLibrary, Integer pageNo, Integer pageSize,
HttpServletRequest req) {
HttpServletRequest req) {
QueryWrapper<LawsProjectLibrary> queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap());
Page<LawsProjectLibrary> page = new Page<>(pageNo, pageSize);
Page<LawsProjectLibrary> libraryPage = page(page, queryWrapper);
return libraryPage;
Page<LawsProjectLibrary> libraryPage = page(page, queryWrapper);
return libraryPage;
}
@Override
public List<LawsProjectLibrary> queryList(LawsProjectLibrary lawsProjectLibrary, HttpServletRequest req) {
QueryWrapper<LawsProjectLibrary> queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap());
List<LawsProjectLibrary> list = list(queryWrapper);
return list;
}
@Override
public List<LawsProjectLibrary> queryList(LawsProjectLibrary lawsProjectLibrary, HttpServletRequest req) {
QueryWrapper<LawsProjectLibrary> queryWrapper = QueryGenerator.initQueryWrapper(lawsProjectLibrary, req.getParameterMap());
List<LawsProjectLibrary> list = list(queryWrapper);
return list;
}
/**
* 保存
*/
@Override
public LawsProjectLibrary queryByWorkNumber(String WorkNumber){
LawsProjectLibrary lawsProjectLibrary = new LawsProjectLibrary();
lawsProjectLibrary.setWorkNumber(WorkNumber);
QueryWrapper<LawsProjectLibrary> queryWrapper = new QueryWrapper<>(lawsProjectLibrary);
List<LawsProjectLibrary> 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<LawsProjectLibrary> 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<LawsProjectLibrary> 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<LawsAssess> 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<LawsEvaluationNotMet> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId());
queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId());
List<LawsEvaluationNotMet> 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<LawsAssess> 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<LawsEvaluationNotMet> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId());
queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId());
List<LawsEvaluationNotMet> 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<String> ids) {
LambdaUpdateWrapper<LawsProjectLibrary> wrapper = new LambdaUpdateWrapper<>();
wrapper.in(LawsProjectLibrary::getId, ids);
List<LawsProjectLibrary> projectLibraryList = this.list(wrapper);
for (LawsProjectLibrary projectLibrary : projectLibraryList) {
checkPermission(projectLibrary);
String id = projectLibrary.getId();
List<LawsAssess> 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<LawsEvaluationNotMet> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId());
queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId());
List<LawsEvaluationNotMet> 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<LawsProjectLibrary> wrapper = new LambdaUpdateWrapper<>();
wrapper.in(LawsProjectLibrary::getId, ids);
List<LawsProjectLibrary> projectLibraryList = this.list(wrapper);
for (LawsProjectLibrary projectLibrary : projectLibraryList) {
checkPermission(projectLibrary);
String id = projectLibrary.getId();
List<LawsAssess> 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<LawsEvaluationNotMet> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(LawsEvaluationNotMet::getProjectId, lawsAssess.getProjectId());
queryWrapper.in(LawsEvaluationNotMet::getStandardId, lawsAssess.getStandardId());
List<LawsEvaluationNotMet> 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 <T> void exportWithExcel(HttpServletResponse response, List<T> 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<LawsProjectLibraryPMS> errImportList = new ArrayList<>();
// 获取当前时间戳(毫秒)
long startTime = System.currentTimeMillis();
@Cleanup
InputStream inputStream = file.getInputStream();
ImportParams params = new ImportParams();
params.setTitleRows(0);
params.setNeedSave(true);
List<LawsProjectLibraryPMS> 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车型项目库导入结束");
}
}