|
|
@ -1,19 +1,26 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.dao.stats.DimGridDao; |
|
|
|
import com.epmet.dao.stats.FactGroupGridDailyDao; |
|
|
|
import com.epmet.dto.group.result.CustomerGridInfoResultDTO; |
|
|
|
import com.epmet.dto.group.result.GroupGridDailyResultDTO; |
|
|
|
import com.epmet.dto.AgencySubTreeDto; |
|
|
|
import com.epmet.dto.group.form.AgencyMonthlyFormDTO; |
|
|
|
import com.epmet.dto.group.result.*; |
|
|
|
import com.epmet.dto.stats.DimAgencyDTO; |
|
|
|
import com.epmet.entity.stats.DimAgencyEntity; |
|
|
|
import com.epmet.entity.stats.DimGridEntity; |
|
|
|
import com.epmet.service.StatsGroupService; |
|
|
|
import com.epmet.service.group.GroupDataService; |
|
|
|
import com.epmet.service.stats.FactGroupGridDailyService; |
|
|
|
import com.epmet.service.stats.*; |
|
|
|
import com.epmet.util.DimIdGenerator; |
|
|
|
import com.epmet.util.ModuleConstant; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.ZoneId; |
|
|
|
import java.time.ZonedDateTime; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
@ -24,11 +31,17 @@ import java.util.stream.Collectors; |
|
|
|
public class StatsGroupServiceImpl implements StatsGroupService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DimGridDao dimGridDao; |
|
|
|
private DimGridService dimGridService; |
|
|
|
@Autowired |
|
|
|
private DimAgencyService dimAgencyService; |
|
|
|
@Autowired |
|
|
|
private GroupDataService groupDataService; |
|
|
|
@Autowired |
|
|
|
private FactGroupGridDailyService factGroupGridDailyService; |
|
|
|
@Autowired |
|
|
|
private FactGroupAgencyDailyService factGroupAgencyDailyService; |
|
|
|
@Autowired |
|
|
|
private FactGroupAgencyMonthlyService factGroupAgencyMonthlyService; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 统计【网格-日】 |
|
|
@ -36,25 +49,189 @@ public class StatsGroupServiceImpl implements StatsGroupService { |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void groupGridDaily() { |
|
|
|
public void groupGridDaily(String customerId) { |
|
|
|
DimIdGenerator.DimIdBean dimIdBean = this.getDimIdBean(); |
|
|
|
List<DimGridEntity> gridsInfo = dimGridService.getGridListByCustomerId(customerId); |
|
|
|
List<GroupGridDailyResultDTO> resultDTOS = groupDataService.groupGridDaily(customerId,dimIdBean,gridsInfo); |
|
|
|
factGroupGridDailyService.statisticsGroupGridDaily(resultDTOS); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 统计 “小组” 有关数据, dim:【机关-日】 |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void groupAgencyDaily(String customerId) { |
|
|
|
DimIdGenerator.DimIdBean dimIdBean = this.getDimIdBean(); |
|
|
|
List<DimAgencyDTO> customerAgencyInfos = dimAgencyService.getAgencyInfoByCustomerId(customerId); |
|
|
|
List<AgencyGroupDailyResultDTO> agencyGroupDaily = this.getAgencyGroupDaily(customerAgencyInfos, dimIdBean, customerId); |
|
|
|
factGroupAgencyDailyService.insertGroupAgencyDaily(agencyGroupDaily); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 统计 “小组” 有关数据, dim:【机关-月】 |
|
|
|
* @author zxc String customerId |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void groupAgencyMonthly() { |
|
|
|
List<AgencyMonthlyFormDTO> lastDayAgency = factGroupAgencyDailyService.getLastDayAgency(); |
|
|
|
DimIdGenerator.DimIdBean dimIdBean = this.getDimIdBean(); |
|
|
|
List<AgencyGroupMonthlyResultDTO> monthGroupIncr = factGroupAgencyDailyService.getMonthGroupIncr(dimIdBean.getMonthId()); |
|
|
|
if (monthGroupIncr.size() == NumConstant.ZERO){ |
|
|
|
lastDayAgency.forEach(agency -> { |
|
|
|
agency.setGroupIncr(NumConstant.ZERO); |
|
|
|
}); |
|
|
|
}else { |
|
|
|
lastDayAgency.forEach(agency -> { |
|
|
|
monthGroupIncr.forEach(monthIncr -> { |
|
|
|
if (agency.getAgencyId().equals(monthIncr.getAgencyId())){ |
|
|
|
agency.setGroupIncr(monthIncr.getMonthGroupIncr()); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
factGroupAgencyMonthlyService.insertAgencyGroupMonthly(lastDayAgency); |
|
|
|
} |
|
|
|
|
|
|
|
//每个客户下的网格信息
|
|
|
|
List<CustomerGridInfoResultDTO> customerGridInfos = dimGridDao.selectCustomerGrid(); |
|
|
|
if (customerGridInfos.size() == NumConstant.ZERO){ |
|
|
|
return; |
|
|
|
public List<AgencyGroupDailyResultDTO> getAgencyGroupDaily(List<DimAgencyDTO> customerAgencyInfos, DimIdGenerator.DimIdBean timeDim,String customerId){ |
|
|
|
if (customerAgencyInfos.size() == NumConstant.ZERO){ |
|
|
|
return new ArrayList<>(); |
|
|
|
} |
|
|
|
//根据customerId分组
|
|
|
|
Map<String, List<CustomerGridInfoResultDTO>> collect = customerGridInfos.stream().collect(Collectors.groupingBy(gridInfo -> gridInfo.getCustomerId())); |
|
|
|
Set<Map.Entry<String, List<CustomerGridInfoResultDTO>>> entries = collect.entrySet(); |
|
|
|
entries.forEach(customerGridInfo -> { |
|
|
|
String customerId = customerGridInfo.getKey(); |
|
|
|
List<CustomerGridInfoResultDTO> gridsInfo = customerGridInfo.getValue(); |
|
|
|
if (gridsInfo.size() != NumConstant.ZERO){ |
|
|
|
List<GroupGridDailyResultDTO> resultDTOS = groupDataService.groupGridDaily(customerId, gridsInfo); |
|
|
|
//执行插入 fact_group_grid_daily
|
|
|
|
factGroupGridDailyService.statisticsGroupGridDaily(resultDTOS); |
|
|
|
List<AgencyGroupDailyResultDTO> result = new ArrayList<>(); |
|
|
|
List<DimAgencyEntity> agencyList = dimAgencyService.getAgencyListByCustomerId(customerId); |
|
|
|
agencyList.forEach(agency -> { |
|
|
|
AgencyGroupDailyResultDTO agencyResult = new AgencyGroupDailyResultDTO(); |
|
|
|
String agencyId = agency.getId(); |
|
|
|
agencyResult.setAgencyId(agencyId); |
|
|
|
BeanUtils.copyProperties(timeDim,agencyResult); |
|
|
|
//机关下的所有网格(包括直属网格)
|
|
|
|
List<String> allGrid = this.getAllGrid(agencyId); |
|
|
|
String pidByAgencyId = dimAgencyService.getPidByAgencyId(agencyId); |
|
|
|
agencyResult.setPid(StringUtils.isBlank(pidByAgencyId)?"0":pidByAgencyId); |
|
|
|
// TODO 1. 机关下有多少网格
|
|
|
|
agencyResult.setGridTotal(allGrid.size()); |
|
|
|
|
|
|
|
// TODO 2. 机关下有多少小组,只算 state = ‘approved’
|
|
|
|
List<AgencyGroupTotalCountResultDTO> agencyGroupTotalCount = groupDataService.getAgencyGroupTotalCount(allGrid); |
|
|
|
Integer groupCount = agencyGroupTotalCount.stream().collect(Collectors.summingInt(AgencyGroupTotalCountResultDTO::getGridGroupCount)); |
|
|
|
agencyResult.setGroupTotalCount(groupCount); |
|
|
|
// TODO 3. 机关下所有组内人数和(不需要去重) 人员状态 != "removed"
|
|
|
|
List<AgencyGridGroupPeopleTotalResultDTO> agencyGridGroupPeopleTotal = groupDataService.selectAgencyGridGroupPeopleTotal(allGrid); |
|
|
|
Integer groupPeopleCount = agencyGridGroupPeopleTotal.stream().collect(Collectors.summingInt(AgencyGridGroupPeopleTotalResultDTO::getGridGroupPeopleTotal)); |
|
|
|
agencyResult.setGroupMemberTotalCount(groupPeopleCount); |
|
|
|
|
|
|
|
// TODO 4. 机关下小组平均人数
|
|
|
|
agencyResult.setGroupMemberAvgCount( |
|
|
|
agencyResult.getGroupTotalCount() == NumConstant.ZERO ? NumConstant.ZERO : |
|
|
|
agencyResult.getGroupMemberTotalCount()/agencyResult.getGroupTotalCount()); |
|
|
|
|
|
|
|
// TODO 5. 机关下小组人数中位数 小组最大(小)成员数、最多(少)成员小组ID
|
|
|
|
List<AgencyGridGroupPeopleResultDTO> agencyGridGroupPeople = groupDataService.selectAgencyEveryGroupPeopleCount(allGrid); |
|
|
|
List<AgencyGridGroupPeopleResultDTO> sorted = agencyGridGroupPeople.stream().sorted(Comparator.comparing(AgencyGridGroupPeopleResultDTO::getGroupCount).reversed()).collect(Collectors.toList()); |
|
|
|
Integer groupPeopleMedian; |
|
|
|
if (sorted.size() == NumConstant.ONE){ |
|
|
|
agencyResult.setGroupMemberMaxCount(sorted.get(NumConstant.ZERO).getGroupCount()); |
|
|
|
agencyResult.setMaxMemberGroupId(sorted.get(NumConstant.ZERO).getGroupId()); |
|
|
|
agencyResult.setGroupMemberMinCount(sorted.get(NumConstant.ZERO).getGroupCount()); |
|
|
|
agencyResult.setMinMemberGroupId(sorted.get(NumConstant.ZERO).getGroupId()); |
|
|
|
}else if (sorted.size() >= NumConstant.TWO){ |
|
|
|
groupPeopleMedian = sorted.size() % 2 == 0 ? |
|
|
|
(sorted.get(sorted.size() / 2 - 1).getGroupCount() + sorted.get(sorted.size() / 2).getGroupCount()) / 2 : |
|
|
|
sorted.get(sorted.size() / 2).getGroupCount(); |
|
|
|
agencyResult.setGroupMedian(groupPeopleMedian); |
|
|
|
|
|
|
|
agencyResult.setGroupMemberMaxCount(sorted.get(NumConstant.ZERO).getGroupCount()); |
|
|
|
agencyResult.setMaxMemberGroupId(sorted.get(NumConstant.ZERO).getGroupId()); |
|
|
|
agencyResult.setGroupMemberMinCount(sorted.get(sorted.size() - NumConstant.ONE).getGroupCount()); |
|
|
|
agencyResult.setMinMemberGroupId(sorted.get(sorted.size() - NumConstant.ONE).getGroupId()); |
|
|
|
} |
|
|
|
// TODO 6. 机关下小组增量
|
|
|
|
List<AgencyGroupIncrResultDTO> agencyGroupIncr = groupDataService.selectAgencyGroupIncr(allGrid, timeDim.getDateId()); |
|
|
|
Integer groupIncr = agencyGroupIncr.stream().collect(Collectors.summingInt(AgencyGroupIncrResultDTO::getGroupIncr)); |
|
|
|
agencyResult.setGroupIncr(groupIncr); |
|
|
|
result.add(agencyResult); |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 根据当前机关ID,查询出直属网格和所有下级机关的网格 |
|
|
|
* @param agencyId |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public List<String> getAllGrid(String agencyId){ |
|
|
|
List<String> result = new ArrayList<>(); |
|
|
|
List<AgencySubTreeDto> allAgency = dimAgencyService.getAllAgency(); |
|
|
|
|
|
|
|
Map<String,Set<String>> subGridOfAgency = new HashMap<>(); |
|
|
|
allAgency.forEach(agency -> { |
|
|
|
this.initAgencyGridMap(agency.getAgencyId(),agency,subGridOfAgency); |
|
|
|
}); |
|
|
|
Set<Map.Entry<String, Set<String>>> entries = subGridOfAgency.entrySet(); |
|
|
|
entries.forEach(entry -> { |
|
|
|
String key = entry.getKey(); |
|
|
|
Set<String> value = entry.getValue(); |
|
|
|
if (key.equals(agencyId)){ |
|
|
|
result.addAll(value); |
|
|
|
} |
|
|
|
}); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 递归获取机关下的网格id【包括直属机关】 |
|
|
|
* @param pid |
|
|
|
* @param agency |
|
|
|
* @param subGridOfAgency |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
void initAgencyGridMap(String pid, AgencySubTreeDto agency, Map<String,Set<String>> subGridOfAgency){ |
|
|
|
|
|
|
|
//向map中放入数据
|
|
|
|
if(subGridOfAgency.containsKey(pid)){ |
|
|
|
//包含key
|
|
|
|
Set<String> grids = subGridOfAgency.get(pid); |
|
|
|
if(null == grids){ |
|
|
|
grids = new HashSet<>(); |
|
|
|
subGridOfAgency.put(pid,grids); |
|
|
|
} |
|
|
|
if(null != agency.getGridIds() && agency.getGridIds().size() > NumConstant.ZERO){ |
|
|
|
grids.addAll(agency.getGridIds()); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
//不包含key
|
|
|
|
Set<String> grids = new HashSet<>(agency.getGridIds()); |
|
|
|
subGridOfAgency.put(pid,grids); |
|
|
|
} |
|
|
|
|
|
|
|
//定义递归出口
|
|
|
|
if(StringUtils.equals(ModuleConstant.AGENCY_LEVEL_COMMUNITY,agency.getLevel()) || null == agency.getSubAgencies() || agency.getSubAgencies().size() == NumConstant.ZERO){ |
|
|
|
return ; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//定义递归入口
|
|
|
|
agency.getSubAgencies().forEach(obj -> { |
|
|
|
initAgencyGridMap(pid,obj,subGridOfAgency); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 获取时间维度 dateId--weekId--monthId--quarterId--yearId |
|
|
|
* @param |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public DimIdGenerator.DimIdBean getDimIdBean(){ |
|
|
|
ZoneId zoneId = ZoneId.systemDefault(); |
|
|
|
LocalDate localDate = LocalDate.now(); |
|
|
|
ZonedDateTime zdt = localDate.atStartOfDay(zoneId); |
|
|
|
Date date = Date.from(zdt.toInstant()); |
|
|
|
return DimIdGenerator.getDimIdBean(date); |
|
|
|
} |
|
|
|
|
|
|
|
public List<String> getAgencyAllGrid(String customerId,String agencyId){ |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|