cuijian
2023-10-30 059c6a5058295cd93741cf04c0a52a4a53e2c284
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/OperationCertificateController.java
@@ -3,6 +3,7 @@
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
@@ -10,9 +11,20 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.eam.entity.OperationCertificateApplyDetail;
import org.jeecg.modules.eam.service.IdentityService;
import org.jeecg.common.util.ImportExcelUtil;
import org.jeecg.modules.eam.entity.*;
import org.jeecg.modules.eam.service.*;
import org.jeecg.modules.eam.vo.OperationCertificateImportVo;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.SysIdentityService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
@@ -24,11 +36,7 @@
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.eam.entity.OperationCertificateDetail;
import org.jeecg.modules.eam.entity.OperationCertificate;
import org.jeecg.modules.eam.vo.OperationCertificatePage;
import org.jeecg.modules.eam.service.IOperationCertificateService;
import org.jeecg.modules.eam.service.IOperationCertificateDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -62,6 +70,16 @@
    @Autowired
    private IdentityService identityService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IEamEquipmentService equipmentService;
    @Autowired
    private IProfessionService professionService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;
   
   /**
    * 分页列表查询
@@ -259,37 +277,121 @@
    */
    //@RequiresPermissions("org.jeecg.modules.mdc:mom_eam_operation_certificate:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          // 获取上传文件对象
          MultipartFile file = entity.getValue();
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<OperationCertificatePage> list = ExcelImportUtil.importExcel(file.getInputStream(), OperationCertificatePage.class, params);
              for (OperationCertificatePage page : list) {
                  OperationCertificate po = new OperationCertificate();
                  BeanUtils.copyProperties(page, po);
                  operationCertificateService.saveMain(po, page.getOperationCertificateDetailList());
              }
              return Result.OK("文件导入成功!数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败!");
    }
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException  {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      // 错误信息
      List<String> errorMessage = new ArrayList<>();
      int successLines = 0, errorLines = 0,totalLines = 0,partSuccessLines = 0;
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
         // 获取上传文件对象
         MultipartFile file = entity.getValue();
         ImportParams params = new ImportParams();
         params.setNeedSave(true);
         try {
               List<OperationCertificateImportVo> list = ExcelImportUtil.importExcel(file.getInputStream(), OperationCertificateImportVo.class, params);
               totalLines = list.size();
               for (int i = 0; i < list.size(); i++) {
               OperationCertificateImportVo operationCertificateImportVo = list.get(i);
               OperationCertificate operationCertificate = new OperationCertificate();
               operationCertificate.setNum(identityService.getNumByTypeAndLength("OperationCertificate",4));
               //根据人员姓名查询人员id
               List<SysUser> userList = userService.lambdaQuery().eq(SysUser::getRealname,operationCertificateImportVo.getName().trim())
                     .eq(SysUser::getWorkNo,operationCertificateImportVo.getWorkNo().trim()).eq(SysUser::getDelFlag, CommonConstant.DEL_FLAG_0).list();
               if(CollectionUtils.isEmpty(userList)){
                  errorMessage.add("第" + (i + 1) + "行导入失败,原因:人员不存在或工作证号与人员姓名不匹配,请先维护人员信息;");
                  errorLines++;
                  continue;
               }else{
                  operationCertificate.setUserId(userList.get(0).getUsername());
               }
               //工种查询
               Profession profession = professionService.lambdaQuery().eq(Profession::getName,operationCertificateImportVo.getProfession().trim()).eq(Profession::getDelFlag,CommonConstant.DEL_FLAG_0).one();
               if(profession == null){
                  Profession profession1 = new Profession();
                  profession1.setNum(identityService.getNumByTypeAndLength("Profession",4));
                  profession1.setName(operationCertificateImportVo.getProfession().trim());
                  professionService.save(profession1);
                  operationCertificate.setProfession(profession1.getId());
               }else {
                  operationCertificate.setProfession(profession.getId());
               }
               //技能等级判断
               if(CollectionUtils.isEmpty(sysDictItemService.lambdaQuery().eq(SysDictItem::getDictId,sysDictService.lambdaQuery().eq(SysDict::getDictCode,"technical_level").one().getId()).eq(SysDictItem::getItemText,operationCertificateImportVo.getTechnicalLevel().trim()).list())){
                  errorMessage.add("第" + (i + 1) + "行导入失败,原因:技能等级不存在,请先维护技能等级;");
                  errorLines++;
                  continue;
               }else{
                  operationCertificate.setTechnicalLevel(sysDictItemService.lambdaQuery().eq(SysDictItem::getDictId,sysDictService.lambdaQuery().eq(SysDict::getDictCode,"technical_level").one().getId()).eq(SysDictItem::getItemText,operationCertificateImportVo.getTechnicalLevel().trim()).one().getItemValue());
               }
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
               operationCertificate.setIssueDate(operationCertificateImportVo.getIssueDate());
               operationCertificate.setStartTime(operationCertificateImportVo.getIssueDate());
               Date currentDate = operationCertificateImportVo.getIssueDate();
               Calendar c = Calendar.getInstance();
               c.setTime(currentDate);
               c.add(Calendar.YEAR, 1);
               currentDate = c.getTime();
               operationCertificate.setEndTime(currentDate);
               operationCertificate.setCurrentCycleScore(12);
               operationCertificate.setRemark(operationCertificateImportVo.getRemark());
               List<String> equipments  = Arrays.asList(operationCertificateImportVo.getEquipmentNum().split("/"));
               List<String> equipmentIds = new ArrayList<>();
               int a = 0;
               for(int j = 0; j<equipments.size();j++){
                  List<Equipment> equipmentList = equipmentService.lambdaQuery().eq(Equipment::getNum,equipments.get(j)).eq(Equipment::getDelFlag,CommonConstant.DEL_FLAG_0).list();
                  if(CollectionUtils.isNotEmpty(equipmentList)){
                     equipmentIds.add(equipmentList.get(0).getId());
                  }else {
                     errorMessage.add("第" + (i + 1) + "行统一编号为"+equipments.get(j)+"的设备导入失败,原因:设备台账中不存在该设备,请先维护设备信息;");
                     a++;
                     continue;
                  }
               }
               if(a == equipments.size()){
                  errorLines++;
                  a = 0;
                  continue;
               }else if(a > 0 && a < equipments.size()){
                  partSuccessLines++;
                  a = 0;
               }
               if(CollectionUtils.isEmpty(equipmentIds)){
                  errorMessage.add("第" + (i + 1) + "行导入失败,原因:设备不存在,请先维护设备信息;");
                  errorLines++;
                  continue;
               }else{
                  operationCertificate.setEquipmentIds(equipmentIds.stream().map(String::toString).collect(Collectors.joining(",")));
               }
               operationCertificate.setStatus("1");
               //查询当前人员是否已有操作证
               List<OperationCertificate> operationCertificateList = operationCertificateService.lambdaQuery().eq(OperationCertificate::getUserId,userList.get(0).getUsername()).eq(OperationCertificate::getDelFlag,CommonConstant.DEL_FLAG_0).list();
               if(CollectionUtils.isEmpty(operationCertificateList)){
                  operationCertificateService.save(operationCertificate);
               }else {
                  //将设备增加到操作证
                  String newEquipmentIds = operationCertificateList.get(0).getEquipmentIds().concat(",").concat(operationCertificate.getEquipmentIds());
                  operationCertificate.setId(operationCertificateList.get(0).getId());
                  HashSet<String> set = new HashSet<String>(Arrays.asList(newEquipmentIds.split(",")));
                  operationCertificate.setEquipmentIds(set.stream().collect(Collectors.joining(",")));
                  operationCertificateService.updateById(operationCertificate);
               }
               successLines++;
            }
         } catch (Exception e) {
            errorMessage.add("发生异常:" + e.getMessage());
            log.error(e.getMessage(), e);
            return (Result<?>) errorMessage;
         } finally {
            try {
               file.getInputStream().close();
            } catch (IOException e) {
               log.error(e.getMessage(), e);
            }
         }
      }
      return ImportExcelUtil.imporReturnOperationCertificate(totalLines,partSuccessLines,errorLines, successLines, errorMessage);
   }
    /**
     * 通过主表ID查询
     * @return