|
|
@@ -0,0 +1,128 @@
|
|
|
+package org.dromara.backstage.basics.listener;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.dromara.backstage.basics.domain.bo.PtAreaBo;
|
|
|
+import org.dromara.backstage.basics.domain.bo.PtRoomBo;
|
|
|
+import org.dromara.backstage.basics.domain.vo.PtAreaVo;
|
|
|
+import org.dromara.backstage.basics.domain.vo.PtRoomTempImportVo;
|
|
|
+import org.dromara.backstage.basics.domain.vo.PtRoomVo;
|
|
|
+import org.dromara.backstage.basics.service.IPtAreaService;
|
|
|
+import org.dromara.backstage.basics.service.IPtRoomService;
|
|
|
+import org.dromara.common.core.exception.ServiceException;
|
|
|
+import org.dromara.common.core.utils.SpringUtils;
|
|
|
+import org.dromara.common.core.utils.ValidatorUtils;
|
|
|
+import org.dromara.common.excel.core.ExcelListener;
|
|
|
+import org.dromara.common.excel.core.ExcelResult;
|
|
|
+import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class PtRoomImportListener extends AnalysisEventListener<PtRoomTempImportVo> implements ExcelListener<PtRoomTempImportVo> {
|
|
|
+
|
|
|
+ private final IPtRoomService ptRoomService;
|
|
|
+
|
|
|
+ private final IPtAreaService ptAreaService;
|
|
|
+
|
|
|
+ private final Boolean isUpdateSupport;
|
|
|
+
|
|
|
+ private final Long operUserId;
|
|
|
+
|
|
|
+ private int successNum = 0;
|
|
|
+ private int failureNum = 0;
|
|
|
+ private final StringBuilder successMsg = new StringBuilder();
|
|
|
+ private final StringBuilder failureMsg = new StringBuilder();
|
|
|
+
|
|
|
+ public PtRoomImportListener(Boolean isUpdateSupport) {
|
|
|
+ this.ptRoomService = SpringUtils.getBean(IPtRoomService.class);
|
|
|
+ this.ptAreaService = SpringUtils.getBean(IPtAreaService.class);
|
|
|
+ this.operUserId = LoginHelper.getUserId();
|
|
|
+ this.isUpdateSupport = isUpdateSupport;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ExcelResult<PtRoomTempImportVo> getExcelResult() {
|
|
|
+ return new ExcelResult<>(){
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<PtRoomTempImportVo> getList() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getErrorList() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getAnalysis() {
|
|
|
+ if (failureNum > 0) {
|
|
|
+ failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
|
|
+ throw new ServiceException(failureMsg.toString());
|
|
|
+ } else {
|
|
|
+ successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
|
|
+ }
|
|
|
+ return successMsg.toString();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void invoke(PtRoomTempImportVo ptRoomTempImportVo, AnalysisContext analysisContext) {
|
|
|
+ //1.查询区域是否存在
|
|
|
+ Integer rowIndex = analysisContext.readRowHolder().getRowIndex() + 1;
|
|
|
+ try{
|
|
|
+ //先验证数据
|
|
|
+ ValidatorUtils.validate(ptRoomTempImportVo);
|
|
|
+ PtAreaBo ptAreaBo = new PtAreaBo();
|
|
|
+ ptAreaBo.setAreaCode(ptRoomTempImportVo.getAreaCode());
|
|
|
+ ptAreaBo.setAreaName(ptRoomTempImportVo.getAreaName());
|
|
|
+ List<PtAreaVo> ptAreaVos = ptAreaService.queryList(ptAreaBo);
|
|
|
+ if (ptAreaVos.size() != 1) {
|
|
|
+ failureMsg.append("<br/>").append("第").append(rowIndex).append("行,区域不存在或存在多个;");
|
|
|
+ failureNum++;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long areaId = ptAreaVos.get(0).getAreaId();
|
|
|
+ ptRoomTempImportVo.setAreaId(areaId);
|
|
|
+ //2.查询房间是否存在
|
|
|
+ List<PtRoomVo> ptRoomVos =
|
|
|
+ ptRoomService.queryByNameOrCode(areaId, ptRoomTempImportVo.getRoomName(), ptRoomTempImportVo.getRoomCode());
|
|
|
+ if (CollectionUtil.isEmpty(ptRoomVos)) {
|
|
|
+ PtRoomBo ptRoomBo = BeanUtil.toBean(ptRoomTempImportVo, PtRoomBo.class);
|
|
|
+ ptRoomBo.setCreateBy(operUserId);
|
|
|
+
|
|
|
+ ptRoomService.insertByBo(ptRoomBo);
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("、第 ").append(rowIndex).append("行,导入成功");
|
|
|
+ } else if (isUpdateSupport && ptRoomVos.size() == 1) {
|
|
|
+ PtRoomVo ptRoomVo = ptRoomVos.get(0);
|
|
|
+ PtRoomBo ptRoomBo = BeanUtil.toBean(ptRoomVo, PtRoomBo.class);
|
|
|
+ ptRoomBo.setCreateBy(operUserId);
|
|
|
+
|
|
|
+ ptRoomService.updateByBo(ptRoomBo);
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("、第 ").append(rowIndex).append("行,更新成功");
|
|
|
+
|
|
|
+ }else{
|
|
|
+ failureNum++;
|
|
|
+ failureMsg.append("<br/>").append(failureNum).append("、第 ").append(rowIndex).append("行,已存在;");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ failureNum++;
|
|
|
+ String msg = "<br/>" +"第 " + rowIndex + "行,导入失败:";
|
|
|
+ failureMsg.append(msg).append(e.getMessage());
|
|
|
+ log.error(msg, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|