package com.panzhihua.common.listen;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.common.collect.Lists;
|
import com.panzhihua.common.enums.PopulIsOkEnum;
|
import com.panzhihua.common.enums.PopulPoliticalOutlookEnum;
|
import com.panzhihua.common.enums.PopulSexEnum;
|
import com.panzhihua.common.exceptions.ServiceException;
|
import com.panzhihua.common.model.vos.R;
|
import com.panzhihua.common.model.vos.community.ComMngPopulationImportErrorVO;
|
import com.panzhihua.common.model.vos.community.ComMngPopulationServeExcelVO;
|
import com.panzhihua.common.model.vos.community.ComMngVillageServeExcelVO;
|
import com.panzhihua.common.service.community.CommunityService;
|
import com.panzhihua.common.utlis.AgeUtils;
|
import com.panzhihua.common.utlis.IdCardUtil;
|
import com.panzhihua.common.utlis.ListUtils;
|
import com.panzhihua.common.utlis.StringUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* @description: 实有人口导入监听
|
* @author: llming
|
*/
|
@Slf4j
|
public class ComMngPopulationServeExcelListen extends AnalysisEventListener<Map<Integer, String>> {
|
private CommunityService communityService;
|
|
private Long communityId;
|
|
private static int headSize = 0;
|
|
private Map<Integer, String> headData;
|
|
|
public ComMngPopulationServeExcelListen(CommunityService communityService, Long communityId) {
|
this.communityService = communityService;
|
this.communityId = communityId;
|
}
|
|
/**
|
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
|
*/
|
private static final int BATCH_COUNT = 5000;
|
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
|
|
@Override
|
public void invoke(Map<Integer, String> data, AnalysisContext context) {
|
list.add(data);
|
if (list.size() >= BATCH_COUNT) {
|
saveData();
|
list.clear();
|
}
|
}
|
|
/**
|
* 这里会一行行的返回头
|
*
|
* @param headMap
|
* @param context
|
*/
|
@Override
|
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
headSize = headMap.size();
|
headData = headMap;
|
}
|
|
@Override
|
public void doAfterAllAnalysed(AnalysisContext context) {
|
saveData();
|
log.info("所有数据解析完成!");
|
}
|
|
/**
|
* 不是固定的列只能手动处理
|
*/
|
private void saveData() {
|
int index = 2;
|
try {
|
ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList();
|
List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
|
for (Map<Integer, String> oneData : list) {
|
ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO();
|
if(oneData.get(0) == null){
|
ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
|
importErrorVO.setErrorPosition("第" + index + "行,第1列");
|
importErrorVO.setErrorMsg("名字不可为空,请填写姓名");
|
populationImportErrorVOList.add(importErrorVO);
|
index++;
|
continue;
|
}
|
vo.setName(oneData.get(0));
|
if(StringUtils.isEmpty(oneData.get(1))){
|
ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
|
importErrorVO.setErrorPosition("第" + index + "行,第2列");
|
importErrorVO.setErrorMsg("身份证号不可为空,请填写身份证号");
|
populationImportErrorVOList.add(importErrorVO);
|
index++;
|
continue;
|
}
|
// if(!IdCardUtil.identityValidator(oneData.get(1))){
|
// throw new ServiceException("500", "身份证号格式错误:第" + index + "行,第2列");
|
// }
|
//判断身份证号码位数
|
if(oneData.get(1).length() != 18){
|
ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
|
importErrorVO.setErrorPosition("第" + index + "行,第2列");
|
importErrorVO.setErrorMsg("身份证号位数有误,请检查身份证号码是否正确");
|
populationImportErrorVOList.add(importErrorVO);
|
index++;
|
continue;
|
}
|
vo.setCardNo(oneData.get(1));
|
//根据身份证号码解析年龄以及性别
|
//获取用户生日
|
String birthday = vo.getCardNo().substring(6, 14);
|
//设置用户年龄
|
vo.setAge(AgeUtils.getAgeFromBirthTime(birthday));
|
//获取用户性别
|
int sex = Integer.parseInt(vo.getCardNo().substring(16, 17));
|
if(sex%2 == 1){
|
vo.setSex(PopulSexEnum.nan.getCode());
|
}else{
|
vo.setSex(PopulSexEnum.nv.getCode());
|
}
|
if(oneData.get(2) != null){
|
vo.setNation(oneData.get(2));
|
}
|
if(oneData.get(3) != null){
|
vo.setPoliticalOutlook(PopulPoliticalOutlookEnum.getCodeByName(oneData.get(3)));
|
}
|
if(oneData.get(4) != null){
|
vo.setIsRent(PopulIsOkEnum.getCodeByName(oneData.get(4)));
|
}
|
if(oneData.get(5) != null){
|
vo.setRelation(oneData.get(5));
|
}
|
if(oneData.get(6) != null){
|
vo.setRoad(oneData.get(6));
|
}
|
if(oneData.get(7) != null){
|
vo.setDoorNo(Integer.valueOf(oneData.get(7)));
|
}
|
if(oneData.get(8) != null){
|
vo.setFloor(oneData.get(8));
|
}
|
if(oneData.get(9) != null){
|
vo.setUnitNo(Integer.valueOf(oneData.get(9)));
|
}
|
if(oneData.get(10) != null){
|
vo.setHouseNo(Integer.valueOf(oneData.get(10).trim()));
|
}
|
if(oneData.get(11) != null){
|
vo.setPhone(oneData.get(11));
|
}
|
if(oneData.get(12) != null){
|
vo.setNativePlace(oneData.get(12));
|
}
|
if(oneData.get(13) != null){
|
vo.setCultureLevel(oneData.get(13));
|
}
|
if(oneData.get(14) != null){
|
vo.setMarriage(oneData.get(14));
|
}
|
if(oneData.get(15) != null){
|
vo.setHealthy(oneData.get(15));
|
}
|
if(oneData.get(16) != null){
|
vo.setWorkCompany(oneData.get(16));
|
}
|
if(oneData.get(17) != null){
|
vo.setOutOrLocal(oneData.get(17));
|
}
|
if(oneData.get(18) != null){
|
vo.setCensusRegister(oneData.get(18));
|
}
|
if(oneData.get(19) != null){
|
vo.setRemark(oneData.get(19));
|
}
|
for (int i = 20; i < headSize; i++) {
|
if (oneData.get(i) != null && oneData.get(i).equals("是")) {
|
vo.getUserTagStr().add(headData.get(i).substring(0,headData.get(i).indexOf("(")));
|
}
|
}
|
|
voList.add(vo);
|
index++;
|
}
|
//客户需要暂时注释,等客户处理完成需要恢复
|
// if(populationImportErrorVOList.isEmpty()){
|
// R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
|
// if (!R.isOk(r)) {
|
// throw new ServiceException(r.getMsg());
|
// }
|
// }else{
|
// throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
|
// }
|
//根据list中的IdCard城市来去重
|
List<ComMngPopulationServeExcelVO> newVoList = voList.stream().filter(ListUtils.distinctByKey(ComMngPopulationServeExcelVO::getCardNo)).collect(Collectors.toList());
|
R r = communityService.listSavePopulationServeExcelVO(newVoList, communityId);
|
if (!R.isOk(r)) {
|
String errMsg = r.getMsg();
|
List<ComMngPopulationImportErrorVO> errorList = JSON.parseArray(errMsg,ComMngPopulationImportErrorVO.class);
|
if(!errorList.isEmpty()){
|
populationImportErrorVOList.addAll(errorList);
|
}
|
throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
|
}else{
|
if(!populationImportErrorVOList.isEmpty()){
|
throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
|
}
|
}
|
} catch (NumberFormatException e) {
|
List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
|
ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
|
importErrorVO.setErrorPosition("第" + index + "行");
|
importErrorVO.setErrorMsg("数据格式有误,请检查文档内数据");
|
populationImportErrorVOList.add(importErrorVO);
|
index++;
|
throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
|
}
|
}
|
}
|