package com.panzhihua.common.listen;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
import com.alibaba.excel.event.AnalysisEventListener;
|
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.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 lombok.extern.slf4j.Slf4j;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @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 = 100;
|
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();
|
for (Map<Integer, String> oneData : list) {
|
ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO();
|
if(oneData.get(0) == null){
|
throw new ServiceException("500", "名字不可为空:第" + index + "行,第1列");
|
}
|
vo.setName(oneData.get(0));
|
if(oneData.get(1) == null){
|
throw new ServiceException("500", "身份证号不可为空:第" + index + "行,第2列");
|
}
|
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)));
|
}
|
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.setRemark(oneData.get(17));
|
}
|
for (int i = 18; i < headSize; i++) {
|
if (oneData.get(i) != null && oneData.get(i).equals("是")) {
|
vo.getUserTagStr().add(headData.get(i));
|
}
|
}
|
voList.add(vo);
|
index++;
|
}
|
R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
|
if (!R.isOk(r)) {
|
throw new ServiceException(r.getMsg());
|
}
|
} catch (NumberFormatException e) {
|
throw new ServiceException("500", "填写数据格式错误:第" + index + "行" + e.getMessage());
|
}
|
}
|
}
|