@ -1,11 +1,16 @@
package com.epmet.service.impl ;
package com.epmet.service.impl ;
import com.epmet.commons.tools.constant.EpmetRoleKeyConstant ;
import com.epmet.commons.tools.constant.EpmetRoleKeyConstant ;
import com.epmet.commons.tools.constant.ServiceConstant ;
import com.epmet.commons.tools.exception.EpmetErrorCode ;
import com.epmet.commons.tools.feign.ResultDataResolver ;
import com.epmet.commons.tools.utils.DateUtils ;
import com.epmet.commons.tools.utils.DateUtils ;
import com.epmet.dto.CustomerGridDTO ;
import com.epmet.dto.form.CustomerGridFormDTO ;
import com.epmet.dto.heart.result.DemandServiceCountResultDTO ;
import com.epmet.dto.heart.result.DemandServiceCountResultDTO ;
import com.epmet.entity.crm.CustomerEntity ;
import com.epmet.entity.heart.VolunteerInfoEntity ;
import com.epmet.entity.heart.VolunteerInfoEntity ;
import com.epmet.entity.stats.FactVolunteerServiceDailyEntity ;
import com.epmet.entity.stats.FactVolunteerServiceDailyEntity ;
import com.epmet.feign.GovOrgOpenFeignClient ;
import com.epmet.service.DemandService ;
import com.epmet.service.DemandService ;
import com.epmet.service.crm.CustomerService ;
import com.epmet.service.crm.CustomerService ;
import com.epmet.service.heart.HeartDemandService ;
import com.epmet.service.heart.HeartDemandService ;
@ -27,7 +32,7 @@ import java.util.stream.Collectors;
* @Version 1 . 0
* @Version 1 . 0
* /
* /
@Service
@Service
public class DemandServiceImpl implements DemandService {
public class DemandServiceImpl implements DemandService , ResultDataResolver {
@Autowired
@Autowired
private HeartDemandService heartDemandService ;
private HeartDemandService heartDemandService ;
@ -37,12 +42,15 @@ public class DemandServiceImpl implements DemandService {
@Autowired
@Autowired
private UserService userService ;
private UserService userService ;
@Autowired
@Autowired
private DemandStatsService demandStatsService ;
private DemandStatsService demandStatsService ;
@Autowired
@Autowired
private CustomerService customerService ;
private CustomerService customerService ;
@Autowired
private GovOrgOpenFeignClient govOrgOpenFeignClient ;
@Override
@Override
public void statsVolunteerDemandServicesDaily ( String customerId ) {
public void statsVolunteerDemandServicesDaily ( String customerId ) {
@ -50,7 +58,7 @@ public class DemandServiceImpl implements DemandService {
Date now = new Date ( ) ;
Date now = new Date ( ) ;
Date today = DateUtils . integrate ( now , "yyyy-MM-dd" ) ;
Date today = DateUtils . integrate ( now , "yyyy-MM-dd" ) ;
Date yestoday = DateUtils . addDateDays ( today , - 1 ) ;
Date yestoday = DateUtils . addDateDays ( today , - 1 ) ;
if ( StringUtils . isNotBlank ( customerId ) ) {
if ( StringUtils . isNotBlank ( customerId ) ) {
// 只计算单个客户
// 只计算单个客户
clearOldDatas ( Arrays . asList ( customerId ) , yestoday ) ;
clearOldDatas ( Arrays . asList ( customerId ) , yestoday ) ;
@ -67,7 +75,8 @@ public class DemandServiceImpl implements DemandService {
/ * *
/ * *
* 清理旧数据
* 清理旧数据
* @param targetDate 要清理哪天的数据
*
* @param targetDate 要清理哪天的数据
* @param customerIds 要清理哪些客户的
* @param customerIds 要清理哪些客户的
* /
* /
private void clearOldDatas ( List < String > customerIds , Date targetDate ) {
private void clearOldDatas ( List < String > customerIds , Date targetDate ) {
@ -80,12 +89,44 @@ public class DemandServiceImpl implements DemandService {
/ * *
/ * *
* 统计单个客户的志愿者服务情况
* 统计单个客户的志愿者服务情况
*
* @param customerId 客户ID
* @param customerId 客户ID
* @param endTime 统计截止时间 ( < endTime )
* @param endTime 统计截止时间 ( < endTime )
* /
* /
private void statsVolunteerDemandServicesDaily4Customer ( String customerId , Date endTime , Date belongTime ) {
private void statsVolunteerDemandServicesDaily4Customer ( String customerId , Date endTime , Date belongTime ) {
// 一.将客户下所有志愿者,按照网格ID,放到map中去
// 网格ID和志愿者列表。key:网格id;value:志愿者列表
HashMap < String , List < VolunteerInfoEntity > > gridIdAndVolunteers = new HashMap < > ( ) ;
heartVolunteerService . listVolunteers ( customerId , endTime ) . forEach ( v - > {
String volunteerGridId = v . getGridId ( ) ;
if ( StringUtils . isNotBlank ( volunteerGridId ) ) {
if ( ! gridIdAndVolunteers . containsKey ( volunteerGridId ) ) {
gridIdAndVolunteers . put ( volunteerGridId , new ArrayList < > ( ) ) ;
}
gridIdAndVolunteers . get ( volunteerGridId ) . add ( v ) ;
}
} ) ;
// 2.按网格分别统计,且持久化
for ( Map . Entry < String , List < VolunteerInfoEntity > > entry : gridIdAndVolunteers . entrySet ( ) ) {
statsVolunteerDemandServicesDaily4Grid ( customerId , entry . getKey ( ) , endTime , entry . getValue ( ) , belongTime ) ;
}
}
/ * *
* 按日统计网格需求服务数据
* @param customerId
* @param gridId
* @param endTime 统计截止时间
* @param volunteers 志愿者volunteerInfo列表
* @param belongTime 该次统计要归属到哪一天 , 即createTime
* @return 统计结果entity , 以备他用
* /
private FactVolunteerServiceDailyEntity statsVolunteerDemandServicesDaily4Grid ( String customerId , String gridId , Date endTime , List < VolunteerInfoEntity > volunteers , Date belongTime ) {
// 1.志愿者分流为党员志愿者&普通居民志愿者
// 1.志愿者分流为党员志愿者&普通居民志愿者
Integer volunteerTotalCount = 0 ;
Integer volunteerTotalCount = volunteers . size ( ) ;
// 党员志愿者数量
// 党员志愿者数量
Integer partymemberVolunteerCount = 0 ;
Integer partymemberVolunteerCount = 0 ;
// 居民志愿者数量
// 居民志愿者数量
@ -94,15 +135,13 @@ public class DemandServiceImpl implements DemandService {
// 党员志愿者用户id列表
// 党员志愿者用户id列表
List < String > partymemberVolunteerUserIds = new ArrayList < > ( 16 ) ;
List < String > partymemberVolunteerUserIds = new ArrayList < > ( 16 ) ;
List < VolunteerInfoEntity > volunteers = heartVolunteerService . listVolunteers ( customerId , endTime ) ;
//--------------------------------【以上是结果数据变量】------------------------------------------
volunteerTotalCount = volunteers . size ( ) ;
// 分片开始下标
// 分片开始下标
int shardingStartIndex = 0 ;
int shardingStartIndex = 0 ;
// 分片大小(条数)
// 分片大小(条数)
int shardingSize = 100 ;
int shardingSize = 100 ;
// 分片去确定党员身份,防止in条件过大
// 分片去确定党员身份,防止in条件过大
while ( true ) {
while ( true ) {
int realShardingSize = Math . min ( shardingSize , volunteerTotalCount - shardingStartIndex ) ;
int realShardingSize = Math . min ( shardingSize , volunteerTotalCount - shardingStartIndex ) ;
@ -141,7 +180,7 @@ public class DemandServiceImpl implements DemandService {
while ( true ) {
while ( true ) {
// 取出每一个服务者的服务次数
// 取出每一个服务者的服务次数
List < DemandServiceCountResultDTO > damendServeTimes = heartDemandService . listDemandServeTimesPage ( customerId , endTime , serviceCountPageNo , serviceCountPageSize ) ;
List < DemandServiceCountResultDTO > damendServeTimes = heartDemandService . listDemandServeTimesPage ( customerId , gridId , endTime , serviceCountPageNo , serviceCountPageSize ) ;
for ( DemandServiceCountResultDTO damendServiceTimes : damendServeTimes ) {
for ( DemandServiceCountResultDTO damendServiceTimes : damendServeTimes ) {
String serverId = damendServiceTimes . getServerId ( ) ;
String serverId = damendServiceTimes . getServerId ( ) ;
@ -158,11 +197,12 @@ public class DemandServiceImpl implements DemandService {
}
}
totalDemandServeTimes = partymemberDemandServeTimes + resiDemandServeTimes ;
totalDemandServeTimes = partymemberDemandServeTimes + resiDemandServeTimes ;
// 3.持久化
// 3.组装entity数据返回,待存储
FactVolunteerServiceDailyEntity insert = new FactVolunteerServiceDailyEntity ( ) ;
FactVolunteerServiceDailyEntity insert = new FactVolunteerServiceDailyEntity ( ) ;
insert . setDateId ( DimIdGenerator . getDateDimId ( belongTime ) ) ;
insert . setDateId ( DimIdGenerator . getDateDimId ( belongTime ) ) ;
insert . setCustomerId ( customerId ) ;
insert . setCustomerId ( customerId ) ;
insert . setGridId ( gridId ) ;
insert . setMonthId ( DimIdGenerator . getMonthDimId ( belongTime ) ) ;
insert . setMonthId ( DimIdGenerator . getMonthDimId ( belongTime ) ) ;
insert . setPartyServiceTotal ( partymemberDemandServeTimes ) ;
insert . setPartyServiceTotal ( partymemberDemandServeTimes ) ;
insert . setServiceTotal ( totalDemandServeTimes ) ;
insert . setServiceTotal ( totalDemandServeTimes ) ;
@ -171,6 +211,19 @@ public class DemandServiceImpl implements DemandService {
insert . setResiTotal ( resiVolunteerCount ) ;
insert . setResiTotal ( resiVolunteerCount ) ;
insert . setVolunteerTotal ( volunteerTotalCount ) ;
insert . setVolunteerTotal ( volunteerTotalCount ) ;
CustomerGridFormDTO gridForm = new CustomerGridFormDTO ( ) ;
gridForm . setGridId ( gridId ) ;
String errorMsg = "【志愿者服务按日统计】查询网格基本信息失败" ;
CustomerGridDTO gridInfo = getResultDataOrThrowsException ( govOrgOpenFeignClient . getGridBaseInfoByGridId ( gridForm ) , ServiceConstant . GOV_ORG_SERVER , EpmetErrorCode . EPMET_COMMON_OPERATION_FAIL . getCode ( ) , errorMsg , errorMsg ) ;
Optional . ofNullable ( gridInfo ) . ifPresent ( gi - > {
insert . setPid ( gi . getPid ( ) ) ;
insert . setPids ( gi . getPids ( ) ) ;
} ) ;
// 持久化
demandStatsService . addVolunteerServiceDaily ( insert ) ;
demandStatsService . addVolunteerServiceDaily ( insert ) ;
return insert ;
}
}
}
}