@ -1,36 +1,62 @@
package com.epmet.service.impl ;
import com.alibaba.excel.EasyExcel ;
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.tools.constant.AppClientConstant ;
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.EpmetErrorCode ;
import com.epmet.commons.tools.exception.EpmetException ;
import com.epmet.commons.tools.exception.ExceptionUtils ;
import com.epmet.commons.tools.feign.ResultDataResolver ;
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.redis.common.bean.GridInfoCache ;
import com.epmet.commons.tools.utils.ConvertUtils ;
import com.epmet.commons.tools.utils.* ;
import com.epmet.constant.IcResiUserConstant ;
import com.epmet.constants.ImportTaskConstants ;
import com.epmet.dao.IcTripReportRecordDao ;
import com.epmet.dao.UserBaseInfoDao ;
import com.epmet.dto.IcEpidemicSpecialAttentionDTO ;
import com.epmet.dto.IcTripReportRecordDTO ;
import com.epmet.dto.form.IcTripReportFormDTO ;
import com.epmet.dto.form.MyReportedTripForm DTO ;
import com.epmet.dto.form.PageTripReportForm DTO ;
import com.epmet.dto.form.* ;
import com.epmet.dto.result.TripList DTO ;
import com.epmet.dto.result.UploadImgResult DTO ;
import com.epmet.entity.IcTripReportRecordEntity ;
import com.epmet.excel.data.IcTripReportExcelData ;
import com.epmet.excel.handler.IcTripReportExcelImportListener ;
import com.epmet.feign.EpmetCommonServiceOpenFeignClient ;
import com.epmet.feign.OssFeignClient ;
import com.epmet.service.IcEpidemicSpecialAttentionService ;
import com.epmet.service.IcNoticeService ;
import com.epmet.service.IcTripReportRecordService ;
import com.github.pagehelper.PageHelper ;
import com.github.pagehelper.PageInfo ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.collections4.CollectionUtils ;
import org.apache.commons.collections4.ListUtils ;
import org.apache.commons.collections4.MapUtils ;
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.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.OutputStream ;
import java.nio.file.Files ;
import java.nio.file.Path ;
import java.util.* ;
import java.util.stream.Collectors ;
@ -40,8 +66,9 @@ import java.util.stream.Collectors;
* @author generator generator @elink - cn . com
* @since v1 . 0 . 0 2022 - 03 - 25
* /
@Slf4j
@Service
public class IcTripReportRecordServiceImpl extends BaseServiceImpl < IcTripReportRecordDao , IcTripReportRecordEntity > implements IcTripReportRecordService {
public class IcTripReportRecordServiceImpl extends BaseServiceImpl < IcTripReportRecordDao , IcTripReportRecordEntity > implements IcTripReportRecordService , ResultDataResolver {
@Autowired
private UserBaseInfoDao userBaseInfoDao ;
//关注
@ -50,6 +77,11 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
//通知
@Autowired
private IcNoticeService icNoticeService ;
@Autowired
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient ;
@Autowired
private OssFeignClient ossFeignClient ;
/ * *
* pc : 行程上报 - 列表
* @param formDTO
@ -58,10 +90,7 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
@Override
public PageData < IcTripReportRecordDTO > page ( PageTripReportFormDTO formDTO ) {
//1.获取工作人员缓存信息
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis . getStaffInfo ( formDTO . getCustomerId ( ) , formDTO . getUserId ( ) ) ;
if ( null = = staffInfo ) {
throw new EpmetException ( EpmetErrorCode . EPMET_COMMON_OPERATION_FAIL . getCode ( ) , "查询工作人员缓存信息异常" , EpmetErrorCode . SERVER_ERROR . getMsg ( ) ) ;
}
CustomerStaffInfoCacheResult staffInfo = queryCurrentStaff ( formDTO . getCustomerId ( ) , formDTO . getUserId ( ) ) ;
formDTO . setAgencyId ( staffInfo . getAgencyId ( ) ) ;
//2.按条件查询业务数据
PageInfo < IcTripReportRecordDTO > data = PageHelper . startPage ( formDTO . getPageNo ( ) , formDTO . getPageSize ( ) , formDTO . isPage ( ) )
@ -69,14 +98,24 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
List < IcTripReportRecordDTO > list = data . getList ( ) ;
//3.查询最近一次通知时间、核算检测关注名单
if ( CollectionUtils . isNotEmpty ( list ) ) {
Set < String > idCardSet = list . stream ( ) . map ( m - > m . getIdCard ( ) ) . collect ( Collectors . toSet ( ) ) ;
Map < String , Date > latestNotice = icNoticeService . getUserLatestNoticeTime ( formDTO . getCustomerId ( ) , idCardSet ) ;
List < String > gzIdCardList = epidemicSpecialAttentionService . getHeSuanIdCardList ( formDTO . getCustomerId ( ) , idCardSet ) ;
Map < String , Date > latestNotice = new HashMap < > ( ) ;
List < String > gzIdCardList = new ArrayList < > ( ) ;
List < String > idCardList = list . stream ( ) . map ( m - > m . getIdCard ( ) ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
//如果不分页可能会有很多个身份证号
List < List < String > > partionList = ListUtils . partition ( idCardList , NumConstant . ONE_HUNDRED ) ;
partionList . forEach ( l - > {
Map < String , Date > map = icNoticeService . getUserLatestNoticeTime ( formDTO . getCustomerId ( ) , l ) ;
List < String > gzIdCards = epidemicSpecialAttentionService . getHeSuanIdCardList ( formDTO . getCustomerId ( ) , l ) ;
latestNotice . putAll ( map ) ;
gzIdCardList . addAll ( gzIdCards ) ;
} ) ;
for ( IcTripReportRecordDTO dto : list ) {
//默认未加入
dto . setHeSuanCheck ( false ) ;
dto . setHeSuanCheckDesc ( "未加入" ) ;
if ( CollectionUtils . isNotEmpty ( gzIdCardList ) & & gzIdCardList . contains ( dto . getIdCard ( ) ) ) {
dto . setHeSuanCheck ( true ) ;
dto . setHeSuanCheckDesc ( "已加入" ) ;
}
if ( MapUtils . isNotEmpty ( latestNotice ) & & latestNotice . containsKey ( dto . getIdCard ( ) ) ) {
dto . setLatestNoticeTime ( latestNotice . get ( dto . getIdCard ( ) ) ) ;
@ -86,6 +125,14 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
return new PageData ( list , data . getTotal ( ) ) ;
}
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 ;
}
@Override
public List < IcTripReportRecordDTO > list ( Map < String , Object > params ) {
List < IcTripReportRecordEntity > entityList = baseDao . selectList ( getWrapper ( params ) ) ;
@ -102,31 +149,118 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
return wrapper ;
}
@Override
public IcTripReportRecordDTO get ( String id ) {
IcTripReportRecordEntity entity = baseDao . selectById ( id ) ;
return ConvertUtils . sourceToTarget ( entity , IcTripReportRecordDTO . class ) ;
}
/ * *
* pc : 行程上报 - 新增
* @param formDTO
* /
@Override
@Transactional ( rollbackFor = Exception . class )
public void save ( IcTripReportRecordDTO dto ) {
IcTripReportRecordEntity entity = ConvertUtils . sourceToTarget ( dto , IcTripReportRecordEntity . class ) ;
public String save ( IcTripReportFormDTO formDTO ) {
//如果是单个录入的,默认数据属于当前用户所属组织id
if ( IcResiUserConstant . USER_TYPE_INPUT . equals ( formDTO . getUserType ( ) ) ) {
//获取当前登录用户所属组织id
CustomerStaffInfoCacheResult staffInfo = queryCurrentStaff ( formDTO . getCustomerId ( ) , formDTO . getCurrentStaffId ( ) ) ;
formDTO . setAgencyId ( staffInfo . getAgencyId ( ) ) ;
}
IcTripReportRecordEntity entity = ConvertUtils . sourceToTarget ( formDTO , IcTripReportRecordEntity . class ) ;
AgencyInfoCache agencyInfoCache = CustomerOrgRedis . getAgencyInfo ( entity . getAgencyId ( ) ) ;
if ( null = = agencyInfoCache ) {
throw new EpmetException ( EpmetErrorCode . EPMET_COMMON_OPERATION_FAIL . getCode ( ) , "查询组织信息异常" , EpmetErrorCode . SERVER_ERROR . getMsg ( ) ) ;
}
entity . setPids ( agencyInfoCache . getPids ( ) ) ;
//1.插入行程记录表
insert ( entity ) ;
if ( formDTO . getHeSuanCheck ( ) ) {
// 2.加入核算检测关注名单
vaccinationAdd ( formDTO ) ;
}
//3.新增通知表信息
if ( formDTO . getChannel ( ) . size ( ) > NumConstant . ZERO & & StringUtils . isNotBlank ( formDTO . getContent ( ) ) ) {
sendNotice ( formDTO , agencyInfoCache . getOrganizationName ( ) ) ;
}
return entity . getId ( ) ;
}
/ * *
* 行程上报 - 发送通知
* @param formDTO
* /
private void sendNotice ( IcTripReportFormDTO formDTO , String agencyName ) {
SendNoticeFormDTO dto = new SendNoticeFormDTO ( ) ;
List < SendNoticeFormDTO . UserListBean > userList = new ArrayList < > ( ) ;
//注意这里的userId是指的pc平台的居民
userList . add ( ConvertUtils . sourceToTarget ( formDTO , SendNoticeFormDTO . UserListBean . class ) ) ;
dto . setCustomerId ( formDTO . getCustomerId ( ) ) ;
dto . setUserList ( userList ) ;
dto . setChannel ( formDTO . getChannel ( ) ) ;
//通知来源 0 行程上报,1 疫苗接种,2 核酸检测
dto . setOrigin ( NumConstant . ZERO_STR ) ;
dto . setContent ( formDTO . getContent ( ) ) ;
dto . setOrgName ( agencyName ) ;
dto . setStaffId ( formDTO . getCurrentStaffId ( ) ) ;
icNoticeService . sendNotice ( dto ) ;
}
/ * *
* 行程上报 - 加入核算检测关注
* @param formDTO
* /
private void vaccinationAdd ( IcTripReportFormDTO formDTO ) {
VaccinationAddFormDTO vaccinationAddFormDTO = new VaccinationAddFormDTO ( ) ;
vaccinationAddFormDTO . setCustomerId ( formDTO . getCustomerId ( ) ) ;
vaccinationAddFormDTO . setUserId ( formDTO . getCurrentStaffId ( ) ) ;
List < IcEpidemicSpecialAttentionDTO > list = new ArrayList < > ( ) ;
IcEpidemicSpecialAttentionDTO dto = new IcEpidemicSpecialAttentionDTO ( ) ;
dto . setName ( formDTO . getName ( ) ) ;
dto . setIdCard ( formDTO . getIdCard ( ) ) ;
dto . setMobile ( formDTO . getMobile ( ) ) ;
//避免再发通知,赋值个空集合吧
dto . setChannel ( Collections . emptyList ( ) ) ;
//关注类型,核酸检测:2,疫苗接种:1;行程上报:0
dto . setAttentionType ( NumConstant . TWO ) ;
list . add ( dto ) ;
vaccinationAddFormDTO . setList ( list ) ;
epidemicSpecialAttentionService . vaccinationAdd ( vaccinationAddFormDTO ) ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public void update ( IcTripReportRecordDTO dto ) {
IcTripReportRecordEntity entity = ConvertUtils . sourceToTarget ( dto , IcTripReportRecordEntity . class ) ;
public String update ( IcTripReportFormDTO formDTO ) {
IcTripReportRecordEntity origin = baseDao . selectById ( formDTO . getId ( ) ) ;
if ( null = = origin ) {
log . warn ( "ic_trip_report_record记录不存在id:" , formDTO . getId ( ) ) ;
return formDTO . getId ( ) ;
}
//如果是单个录入的,默认数据属于当前用户所属组织id
if ( IcResiUserConstant . USER_TYPE_INPUT . equals ( formDTO . getUserType ( ) ) ) {
//获取当前登录用户所属组织id
CustomerStaffInfoCacheResult staffInfo = queryCurrentStaff ( formDTO . getCustomerId ( ) , formDTO . getCurrentStaffId ( ) ) ;
formDTO . setAgencyId ( staffInfo . getAgencyId ( ) ) ;
}
IcTripReportRecordEntity entity = ConvertUtils . sourceToTarget ( formDTO , IcTripReportRecordEntity . class ) ;
AgencyInfoCache agencyInfoCache = CustomerOrgRedis . getAgencyInfo ( entity . getAgencyId ( ) ) ;
if ( null = = agencyInfoCache ) {
throw new EpmetException ( EpmetErrorCode . EPMET_COMMON_OPERATION_FAIL . getCode ( ) , "查询组织信息异常" , EpmetErrorCode . SERVER_ERROR . getMsg ( ) ) ;
}
entity . setPids ( agencyInfoCache . getPids ( ) ) ;
//1.修改行程记录表
updateById ( entity ) ;
if ( formDTO . getHeSuanCheck ( ) ) {
// 2.加入核算检测关注名单,如果之前关注了,会在通知的service层移除
vaccinationAdd ( formDTO ) ;
}
//3.新增通知表信息
if ( formDTO . getChannel ( ) . size ( ) > NumConstant . ZERO & & StringUtils . isNotBlank ( formDTO . getContent ( ) ) ) {
sendNotice ( formDTO , agencyInfoCache . getOrganizationName ( ) ) ;
}
return entity . getId ( ) ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public void delete ( String [ ] ids ) {
// 逻辑删除(@TableLogic 注解)
baseDao . deleteBatchIds ( Arrays . asList ( ids ) ) ;
public void delete ( String customerId , String userId , String [ ] ids ) {
CustomerStaffInfoCacheResult staffInfo = queryCurrentStaff ( customerId , userId ) ;
baseDao . batchDel ( userId , staffInfo . getAgencyId ( ) , ids ) ;
}
/ * *
@ -169,5 +303,127 @@ public class IcTripReportRecordServiceImpl extends BaseServiceImpl<IcTripReportR
return result . getList ( ) ;
}
/ * *
* 获取居民行程信息
*
* @param idCard
* @Param idCard
* @Return { @link List < TripListDTO > }
* @Author zhaoqifeng
* @Date 2022 / 3 / 30 10 : 31
* /
@Override
public List < TripListDTO > tripList ( String customerId , String idCard ) {
LambdaQueryWrapper < IcTripReportRecordEntity > wrapper = new LambdaQueryWrapper < > ( ) ;
wrapper . eq ( IcTripReportRecordEntity : : getCustomerId , customerId ) ;
wrapper . eq ( IcTripReportRecordEntity : : getIdCard , idCard ) ;
wrapper . orderByDesc ( IcTripReportRecordEntity : : getArriveDate ) ;
List < IcTripReportRecordEntity > list = baseDao . selectList ( wrapper ) ;
if ( CollectionUtils . isEmpty ( list ) ) {
return Collections . emptyList ( ) ;
}
return list . stream ( ) . map ( item - > {
TripListDTO dto = new TripListDTO ( ) ;
dto . setArrivalTime ( DateUtils . format ( item . getArriveDate ( ) , DateUtils . DATE_PATTERN ) ) ;
dto . setFromRegion ( item . getSourceAddress ( ) ) ;
dto . setLeaveTime ( DateUtils . format ( item . getLeaveDate ( ) , DateUtils . DATE_PATTERN ) ) ;
dto . setRemark ( item . getRemark ( ) ) ;
return dto ;
} ) . collect ( Collectors . toList ( ) ) ;
}
@Async
@Override
public void execAsyncExcelImport ( Path filePath , String importTaskId ) {
String userId = null ;
String customerId = null ;
try {
userId = EpmetRequestHolder . getHeader ( AppClientConstant . USER_ID ) ;
customerId = EpmetRequestHolder . getHeader ( AppClientConstant . CUSTOMER_ID ) ;
//获取当前登录用户所属组织id
CustomerStaffInfoCacheResult staffInfo = queryCurrentStaff ( customerId , userId ) ;
IcTripReportExcelImportListener listener = new IcTripReportExcelImportListener ( customerId , staffInfo , this ) ;
EasyExcel . read ( filePath . toFile ( ) , IcTripReportExcelData . class , listener ) . headRowNumber ( 2 ) . sheet ( 0 ) . doRead ( ) ;
Path errorDescFile = null ;
String errorDesFileUrl = null ;
List < IcTripReportExcelData . ErrorRow > errorRows = listener . getErrorRows ( ) ;
// 生成并上传错误文件
try {
// 文件生成
Path errorDescDir = FileUtils . getAndCreateDirUnderEpmetFilesDir ( "ic_trip_preport" , "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 , IcTripReportExcelData . 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 ( errorRows . size ( ) < = 0 ? ImportTaskConstants . PROCESS_STATUS_FINISHED_SUCCESS : ImportTaskConstants . PROCESS_STATUS_FINISHED_FAIL ) ;
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 ) {
e . printStackTrace ( ) ;
}
}
}
}
/ * *
* 批量持久化
* @param entities
* /
public void batchPersist ( List < IcTripReportRecordEntity > entities ) {
String currentUserId = EpmetRequestHolder . getHeader ( AppClientConstant . USER_ID ) ;
entities . forEach ( e - > {
String id = IdWorker . getIdStr ( e ) ;
e . setId ( id ) ;
e . setUpdatedBy ( currentUserId ) ;
baseDao . insert ( e ) ;
} ) ;
}
}