package com.gkhy.safePlatform.specialWork.service.impl; import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO; import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepRPCRespDTO; import com.gkhy.safePlatform.commons.co.ContextCacheUser; import com.gkhy.safePlatform.commons.enums.E; import com.gkhy.safePlatform.commons.enums.ResultCodes; import com.gkhy.safePlatform.commons.exception.AusinessException; import com.gkhy.safePlatform.commons.exception.BusinessException; import com.gkhy.safePlatform.commons.query.PageQuery; import com.gkhy.safePlatform.commons.vo.ResultVO; import com.gkhy.safePlatform.commons.vo.SearchResultVO; import com.gkhy.safePlatform.specialWork.entity.SpecialWorkAppointmentInfo; import com.gkhy.safePlatform.specialWork.model.bo.WorkStatisticsBO; import com.gkhy.safePlatform.specialWork.model.dto.req.DeleteForm; import com.gkhy.safePlatform.specialWork.model.dto.req.SpecialWorkAppointmentAddReqDTO; import com.gkhy.safePlatform.specialWork.model.dto.req.SpecialWorkAppointmentModReqDTO; import com.gkhy.safePlatform.specialWork.model.dto.resp.SpecialWorkAppointmentRespDTO; import com.gkhy.safePlatform.specialWork.model.dto.resp.WorkStatisticsRespDTO; import com.gkhy.safePlatform.specialWork.model.query.SpecialWorkAppointmentQuery; import com.gkhy.safePlatform.specialWork.service.SpecialWorkAppointmentService; import com.gkhy.safePlatform.specialWork.service.baseService.SpecialWorkAppointmentInfoService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.BeanUtils; 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("SpecialWorkAppointmentService") public class SpecialWorkAppointmentServiceImpl implements SpecialWorkAppointmentService { @DubboReference(check = false) private AccountDepartmentService accountDepartmentService; @Autowired private SpecialWorkAppointmentInfoService appointmentInfoService; /** * 新增 * @param currentUser * @param addReqDTO * @return */ @Override public ResultVO save(ContextCacheUser currentUser, SpecialWorkAppointmentAddReqDTO addReqDTO) { //先获取当前申请人所在的部门当天是否已预约 Long count = appointmentInfoService.getCountByDepAndTime(currentUser.getDepId(), addReqDTO.getAppointmentTime()); if(count>0){ throw new AusinessException(E.DATA_DATABASE_EXIST,"该部门下已有当天预约,不可重复预约"); } //获取部门相关信息 DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO)accountDepartmentService.getDepInfoByDepId(currentUser.getDepId()).getData(); if(null == depInfo){ throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"该申请部门不存在"); } //复制数据 SpecialWorkAppointmentInfo specialWorkAppointmentInfo = new SpecialWorkAppointmentInfo(); BeanUtils.copyProperties(addReqDTO,specialWorkAppointmentInfo); //填充 specialWorkAppointmentInfo.setId(IdUtil.getSnowflake(0,0).nextId()); specialWorkAppointmentInfo.setApplyDepId(currentUser.getDepId()); specialWorkAppointmentInfo.setApplyDepName(depInfo.getDepName()); specialWorkAppointmentInfo.setCreateUname(currentUser.getUsername()); specialWorkAppointmentInfo.setCreateUid(currentUser.getUid()); int i = appointmentInfoService.saveOne(specialWorkAppointmentInfo); if(i > 0){ return ResultVO.success(); } return new ResultVO(ResultCodes.SERVER_ADD_ERROR); } /** * 修改 * @param currentUser * @param modReqDTO * @return */ @Override public ResultVO update(ContextCacheUser currentUser, SpecialWorkAppointmentModReqDTO modReqDTO) { //复制数据 SpecialWorkAppointmentInfo specialWorkAppointmentInfo = new SpecialWorkAppointmentInfo(); BeanUtils.copyProperties(modReqDTO,specialWorkAppointmentInfo); //填充 specialWorkAppointmentInfo.setModifiedUname(currentUser.getUsername()); specialWorkAppointmentInfo.setModifiedUid(currentUser.getUid()); int i = appointmentInfoService.updateOne(specialWorkAppointmentInfo); if(i > 0){ return ResultVO.success(); } return new ResultVO(ResultCodes.SERVER_UPDATE_ERROR); } /** * 删除 * @param currentUser * @param id * @return */ @Override public ResultVO deleteOne(ContextCacheUser currentUser, Long id) { if(null == id){ throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } int i = appointmentInfoService.updateStatus(id); if(i > 0){ return ResultVO.success(); } return new ResultVO(ResultCodes.SERVER_DEL_ERROR); } @Override public ResultVO batchDelete(ContextCacheUser currentUser, DeleteForm deleteForm) { List ids = deleteForm.getIds(); int i = appointmentInfoService.updateStatusByIds(ids); if(i > 0){ return ResultVO.success(); } return new ResultVO(ResultCodes.SERVER_DEL_ERROR); } @Override public SearchResultVO> listAll(ContextCacheUser currentUser, PageQuery pageQuery) { Page page = new Page(pageQuery.getPageIndex(),pageQuery.getPageSize()); List specialWorkAppointmentInfos = appointmentInfoService.listByPage(page, pageQuery.getSearchParams()); List respDTOList = new ArrayList<>(); specialWorkAppointmentInfos.forEach(item -> { SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO(); BeanUtils.copyProperties(item,respDTO); respDTOList.add(respDTO); }); return new SearchResultVO<>(true, page.getCurrent(), page.getSize(), page.getPages(), page.getTotal(), respDTOList, ResultCodes.OK); } /** * 单条查询 * @param currentUser * @param id * @return */ @Override public ResultVO queryById(ContextCacheUser currentUser, Long id) { if(null == id){ throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL); } SpecialWorkAppointmentInfo specialWorkAppointmentInfo = appointmentInfoService.queryById(id); SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO(); if(null != specialWorkAppointmentInfo){ BeanUtils.copyProperties(specialWorkAppointmentInfo,respDTO); } return new ResultVO<>(ResultCodes.OK,respDTO); } @Override public SearchResultVO> listByDep(ContextCacheUser currentUser, PageQuery pageQuery) { Page page = new Page(pageQuery.getPageIndex(),pageQuery.getPageSize()); SpecialWorkAppointmentQuery query = pageQuery.getSearchParams(); query.setApplyDepId(currentUser.getDepId()); List specialWorkAppointmentInfos = appointmentInfoService.listByPage(page, query); List respDTOList = new ArrayList<>(); specialWorkAppointmentInfos.forEach(item -> { SpecialWorkAppointmentRespDTO respDTO = new SpecialWorkAppointmentRespDTO(); BeanUtils.copyProperties(item,respDTO); respDTOList.add(respDTO); }); return new SearchResultVO<>(true, page.getCurrent(), page.getSize(), page.getPages(), page.getTotal(), respDTOList, ResultCodes.OK); } @Override public ResultVO> statisticsAppointment(SpecialWorkAppointmentQuery query) { List respDTOList = new ArrayList<>(); //获取所有部门 List depList = getDepList(); //统计各部门每种作业数量 List workStatisticsBOList = appointmentInfoService.statisticsAppointment(query); //遍历部门 for(DepRPCRespDTO dep : depList){ WorkStatisticsRespDTO respDTO = new WorkStatisticsRespDTO(); respDTO.setApplyDepId(dep.getDepId()); respDTO.setApplyDepName(dep.getDepName()); List selectList = workStatisticsBOList .stream() .filter(bo -> bo.getApplyDepId().equals(dep.getDepId())) .collect(Collectors.toList()); if(selectList.size()>0){ WorkStatisticsBO workStatisticsBO = selectList.get(0); respDTO.setBlindPlatePluggingCount(workStatisticsBO.getBlindPlatePluggingCount()); respDTO.setConfinedSpaceCount(workStatisticsBO.getConfinedSpaceCount()); respDTO.setHeightCount(workStatisticsBO.getHeightCount()); respDTO.setHotCount(workStatisticsBO.getHotCount()); respDTO.setLiftingCount(workStatisticsBO.getLiftingCount()); respDTO.setOpenCircuitCout(workStatisticsBO.getOpenCircuitCout()); respDTO.setGroundBreakingCount(workStatisticsBO.getGroundBreakingCount()); respDTO.setTemporaryPowerCount(workStatisticsBO.getTemporaryPowerCount()); respDTO.setTotalCount(workStatisticsBO.getTotalCount()); respDTOList.add(respDTO); } //子级 List children = dep.getChildren(); if(null != children && children.size()>0){ traverseChildren(children,workStatisticsBOList,respDTOList); } } return new ResultVO<>(ResultCodes.OK,respDTOList); } public void traverseChildren(List depRPCRespDTOList,List workStatisticsBOList,List respDTOList){ for (DepRPCRespDTO depRPCRespDTO : depRPCRespDTOList) { WorkStatisticsRespDTO respDTO = new WorkStatisticsRespDTO(); respDTO.setApplyDepId(depRPCRespDTO.getDepId()); respDTO.setApplyDepName(depRPCRespDTO.getDepName()); List selectList = workStatisticsBOList .stream() .filter(bo -> bo.getApplyDepId().equals(depRPCRespDTO.getDepId())) .collect(Collectors.toList()); if(selectList.size()>0){ WorkStatisticsBO workStatisticsBO = selectList.get(0); respDTO.setBlindPlatePluggingCount(workStatisticsBO.getBlindPlatePluggingCount()); respDTO.setConfinedSpaceCount(workStatisticsBO.getConfinedSpaceCount()); respDTO.setHeightCount(workStatisticsBO.getHeightCount()); respDTO.setHotCount(workStatisticsBO.getHotCount()); respDTO.setLiftingCount(workStatisticsBO.getLiftingCount()); respDTO.setOpenCircuitCout(workStatisticsBO.getOpenCircuitCout()); respDTO.setGroundBreakingCount(workStatisticsBO.getGroundBreakingCount()); respDTO.setTemporaryPowerCount(workStatisticsBO.getTemporaryPowerCount()); respDTO.setTotalCount(workStatisticsBO.getTotalCount()); respDTOList.add(respDTO); } //子级 List children = depRPCRespDTO.getChildren(); if(null != children && children.size()>0){ traverseChildren(children,workStatisticsBOList,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; } }