Browse Source

联建单位导入

master
yinzuomei 3 years ago
parent
commit
8a73ea7fa7
  1. 93
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyUnitController.java
  2. 80
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyUnitImportExcel.java
  3. 132
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyUnitExcelImportListener.java
  4. 22
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyUnitService.java
  5. 333
      epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcPartyUnitServiceImpl.java

93
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/controller/IcPartyUnitController.java

@ -8,16 +8,16 @@ import com.epmet.commons.tools.annotation.LoginUser;
import com.epmet.commons.tools.aop.NoRepeatSubmit;
import com.epmet.commons.tools.constant.AppClientConstant;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.constant.ServiceConstant;
import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.dto.result.OptionDataResultDTO;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.feign.ResultDataResolver;
import com.epmet.commons.tools.page.PageData;
import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.DateUtils;
import com.epmet.commons.tools.utils.ExcelUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.utils.*;
import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter;
import com.epmet.commons.tools.validator.AssertUtils;
import com.epmet.commons.tools.validator.ValidatorUtils;
@ -34,7 +34,6 @@ import com.epmet.feign.EpmetCommonServiceOpenFeignClient;
import com.epmet.service.IcPartyUnitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -46,12 +45,14 @@ import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.UUID;
import java.util.stream.Collectors;
@ -64,7 +65,7 @@ import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("icpartyunit")
public class IcPartyUnitController {
public class IcPartyUnitController implements ResultDataResolver {
@Autowired
private IcPartyUnitService icPartyUnitService;
@ -192,37 +193,48 @@ public class IcPartyUnitController {
*/
@PostMapping("import")
public Result importData(@LoginUser TokenDto tokenDto, HttpServletRequest multipartRequest, HttpServletResponse response, @RequestPart("file") MultipartFile file) throws IOException {
// 1.暂存文件
String originalFilename = file.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
if (file.isEmpty()) {
throw new RenException("请上传文件");
Path fileSavePath;
try {
Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_party_unit", "import");
fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName));
} catch (IOException e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【联建单位导入】创建临时存储文件失败:{}", errorMsg);
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败");
}
String originalFilename = file.getOriginalFilename();
// 校验文件类型
String extension = FilenameUtils.getExtension(originalFilename);
if (!"xls".equals(extension) && !"xlsx".equals(extension)) {
throw new RenException("文件类型不匹配");
InputStream is = null;
FileOutputStream os = null;
try {
is = file.getInputStream();
os = new FileOutputStream(fileSavePath.toString());
IOUtils.copy(is, os);
} catch (Exception e) {
log.error("method exception", e);
} finally {
org.apache.poi.util.IOUtils.closeQuietly(is);
org.apache.poi.util.IOUtils.closeQuietly(os);
}
//1.查询当前工作人员是否有再导入的党员先锋数据,有则不允许导入,没有则进行新的导入
// 2.生成导入任务记录
ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO();
importTaskForm.setOriginFileName(file.getOriginalFilename());
importTaskForm.setOperatorId(tokenDto.getUserId());
importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_UNIT);
Result<ImportTaskCommonResultDTO> result = commonServiceOpenFeignClient.createImportTask(importTaskForm);
if (!result.success()) {
throw new RenException(result.getInternalMsg());
}
importTaskForm.setOriginFileName(originalFilename);
// 异步执行导入
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
log.error("method exception", e);
}
submitResiImportTask(tokenDto, response, file, result.getData().getTaskId());
});
ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException(commonServiceOpenFeignClient.createImportTask(importTaskForm),
ServiceConstant.EPMET_COMMON_SERVICE,
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
"excel行程上报导入错误",
"行程上报导入失败");
// 3.执行导入
icPartyUnitService.execAsyncExcelImport(fileSavePath, rstData.getTaskId(),tokenDto.getCustomerId(),tokenDto.getUserId());
return new Result();
}
@ -297,27 +309,6 @@ public class IcPartyUnitController {
return new Result();
}
private void submitResiImportTask(TokenDto tokenDto, HttpServletResponse response, MultipartFile file, String importTaskId) {
try {
icPartyUnitService.importData(tokenDto, response, file, importTaskId);
} catch (Throwable e) {
String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e);
log.error("【导入联建单位信息失败】导入失败:{}", errorMsg);
ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO();
importTaskForm.setOperatorId(tokenDto.getUserId());
importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_UNIT);
importTaskForm.setTaskId(importTaskId);
importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL);
importTaskForm.setResultDesc("联建单位信息导入失败,请查看系统日志");
Result result = commonServiceOpenFeignClient.finishImportTask(importTaskForm);
if (!result.success()) {
throw new RenException(result.getInternalMsg());
}
}
}
/**
* Desc: 获取联建单位名字
* @param formDTO

80
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/IcPartyUnitImportExcel.java

@ -17,9 +17,14 @@
package com.epmet.excel;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.epmet.commons.tools.utils.ExcelVerifyInfo;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* 联建单位
@ -30,34 +35,73 @@ import lombok.Data;
@Data
public class IcPartyUnitImportExcel extends ExcelVerifyInfo {
@Excel(name = "单位名称*")
@NotBlank(message = "单位名称必填")
@Length(max = 50,message = "单位名称最多输入50字")
@ExcelProperty(value = "单位名称*")
private String unitName;
@Excel(name = "分类*")
private String type;
// @Excel(name = "服务事项")
// private String serviceMatter;
@NotBlank(message = "分类必填")
@ExcelProperty(value = "分类*")
private String typeName;
@Excel(name = "联系人*")
@Length(max = 30,message = "联系人最多输入30字")
@NotBlank(message = "联系人必填")
@ExcelProperty(value = "联系人*")
private String contact;
@Excel(name = "联系电话*")
@Length(max = 30,message = "联系电话最多输入30字")
@NotBlank(message = "联系电话必填")
@ExcelProperty(value = "联系电话*")
private String contactMobile;
@Excel(name = "在职党员*")
@NotNull(message = "在职党员必填")
@ExcelProperty(value = "在职党员*")
private Integer memberCount;
@Excel(name = "备注")
@Length(max = 500,message = "备注最多输入50字")
@ExcelProperty(value = "备注")
private String remark;
@Excel(name = "详细地址*")
@Length(max = 250,message = "详细地址最多输入250字")
@NotBlank(message = "详细地址必填")
@ExcelProperty(value = "详细地址*")
private String address;
//
// @Excel(name = "中心位置经度")
// private String longitude;
//
// @Excel(name = "中心位置纬度")
// private String latitude;
@Data
public static class ErrorRow {
@ColumnWidth(40)
@ExcelProperty("单位名称*")
private String unitName;
@ColumnWidth(20)
@ExcelProperty("分类*")
private String type;
@ColumnWidth(20)
@ExcelProperty("联系人*")
private String contact;
@ColumnWidth(20)
@ExcelProperty("联系电话*")
private String contactMobile;
@ColumnWidth(10)
@ExcelProperty("在职党员*")
private Integer memberCount;
@ColumnWidth(30)
@ExcelProperty("备注")
private String remark;
@ColumnWidth(50)
@ExcelProperty("详细地址*")
private String address;
@ColumnWidth(50)
@ExcelProperty("错误信息")
private String errorInfo;
}
}

132
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/excel/handler/IcPartyUnitExcelImportListener.java

@ -0,0 +1,132 @@
package com.epmet.excel.handler;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.ValidateException;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.entity.IcPartyUnitEntity;
import com.epmet.excel.IcPartyUnitImportExcel;
import com.epmet.service.impl.IcPartyUnitServiceImpl;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @Description
* @Author yzm
* @Date 2023/2/20 15:36
*/
@Slf4j
public class IcPartyUnitExcelImportListener implements ReadListener<IcPartyUnitImportExcel> {
/**
* 最大条数阈值
*/
public static final int MAX_THRESHOLD = 200;
/**
* 当前操作用户
*/
private CustomerStaffInfoCacheResult staffInfo;
private String customerId;
/**
* 数据
*/
private List<IcPartyUnitEntity> datas = new ArrayList<>();
/**
* 错误项列表
*/
private List<IcPartyUnitImportExcel.ErrorRow> errorRows = new ArrayList<>();
private IcPartyUnitServiceImpl icPartyUnitService;
//字典表数据
private Map<String, String> partyUnitTypeMap;
public IcPartyUnitExcelImportListener(String customerId, CustomerStaffInfoCacheResult staffInfo, IcPartyUnitServiceImpl icPartyUnitService,Map<String, String> partyUnitTypeMap) {
this.customerId=customerId;
this.staffInfo = staffInfo;
this.icPartyUnitService = icPartyUnitService;
this.partyUnitTypeMap=partyUnitTypeMap;
}
@Override
public void invoke(IcPartyUnitImportExcel data, AnalysisContext context) {
try {
// 先校验数据
ValidatorUtils.validateEntity(data);
AtomicBoolean bl = new AtomicBoolean(false);
StringBuffer errMsg = new StringBuffer("");
//先对一下字段值填写是否正确做判断
if (!partyUnitTypeMap.containsKey(data.getTypeName())) {
errMsg.append("‘返回方式’值填写错误;");
bl.set(true);
}
//错误数据记录到错误文件
if (bl.get()) {
IcPartyUnitImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyUnitImportExcel.ErrorRow.class);
errorRow.setErrorInfo(errMsg.toString());
errorRows.add(errorRow);
return;
}
IcPartyUnitEntity e = ConvertUtils.sourceToTarget(data, IcPartyUnitEntity.class);
if (partyUnitTypeMap.containsKey(data.getTypeName())) {
e.setType(partyUnitTypeMap.get(data.getTypeName()));
}
e.setCustomerId(customerId);
e.setAgencyId(staffInfo.getAgencyId());
e.setPids(staffInfo.getAgencyPIds());
datas.add(e);
if (datas.size() == MAX_THRESHOLD) {
execPersist();
}
} catch (Exception e) {
String errorMsg = null;
if (e instanceof ValidateException) {
errorMsg = ((ValidateException) e).getMsg();
} else {
errorMsg = "未知错误";
log.error("【联建单位导入】出错:{}", ExceptionUtils.getErrorStackTrace(e));
}
IcPartyUnitImportExcel.ErrorRow errorRow = ConvertUtils.sourceToTarget(data,IcPartyUnitImportExcel.ErrorRow.class);
errorRow.setErrorInfo(errorMsg);
errorRows.add(errorRow);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
// 最后几条达不到阈值,这里必须再调用一次
execPersist();
}
/**
* 执行持久化
*/
private void execPersist() {
try {
if (datas != null && datas.size() > 0) {
icPartyUnitService.batchPersist(datas);
}
} finally {
datas.clear();
}
}
/**
* 获取错误行
* @return
*/
public List<IcPartyUnitImportExcel.ErrorRow> getErrorRows() {
return errorRows;
}
}

