huanghongfa
2021-10-15 089660d464ee9423dd724177ba5fa82afb9d5920
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/listen/ComMngBuildingExcelListen.java
@@ -4,21 +4,28 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.panzhihua.common.constants.Constants;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngBuildingExcelVO;
import com.panzhihua.common.model.vos.community.ComMngPopulationImportErrorVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.service.grid.GridService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
/**
 * @description: 社区楼栋导入监听
@@ -30,12 +37,14 @@
    private static int headSize = 0;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
    private CommunityService communityService;
    private StringRedisTemplate stringRedisTemplate;
    private GridService gridService;
    private Long communityId;
    private Map<Integer, String> headData;
    public ComMngBuildingExcelListen(CommunityService communityService, Long communityId) {
    public ComMngBuildingExcelListen(CommunityService communityService, Long communityId, StringRedisTemplate stringRedisTemplate) {
        this.communityService = communityService;
        this.communityId = communityId;
        this.stringRedisTemplate = stringRedisTemplate;
    }
    @Override
@@ -71,103 +80,123 @@
            int index = 2;
            ArrayList<ComMngBuildingExcelVO> voList = Lists.newArrayList();
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
            ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
            String key = Constants.BUILDING_ERROR_LIST + communityId;
            for (int i = 0; i < list.size(); i++) {
                Map<Integer, String> oneData = list.get(i);
                ComMngBuildingExcelVO vo = new ComMngBuildingExcelVO();
                vo.setId(Snowflake.getId());
                vo.setUseType(oneData.get(1));
                vo.setBuildType(oneData.get(2));
                if (StringUtils.isEmpty(oneData.get(oneData.get(3)))) {
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
//                vo.setUseType(oneData.get(0));
//                vo.setBuildType(oneData.get(1));
                if (StringUtils.isEmpty(oneData.get(0))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("街路巷不能为空");
                    importErrorVO.setErrorPosition("第" + index + "行");
                    populationImportErrorVOList.add(importErrorVO);
                    vo.setAlley(oneData.get(3));
                    continue;
                }
                vo.setVillageName(oneData.get(4));
                vo.setDoorNo(oneData.get(5));
                vo.setName(oneData.get(6));
                try {
                    vo.setUnitTotal(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
                } catch (Exception e) {
                    if (StringUtils.isEmpty(oneData.get(oneData.get(5)))) {
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorMsg("门牌号不能为空");
                        importErrorVO.setErrorPosition("第" + index + "行");
                        populationImportErrorVOList.add(importErrorVO);
                        vo.setDoorNo(oneData.get(5));
                    }
                    if (StringUtils.isEmpty(oneData.get(oneData.get(5)))) {
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorMsg("楼栋号不能为空");
                        importErrorVO.setErrorPosition("第" + index + "行");
                        populationImportErrorVOList.add(importErrorVO);
                        vo.setName(oneData.get(6));
                    }
                    try {
                        vo.setUnitTotal(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
                    } catch (Exception e1) {
                        vo.setUnitTotal(0);
                    }
                    try {
                        vo.setBuildFloorSum(oneData.get(8) == null ? 0 : Integer.valueOf(oneData.get(8)));
                    } catch (Exception e1) {
                        vo.setBuildFloorSum(0);
                    }
                    try {
                        vo.setEveryFloorSum(oneData.get(9) == null ? 0 : Integer.valueOf(oneData.get(9)));
                    } catch (Exception e1) {
                        vo.setEveryFloorSum(0);
                    }
                    try {
                        vo.setBuildUserSum(oneData.get(10) == null ? 0 : Integer.valueOf(oneData.get(10)));
                    } catch (Exception e1) {
                        vo.setBuildUserSum(0);
                    }
                    try {
                        vo.setElevatorTotal(oneData.get(11) == null ? 0 : Integer.valueOf(oneData.get(11)));
                    } catch (Exception e1) {
                        vo.setElevatorTotal(0);
                    }
                    vo.setPropertyCompanyName(oneData.get(12));
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                        vo.setBuildDate(sdf.parse(oneData.get(13)));
                    } catch (Exception e1) {
                    }
                    vo.setRemark(oneData.get(14));
                    vo.setGridId(oneData.get(15));
                    vo.setGridName(oneData.get(15));
                    voList.add(vo);
                    index++;
                vo.setAlley(oneData.get(0));
                vo.setVillageName(oneData.get(1));
                if (StringUtils.isEmpty(oneData.get(2))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("门牌号不能为空");
                    populationImportErrorVOList.add(importErrorVO);
                    continue;
                }
                vo.setDoorNo(oneData.get(2));
                R r = communityService.listSaveBuildingExcelVO(voList, 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));
                    }
                if (StringUtils.isEmpty(oneData.get(3))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("楼栋号不能为空");
                    populationImportErrorVOList.add(importErrorVO);
                    continue;
                }
                vo.setName(oneData.get(3));
//                try {
//                    vo.setUnitTotal(oneData.get(6) == null ? 0 : Integer.valueOf(oneData.get(6)));
//                } catch (Exception e1) {
//                    vo.setUnitTotal(0);
//                }
//                try {
//                    vo.setBuildFloorSum(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
//                } catch (Exception e1) {
//                    vo.setBuildFloorSum(0);
//                }
//
//                try {
//                    vo.setEveryFloorSum(oneData.get(8) == null ? 0 : Integer.valueOf(oneData.get(8)));
//                } catch (Exception e1) {
//                    vo.setEveryFloorSum(0);
//                }
//
//                try {
//                    vo.setBuildUserSum(oneData.get(9) == null ? 0 : Integer.valueOf(oneData.get(9)));
//                } catch (Exception e1) {
//                    vo.setBuildUserSum(0);
//                }
//                try {
//                    vo.setElevatorTotal(oneData.get(10) == null ? 0 : Integer.valueOf(oneData.get(10)));
//                } catch (Exception e1) {
//                    vo.setElevatorTotal(0);
//                }
//
//                vo.setPropertyCompanyName(oneData.get(11));
//                try {
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                    vo.setBuildDate(sdf.parse(oneData.get(12)));
//                } catch (Exception e1) {
//                    vo.setBuildDate(null);
//                }
//                vo.setRemark(oneData.get(13));
//                vo.setGridId(oneData.get(14));
//                vo.setGridName(oneData.get(14));
                voList.add(vo);
                index++;
            }
            R r = communityService.listSaveBuildingExcelVO(voList, communityId);
            if (!R.isOk(r)) {
                List<ComMngPopulationImportErrorVO> errorList =
                        JSONArray.parseArray(JSONArray.toJSONString(r.getData()), ComMngPopulationImportErrorVO.class);
                if (!errorList.isEmpty()) {
                    populationImportErrorVOList.addAll(errorList);
                }
                log.info("将错误数据存入redis中");
                valueOperations.set(key, JSONArray.toJSONString(populationImportErrorVOList), 1, TimeUnit.HOURS);
                log.info("将错误数据存入redis中成功");
                throw new ServiceException("500", key);
            } else {
                if (!populationImportErrorVOList.isEmpty()) {
                    log.info("将错误数据存入redis中");
                    valueOperations.set(key, JSONArray.toJSONString(populationImportErrorVOList), 1, TimeUnit.HOURS);
                    log.info("将错误数据存入redis中成功");
                    throw new ServiceException("500", key);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    private ComMngPopulationImportErrorVO setError(Map<Integer, String> oneData){
        ComMngPopulationImportErrorVO result = new ComMngPopulationImportErrorVO();
        if(oneData != null){
//            result.setUseType(oneData.get(0));
//            result.setBuildType(oneData.get(1));
            result.setAlley(oneData.get(0));
            result.setVillageName(oneData.get(1));
            result.setDoorNo(oneData.get(2));
            result.setName(oneData.get(3));
//            result.setUnitTotal(Integer.parseInt(oneData.get(6)));
//            result.setBuildFloorSum(Integer.parseInt(oneData.get(7)));
//            result.setEveryFloorSum(Integer.parseInt(oneData.get(8)));
//            result.setBuildUserSum(Integer.parseInt(oneData.get(9)));
//            result.setElevatorTotal(Integer.parseInt(oneData.get(10)));
//            result.setPropertyCompanyName(oneData.get(11));
//            result.setBuildDate(DateUtils.stringToDate(oneData.get(12),DateUtils.format_yyyy));
//            result.setRemark(oneData.get(13));
//            result.setGridId(oneData.get(14));
        }
        return result;
    }
}