New file |
| | |
| | | package com.panzhihua.common.listen; |
| | | |
| | | import cn.hutool.core.util.IdcardUtil; |
| | | import com.alibaba.excel.context.AnalysisContext; |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.enums.EldersAuthLevelEnum; |
| | | import com.panzhihua.common.exceptions.ServiceException; |
| | | import com.panzhihua.common.model.vos.R; |
| | | import com.panzhihua.common.model.vos.community.EldersAuthElderlyExcelVO; |
| | | import com.panzhihua.common.service.community.CommunityService; |
| | | import com.panzhihua.common.utlis.DateUtils; |
| | | import com.panzhihua.common.utlis.ListUtils; |
| | | import com.panzhihua.common.utlis.StringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @description: 高龄老人导入监听 |
| | | * @author: xyh |
| | | * @date: 2021/3/11 13:26 |
| | | */ |
| | | @Slf4j |
| | | public class ComEldersAuthRecordExcelListen extends AnalysisEventListener<EldersAuthElderlyExcelVO> { |
| | | |
| | | private CommunityService communityService; |
| | | |
| | | private Long communityId; |
| | | |
| | | private Long createBy; |
| | | |
| | | public ComEldersAuthRecordExcelListen(CommunityService communityService, Long communityId, Long createBy){ |
| | | this.communityService = communityService; |
| | | this.communityId = communityId; |
| | | this.createBy = createBy; |
| | | } |
| | | |
| | | |
| | | private static final int BATCH_COUNT = 500; |
| | | private List<EldersAuthElderlyExcelVO> list = new ArrayList<>(); |
| | | |
| | | @Override |
| | | public void invoke(EldersAuthElderlyExcelVO eldersAuthElderlyExcelVO, AnalysisContext analysisContext) { |
| | | if(StringUtils.isEmpty(eldersAuthElderlyExcelVO.getName())){ |
| | | throw new ServiceException("姓名不可为空"); |
| | | } |
| | | if(StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIsExist())){ |
| | | eldersAuthElderlyExcelVO.setIsExist("是"); |
| | | } |
| | | if(StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIsBigAge())){ |
| | | eldersAuthElderlyExcelVO.setIsBigAge("是"); |
| | | } |
| | | if(StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIdCard()) || IdcardUtil.isvalidCard18(eldersAuthElderlyExcelVO.getIdCard())){ |
| | | throw new ServiceException("身份证号错误"); |
| | | } |
| | | |
| | | eldersAuthElderlyExcelVO.setBirthday(DateUtils.getDateStringYMD(IdcardUtil.getBirthDate(eldersAuthElderlyExcelVO.getIdCard()))); |
| | | eldersAuthElderlyExcelVO.setLevel(EldersAuthLevelEnum.getCode(IdcardUtil.getAgeByIdCard(eldersAuthElderlyExcelVO.getIdCard()))); |
| | | list.add(eldersAuthElderlyExcelVO); |
| | | // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM |
| | | if(list.size() >= BATCH_COUNT){ |
| | | log.info("excel导入数据【{}】", JSONObject.toJSONString(list)); |
| | | List<EldersAuthElderlyExcelVO> newVoList = list.stream().filter(ListUtils.distinctByKey(EldersAuthElderlyExcelVO::getIdCard)).collect(Collectors.toList()); |
| | | R r = this.communityService.listSaveEldersAuthElderlyExcelVO(newVoList,this.communityId,this.createBy); |
| | | if (!R.isOk(r)) { |
| | | throw new ServiceException(r.getMsg()); |
| | | } |
| | | list.clear(); //清空list |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void doAfterAllAnalysed(AnalysisContext analysisContext) { |
| | | List<EldersAuthElderlyExcelVO> newVoList = list.stream().filter(ListUtils.distinctByKey(EldersAuthElderlyExcelVO::getIdCard)).collect(Collectors.toList()); |
| | | R r = this.communityService.listSaveEldersAuthElderlyExcelVO(newVoList,this.communityId,this.createBy); |
| | | if (!R.isOk(r)) { |
| | | throw new ServiceException(r.getMsg()); |
| | | } |
| | | } |
| | | } |