22
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/IcPartyUnitService.java

@ -14,10 +14,8 @@ import com.epmet.dto.result.PartyUnitDistributionResultDTO;
import com.epmet.dto.result.PartyUnitListResultDTO;
import com.epmet.dto.result.demand.OptionDTO;
import com.epmet.entity.IcPartyUnitEntity;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
/**
@ -107,18 +105,6 @@ public interface IcPartyUnitService extends BaseService<IcPartyUnitEntity> {
*/
List<OptionDTO> options(IcPartyUnitDTO dto);
/**
* 导入数据
*
* @Param tokenDto
* @Param response
* @Param file
* @Return
* @Author zhaoqifeng
* @Date 2021/11/29 11:01
*/
void importData(TokenDto tokenDto, HttpServletResponse response, MultipartFile file, String taskId) throws IOException;
/**
* @Description 按类型统计单位数量
* @Param formDTO
@ -187,4 +173,10 @@ public interface IcPartyUnitService extends BaseService<IcPartyUnitEntity> {
* @return
*/
List<String> getUnitNames(List<String> unitIds);
/**
* 执行Excel导入
* @param filePath
*/
void execAsyncExcelImport(Path filePath, String importTaskId,String customerId,String userId);
}

333
epmet-module/epmet-heart/epmet-heart-server/src/main/java/com/epmet/service/impl/IcPartyUnitServiceImpl.java

