|
@@ -0,0 +1,227 @@
|
|
|
+package com.rongwei.zhsw.system.importListener;
|
|
|
+
|
|
|
+import com.alibaba.excel.context.AnalysisContext;
|
|
|
+import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.rongwe.zhsw.system.domain.SwBookManagementDo;
|
|
|
+import com.rongwe.zhsw.system.domain.SwUserManagementDo;
|
|
|
+import com.rongwe.zhsw.system.vo.ImportUserManagementRecordVo;
|
|
|
+import com.rongwe.zhsw.system.vo.SwPreferentialUserCategoriesVo;
|
|
|
+import com.rongwe.zhsw.system.vo.SwWaterPriceManagementVo;
|
|
|
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.zhsw.system.service.SwBookManagementService;
|
|
|
+import com.rongwei.zhsw.system.service.SwPreferentialUserCategoriesService;
|
|
|
+import com.rongwei.zhsw.system.service.SwWaterPriceManagementService;
|
|
|
+import com.rongwei.zhsw.system.service.impl.SwBookManagementServiceImpl;
|
|
|
+import com.rongwei.zhsw.system.service.impl.SwPreferentialUserCategoriesServiceImpl;
|
|
|
+import com.rongwei.zhsw.system.service.impl.SwUserManagementServiceImpl;
|
|
|
+import com.rongwei.zhsw.system.service.impl.SwWaterPriceManagementServiceImpl;
|
|
|
+import com.rongwei.zhsw.system.utils.ZHSWCommonUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static com.rongwei.zhsw.system.utils.ZHSWCommonUtils.getCurrentUser;
|
|
|
+
|
|
|
+public class UserManagementRecordListener extends AnalysisEventListener<ImportUserManagementRecordVo> {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(UserManagementRecordListener.class);
|
|
|
+ private List<ImportUserManagementRecordVo> recordList = new ArrayList<>();
|
|
|
+ private List<SwUserManagementDo> saveList = new ArrayList<>();
|
|
|
+
|
|
|
+ private SwUserManagementServiceImpl swUserManagementService;
|
|
|
+
|
|
|
+ private SwBookManagementService swBookManagementService;
|
|
|
+
|
|
|
+ private SwPreferentialUserCategoriesService swPreferentialUserCategoriesService;
|
|
|
+
|
|
|
+ private SwWaterPriceManagementService swWaterPriceManagementService;
|
|
|
+
|
|
|
+
|
|
|
+ // 出现重复记录的数据
|
|
|
+ private List<ImportUserManagementRecordVo> duplicateRecordList = new ArrayList<>();
|
|
|
+
|
|
|
+ public UserManagementRecordListener(SwUserManagementServiceImpl userManagementService,
|
|
|
+ SwPreferentialUserCategoriesServiceImpl preferentialUserCategoriesService,
|
|
|
+ SwBookManagementServiceImpl bookManagementService, SwWaterPriceManagementServiceImpl waterPriceManagementService) {
|
|
|
+ this.swUserManagementService = userManagementService;
|
|
|
+ this.swBookManagementService = bookManagementService;
|
|
|
+ this.swPreferentialUserCategoriesService = preferentialUserCategoriesService;
|
|
|
+ this.swWaterPriceManagementService = waterPriceManagementService;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入前数据校验
|
|
|
+ * @param importUserManagementRecordVo
|
|
|
+ * @param analysisContext
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void invoke(ImportUserManagementRecordVo importUserManagementRecordVo, AnalysisContext analysisContext) {
|
|
|
+ // Integer currentRowNum = analysisContext.readRowHolder().getRowIndex();
|
|
|
+ if (StringUtils.isBlank(importUserManagementRecordVo.getUsernumber())) {
|
|
|
+ return;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断数据是否重复
|
|
|
+ boolean b = recordList.stream().anyMatch(record -> record.getUsernumber().
|
|
|
+ equals(importUserManagementRecordVo.getUsernumber()));
|
|
|
+ if (b) {
|
|
|
+ duplicateRecordList.add(importUserManagementRecordVo);
|
|
|
+ throw new CustomException("户号重复:"+importUserManagementRecordVo.getUsernumber());
|
|
|
+ // return;
|
|
|
+ }
|
|
|
+ recordList.add(importUserManagementRecordVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
+// if (!duplicateRecordList.isEmpty()) {
|
|
|
+// throw new CustomException(
|
|
|
+// "Excel表中存在相同户号,请检查! 户号为:" +
|
|
|
+// duplicateRecordList.stream().map(ImportUserManagementRecordVo::getUsernumber).collect(Collectors.joining("、")));
|
|
|
+// }
|
|
|
+ if (recordList.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取数据中的所有户号 ,数据匹配范围(启用 + 停用)
|
|
|
+ List<String> statusList = new ArrayList<>(Arrays.asList("enable","disable"));
|
|
|
+ List<String> accountNumList = recordList.stream().map(ImportUserManagementRecordVo::getUsernumber).distinct().
|
|
|
+ collect(Collectors.toList());
|
|
|
+ List<SwUserManagementDo> repeatDataList = swUserManagementService.list(new LambdaQueryWrapper<SwUserManagementDo>()
|
|
|
+ .in(SwUserManagementDo::getUsernumber, accountNumList).eq(SwUserManagementDo::getDeleted, "0")
|
|
|
+ .in(SwUserManagementDo::getStatus,statusList));
|
|
|
+ if(!repeatDataList.isEmpty()){
|
|
|
+ throw new CustomException(
|
|
|
+ repeatDataList.stream().map(SwUserManagementDo::getUsernumber).distinct().
|
|
|
+ collect(Collectors.joining("、"))+" 户号已存在,请检查后重新导入"
|
|
|
+ );
|
|
|
+ }
|
|
|
+ //查询册号数据
|
|
|
+ Map<String, List<SwBookManagementDo>> volumenoMap =new HashMap<>();
|
|
|
+ try {
|
|
|
+ volumenoMap =swBookManagementService.list(new LambdaQueryWrapper<SwBookManagementDo>().
|
|
|
+ eq(SwBookManagementDo::getDeleted, "0").eq(SwBookManagementDo::getStatus, "enable"))
|
|
|
+ .stream().collect(Collectors.groupingBy(info -> info.getBooknumber()));
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new CustomException(
|
|
|
+ "册号存在相同记录,请联系管理员"
|
|
|
+ );
|
|
|
+ }
|
|
|
+ //查询用水类型数据
|
|
|
+ Map<String, List<SwWaterPriceManagementVo>> watertypeMap = swWaterPriceManagementService.selectWatertype().stream().
|
|
|
+ collect(Collectors.groupingBy(info -> info.getWatertypename()));
|
|
|
+
|
|
|
+ //查询用户类别数据
|
|
|
+ Map<String, List<SwPreferentialUserCategoriesVo>> usertypeMap = swPreferentialUserCategoriesService.selectUserType().stream().
|
|
|
+ collect(Collectors.groupingBy(info -> info.getDiscounttypename()));
|
|
|
+
|
|
|
+ setRecordData(volumenoMap,watertypeMap,usertypeMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRecordData(Map<String, List<SwBookManagementDo>> volumenoMap, Map<String, List<SwWaterPriceManagementVo>> watertypeMap, Map<String, List<SwPreferentialUserCategoriesVo>> usertypeMap){
|
|
|
+ // 当前导入用户的操作人
|
|
|
+ SysUserVo currentUser = getCurrentUser();
|
|
|
+ Date date = new Date();
|
|
|
+ SwUserManagementDo saveUserDo=null;
|
|
|
+ BigDecimal meterMaxValue = new BigDecimal("9999");
|
|
|
+ BigDecimal zero = BigDecimal.ZERO;
|
|
|
+ for (ImportUserManagementRecordVo recordVo:recordList){
|
|
|
+ saveUserDo = new SwUserManagementDo();
|
|
|
+ saveUserDo.setId(SecurityUtil.getUUID());
|
|
|
+ saveUserDo.setUsernumber(recordVo.getUsernumber());
|
|
|
+ saveUserDo.setUsername(recordVo.getUsername());
|
|
|
+ saveUserDo.setStatus("enable");
|
|
|
+
|
|
|
+ saveUserDo.setAccountbalance(recordVo.getAccountbalance());
|
|
|
+ saveUserDo.setBldg(recordVo.getBldg());
|
|
|
+ saveUserDo.setUnitnum(recordVo.getUnitnum());
|
|
|
+ saveUserDo.setHouseno(recordVo.getHouseno());
|
|
|
+ //METERMAXVALUE 表具最大读数 默认=9999
|
|
|
+ saveUserDo.setMetermaxvalue(meterMaxValue);
|
|
|
+ //LASTMETERREADDATE 最近抄表日期 默认= 当前日期
|
|
|
+ saveUserDo.setLastmeterreaddate(date);
|
|
|
+ //LASTMETERREADING 最近抄表读数 默认= 0
|
|
|
+ saveUserDo.setLastmeterreading(zero);
|
|
|
+
|
|
|
+ //册号
|
|
|
+ saveUserDo.setVolumeno(recordVo.getVolumeno());
|
|
|
+ saveUserDo.setVolumenoid(Optional.ofNullable(volumenoMap.get(recordVo.getVolumeno()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwBookManagementDo::getId)
|
|
|
+ .orElse(null));
|
|
|
+ saveUserDo.setVillagename(Optional.ofNullable(volumenoMap.get(recordVo.getVolumeno()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwBookManagementDo::getVillagename)
|
|
|
+ .orElse(null));
|
|
|
+ saveUserDo.setAddress(recordVo.getAddress());
|
|
|
+ saveUserDo.setPhonenumber(recordVo.getPhonenumber());
|
|
|
+
|
|
|
+ //用水类型
|
|
|
+ saveUserDo.setWatertype(Optional.ofNullable(watertypeMap.get(recordVo.getWatertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwWaterPriceManagementVo::getWatertype)
|
|
|
+ .orElse(null));
|
|
|
+ //WATERPRICE 水价 根据 用水类型 进行更新为对应价格
|
|
|
+ saveUserDo.setWaterprice(Optional.ofNullable(watertypeMap.get(recordVo.getWatertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwWaterPriceManagementVo::getPrice)
|
|
|
+ .orElse(null));
|
|
|
+
|
|
|
+ //用户类别
|
|
|
+ saveUserDo.setUsertype(Optional.ofNullable(usertypeMap.get(recordVo.getUsertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwPreferentialUserCategoriesVo::getDiscounttype)
|
|
|
+ .orElse(null));
|
|
|
+
|
|
|
+ saveUserDo.setUsertypeid(Optional.ofNullable(usertypeMap.get(recordVo.getUsertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwPreferentialUserCategoriesVo::getId)
|
|
|
+ .orElse(null));
|
|
|
+
|
|
|
+ //EXEMPTIONWATER 减免用水 根据 用户类别 进行更新为对应减免用水
|
|
|
+ saveUserDo.setExemptionamount(Optional.ofNullable(usertypeMap.get(recordVo.getUsertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwPreferentialUserCategoriesVo::getMonthlywaterdiscount)
|
|
|
+ .orElse(null));
|
|
|
+
|
|
|
+ //EXEMPTIONAMOUNT 减免金额 根据 用户类别 进行更新为对应减免金额
|
|
|
+ saveUserDo.setExemptionwater(Optional.ofNullable(usertypeMap.get(recordVo.getUsertypename()))
|
|
|
+ .orElse(new ArrayList<>())
|
|
|
+ .stream()
|
|
|
+ .findFirst()
|
|
|
+ .map(SwPreferentialUserCategoriesVo::getDiscountwater)
|
|
|
+ .orElse(null));
|
|
|
+
|
|
|
+ ZHSWCommonUtils.initModelGeneralParameters(saveUserDo, currentUser);
|
|
|
+ saveList.add(saveUserDo);
|
|
|
+ };
|
|
|
+ }
|
|
|
+ // 获取Excel 中的数据
|
|
|
+ public List<SwUserManagementDo> getData() {
|
|
|
+ return saveList;
|
|
|
+ }
|
|
|
+}
|