SaasProjectExcelDataHandler.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.persagy.person.manage;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import org.springframework.beans.BeanUtils;
  8. import com.alibaba.excel.context.AnalysisContext;
  9. import com.alibaba.excel.event.AnalysisEventListener;
  10. import com.alibaba.excel.exception.ExcelAnalysisException;
  11. import com.persagy.common.constant.SaasCommonConstant;
  12. import com.persagy.common.model.BaseUserModel;
  13. import com.persagy.common.utils.IdGenerator;
  14. import com.persagy.common.utils.StringUtil;
  15. import com.persagy.person.pojo.dto.SaasArea;
  16. import com.persagy.person.pojo.dto.SaasProject;
  17. import com.persagy.person.pojo.vo.project.SaasProjectExcel;
  18. import com.persagy.person.service.ISaasAreaService;
  19. import com.persagy.person.service.ISaasProjectService;
  20. import cn.hutool.core.collection.CollectionUtil;
  21. import lombok.extern.slf4j.Slf4j;
  22. /**
  23. * 传感器数据导入, 此类不能被spring管理,要每次读取excel都要new
  24. *
  25. * @version v1.0.0
  26. * @company persagy
  27. * @author zhangqiankun
  28. * @date 2020-11-25 19:52:37
  29. */
  30. @Slf4j
  31. public class SaasProjectExcelDataHandler extends AnalysisEventListener<SaasProjectExcel> {
  32. private BaseUserModel baseVO;
  33. private ISaasAreaService saasAreaService;
  34. private ISaasProjectService saasProjectService;
  35. /**
  36. * 上传的数据
  37. */
  38. private List<SaasProject> list = new ArrayList<SaasProject>();
  39. private Set<String> validProjectId = new HashSet<String>();
  40. private Set<String> validLocalId = new HashSet<String>();
  41. private Set<String> validProjectName = new HashSet<String>();
  42. public SaasProjectExcelDataHandler() {
  43. }
  44. public SaasProjectExcelDataHandler(BaseUserModel baseVO, ISaasAreaService saasAreaService, ISaasProjectService saasProjectService) {
  45. this.baseVO = baseVO;
  46. this.saasAreaService = saasAreaService;
  47. this.saasProjectService = saasProjectService;
  48. }
  49. /**
  50. * 每一条数据解析都会来调用
  51. *
  52. * @param data one row value {@link AnalysisContext#readRowHolder()}
  53. * @param context
  54. */
  55. @Override
  56. public void invoke(SaasProjectExcel data, AnalysisContext context) {
  57. // 数据不可为空校验
  58. int rowIndex = context.readRowHolder().getRowIndex();
  59. if (list.size() > 299) {
  60. throw new ExcelAnalysisException("导入数据量过大,不允许超过300条!");
  61. }
  62. if (!StringUtil.isAllNotBlank(data.getGroupCode(), data.getProjectId(), data.getProjectLocalId(), data.getProjectName())) {
  63. throw new ExcelAnalysisException("第" + rowIndex + "行数据,红色必填项存在为空的单元格,请检查!");
  64. }
  65. String projectId = data.getGroupCode() + data.getProjectId();
  66. String localId = data.getGroupCode() + data.getProjectLocalId();
  67. String projectName = data.getGroupCode() + data.getProjectName();
  68. if (validProjectId.contains(projectId)) {
  69. throw new ExcelAnalysisException("第" + rowIndex + "行数据,项目ID已存在表中,请检查!");
  70. }
  71. if (validLocalId.contains(localId)) {
  72. throw new ExcelAnalysisException("第" + rowIndex + "行数据,项目本地编码已存在表中,请检查!");
  73. }
  74. if (validProjectName.contains(projectName)) {
  75. throw new ExcelAnalysisException("第" + rowIndex + "行数据,项目名称已存在表中,请检查!");
  76. }
  77. // 格式转换
  78. list.add(this.convertSaasProjectData(data, rowIndex));
  79. }
  80. /**
  81. * 所有数据解析完成了都会来调用,在这里进行数据存储,可确保最后遗留的数据也被存储到数据库
  82. * @param context
  83. */
  84. @Override
  85. public void doAfterAllAnalysed(AnalysisContext context) {
  86. if (CollectionUtil.isNotEmpty(list)) {
  87. this.saasProjectService.batchInsertProjectData(list);
  88. }
  89. log.info("项目数据导入成功,操作者: {}", baseVO.getAccountId());
  90. }
  91. /**
  92. * 转变为能够直接存入数据库的数据形式
  93. * @param data
  94. */
  95. private SaasProject convertSaasProjectData(SaasProjectExcel data, int rowIndex) {
  96. SaasProject info = new SaasProject();
  97. BeanUtils.copyProperties(data, info);
  98. Date date = new Date();
  99. info.setCreateTime(date);
  100. info.setUpdateTime(date);
  101. info.setGroupCode(data.getGroupCode());
  102. info.setUpdateUser(baseVO.getAccountId());
  103. info.setId(IdGenerator.getSnowIdStr(SaasCommonConstant.SAAS_PROJECT_ID_PREFIX));
  104. // 数据分割
  105. String groupCode = data.getGroupCode().split("_")[0];
  106. info.setGroupCode(groupCode);
  107. String areaName = data.getAreaId();
  108. if (StringUtil.isBlank(areaName)) {
  109. info.setAreaId(SaasCommonConstant.STR_STATUS_MINUS_1);
  110. } else {
  111. // 查询出当前name对应的ID
  112. SaasArea areaOne = this.saasAreaService.getAreaOne(null, groupCode, null, areaName);
  113. if (areaOne == null) {
  114. throw new ExcelAnalysisException("第" + rowIndex + "行数据,项目所属区域不存在,请检查!");
  115. }
  116. info.setAreaId(areaOne.getId());
  117. }
  118. // 验证数据的唯一性
  119. boolean result = this.saasProjectService.validProjectData(info, false);
  120. if (!result) {
  121. throw new ExcelAnalysisException("第" + rowIndex + "行数据,数据库中存已存在该条记录,请检查!");
  122. }
  123. return info;
  124. }
  125. }