@ -17,12 +17,11 @@
package com.epmet.service.impl;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.epmet.commons.mybatis.service.impl.BaseServiceImpl;
import com.epmet.commons.rocketmq.messages.ServerSatisfactionCalFormDTO;
import com.epmet.commons.tools.constant.FieldConstant;
@ -33,11 +32,9 @@ import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult;
import com.epmet.commons.tools.dto.result.DictListResultDTO;
import com.epmet.commons.tools.dto.result.OptionDataResultDTO;
import com.epmet.commons.tools.enums.DictTypeEnum;
import com.epmet.commons.tools.enums.PartyUnitTypeEnum;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.page.PageData;
import com.epmet.commons.tools.redis.common.CustomerOrgRedis;
import com.epmet.commons.tools.redis.common.CustomerStaffRedis;
@ -45,7 +42,7 @@ import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache;
import com.epmet.commons.tools.security.dto.TokenDto;
import com.epmet.commons.tools.security.user.LoginUserUtil;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.ExcelPoiUtils;
import com.epmet.commons.tools.utils.FileUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.constant.UserDemandConstant;
import com.epmet.constants.ImportTaskConstants;
@ -59,7 +56,7 @@ import com.epmet.dto.result.demand.ServiceStatDTO;
import com.epmet.entity.IcCommunitySelfOrganizationEntity;
import com.epmet.entity.IcPartyUnitEntity;
import com.epmet.excel.IcPartyUnitImportExcel;
import com.epmet.excel.IcPartyUnitImportFailedExcel;
import com.epmet.excel.handler.IcPartyUnitExcelImportListener;
import com.epmet.feign.*;
import com.epmet.service.IcCommunitySelfOrganizationService;
import com.epmet.service.IcPartyUnitService;
@ -75,18 +72,17 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
@ -380,214 +376,6 @@ public class IcPartyUnitServiceImpl extends BaseServiceImpl<IcPartyUnitDao, IcPa
}).collect(Collectors.toList());
}
/**
* 导入数据
*
* @param tokenDto
* @param response
* @param file
* @Param tokenDto
* @Param response
* @Param file
* @Return
* @Author zhaoqifeng
* @Date 2021/11/29 11:01
*/
@Override
public void importData(TokenDto tokenDto, HttpServletResponse response, MultipartFile file, String taskId) throws IOException {
List<IcPartyUnitImportFailedExcel> fileList = new ArrayList<>();
ExcelImportResult<IcPartyUnitImportExcel> importResult = ExcelPoiUtils.importExcelMore(file, 0, 1, IcPartyUnitImportExcel.class);
List<IcPartyUnitImportExcel> failList = importResult.getFailList();
//存放错误数据行号
if (!org.springframework.util.CollectionUtils.isEmpty(failList)) {
for (IcPartyUnitImportExcel entity : failList) {
//打印失败的行 和失败的信息
log.warn("第{}行,{}", entity.getRowNum(), entity.getErrorMsg());
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(entity, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo(entity.getErrorMsg());
fileList.add(failed);
}
}
List<IcPartyUnitImportExcel> result = importResult.getList();
CustomerStaffInfoCacheResult staffInfoCache = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId());
if (null == staffInfoCache) {
throw new EpmetException(8000, "获取用户缓存失败");
}
List<OptionDTO> serviceItemList = icServiceItemDictService.queryDictList(tokenDto.getCustomerId());
Map<String, String> categoryMap = serviceItemList.stream().collect(Collectors.toMap(OptionDTO::getValue, OptionDTO::getLabel));
//1.数据校验
Iterator<IcPartyUnitImportExcel> iterator = result.iterator();
while (iterator.hasNext()) {
IcPartyUnitImportExcel obj = iterator.next();
//单位名称不能为空,不可重复
if (StringUtils.isBlank(obj.getUnitName())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("单位名称为空");
fileList.add(failed);
log.warn(String.format("单位名称为空,行号->%s", obj.getRowNum()));
iterator.remove();
} else {
LambdaQueryWrapper<IcPartyUnitEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(IcPartyUnitEntity::getAgencyId, staffInfoCache.getAgencyId());
wrapper.eq(IcPartyUnitEntity::getUnitName, obj.getUnitName());
List<IcPartyUnitEntity> list = baseDao.selectList(wrapper);
if (CollectionUtils.isNotEmpty(list)) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("单位名称已存在");
fileList.add(failed);
log.warn(String.format("单位名称已存在,行号->%s", obj.getRowNum()));
iterator.remove();
}
}
//分类校验
if (StringUtils.isBlank(obj.getType()) || null == PartyUnitTypeEnum.getCode(obj.getType())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("分类名不存在");
fileList.add(failed);
log.warn(String.format("分类名不存在,行号->%s", obj.getRowNum()));
iterator.remove();
}
//服务事项校验
/*if (StringUtils.isNotBlank(obj.getServiceMatter())) {
List<String> matters = Arrays.asList(obj.getServiceMatter().split(StrConstant.COLON));
matters.forEach(item -> {
if (null == categoryMap.get(item)) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("服务事项不存在");
fileList.add(failed);
log.warn(String.format("服务事项不存在,行号->%s", obj.getRowNum()));
iterator.remove();
}
});
}*/
//联系人 联系电话 在职党员 地址 中心位置经度 中心位置纬度 校验
if (StringUtils.isBlank(obj.getContact())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("联系人为空");
fileList.add(failed);
log.warn(String.format("联系人为空,行号->%s", obj.getRowNum()));
iterator.remove();
} else if (StringUtils.isBlank(obj.getContactMobile())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("联系电话为空");
fileList.add(failed);
log.warn(String.format("联系电话为空,行号->%s", obj.getRowNum()));
} else if (StringUtils.isBlank(obj.getAddress())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("地址为空");
fileList.add(failed);
log.warn(String.format("地址为空,行号->%s", obj.getRowNum()));
} else if (null == obj.getMemberCount()) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("在职党员为空");
fileList.add(failed);
log.warn(String.format("在职党员为空,行号->%s", obj.getRowNum()));
} /*else if (StringUtils.isBlank(obj.getLatitude())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("中心位置纬度为空");
fileList.add(failed);
log.warn(String.format("中心位置纬度为空,行号->%s", obj.getRowNum()));
} else if (StringUtils.isBlank(obj.getLongitude())) {
IcPartyUnitImportFailedExcel failed = ConvertUtils.sourceToTarget(obj, IcPartyUnitImportFailedExcel.class);
failed.setErrorInfo("中心位置经度为空");
fileList.add(failed);
log.warn(String.format("中心位置经度为空,行号->%s", obj.getRowNum()));
}*/
}
if (CollectionUtils.isNotEmpty(result)) {
List<IcPartyUnitEntity> list = result.stream().map(item -> {
IcPartyUnitEntity entity = new IcPartyUnitEntity();
entity.setCustomerId(tokenDto.getCustomerId());
entity.setAgencyId(staffInfoCache.getAgencyId());
entity.setPids(staffInfoCache.getAgencyPIds());
entity.setUnitName(item.getUnitName());
entity.setType(PartyUnitTypeEnum.getCode(item.getType()));
// if (StringUtils.isNotBlank(item.getServiceMatter())) {
// entity.setServiceMatter(getServiceMatter(categoryMap, item.getServiceMatter()));
// }
entity.setContact(item.getContact());
entity.setContactMobile(item.getContactMobile());
entity.setAddress(item.getAddress());
// entity.setLatitude(item.getLatitude());
// entity.setLongitude(item.getLongitude());
entity.setMemberCount(item.getMemberCount());
entity.setRemark(item.getRemark());
return entity;
}).collect(Collectors.toList());
insertBatch(list);
}
String str = String.format("共%s条,成功导入%s条。", fileList.size() + result.size(), fileList.size() + result.size() - fileList.size());
if (fileList.size() > NumConstant.ZERO) {
List<Integer> numList = fileList.stream().map(IcPartyUnitImportFailedExcel::getRowNum).sorted().collect(Collectors.toList());
String subList = numList.stream().map(String::valueOf).collect(Collectors.joining("、"));
log.warn(str + "第" + subList + "行未成功!");
}
//错误数据生成文件,修改导入任务状态
erroeImport(fileList, taskId, tokenDto.getUserId());
}
private void erroeImport(List<IcPartyUnitImportFailedExcel> fileList, String importTaskId, String staffId) throws IOException {
String url = "";
//1.有错误数据则生成错误数据存放文件传到阿里云服务
if (CollectionUtils.isNotEmpty(fileList)) {
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的数据列表", "导入失败的数据列表"),
IcPartyUnitImportFailedExcel.class, fileList);
// 文件名
String resultDescFileName = UUID.randomUUID().toString().concat(".xlsx");
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, resultDescFileName);
OutputStream os = fileItem.getOutputStream();
Result<UploadImgResultDTO> uploadResult = null;
try {
workbook.write(os);
uploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem));
} catch (Exception e) {
String errormsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【联建单位信息导入】上传错误描述文件:{}", errormsg);
} finally {
try {
os.close();
} catch (IOException e) {
String errormsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【联建单位信息导入】上传错误描述文件关闭输出流:{}", errormsg);
}
try {
fileItem.delete();
} catch (Exception e) {
String errormsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【联建单位信息导入】上传错误描述文件删除临时文件:{}", errormsg);
}
}
if (uploadResult == null || !uploadResult.success()) {
log.error("【联建单位信息导入】调用OSS上传结果描述文件失败");
} else {
url = uploadResult.getData().getUrl();
}
}
//2.更新导入任务数据
ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO();
importTaskForm.setOperatorId(staffId);
importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_UNIT);
importTaskForm.setTaskId(importTaskId);
importTaskForm.setResultDescFilePath(url);
importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS);
if (CollectionUtils.isNotEmpty(fileList)) {
importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL);
importTaskForm.setResultDesc("联建单位导入存在错误数据");
}
Result result = commonServiceOpenFeignClient.finishImportTask(importTaskForm);
if (!result.success()) {
throw new RenException(result.getInternalMsg());
}
}
/**
* @param formDTO
* @Description 按类型统计单位数量
@ -798,4 +586,113 @@ public class IcPartyUnitServiceImpl extends BaseServiceImpl<IcPartyUnitDao, IcPa
}
return baseDao.getUnitNames(unitIds);
}
/**
* 执行Excel导入
*
* @param filePath
* @param importTaskId
* @param customerId
* @param userId
*/
@Override
public void execAsyncExcelImport(Path filePath, String importTaskId, String customerId, String userId) {
try {
//获取当前登录用户所属组织id
CustomerStaffInfoCacheResult staffInfo= queryCurrentStaff(customerId,userId);
//交通方式
Result<Map<String, String>> partyUnitTypeMap = epmetAdminOpenFeignClient.dictMap(DictTypeEnum.PARTY_UNIT_TYPE.getCode());
Map<String, String> tMap = partyUnitTypeMap.getData().entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue(), entry -> entry.getKey()));
IcPartyUnitExcelImportListener listener = new IcPartyUnitExcelImportListener(customerId,staffInfo, this,tMap);
EasyExcel.read(filePath.toFile(), IcPartyUnitImportExcel.class, listener).headRowNumber(1).sheet(0).doRead();
Path errorDescFile = null;
String errorDesFileUrl = null;
List<IcPartyUnitImportExcel.ErrorRow> errorRows = listener.getErrorRows();
boolean failed = errorRows.size() > 0;
if (failed) {
// 生成并上传错误文件
try {
// 文件生成
Path errorDescDir = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_party_unit", "import", "error_des");
String fileName = UUID.randomUUID().toString().concat(".xlsx");
errorDescFile = errorDescDir.resolve(fileName);
FileItemFactory factory = new DiskFileItemFactory(16, errorDescDir.toFile());
FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, fileName);
OutputStream os = fileItem.getOutputStream();
EasyExcel.write(os, IcPartyUnitImportExcel.ErrorRow.class).sheet("导入失败列表").doWrite(errorRows);
// 文件上传oss
Result<UploadImgResultDTO> errorDesFileUploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem));
if (errorDesFileUploadResult.success()) {
errorDesFileUrl = errorDesFileUploadResult.getData().getUrl();
}
} finally {
if (Files.exists(errorDescFile)) {
Files.delete(errorDescFile);
}
}
}
ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO();
importFinishTaskForm.setTaskId(importTaskId);
importFinishTaskForm.setProcessStatus(failed ? ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL : ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS);
importFinishTaskForm.setOperatorId(userId);
importFinishTaskForm.setResultDesc("");
importFinishTaskForm.setResultDescFilePath(errorDesFileUrl);
Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm);
if (!result.success()) {
log.error("【联建单位导入】finishImportTask失败");
}
} catch (Exception e) {
String errorMsg = ExceptionUtils.getErrorStackTrace(e);
log.error("【联建单位导入】出错:{}", errorMsg);
ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO();
importFinishTaskForm.setTaskId(importTaskId);
importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL);
importFinishTaskForm.setOperatorId(userId);
importFinishTaskForm.setResultDesc("导入失败");
Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm);
if (!result.success()) {
log.error("【联建单位导入】导入记录状态修改为'完成'失败");
}
} finally {
// 删除临时文件
if (Files.exists(filePath)) {
try {
Files.delete(filePath);
} catch (IOException e) {
log.error("method exception", e);
}
}
}
}
private CustomerStaffInfoCacheResult queryCurrentStaff(String customerId, String userId) {
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(customerId, userId);
if (null == staffInfo) {
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "查询工作人员缓存信息异常", EpmetErrorCode.SERVER_ERROR.getMsg());
}
return staffInfo;
}
/**
* 批量持久化
* @param entities
*/
public void batchPersist(List<IcPartyUnitEntity> entities) {
entities.forEach(e -> {
String id = IdWorker.getIdStr(e);
e.setId(id);
baseDao.insert(e);
});
}
}

Loading…
Cancel
Save