5 changed files with 739 additions and 653 deletions
@ -0,0 +1,12 @@ |
|||||
|
package com.epmet.service; |
||||
|
|
||||
|
/** |
||||
|
*@Description 居民信息导入service |
||||
|
*@Author wangxianzhang |
||||
|
*@Date 2021/11/4 |
||||
|
*/ |
||||
|
public interface IcResiUserImportService { |
||||
|
|
||||
|
Object importIcResiInfoFromExcel(); |
||||
|
|
||||
|
} |
@ -0,0 +1,718 @@ |
|||||
|
package com.epmet.service.impl; |
||||
|
|
||||
|
import com.alibaba.excel.EasyExcelFactory; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.epmet.commons.tools.constant.ServiceConstant; |
||||
|
import com.epmet.commons.tools.dto.result.OptionResultDTO; |
||||
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.commons.tools.feign.ResultDataResolver; |
||||
|
import com.epmet.commons.tools.security.user.LoginUserUtil; |
||||
|
import com.epmet.commons.tools.utils.Result; |
||||
|
import com.epmet.dao.IcResiUserDao; |
||||
|
import com.epmet.dto.*; |
||||
|
import com.epmet.dto.form.AgencyIdFormDTO; |
||||
|
import com.epmet.dto.form.CustomerFormQueryDTO; |
||||
|
import com.epmet.dto.form.HouseFormDTO; |
||||
|
import com.epmet.dto.form.LoginUserDetailsFormDTO; |
||||
|
import com.epmet.dto.result.FormItem; |
||||
|
import com.epmet.dto.result.LoginUserDetailsResultDTO; |
||||
|
import com.epmet.dto.result.OptionDTO; |
||||
|
import com.epmet.entity.IcResiUserEntity; |
||||
|
import com.epmet.excel.handler.DynamicEasyExcelListener; |
||||
|
import com.epmet.feign.EpmetAdminOpenFeignClient; |
||||
|
import com.epmet.feign.EpmetUserOpenFeignClient; |
||||
|
import com.epmet.feign.GovOrgOpenFeignClient; |
||||
|
import com.epmet.feign.OperCustomizeOpenFeignClient; |
||||
|
import com.epmet.service.IcResiUserImportService; |
||||
|
import lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.apache.commons.collections4.CollectionUtils; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* @ClassName IcResiUserImportServiceImpl |
||||
|
* @Description TODO |
||||
|
* @Author wangxianzhang |
||||
|
* @Date 2021/11/4 8:53 下午 |
||||
|
* @Version 1.0 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class IcResiUserImportServiceImpl implements IcResiUserImportService, ResultDataResolver { |
||||
|
|
||||
|
public static final ThreadLocal errorRow = new ThreadLocal<ErrorRow>(); |
||||
|
|
||||
|
@Autowired |
||||
|
private LoginUserUtil loginUserUtil; |
||||
|
|
||||
|
@Autowired |
||||
|
private EpmetUserOpenFeignClient epmetUserOpenFeignClient; |
||||
|
|
||||
|
@Autowired |
||||
|
private EpmetAdminOpenFeignClient adminOpenFeignClient; |
||||
|
|
||||
|
@Autowired |
||||
|
private GovOrgOpenFeignClient govOrgOpenFeignClient; |
||||
|
|
||||
|
@Autowired |
||||
|
private OperCustomizeOpenFeignClient operCustomizeOpenFeignClient; |
||||
|
|
||||
|
@Autowired |
||||
|
private IcResiUserDao icResiUserDao; |
||||
|
|
||||
|
/** |
||||
|
* @description 列信息封装 |
||||
|
* |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.28 22:18:05 |
||||
|
*/ |
||||
|
@Data |
||||
|
public static class ColumnWrapper { |
||||
|
private String combinedLabel; |
||||
|
private String columnName; |
||||
|
private String itemType; |
||||
|
private String itemId; |
||||
|
private List<Integer> colIndexs; |
||||
|
//private List<String> colContents;
|
||||
|
// 单元格内容
|
||||
|
private String cellContent; |
||||
|
// 数据库中列的值
|
||||
|
private String colValue; |
||||
|
|
||||
|
// key:label,value:value
|
||||
|
private Map<String, String> options; |
||||
|
|
||||
|
/** |
||||
|
* 选项来源,REMOTE;LOCAL;如果是动态加载的下拉框或者CHECKBOX等的情况下使用。URL:接口获取(LABEL,VALUE);JSON:直接从JSON中取 |
||||
|
*/ |
||||
|
private String optionSourceType; |
||||
|
|
||||
|
/** |
||||
|
* 来源地址,REMOTE才有,固定格式;如果OPTIONS_SOURCE是URL,则此处填写要调用的接口的URL相对路径,例如:/API/GOV/ORG/XXXX。此处不应设置参数,若需要参数应当完全由后端,通过TOKEN信息来获取 |
||||
|
*/ |
||||
|
private String optionSourceValue; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 错误行信息 |
||||
|
*/ |
||||
|
public static class ErrorRow { |
||||
|
private String sheetName; |
||||
|
private String idCard; |
||||
|
private String name; |
||||
|
private String errorInfo; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 导入居民信息 |
||||
|
* 导入主表和所有子表信息 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Override |
||||
|
public Object importIcResiInfoFromExcel() { |
||||
|
String loginUserId = loginUserUtil.getLoginUserId(); |
||||
|
String loginUserApp = loginUserUtil.getLoginUserApp(); |
||||
|
String loginUserClient = loginUserUtil.getLoginUserClient(); |
||||
|
|
||||
|
LoginUserDetailsFormDTO userForm = new LoginUserDetailsFormDTO(); |
||||
|
userForm.setApp(loginUserApp); |
||||
|
userForm.setClient(loginUserClient); |
||||
|
userForm.setUserId(loginUserId); |
||||
|
|
||||
|
LoginUserDetailsResultDTO loginUserDetails = getResultDataOrThrowsException(epmetUserOpenFeignClient.getLoginUserDetails(userForm), ServiceConstant.EPMET_USER_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
String currUserAgencyId = loginUserDetails.getAgencyId(); |
||||
|
|
||||
|
String excelPathName = "/opt/test/基础信息表/resi_info.xls"; |
||||
|
CustomerAgencyDTO agencyInfo = getResultDataOrThrowsException(govOrgOpenFeignClient.getAgencyById(currUserAgencyId), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
String customerId = agencyInfo.getCustomerId(); |
||||
|
|
||||
|
importIcResiBaseInfoFromExcel(excelPathName, 0, 3, currUserAgencyId, agencyInfo.getPids(), loginUserId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 1, 2, currUserAgencyId, loginUserId, "ic_party_member", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 2, 2, currUserAgencyId, loginUserId, "ic_ensure_house", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 3, 2, currUserAgencyId, loginUserId, "ic_unemployed", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 4, 2, currUserAgencyId, loginUserId, "ic_veterans", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 5, 2, currUserAgencyId, loginUserId, "ic_united_front", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 6, 3, currUserAgencyId, loginUserId, "ic_volunteer", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 7, 2, currUserAgencyId, loginUserId, "ic_old_people", customerId); |
||||
|
importIcResiExtraInfoFromExcel(excelPathName, 8, 3, currUserAgencyId, loginUserId, "ic_special", customerId); |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* excel导入居民基本信息 |
||||
|
* @param sheetNo |
||||
|
* @param headRowNumber |
||||
|
* @param currUserAgencyId |
||||
|
* @param currUserAgencyPids |
||||
|
* @param currentUserId |
||||
|
* @return |
||||
|
*/ |
||||
|
private Object importIcResiBaseInfoFromExcel(String excelPathName, int sheetNo, int headRowNumber, String currUserAgencyId, String currUserAgencyPids, String currentUserId) { |
||||
|
DynamicEasyExcelListener readListener = new DynamicEasyExcelListener(); |
||||
|
EasyExcelFactory.read(new File(excelPathName)).registerReadListener(readListener).headRowNumber(headRowNumber).sheet(sheetNo).doRead(); |
||||
|
|
||||
|
List<Map<Integer, String>> headList = readListener.getHeadList(); |
||||
|
List<Map<Integer, String>> dataList = readListener.getDataList(); |
||||
|
|
||||
|
Map<Integer, List<String>> headers = mergeHead(headList); |
||||
|
|
||||
|
// 查询form相关信息
|
||||
|
List<FormItem> customerItems = listFormItems("resi_base_info"); |
||||
|
|
||||
|
// 清洗表头数据
|
||||
|
Map<Integer, String> abandonedHeaders = washHeaders(headers, customerItems); |
||||
|
|
||||
|
//合并多级表头
|
||||
|
HashMap<String, List<Integer>> combinedHeaders = combineHeaders(headers); |
||||
|
|
||||
|
// 得到客户配置item数据
|
||||
|
Map<String, FormItem> formItemMap = customerItems.stream().collect( |
||||
|
Collectors.toMap(formItem -> { |
||||
|
String groupLabel = formItem.getGroupLabel(); |
||||
|
String label = formItem.getLabel(); |
||||
|
if (StringUtils.isNotBlank(groupLabel)) { |
||||
|
return groupLabel.concat(":").concat(label); |
||||
|
} else { |
||||
|
return label; |
||||
|
} |
||||
|
}, formItem -> formItem) |
||||
|
); |
||||
|
Map<String, ColumnWrapper> headerColumnWrapper = convertExcelHeaders2DBColumnWrappers(formItemMap, combinedHeaders, dataList, abandonedHeaders); |
||||
|
|
||||
|
// 持久化
|
||||
|
persistIcResiBaseInfo(headerColumnWrapper, dataList, currUserAgencyId, abandonedHeaders, currUserAgencyPids, currentUserId); |
||||
|
|
||||
|
return headers; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* excel导入居民附加信息 |
||||
|
* @param excelPathName |
||||
|
* @param sheetNo |
||||
|
* @param headRowNumber |
||||
|
* @param currUserAgencyId |
||||
|
* @param currentUserId |
||||
|
* @param targetTableName 要插入哪一个表 |
||||
|
* @return |
||||
|
*/ |
||||
|
private Object importIcResiExtraInfoFromExcel(String excelPathName, int sheetNo, int headRowNumber, String currUserAgencyId, String currentUserId, |
||||
|
String targetTableName, String customerId) { |
||||
|
DynamicEasyExcelListener readListener = new DynamicEasyExcelListener(); |
||||
|
EasyExcelFactory.read(new File(excelPathName)).registerReadListener(readListener).headRowNumber(headRowNumber).sheet(sheetNo).doRead(); |
||||
|
|
||||
|
List<Map<Integer, String>> headList = readListener.getHeadList(); |
||||
|
List<Map<Integer, String>> dataList = readListener.getDataList(); |
||||
|
|
||||
|
Map<Integer, List<String>> headers = mergeHead(headList); |
||||
|
|
||||
|
// 查询form相关信息
|
||||
|
List<FormItem> customerItems = listFormItems("resi_base_info"); |
||||
|
|
||||
|
// 清洗表头数据
|
||||
|
Map<Integer, String> abandonedHeaders = washHeaders(headers, customerItems); |
||||
|
|
||||
|
//合并多级表头
|
||||
|
HashMap<String, List<Integer>> combinedHeaders = combineHeaders(headers); |
||||
|
|
||||
|
// 得到客户配置item数据
|
||||
|
Map<String, FormItem> formItemMap = customerItems.stream().collect( |
||||
|
Collectors.toMap(formItem -> { |
||||
|
String groupLabel = formItem.getGroupLabel(); |
||||
|
String label = formItem.getLabel(); |
||||
|
if (StringUtils.isNotBlank(groupLabel)) { |
||||
|
return groupLabel.concat(":").concat(label); |
||||
|
} else { |
||||
|
return label; |
||||
|
} |
||||
|
}, formItem -> formItem) |
||||
|
); |
||||
|
Map<String, ColumnWrapper> headerColumnWrapper = convertExcelHeaders2DBColumnWrappers(formItemMap, combinedHeaders, dataList, abandonedHeaders); |
||||
|
persistIcResiExtraInfo(headerColumnWrapper, dataList, currUserAgencyId, abandonedHeaders, currentUserId, targetTableName, customerId); |
||||
|
return headerColumnWrapper; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 持久化IC居民基础信息 |
||||
|
* @param headerColumnWrapper 数据库列包装信息 |
||||
|
* @param dataRows 数据行集合 |
||||
|
* @param currUserAgencyId 当前用户的组织id |
||||
|
* @param checkBoxOptionColumnIdxAndLabel 复选框的列号&label中文 |
||||
|
*/ |
||||
|
private void persistIcResiBaseInfo(Map<String, ColumnWrapper> headerColumnWrapper, List<Map<Integer, String>> dataRows, |
||||
|
String currUserAgencyId, Map<Integer, String> checkBoxOptionColumnIdxAndLabel, |
||||
|
String currUserAgencyPids, String currentUserId) { |
||||
|
|
||||
|
// 遍历每一行,将行内容转化为
|
||||
|
for (Map<Integer, String> row : dataRows) { |
||||
|
|
||||
|
LinkedHashMap<String, String> columnAndValues = convertColumnWrappers2Map4Persist(headerColumnWrapper, row, currUserAgencyId, checkBoxOptionColumnIdxAndLabel); |
||||
|
|
||||
|
columnAndValues.put("IS_ENSURE_HOUSE", "0"); |
||||
|
columnAndValues.put("IS_OLD_PEOPLE", "0"); |
||||
|
columnAndValues.put("IS_PARTY", "0"); |
||||
|
columnAndValues.put("IS_SPECIAL", "0"); |
||||
|
columnAndValues.put("IS_UNEMPLOYED", "0"); |
||||
|
columnAndValues.put("IS_UNITED_FRONT", "0"); |
||||
|
columnAndValues.put("IS_VETERANS", "0"); |
||||
|
columnAndValues.put("IS_VOLUNTEER", "0"); |
||||
|
|
||||
|
columnAndValues.put("AGENCY_ID", currUserAgencyId); |
||||
|
columnAndValues.put("PIDS", currUserAgencyPids); |
||||
|
columnAndValues.put("CUSTOMER_ID", loginUserUtil.getCurrentCustomerId()); |
||||
|
columnAndValues.put("CREATED_BY", currentUserId); |
||||
|
columnAndValues.put("UPDATED_BY", currentUserId); |
||||
|
columnAndValues.put("ID", UUID.randomUUID().toString().replace("-", "")); |
||||
|
|
||||
|
// 验证居民信息是否存在
|
||||
|
String idCard = columnAndValues.get("ID_CARD"); |
||||
|
LambdaQueryWrapper<IcResiUserEntity> idCardQuery = new LambdaQueryWrapper<>(); |
||||
|
idCardQuery.eq(IcResiUserEntity::getIdCard, idCard); |
||||
|
|
||||
|
if (icResiUserDao.selectCount(idCardQuery) > 0) { |
||||
|
log.info("身份证号为【{}】的居民信息已存在,跳过导入", idCard); |
||||
|
} |
||||
|
|
||||
|
icResiUserDao.add("ic_resi_user", columnAndValues); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 持久化IC居民附加信息 |
||||
|
* @param headerColumnWrapper 数据库列包装信息 |
||||
|
* @param dataRows 数据行集合 |
||||
|
* @param currUserAgencyId 当前用户的组织id |
||||
|
* @param checkBoxOptionColumnIdxAndLabel 复选框的列号&label中文 |
||||
|
* @param targetTableName 要插入到哪一个表 |
||||
|
*/ |
||||
|
private void persistIcResiExtraInfo(Map<String, ColumnWrapper> headerColumnWrapper, List<Map<Integer, String>> dataRows, |
||||
|
String currUserAgencyId, Map<Integer, String> checkBoxOptionColumnIdxAndLabel, |
||||
|
String currentUserId, String targetTableName, String customerId) { |
||||
|
|
||||
|
// 遍历每一行,将行内容转化为
|
||||
|
for (Map<Integer, String> row : dataRows) { |
||||
|
|
||||
|
LinkedHashMap<String, String> columnAndValues = convertColumnWrappers2Map4Persist(headerColumnWrapper, row, currUserAgencyId, checkBoxOptionColumnIdxAndLabel); |
||||
|
|
||||
|
// 检验身份证号
|
||||
|
String idCard = columnAndValues.get("ID_CARD"); |
||||
|
if (StringUtils.isBlank(idCard)) { |
||||
|
throw new RenException(EpmetErrorCode.IDCARDNO_ERROR.getCode(), String.format("用户【%s】身份证号未填写或格式错误", columnAndValues.get("NAME"))); |
||||
|
} |
||||
|
|
||||
|
// 检查用户是否存在
|
||||
|
LambdaQueryWrapper<IcResiUserEntity> idCardQuery = new LambdaQueryWrapper<>(); |
||||
|
idCardQuery.eq(IcResiUserEntity::getIdCard, idCard); |
||||
|
|
||||
|
IcResiUserEntity icResiUserBaseInfo = icResiUserDao.selectOne(idCardQuery); |
||||
|
if (icResiUserBaseInfo == null) { |
||||
|
throw new RenException(EpmetErrorCode.RESI_NOT_FOUND.getCode(), String.format("身份证号为【%s】的居民信息未找到,请确认该居民信息存在", idCard)); |
||||
|
} |
||||
|
|
||||
|
String icResiId = icResiUserBaseInfo.getId(); |
||||
|
|
||||
|
// 验证党员信息是否存在
|
||||
|
if (CollectionUtils.isNotEmpty(icResiUserDao.selectSubTableRecords(customerId, icResiId, targetTableName))) { |
||||
|
log.info("身份证号为【{}】的居民【{}】信息已存在,跳过导入", idCard, targetTableName); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
columnAndValues.put("IC_RESI_USER", icResiId); |
||||
|
|
||||
|
columnAndValues.put("CUSTOMER_ID", loginUserUtil.getCurrentCustomerId()); |
||||
|
columnAndValues.put("CREATED_BY", currentUserId); |
||||
|
columnAndValues.put("UPDATED_BY", currentUserId); |
||||
|
columnAndValues.put("ID", UUID.randomUUID().toString().replace("-", "")); |
||||
|
|
||||
|
columnAndValues = removeNeedlessColumns(columnAndValues); |
||||
|
|
||||
|
icResiUserDao.add(targetTableName, columnAndValues); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 去掉多余的列 |
||||
|
* @param originColumnAndValues |
||||
|
* @return |
||||
|
*/ |
||||
|
private LinkedHashMap<String, String> removeNeedlessColumns(LinkedHashMap<String, String> originColumnAndValues) { |
||||
|
List<String> needlessColumns = Arrays.asList("GRID_ID", "VILLAGE_ID", "BUILD_ID", "UNIT_ID", "HOME_ID", |
||||
|
"IS_BDHJ", "NAME", "MOBILE", "GENDER", "ID_CARD", |
||||
|
"BIRTHDAY", "CONTACTS", "CONTACTS_MOBILE"); |
||||
|
|
||||
|
LinkedHashMap<String, String> newMap = new LinkedHashMap(); |
||||
|
|
||||
|
originColumnAndValues.entrySet().forEach(b -> { |
||||
|
if (!needlessColumns.contains(b.getKey())) { |
||||
|
newMap.put(b.getKey(), b.getValue()); |
||||
|
} |
||||
|
}); |
||||
|
|
||||
|
return newMap; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 将当前行数据转化成LinkedHashMap,供后续插入 |
||||
|
* @param headerColumnWrapper 当前行的ColumnWrapper,每一个ColumnWrapper元素都是当前行中的一个列 |
||||
|
* @param row 当前行数据 |
||||
|
* @param currUserAgencyId 当前用户所属机构ID |
||||
|
* @param checkBoxOptionColumnIdxAndLabel 复选框options列表。key:列号,value:复选框中文 |
||||
|
* @return |
||||
|
*/ |
||||
|
private LinkedHashMap<String, String> convertColumnWrappers2Map4Persist(Map<String, ColumnWrapper> headerColumnWrapper, Map<Integer, String> row, |
||||
|
String currUserAgencyId, Map<Integer, String> checkBoxOptionColumnIdxAndLabel) { |
||||
|
LinkedHashMap<String, String> columnAndValues = new LinkedHashMap<>(); |
||||
|
|
||||
|
for (Map.Entry<String, ColumnWrapper> columnWrapperEntry : headerColumnWrapper.entrySet()) { |
||||
|
|
||||
|
ColumnWrapper columnWrapper = columnWrapperEntry.getValue(); |
||||
|
if ("input".equals(columnWrapper.getItemType()) |
||||
|
|| "textarea".equals(columnWrapper.getItemType()) |
||||
|
|| "datepicker".equals(columnWrapper.getItemType()) |
||||
|
|| "daterange".equals(columnWrapper.getItemType()) |
||||
|
) { |
||||
|
|
||||
|
String cellContent = row.get(columnWrapper.getColIndexs().get(0)); |
||||
|
columnWrapper.setCellContent(cellContent); |
||||
|
columnWrapper.setColValue(cellContent); |
||||
|
|
||||
|
} else if ("select".equals(columnWrapper.getItemType()) |
||||
|
|| "radio".equals(columnWrapper.getItemType())){ |
||||
|
|
||||
|
String optionSourceType = columnWrapper.getOptionSourceType(); |
||||
|
// 取单元格的内容
|
||||
|
String cellContent = row.get(columnWrapper.getColIndexs().get(0)); |
||||
|
columnWrapper.setCellContent(cellContent); |
||||
|
|
||||
|
if ("local".equals(optionSourceType)) { |
||||
|
// 根据单元格内容,取到指定的option
|
||||
|
Map<String, String> options = columnWrapper.getOptions(); |
||||
|
String colValue = options.get(cellContent); |
||||
|
columnWrapper.setColValue(colValue); |
||||
|
} else { |
||||
|
// remote类型
|
||||
|
|
||||
|
Map<String, String> options = listRemoteOptions(headerColumnWrapper, columnWrapper.getOptionSourceValue(), currUserAgencyId); |
||||
|
String colValue = options.get(cellContent); |
||||
|
columnWrapper.setColValue(colValue); |
||||
|
} |
||||
|
} else if ("checkbox".equals(columnWrapper.getItemType())) { |
||||
|
String checkBoxColValue = getCheckBoxColValue(columnWrapper, row, checkBoxOptionColumnIdxAndLabel); |
||||
|
columnWrapper.setColValue(checkBoxColValue); |
||||
|
} |
||||
|
columnAndValues.put(columnWrapper.columnName, columnWrapper.colValue); |
||||
|
} |
||||
|
|
||||
|
return columnAndValues; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 根据formCode查询该form的item列表 |
||||
|
* @param formCode |
||||
|
* @return |
||||
|
*/ |
||||
|
private List<FormItem> listFormItems(String formCode) { |
||||
|
CustomerFormQueryDTO form = new CustomerFormQueryDTO(); |
||||
|
form.setFormCode(formCode); |
||||
|
Result<List<FormItem>> result = operCustomizeOpenFeignClient.listItems(form); |
||||
|
return getResultDataOrThrowsException(result, ServiceConstant.OPER_CUSTOMIZE_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), "【居民信息excel导入】查询表单相关信息失败"); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description 合并头 |
||||
|
* |
||||
|
* @param headers |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.28 21:27:18 |
||||
|
*/ |
||||
|
private HashMap<String, List<Integer>> combineHeaders(Map<Integer, List<String>> headers) { |
||||
|
HashMap<String, List<Integer>> itemAndColIndexs = new LinkedHashMap<>(); |
||||
|
|
||||
|
headers.forEach((k, v) -> { |
||||
|
String tempKey = String.join(":", v); |
||||
|
List<Integer> colIndexs = itemAndColIndexs.get(tempKey); |
||||
|
if (colIndexs == null) { |
||||
|
colIndexs = new ArrayList<>(); |
||||
|
itemAndColIndexs.put(tempKey, colIndexs); |
||||
|
} |
||||
|
colIndexs.add(k); |
||||
|
}); |
||||
|
|
||||
|
return itemAndColIndexs; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description 洗头 |
||||
|
* |
||||
|
* @param headers |
||||
|
* @param items |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.28 21:07:12 |
||||
|
*/ |
||||
|
private Map<Integer, String> washHeaders(Map<Integer, List<String>> headers, List<FormItem> items) { |
||||
|
List<String> itemLabels = items.stream().map(i -> i.getLabel()).collect(Collectors.toList()); |
||||
|
Map<Integer, String> abandonedHeaders = new HashMap<>(); |
||||
|
for (Map.Entry<Integer, List<String>> entry:headers.entrySet()) { |
||||
|
Integer colIdx = entry.getKey(); |
||||
|
List<String> v = entry.getValue(); |
||||
|
int lastPartIndex = v.size() - 1; |
||||
|
String lastValuePart = v.get(lastPartIndex); |
||||
|
if (!itemLabels.contains(lastValuePart)) { |
||||
|
// 该部分为options,它的上一级是item,那么去掉options这一级
|
||||
|
v.remove(lastPartIndex); |
||||
|
abandonedHeaders.put(colIdx, lastValuePart); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return abandonedHeaders; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description 数据整合,将excel表头汉字信息转化成ColumnWrapper列封装数据 |
||||
|
* * key:itemId |
||||
|
* * value:ColumnWrapper列包装信息 |
||||
|
* @param formItemMap item map |
||||
|
* key:itemId; |
||||
|
* value: 合并之后的head字符串,例如:健康信息:监护人 |
||||
|
* @param combinedHeaders 合并之后的head信息。 |
||||
|
* key:合并之后head字符串(例如:党员信息:入党时间); |
||||
|
* value:列号组成的列表(例如[1,2,3]) |
||||
|
* @param datas |
||||
|
* @param abandonedHeaders |
||||
|
* @return |
||||
|
*/ |
||||
|
private Map<String, ColumnWrapper> convertExcelHeaders2DBColumnWrappers(Map<String, FormItem> formItemMap, Map<String, List<Integer>> combinedHeaders, |
||||
|
List<Map<Integer, String>> datas, Map<Integer, String> abandonedHeaders) { |
||||
|
|
||||
|
// HashMap<String, List<ColumnWrapper>> tables = new HashMap<>();
|
||||
|
Map<String, ColumnWrapper> columns = new LinkedHashMap<>(combinedHeaders.size()); |
||||
|
|
||||
|
for (Map.Entry<String, List<Integer>> entry : combinedHeaders.entrySet()) { |
||||
|
String combinedHeader = entry.getKey(); |
||||
|
|
||||
|
FormItem item = formItemMap.get(combinedHeader); |
||||
|
|
||||
|
if (item == null) { |
||||
|
// 如果数据库中没有该项,可能是用户自己定义的项,忽略
|
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
ColumnWrapper columnWrapper = new ColumnWrapper(); |
||||
|
|
||||
|
// 填充options
|
||||
|
columnWrapper.setItemType(item.getItemType()); |
||||
|
columnWrapper.setItemId(item.getItemId()); |
||||
|
String groupLabel = item.getGroupLabel(); |
||||
|
String combinedLabel = StringUtils.isBlank(groupLabel) ? item.getLabel() : groupLabel.concat(":").concat(item.getLabel()); |
||||
|
columnWrapper.setCombinedLabel(combinedLabel); |
||||
|
columnWrapper.setColumnName(item.getColumnName()); |
||||
|
columnWrapper.setColIndexs(entry.getValue()); |
||||
|
|
||||
|
columnWrapper.setOptionSourceType(item.getOptionSourceType()); |
||||
|
columnWrapper.setOptionSourceValue(item.getOptionSourceValue()); |
||||
|
columnWrapper.setOptions(item.getOptions().stream().collect(Collectors.toMap(OptionDTO::getLabel, OptionDTO::getValue))); |
||||
|
|
||||
|
columns.put(item.getItemId(), columnWrapper); |
||||
|
} |
||||
|
return columns; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取checkbox列值 |
||||
|
* @param columnWrapper 数据库列包装信息,每一列跳数据对应数据库的一个列 |
||||
|
* @param dataRow 数据行,每一条都是一行中的一个单元格 |
||||
|
* @param checkboxOptions 复选框的选项。k: 列号, value: checkboxlabel中文 |
||||
|
* @return |
||||
|
*/ |
||||
|
private String getCheckBoxColValue(ColumnWrapper columnWrapper, Map<Integer, String> dataRow, Map<Integer, String> checkboxOptions) { |
||||
|
|
||||
|
Map<String, String> options = columnWrapper.getOptions(); |
||||
|
List<Integer> colIndexs = columnWrapper.getColIndexs(); |
||||
|
|
||||
|
List<String> optionValues = colIndexs.stream().filter(i -> { |
||||
|
String cellContent = dataRow.get(i); |
||||
|
return StringUtils.isNotBlank(cellContent) && cellContent.equals("是"); |
||||
|
}).map(i -> { |
||||
|
String checkboxOptionLabel = checkboxOptions.get(i); |
||||
|
return options.get(checkboxOptionLabel); |
||||
|
}).collect(Collectors.toList()); |
||||
|
|
||||
|
return String.join(",", optionValues); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description 合并表头 |
||||
|
* Map<Integer, List<String>> |
||||
|
* |
||||
|
* @param headList |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.27 16:17:34 |
||||
|
*/ |
||||
|
private Map<Integer, List<String>> mergeHead(List<Map<Integer, String>> headList) { |
||||
|
|
||||
|
Map<Integer, String> lastNotNullHeads = new LinkedHashMap<>(); |
||||
|
|
||||
|
Map<Integer, List<String>> mergedHead = new LinkedHashMap<>(); |
||||
|
|
||||
|
int levelCount = headList.size(); |
||||
|
|
||||
|
Map<Integer, String> firstLevel = headList.get(0); |
||||
|
|
||||
|
// 遍历所有列
|
||||
|
for (Map.Entry<Integer, String> column : firstLevel.entrySet()) { |
||||
|
Integer columIndex = column.getKey(); |
||||
|
|
||||
|
List<String> labels = new ArrayList<>(); |
||||
|
|
||||
|
// 竖着遍历该列的所有行
|
||||
|
for (int currentLevel = 0; currentLevel < levelCount; currentLevel ++) { |
||||
|
String label = headList.get(currentLevel).get(columIndex); |
||||
|
|
||||
|
if (StringUtils.isNotBlank(label)) { |
||||
|
lastNotNullHeads.put(currentLevel, label); |
||||
|
} else { |
||||
|
if (currentLevel == 0 || StringUtils.isBlank(headList.get(currentLevel - 1).get(columIndex))) { |
||||
|
label = lastNotNullHeads.get(currentLevel); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(label)) { |
||||
|
labels.add(label); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
mergedHead.put(columIndex, labels); |
||||
|
} |
||||
|
|
||||
|
return mergedHead; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 远程获取options |
||||
|
* @param fullUri |
||||
|
* @return |
||||
|
*/ |
||||
|
public Map<String, String> listRemoteOptions(Map<String, ColumnWrapper> columnWrappers, String fullUri, String currUserAgencyId) { |
||||
|
String pureUri = null; |
||||
|
String cascadeItemId = null; |
||||
|
ColumnWrapper cascadeItemColumnWrapper = null; |
||||
|
|
||||
|
if (fullUri.indexOf("?") != -1) { |
||||
|
String[] uriParts = fullUri.split("\\?"); |
||||
|
pureUri = uriParts[0]; |
||||
|
cascadeItemId = uriParts[1]; |
||||
|
|
||||
|
// 根据uri上的id,找到关联的itemid,从而找到关联的item的值
|
||||
|
if (columnWrappers != null){ |
||||
|
cascadeItemColumnWrapper = columnWrappers.get(cascadeItemId); |
||||
|
} |
||||
|
} else { |
||||
|
pureUri = fullUri; |
||||
|
} |
||||
|
|
||||
|
List<OptionResultDTO> options = null; |
||||
|
|
||||
|
switch (pureUri) { |
||||
|
case "/epmetuser/icresidemanddict/demandoption": |
||||
|
options = getResultDataOrThrowsException(epmetUserOpenFeignClient.getDemandOptions(), ServiceConstant.EPMET_USER_SERVER, |
||||
|
EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/customergrid/gridoption": |
||||
|
AgencyIdFormDTO form = new AgencyIdFormDTO(); |
||||
|
form.setAgencyId(currUserAgencyId); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getGridOption(form), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/customerpartybranch/branchoption": |
||||
|
|
||||
|
CustomerPartyBranchDTO bform = new CustomerPartyBranchDTO(); |
||||
|
bform.setGridId(cascadeItemColumnWrapper.getColValue()); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getBranchOption(bform), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/icbuilding/buildingoption": |
||||
|
IcBuildingDTO buildingform = new IcBuildingDTO(); |
||||
|
buildingform.setNeighborHoodId(cascadeItemColumnWrapper.getColValue()); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getBuildingOptions(buildingform), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/icbuildingunit/unitoption": |
||||
|
IcBuildingUnitDTO buForm = new IcBuildingUnitDTO(); |
||||
|
buForm.setBuildingId(cascadeItemColumnWrapper.getColValue()); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getUnitOptions(buForm), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/ichouse/houseoption": |
||||
|
HouseFormDTO hform = new HouseFormDTO(); |
||||
|
hform.setUnitId(cascadeItemColumnWrapper.getColValue()); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getHouseOption(hform), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/gov/org/icneighborhood/neighborhoodoption": |
||||
|
IcNeighborHoodDTO nform = new IcNeighborHoodDTO(); |
||||
|
nform.setAgencyId(currUserAgencyId); |
||||
|
nform.setGridId(cascadeItemColumnWrapper.getColValue()); |
||||
|
options = getResultDataOrThrowsException(govOrgOpenFeignClient.getNeighborHoodOptions(nform), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/sys/dict/data/education": |
||||
|
options = getResultDataOrThrowsException(adminOpenFeignClient.getEducationOption(), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/sys/dict/data/house": |
||||
|
options = getResultDataOrThrowsException(adminOpenFeignClient.getHouseOption(), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/sys/dict/data/nation": |
||||
|
options = getResultDataOrThrowsException(adminOpenFeignClient.getNationOption(), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/sys/dict/data/ninesmallplaces": |
||||
|
options = getResultDataOrThrowsException(adminOpenFeignClient.getNineSmallPlacesOption(), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
case "/sys/dict/data/relationship": |
||||
|
options = getResultDataOrThrowsException(adminOpenFeignClient.getRelationshipOption(), ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), null); |
||||
|
break; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
if (options == null) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
return options.stream().collect(Collectors.toMap(OptionResultDTO::getLabel, OptionResultDTO::getValue)); |
||||
|
|
||||
|
// 通用api调用,无法实现
|
||||
|
/*if (!uri.startsWith("/api")) uri = "/api".concat(uri); |
||||
|
|
||||
|
NamingService namingService = discoveryProperties.namingServiceInstance(); |
||||
|
ResponseEntity<Result<List<OptionResultDTO>>> response = null; |
||||
|
try { |
||||
|
// 调用gateway服务,查询相关接口
|
||||
|
Instance gatewayInstance = namingService.getAllInstances(ServiceConstant.EPMET_GATEWAY).get(0); |
||||
|
String ip = gatewayInstance.getIp(); |
||||
|
int port = gatewayInstance.getPort(); |
||||
|
|
||||
|
String url = String.format("http://%s:%s%s", ip, port, uri); |
||||
|
|
||||
|
ParameterizedTypeReference<Result<List<OptionResultDTO>>> tr = new ParameterizedTypeReference<Result<List<OptionResultDTO>>>() {}; |
||||
|
response = new RestTemplate().exchange(url, HttpMethod.POST, null, tr); |
||||
|
} catch (NacosException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
if (response != null && (response.getStatusCode() == HttpStatus.OK)) { |
||||
|
List<OptionResultDTO> options = response.getBody().getData(); |
||||
|
if (options == null) { |
||||
|
System.out.println(6); |
||||
|
} |
||||
|
return options.stream().collect(Collectors.toMap(OptionResultDTO::getLabel, OptionResultDTO::getValue)); |
||||
|
} else { |
||||
|
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode()); |
||||
|
}*/ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue