|
|
@ -1,32 +1,67 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import cn.afterturn.easypoi.excel.ExcelExportUtil; |
|
|
|
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; |
|
|
|
import com.epmet.commons.tools.constant.FieldConstant; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; |
|
|
|
import com.epmet.commons.tools.exception.EpmetException; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
import com.epmet.commons.tools.redis.common.CustomerOrgRedis; |
|
|
|
import com.epmet.commons.tools.redis.common.CustomerStaffRedis; |
|
|
|
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.DateUtils; |
|
|
|
import com.epmet.commons.tools.utils.ExcelPoiUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
|
|
import com.epmet.commons.tools.validator.group.DefaultGroup; |
|
|
|
import com.epmet.commons.tools.validator.group.UpdateGroup; |
|
|
|
import com.epmet.constants.ImportTaskConstants; |
|
|
|
import com.epmet.dao.ChangeWelfareDao; |
|
|
|
import com.epmet.dao.IcResiUserDao; |
|
|
|
import com.epmet.dto.ChangeWelfareDTO; |
|
|
|
import com.epmet.dto.CheckWelfareByIdCardDTO; |
|
|
|
import com.epmet.dto.IcResiUserDTO; |
|
|
|
import com.epmet.dto.form.IcResiUserPageFormDTO; |
|
|
|
import com.epmet.dto.form.ImportTaskCommonFormDTO; |
|
|
|
import com.epmet.dto.form.RentTenantDataFormDTO; |
|
|
|
import com.epmet.dto.result.RentTenantDataResultDTO; |
|
|
|
import com.epmet.dto.result.UploadImgResultDTO; |
|
|
|
import com.epmet.entity.ChangeWelfareEntity; |
|
|
|
import com.epmet.excel.ChangeWelfareImportExcel; |
|
|
|
import com.epmet.excel.error.ChangeWelfareErrorExcel; |
|
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|
|
|
import com.epmet.feign.GovOrgFeignClient; |
|
|
|
import com.epmet.feign.OssFeignClient; |
|
|
|
import com.epmet.redis.ChangeWelfareRedis; |
|
|
|
import com.epmet.service.ChangeWelfareService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.fileupload.FileItem; |
|
|
|
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.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* 福利表 |
|
|
@ -35,6 +70,7 @@ import java.util.*; |
|
|
|
* @since v1.0.0 2022-05-09 |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class ChangeWelfareServiceImpl extends BaseServiceImpl<ChangeWelfareDao, ChangeWelfareEntity> implements ChangeWelfareService { |
|
|
|
|
|
|
|
@Autowired |
|
|
@ -46,6 +82,18 @@ public class ChangeWelfareServiceImpl extends BaseServiceImpl<ChangeWelfareDao, |
|
|
|
@Autowired |
|
|
|
private IcResiUserServiceImpl icResiUserServiceImpl; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private OssFeignClient ossFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private GovOrgFeignClient govOrgFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IcResiUserDao icResiUserDao; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageData<ChangeWelfareDTO> page(Map<String, Object> params) { |
|
|
|
params.put("customerId", loginUserUtil.getLoginUserCustomerId()); |
|
|
@ -163,4 +211,199 @@ public class ChangeWelfareServiceImpl extends BaseServiceImpl<ChangeWelfareDao, |
|
|
|
return new Result(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 【福利人员】导入 |
|
|
|
* @param tokenDto |
|
|
|
* @param inputStream |
|
|
|
* @param taskId |
|
|
|
* @author wgf |
|
|
|
* @date 2022/11/28 15:35 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Async |
|
|
|
public void importFile(TokenDto tokenDto, InputStream inputStream, String taskId) { |
|
|
|
List<ChangeWelfareErrorExcel> errorInfo = new ArrayList<>(); |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
|
|
|
|
try { |
|
|
|
List<ChangeWelfareImportExcel> list = ExcelPoiUtils.importExcel(inputStream, 0,1,ChangeWelfareImportExcel.class); |
|
|
|
if (CollectionUtils.isEmpty(list)){ |
|
|
|
closeTask(taskId,tokenDto.getUserId(), ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL,""); |
|
|
|
return; |
|
|
|
} |
|
|
|
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId()); |
|
|
|
if (null == staffInfo){ |
|
|
|
throw new EpmetException("未查询到工作人员信息"+tokenDto.getUserId()); |
|
|
|
} |
|
|
|
AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(staffInfo.getAgencyId()); |
|
|
|
if (null == agencyInfo){ |
|
|
|
throw new EpmetException("未查询到组织信息"+staffInfo.getAgencyId()); |
|
|
|
} |
|
|
|
// 获取所有居民信息
|
|
|
|
IcResiUserPageFormDTO formDTO = new IcResiUserPageFormDTO(); |
|
|
|
formDTO.setCustomerId(tokenDto.getCustomerId()); |
|
|
|
formDTO.setStaffId(tokenDto.getUserId()); |
|
|
|
formDTO.setListType("resi"); |
|
|
|
formDTO.setConditions(new ArrayList<>()); |
|
|
|
|
|
|
|
|
|
|
|
// 获取居民信息
|
|
|
|
CustomerStaffInfoCacheResult staffInfoCache = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId()); |
|
|
|
List<IcResiUserDTO> userList = icResiUserDao.getImportUserList(tokenDto.getCustomerId(),staffInfoCache.getAgencyId()); |
|
|
|
// 获取证件号List
|
|
|
|
List<String> idCardList = userList.stream().map(IcResiUserDTO::getIdCard).collect(Collectors.toList()); |
|
|
|
|
|
|
|
// 获取已存在福利人员
|
|
|
|
List<String> welfareList = baseDao.getAllWelfareIdCard(tokenDto.getCustomerId()); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
list.get(i).setNum(i+1); |
|
|
|
if (StringUtils.isBlank(list.get(i).getIdCard()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "证件号不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
list.get(i).setIdCard("10110"); // 后面有逻辑用到了证件号,10110代表证件号为空
|
|
|
|
continue; |
|
|
|
} |
|
|
|
// 校验是否为居民
|
|
|
|
if (!idCardList.contains(list.get(i).getIdCard()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "身份证号为【" + list.get(i).getIdCard() + "】的居民信息未找到,请确认该居民信息存在",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
}else if(idCardList.contains(list.get(i).getIdCard()) && !list.get(i).getAddStatus()){ |
|
|
|
// 组装居民数据
|
|
|
|
for(IcResiUserDTO icResiUserDTO : userList){ |
|
|
|
if(list.get(i).getIdCard().equals(icResiUserDTO.getIdCard())){ |
|
|
|
list.get(i).setGender(icResiUserDTO.getGender()); |
|
|
|
list.get(i).setMobile(icResiUserDTO.getMobile()); |
|
|
|
list.get(i).setName(icResiUserDTO.getName()); |
|
|
|
list.get(i).setUserId(icResiUserDTO.getId()); |
|
|
|
list.get(i).setGridId(icResiUserDTO.getGridId()); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
list.get(i).setJoinDate(sdf.format(new Date())); |
|
|
|
} |
|
|
|
// 校验数据库是否存在该福利人员
|
|
|
|
if (welfareList.contains(list.get(i).getIdCard()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "该福利人员已存在",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (list.size() > errorInfo.size()){ |
|
|
|
Map<String, Long> groupByIdCard = list.stream().collect(Collectors.groupingBy(ChangeWelfareImportExcel::getIdCard, Collectors.counting())); |
|
|
|
groupByIdCard.forEach((idCard,count) -> { |
|
|
|
if (!"10110".equals(idCard) && Integer.valueOf(count.toString()).compareTo(1) != 0){ |
|
|
|
for (ChangeWelfareImportExcel i : list) { |
|
|
|
if (idCard.equals(i.getIdCard()) && !i.getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(i,"数据重复",i.getNum())); |
|
|
|
i.setAddStatus(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
Map<Boolean, List<ChangeWelfareImportExcel>> groupByStatus = list.stream().collect(Collectors.groupingBy(ChangeWelfareImportExcel::getAddStatus)); |
|
|
|
List<ChangeWelfareImportExcel> needInsert = groupByStatus.get(false); |
|
|
|
if (CollectionUtils.isNotEmpty(needInsert)){ |
|
|
|
List<ChangeWelfareEntity> entities = ConvertUtils.sourceToTarget(needInsert, ChangeWelfareEntity.class); |
|
|
|
entities.forEach(e -> { |
|
|
|
// 设置客户ID
|
|
|
|
e.setCustomerId(tokenDto.getCustomerId()); |
|
|
|
}); |
|
|
|
insertBatch(entities); |
|
|
|
} |
|
|
|
if (CollectionUtils.isNotEmpty(errorInfo)){ |
|
|
|
String url = importOssUpload(errorInfo, ChangeWelfareErrorExcel.class); |
|
|
|
closeTask(taskId,tokenDto.getUserId(), ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL,url); |
|
|
|
}else { |
|
|
|
closeTask(taskId,tokenDto.getUserId(),ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS,""); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error(e.getMessage()); |
|
|
|
closeTask(taskId,tokenDto.getUserId(),ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL,""); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 关闭任务 |
|
|
|
* @param taskId |
|
|
|
* @param userId |
|
|
|
* @param status |
|
|
|
* @param url |
|
|
|
* @author wgf |
|
|
|
* @date 2022/11/28 09:05 |
|
|
|
*/ |
|
|
|
public void closeTask(String taskId,String userId,String status,String url){ |
|
|
|
ImportTaskCommonFormDTO input = new ImportTaskCommonFormDTO(); |
|
|
|
input.setOperatorId(userId); |
|
|
|
input.setTaskId(taskId); |
|
|
|
input.setProcessStatus(status); |
|
|
|
input.setResultDescFilePath(url); |
|
|
|
commonServiceOpenFeignClient.finishImportTask(input); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 构造错误信息 |
|
|
|
* @param dto |
|
|
|
* @param info |
|
|
|
* @param num |
|
|
|
* @author wgf |
|
|
|
* @date 2022/11/28 17:17 |
|
|
|
*/ |
|
|
|
public ChangeWelfareErrorExcel getErrorInfo(ChangeWelfareImportExcel dto, String info, Integer num){ |
|
|
|
ChangeWelfareErrorExcel result = ConvertUtils.sourceToTarget(dto, ChangeWelfareErrorExcel.class); |
|
|
|
result.setErrorMsg(info); |
|
|
|
result.setNum(num); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 文件上传并返回url |
|
|
|
* @param errorRows |
|
|
|
* @param tClass |
|
|
|
* @author wgf |
|
|
|
* @date 2022/11/28 17:17 |
|
|
|
*/ |
|
|
|
public <T> String importOssUpload(Collection<?> errorRows, Class<T> tClass) throws IOException { |
|
|
|
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的数据列表","导入失败的数据列表"), |
|
|
|
tClass, errorRows); |
|
|
|
|
|
|
|
// 文件名
|
|
|
|
String resultDescFileName = UUID.randomUUID().toString().concat(".xls"); |
|
|
|
|
|
|
|
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上传结果描述文件失败"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
return uploadResult.getData().getUrl(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|