|
|
@@ -0,0 +1,164 @@
|
|
|
+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.PtAccountBo;
|
|
|
+import org.dromara.backstage.basics.domain.bo.PtAreaBo;
|
|
|
+import org.dromara.backstage.basics.domain.bo.PtRoomBo;
|
|
|
+import org.dromara.backstage.basics.domain.vo.PtAccountVo;
|
|
|
+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.IPtAccountService;
|
|
|
+import org.dromara.backstage.basics.service.IPtAreaService;
|
|
|
+import org.dromara.backstage.basics.service.IPtRoomService;
|
|
|
+import org.dromara.backstage.consumption.domain.XfTerm;
|
|
|
+import org.dromara.backstage.consumption.domain.bo.XfTermBo;
|
|
|
+import org.dromara.backstage.consumption.domain.bo.XfTermImportBo;
|
|
|
+import org.dromara.backstage.consumption.domain.vo.XfTermVo;
|
|
|
+import org.dromara.backstage.consumption.service.IXfTermService;
|
|
|
+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 XfTermImportListener extends AnalysisEventListener<XfTermImportBo> implements ExcelListener<XfTermImportBo> {
|
|
|
+
|
|
|
+ private final IPtRoomService ptRoomService;
|
|
|
+
|
|
|
+ private final IXfTermService xfTermService;
|
|
|
+ private final IPtAccountService ptAccountService;
|
|
|
+
|
|
|
+ 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 XfTermImportListener(Boolean isUpdateSupport) {
|
|
|
+ this.ptRoomService = SpringUtils.getBean(IPtRoomService.class);
|
|
|
+ this.xfTermService = SpringUtils.getBean(IXfTermService.class);
|
|
|
+ this.ptAccountService = SpringUtils.getBean(IPtAccountService.class);
|
|
|
+ this.operUserId = LoginHelper.getUserId();
|
|
|
+ this.isUpdateSupport = isUpdateSupport;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ExcelResult<XfTermImportBo> getExcelResult() {
|
|
|
+ return new ExcelResult<>(){
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<XfTermImportBo> 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(XfTermImportBo bo, AnalysisContext analysisContext) {
|
|
|
+ //1.查询区域是否存在
|
|
|
+ Integer rowIndex = analysisContext.readRowHolder().getRowIndex() + 1;
|
|
|
+ try{
|
|
|
+ //先验证数据
|
|
|
+ ValidatorUtils.validate(bo);
|
|
|
+
|
|
|
+ //查询并设置房间
|
|
|
+ PtRoomBo roomBo = new PtRoomBo();
|
|
|
+ roomBo.setRoomCode(bo.getRoomCode());
|
|
|
+ List<PtRoomVo> roomVos = ptRoomService.queryList(roomBo);
|
|
|
+ if(CollectionUtil.isEmpty(roomVos)){
|
|
|
+ failureMsg.append("<br/>").append("第").append(rowIndex).append("行,房间编号不存在;");
|
|
|
+ failureNum++;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(roomVos.size()>1){
|
|
|
+ failureMsg.append("<br/>").append("第").append(rowIndex).append("行,房间编号不唯一;");
|
|
|
+ failureNum++;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ XfTermBo xfTerm = BeanUtil.toBean(bo, XfTermBo.class);
|
|
|
+ xfTerm.setRoomId(roomVos.get(0).getRoomId());
|
|
|
+ //查询并设置商户
|
|
|
+ PtAccountBo ptAccountBo = new PtAccountBo();
|
|
|
+ ptAccountBo.setAccountName(bo.getAccountName());
|
|
|
+ List<PtAccountVo> ptRoomVos = ptAccountService.queryListByName(ptAccountBo);
|
|
|
+ if(CollectionUtil.isEmpty(ptRoomVos)){
|
|
|
+ failureMsg.append("<br/>").append("第").append(rowIndex).append("行,商户名称不存在;");
|
|
|
+ failureNum++;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(roomVos.size()>1){
|
|
|
+ failureMsg.append("<br/>").append("第").append(rowIndex).append("行,商户名称不唯一;");
|
|
|
+ failureNum++;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ xfTerm.setAccountId(ptRoomVos.get(0).getAccountId());
|
|
|
+ xfTerm.setCreateBy(operUserId);
|
|
|
+
|
|
|
+ //设备机号或设备名称不能重复
|
|
|
+ XfTermBo xfTermBo = new XfTermBo();
|
|
|
+ xfTermBo.setTermNo(bo.getTermNo());
|
|
|
+ xfTermBo.setTermName(bo.getTermName());
|
|
|
+ List<XfTermVo> xfTermVos = xfTermService.queryListByNoOrName(xfTermBo);
|
|
|
+ if (CollectionUtil.isEmpty(xfTermVos)) {
|
|
|
+ xfTermService.insertByBo(xfTerm);
|
|
|
+
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("、第 ").append(rowIndex).append("行,导入成功");
|
|
|
+ } else if (isUpdateSupport && xfTermVos.size() == 1) {
|
|
|
+ XfTermVo xfTermVo = xfTermVos.get(0);
|
|
|
+ XfTermBo xfTermBo1 = BeanUtil.toBean(xfTermVo, XfTermBo.class);
|
|
|
+ xfTermBo1.setCreateBy(operUserId);
|
|
|
+ xfTermBo1.setRoomId(xfTerm.getRoomId());
|
|
|
+ xfTermBo1.setAccountId(xfTerm.getAccountId());
|
|
|
+
|
|
|
+ xfTermService.updateByBo(xfTermBo1);
|
|
|
+ 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) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|