package com.gkhy.safePlatform.specialWork.service.impl; import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepRPCRespDTO; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.specialWork.entity.WorkApplyCountDO; import com.gkhy.safePlatform.specialWork.enums.WorkTypeEnum; import com.gkhy.safePlatform.specialWork.model.dto.resp.WorkDepStatisticRespDTO; import com.gkhy.safePlatform.specialWork.model.dto.resp.WorkTypeStatisticRespDTO; import com.gkhy.safePlatform.specialWork.service.WorkStatisticService; import com.gkhy.safePlatform.specialWork.service.baseService.WorkApplyInfoService; import org.apache.commons.collections.CollectionUtils; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service("WorkStatisticService") public class WorkStatisticServiceImpl implements WorkStatisticService { @Autowired private WorkApplyInfoService workApplyInfoService; @DubboReference(check = false) private AccountDepartmentService accountDepartmentService; @Override public ResultVO> getWorkApplyCountByDep() { //获取所有部门 List depList = getDepList(); //统计各部门每种作业数量 List workApplyCountDOList = workApplyInfoService.getWorkApplyCountByDep(); List respDTOList = new ArrayList<>(); //循环部门 for(DepRPCRespDTO dep:depList){ WorkDepStatisticRespDTO respDTO = new WorkDepStatisticRespDTO(); respDTO.setDepId(dep.getDepId()); respDTO.setDepName(dep.getDepName()); //获取该部门下的作业数量 List selectDepList = workApplyCountDOList .stream() .filter(coutDo -> coutDo.getDepId().equals(dep.getDepId())) .collect(Collectors.toList()); List typeStatisticRespDTOS = new ArrayList<>(); //声明变量 部门总数量 int totalCount = 0; //循环作业类型 for(WorkTypeEnum workTypeEnum:WorkTypeEnum.values()){ WorkTypeStatisticRespDTO typeStatisticRespDTO = new WorkTypeStatisticRespDTO(); typeStatisticRespDTO.setWorkType(workTypeEnum.getType()); typeStatisticRespDTO.setWorkTypeName(workTypeEnum.getName()); //获取该类型下的数据 List selectTypeList = selectDepList .stream() .filter(coutDo -> coutDo.getWorkType().equals(workTypeEnum.getType())) .collect(Collectors.toList()); if(selectTypeList.size()>0){ typeStatisticRespDTO.setCount(selectTypeList.get(0).getCount()); totalCount += selectTypeList.get(0).getCount(); }else{ typeStatisticRespDTO.setCount(0); } typeStatisticRespDTOS.add(typeStatisticRespDTO); } respDTO.setTotalCount(totalCount); respDTO.setTypeList(typeStatisticRespDTOS); respDTOList.add(respDTO); //如果有子级 List children = dep.getChildren(); if(!CollectionUtils.isEmpty(children)){ traverseChildren(children,workApplyCountDOList,respDTOList); } } return new ResultVO<>(ResultCodes.OK,respDTOList); } private void traverseChildren(List depList, List workApplyCountDOList, List respDTOList) { for(DepRPCRespDTO dep:depList){ WorkDepStatisticRespDTO respDTO = new WorkDepStatisticRespDTO(); respDTO.setDepId(dep.getDepId()); respDTO.setDepName(dep.getDepName()); //获取该部门下的作业数量 List selectDepList = workApplyCountDOList .stream() .filter(coutDo -> coutDo.getDepId().equals(dep.getDepId())) .collect(Collectors.toList()); List typeStatisticRespDTOS = new ArrayList<>(); //声明变量 部门总数量 int totalCount = 0; //循环作业类型 for(WorkTypeEnum workTypeEnum:WorkTypeEnum.values()){ WorkTypeStatisticRespDTO typeStatisticRespDTO = new WorkTypeStatisticRespDTO(); typeStatisticRespDTO.setWorkType(workTypeEnum.getType()); typeStatisticRespDTO.setWorkTypeName(workTypeEnum.getName()); //获取该类型下的数据 List selectTypeList = selectDepList .stream() .filter(coutDo -> coutDo.getWorkType().equals(workTypeEnum.getType())) .collect(Collectors.toList()); if(selectTypeList.size()>0){ typeStatisticRespDTO.setCount(selectTypeList.get(0).getCount()); totalCount += selectTypeList.get(0).getCount(); }else{ typeStatisticRespDTO.setCount(0); } typeStatisticRespDTOS.add(typeStatisticRespDTO); } respDTO.setTotalCount(totalCount); respDTO.setTypeList(typeStatisticRespDTOS); respDTOList.add(respDTO); //如果有子级 List children = dep.getChildren(); if(!CollectionUtils.isEmpty(children)){ traverseChildren(children,workApplyCountDOList,respDTOList); } } } public List getDepList(){ List depList = new ArrayList<>(); try{ ResultVO> listResultVO = accountDepartmentService.depList(); if (listResultVO != null && listResultVO.getCode().equals(ResultCodes.OK.getCode())) { if (listResultVO.getData() != null) { depList = (List) listResultVO.getData(); }else{ throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST); } } else { throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION); } }catch (Exception e){ throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION); } return depList; } }