package com.gkhy.safePlatform.specialWork.service.impl;
|
|
import cn.hutool.core.date.DateUtil;
|
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
|
import com.gkhy.safePlatform.account.rpc.apimodel.model.enums.DepartmentLevelEnum;
|
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
|
import com.gkhy.safePlatform.commons.enums.ResultCodes;
|
import com.gkhy.safePlatform.commons.utils.TimeUtils;
|
import com.gkhy.safePlatform.commons.vo.ResultVO;
|
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
|
import com.gkhy.safePlatform.specialWork.enums.WorkApplyStatusEnum;
|
import com.gkhy.safePlatform.specialWork.enums.WorkTypeEnum;
|
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkCountRespDTO;
|
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkDepCountRespDTO;
|
import com.gkhy.safePlatform.specialWork.model.dto.resp.count.WorkTypeCountRespDTO;
|
import com.gkhy.safePlatform.specialWork.model.query.db.WorkCountDbQuery;
|
import com.gkhy.safePlatform.specialWork.service.WorkCountService;
|
import com.gkhy.safePlatform.specialWork.service.baseService.WorkInfoService;
|
import org.apache.dubbo.config.annotation.DubboReference;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.time.LocalDateTime;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Service
|
public class WorkCountServiceImpl implements WorkCountService {
|
|
@Autowired
|
private WorkInfoService workInfoService;
|
|
@DubboReference(check = false)
|
private AccountDepartmentService departmentService;
|
|
|
@Override
|
public List<WorkTypeCountRespDTO> countEveryTypeByOptions(LocalDateTime startTimne, LocalDateTime endTime, Long depId) {
|
List<WorkTypeCountRespDTO> countList = new ArrayList<>();
|
//1、校验部门是否存在,无效部门返回全零数据
|
ResultVO<DepInfoRPCRespDTO> depSearchVo = departmentService.getDepInfoByDepId(depId);
|
if(depSearchVo == null || !depSearchVo.getCode().equals(ResultCodes.OK.getCode()) || depSearchVo.getData() == null){
|
countList = initEveryTypeZeroCountResult();
|
return countList;
|
}
|
//2、解析该部门需要查询的深度,车间级只统计本车间,往上级别需要统计全部子部门数据
|
DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depSearchVo.getData();
|
if(depInfo == null || depInfo.getDepLevel() == null){
|
countList = initEveryTypeZeroCountResult();
|
return countList;
|
}
|
//3、查询条件 - 部门ID列表
|
List<Long> queryDepIdList = new ArrayList<>();
|
if(depInfo.getDepLevel().equals(DepartmentLevelEnum.WORKSHOP.code)){
|
//部门为车间
|
queryDepIdList.add(depId);
|
}else if(depInfo.getDepLevel().equals(DepartmentLevelEnum.GROUP.code) || depInfo.getDepLevel().equals(DepartmentLevelEnum.OTHER.code)){
|
//部门为班组或其他
|
//获取本部门隶属的车间
|
DepInfoRPCRespDTO chejianDep = praseDepWorkShop(depInfo);
|
//获取该车间的所有子部门
|
if(chejianDep != null){
|
ResultVO subDepIdListVo = departmentService.listDepAndSubDepIds(chejianDep.getDepId());
|
if(subDepIdListVo != null && subDepIdListVo.getCode().equals(ResultCodes.OK.getCode())){
|
queryDepIdList = (List<Long>) subDepIdListVo.getData();
|
}
|
}
|
}else {
|
//部门为公司或事业部
|
//获取其下面的所有部门
|
ResultVO subDepIdListVo = departmentService.listDepAndSubDepIds(depInfo.getDepId());
|
if(subDepIdListVo != null && subDepIdListVo.getCode().equals(ResultCodes.OK.getCode())){
|
queryDepIdList = (List<Long>) subDepIdListVo.getData();
|
}
|
}
|
//如果查询条件部门id列表为空,就不查询了,返回全零数据
|
if(queryDepIdList == null || queryDepIdList.isEmpty()){
|
countList = initEveryTypeZeroCountResult();
|
return countList;
|
}
|
//5、为每种作业分别统计数量
|
for(WorkTypeEnum workType : WorkTypeEnum.values()){
|
WorkCountDbQuery dbQuery = new WorkCountDbQuery();
|
dbQuery.setDepIdList(queryDepIdList);
|
dbQuery.setType(workType.getType());
|
dbQuery.setStartTime(startTimne);
|
dbQuery.setEndTime(endTime);
|
Integer count = workInfoService.countEvertWorkByOptions(dbQuery);
|
WorkTypeCountRespDTO workTypeCountRespDTO = new WorkTypeCountRespDTO();
|
workTypeCountRespDTO.setName(workType.getName());
|
workTypeCountRespDTO.setValue(count);
|
countList.add(workTypeCountRespDTO);
|
}
|
return countList;
|
}
|
|
/**
|
* 统计子部门的作业数量
|
* @param startTimne
|
* @param endTime
|
* @param depId
|
* @return
|
*/
|
@Override
|
public List<WorkDepCountRespDTO> countWorkBySubDeps(LocalDateTime startTimne, LocalDateTime endTime, Long depId,Byte type) {
|
if(depId == null || depId < 1)
|
return null;
|
//1、查询部门信息
|
ResultVO<DepInfoRPCRespDTO> depSearchVo = departmentService.getDepInfoByDepId(depId);
|
if(depSearchVo == null || !depSearchVo.getCode().equals(ResultCodes.OK.getCode()) || depSearchVo.getData() == null){
|
return null;
|
}
|
//2、查询直属子部门列表
|
ResultVO<List<DepInfoRPCRespDTO>> subDepSearchVO = departmentService.listSubDepsByDepId(depId);
|
if(subDepSearchVO == null || !subDepSearchVO.getCode().equals(ResultCodes.OK.getCode()) || subDepSearchVO.getData() == null)
|
return null;
|
List<DepInfoRPCRespDTO> subDepList = (List<DepInfoRPCRespDTO>) subDepSearchVO.getData();
|
if(subDepList.isEmpty())
|
return null;
|
//3、统计子部门作业数据
|
//4、结构化数据
|
List<WorkDepCountRespDTO> resultList = new ArrayList<>();
|
subDepList.forEach(dep -> {
|
//获取该部门的全部子部门
|
ResultVO<List<Long>> subDepIdsVO = departmentService.listDepAndSubDepIds(dep.getDepId());
|
if(subDepIdsVO != null && subDepIdsVO.getCode().equals(ResultCodes.OK.getCode()) && subDepIdsVO.getData() != null){
|
List<Long> subDepIds = (List<Long>) subDepIdsVO.getData();
|
WorkDepCountRespDTO depCountDTO = new WorkDepCountRespDTO();
|
WorkCountDbQuery query = new WorkCountDbQuery();
|
query.setDepIdList(subDepIds);
|
query.setType(type);
|
query.setStartTime(startTimne);
|
query.setEndTime(endTime);
|
// Integer workCount = workInfoService.countAllWorkByDep(dep.getDepId(),startTimne,endTime);
|
Integer workCount = workInfoService.countEvertWorkByOptions(query);
|
depCountDTO.setName(dep.getDepName());
|
if(workCount == null)
|
depCountDTO.setValue(0);
|
else
|
depCountDTO.setValue(workCount);
|
resultList.add(depCountDTO);
|
}
|
});
|
return resultList;
|
}
|
|
|
/**
|
* 统计指定部门最近12个月的作业数量,按月份统计
|
* @param depId
|
* @return
|
*/
|
@Override
|
public List<WorkCountRespDTO> count12MonthWorkByDep(Long depId) {
|
if(depId == null || depId < 1)
|
return null;
|
//查询部门信息
|
ResultVO<DepInfoRPCRespDTO> depSearchVO = departmentService.getDepInfoByDepId(depId);
|
if(depSearchVO == null || !depSearchVO.getCode().equals(ResultCodes.OK.getCode()) || depSearchVO.getData() == null)
|
return null;
|
DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depSearchVO.getData();
|
//获取全部子部门数据
|
List<Long> subDepIds = null;
|
ResultVO<List<Long>> subDepIdsVO = departmentService.listDepAndSubDepIds(depInfo.getDepId());
|
if(subDepIdsVO != null && subDepIdsVO.getCode().equals(ResultCodes.OK.getCode()) && subDepIdsVO.getData() != null){
|
subDepIds = (List<Long>) subDepIdsVO.getData();
|
}
|
List<WorkCountRespDTO> respDTOList = new ArrayList<>();
|
LocalDateTime time = LocalDateTime.now();
|
//重置时间,从一年前开始统计
|
time = time.minusYears(1);
|
for(long i=0;i<12;i++){
|
time = time.plusMonths(1);
|
LocalDateTime monthBeginTime = calBeginOrEndOfMonth(time,true);
|
LocalDateTime monthEndTime = calBeginOrEndOfMonth(time,false);
|
WorkCountDbQuery query = new WorkCountDbQuery();
|
query.setDepIdList(subDepIds);
|
query.setStartTime(monthBeginTime);
|
query.setEndTime(monthEndTime);
|
Integer workCount = workInfoService.countEvertWorkByOptions(query);
|
WorkCountRespDTO workCountRespDTO = new WorkCountRespDTO();
|
workCountRespDTO.setName(sayMonthName(time));
|
workCountRespDTO.setValue(workCount);
|
respDTOList.add(workCountRespDTO);
|
}
|
return respDTOList;
|
}
|
|
private String sayMonthName(LocalDateTime time){
|
if(time == null)
|
return null;
|
int month = time.getMonthValue();
|
if(month == 1){
|
return "一月";
|
}else if(month == 2){
|
return "二月";
|
}else if(month == 3){
|
return "三月";
|
}else if(month == 4){
|
return "四月";
|
}else if(month == 5){
|
return "五月";
|
}else if(month == 6){
|
return "六月";
|
}else if(month == 7){
|
return "七月";
|
}else if(month == 8){
|
return "八月";
|
}else if(month == 9){
|
return "九月";
|
}else if(month == 10){
|
return "十月";
|
}else if(month == 11){
|
return "十一月";
|
}else if(month == 12){
|
return "十二月";
|
}else {
|
return null;
|
}
|
}
|
|
/**
|
* 给定时间,计算月初或月末时间
|
* @param time
|
* @param isBegin
|
* @return
|
*/
|
private LocalDateTime calBeginOrEndOfMonth(LocalDateTime time,boolean isBegin){
|
if(time == null)
|
return null;
|
String str = DateUtil.formatLocalDateTime(time);
|
Date date = DateUtil.parseDate(str);
|
if(isBegin){
|
//计算月开始时间
|
return DateUtil.beginOfMonth(date).toLocalDateTime();
|
}else {
|
//计算月结束时间
|
return DateUtil.endOfMonth(date).toLocalDateTime();
|
}
|
}
|
|
|
private List<WorkTypeCountRespDTO> initEveryTypeZeroCountResult(){
|
List<WorkTypeCountRespDTO> countList = new ArrayList<>();
|
for(WorkTypeEnum workType : WorkTypeEnum.values()){
|
WorkTypeCountRespDTO dto = new WorkTypeCountRespDTO();
|
dto.setName(workType.getName());
|
dto.setValue(0);
|
countList.add(dto);
|
}
|
return countList;
|
}
|
|
/**
|
* 解析部门所属的车间
|
* @param dep
|
* @return
|
*/
|
private DepInfoRPCRespDTO praseDepWorkShop(DepInfoRPCRespDTO dep){
|
if(dep == null || dep.getDepLevel() == null)
|
dep = null;
|
if(dep.getDepLevel().equals(DepartmentLevelEnum.GROUP.code) || dep.getDepLevel().equals(DepartmentLevelEnum.OTHER.code)){
|
if(dep.getParentDepId() == null)
|
dep = null;
|
ResultVO<DepInfoRPCRespDTO> depResult = departmentService.getDepInfoByDepId(dep.getParentDepId());
|
if(depResult == null || !depResult.getCode().equals(ResultCodes.OK.getCode()) || depResult.getData() == null)
|
dep = null;
|
DepInfoRPCRespDTO prevDepInfo = (DepInfoRPCRespDTO) depResult.getData();
|
dep = praseDepWorkShop(prevDepInfo);
|
}
|
return dep;
|
}
|
}
|