|
|
@ -1,24 +1,58 @@ |
|
|
|
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.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.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.constant.FieldConstant; |
|
|
|
import com.epmet.commons.tools.utils.ExcelPoiUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.constants.ImportTaskConstants; |
|
|
|
import com.epmet.dao.IcPointNucleicMonitoringDao; |
|
|
|
import com.epmet.dto.IcPointNucleicMonitoringDTO; |
|
|
|
import com.epmet.dto.form.ImportTaskCommonFormDTO; |
|
|
|
import com.epmet.dto.result.CommunityInfoResultDTO; |
|
|
|
import com.epmet.dto.result.UploadImgResultDTO; |
|
|
|
import com.epmet.entity.IcEpidemicSpecialAttentionEntity; |
|
|
|
import com.epmet.entity.IcPointNucleicMonitoringEntity; |
|
|
|
import com.epmet.excel.IcPointNucleicMonitoringImportExcel; |
|
|
|
import com.epmet.excel.error.EpidemicSpecialAttentionErrorModel; |
|
|
|
import com.epmet.excel.error.PointNucleicMonitoringErrorModel; |
|
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|
|
|
import com.epmet.feign.GovOrgFeignClient; |
|
|
|
import com.epmet.feign.OssFeignClient; |
|
|
|
import com.epmet.redis.IcPointNucleicMonitoringRedis; |
|
|
|
import com.epmet.service.IcPointNucleicMonitoringService; |
|
|
|
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.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* 核酸监测点 |
|
|
@ -27,11 +61,21 @@ import java.util.Map; |
|
|
|
* @since v1.0.0 2022-06-20 |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class IcPointNucleicMonitoringServiceImpl extends BaseServiceImpl<IcPointNucleicMonitoringDao, IcPointNucleicMonitoringEntity> implements IcPointNucleicMonitoringService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IcPointNucleicMonitoringRedis icPointNucleicMonitoringRedis; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private OssFeignClient ossFeignClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private GovOrgFeignClient govOrgFeignClient; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageData<IcPointNucleicMonitoringDTO> page(Map<String, Object> params) { |
|
|
|
IPage<IcPointNucleicMonitoringEntity> page = baseDao.selectPage( |
|
|
@ -84,4 +128,196 @@ public class IcPointNucleicMonitoringServiceImpl extends BaseServiceImpl<IcPoint |
|
|
|
baseDao.deleteBatchIds(Arrays.asList(ids)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 【核酸检测点】导入 |
|
|
|
* @param tokenDto |
|
|
|
* @param inputStream |
|
|
|
* @param taskId |
|
|
|
* @author wgf |
|
|
|
* @date 2022/3/29 15:35 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Async |
|
|
|
public void importFile(TokenDto tokenDto, InputStream inputStream, String taskId) { |
|
|
|
List<PointNucleicMonitoringErrorModel> errorInfo = new ArrayList<>(); |
|
|
|
|
|
|
|
try { |
|
|
|
List<IcPointNucleicMonitoringImportExcel> list = ExcelPoiUtils.importExcel(inputStream, 0,1,IcPointNucleicMonitoringImportExcel.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()); |
|
|
|
} |
|
|
|
// 获取所有地点名称
|
|
|
|
List<String> addressList = baseDao.getAllAddressName(); |
|
|
|
for (int i = 0; i < list.size(); i++) { |
|
|
|
list.get(i).setNum(i+1); |
|
|
|
if (StringUtils.isBlank(list.get(i).getOrgName()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "所属组织名称不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(list.get(i).getName()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "核酸监测点名称不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(list.get(i).getServeTime()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "服务时间不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(list.get(i).getMobile()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "咨询电话不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (StringUtils.isBlank(list.get(i).getAddress()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "监测点地址不能为空",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 校验数据库是否存在该地点名称
|
|
|
|
if (addressList.contains(list.get(i).getName()) && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "核酸监测点名称已存在",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 校验所属组织通过名称能否匹配到ID
|
|
|
|
Result<CommunityInfoResultDTO> resultDTOResult = govOrgFeignClient.getCommunityInfo(list.get(i).getOrgName()); |
|
|
|
CommunityInfoResultDTO communityInfoResultDTO = resultDTOResult.getData(); |
|
|
|
if(communityInfoResultDTO == null && !list.get(i).getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(list.get(i), "所属组织匹配失败",i+1)); |
|
|
|
list.get(i).setAddStatus(true); |
|
|
|
continue; |
|
|
|
}else{ |
|
|
|
list.get(i).setOrgId(communityInfoResultDTO.getDeptId()); |
|
|
|
list.get(i).setPid(communityInfoResultDTO.getPid()); |
|
|
|
list.get(i).setPids(communityInfoResultDTO.getPids()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (list.size() > errorInfo.size()){ |
|
|
|
Map<String, Long> groupByName = list.stream().collect(Collectors.groupingBy(IcPointNucleicMonitoringImportExcel::getName, Collectors.counting())); |
|
|
|
groupByName.forEach((name,count) -> { |
|
|
|
if (Integer.valueOf(count.toString()).compareTo(1) != 0){ |
|
|
|
for (IcPointNucleicMonitoringImportExcel i : list) { |
|
|
|
if (name.equals(i.getName()) && !i.getAddStatus()){ |
|
|
|
errorInfo.add(getErrorInfo(i,"数据重复",i.getNum())); |
|
|
|
i.setAddStatus(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
Map<Boolean, List<IcPointNucleicMonitoringImportExcel>> groupByStatus = list.stream().collect(Collectors.groupingBy(IcPointNucleicMonitoringImportExcel::getAddStatus)); |
|
|
|
List<IcPointNucleicMonitoringImportExcel> needInsert = groupByStatus.get(false); |
|
|
|
if (CollectionUtils.isNotEmpty(needInsert)){ |
|
|
|
List<IcPointNucleicMonitoringEntity> entities = ConvertUtils.sourceToTarget(needInsert, IcPointNucleicMonitoringEntity.class); |
|
|
|
entities.forEach(e -> { |
|
|
|
// 设置客户ID
|
|
|
|
e.setCustomerId(tokenDto.getCustomerId()); |
|
|
|
}); |
|
|
|
insertBatch(entities); |
|
|
|
} |
|
|
|
if (CollectionUtils.isNotEmpty(errorInfo)){ |
|
|
|
String url = importOssUpload(errorInfo, EpidemicSpecialAttentionErrorModel.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/3/30 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/3/29 17:17 |
|
|
|
*/ |
|
|
|
public PointNucleicMonitoringErrorModel getErrorInfo(IcPointNucleicMonitoringImportExcel dto, String info, Integer num){ |
|
|
|
PointNucleicMonitoringErrorModel result = ConvertUtils.sourceToTarget(dto, PointNucleicMonitoringErrorModel.class); |
|
|
|
result.setErrorMsg(info); |
|
|
|
result.setNum(num); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Desc: 文件上传并返回url |
|
|
|
* @param errorRows |
|
|
|
* @param tClass |
|
|
|
* @author zxc |
|
|
|
* @date 2022/3/30 09:16 |
|
|
|
*/ |
|
|
|
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(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |