songhuangfeng123
2022-09-05 90a86453a5266a88c8cbdbbddc888070ccb8df29
incident-manage/incident-manage-service/src/main/java/com/gkhy/safePlatform/incidentManage/service/impl/AccidentCountServiceImpl.java
@@ -1,19 +1,33 @@
package com.gkhy.safePlatform.incidentManage.service.impl;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.BeanCopyUtils;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.incidentManage.entity.*;
import com.gkhy.safePlatform.incidentManage.enums.AccidentResultCodes;
import com.gkhy.safePlatform.incidentManage.exception.AccidentException;
import com.gkhy.safePlatform.incidentManage.model.dto.resp.*;
import com.gkhy.safePlatform.incidentManage.query.AccidentReportCountQuery;
import com.gkhy.safePlatform.incidentManage.query.db.AccidentReportCountDBQuery;
import com.gkhy.safePlatform.incidentManage.rpc.api.model.dto.req.IncidentManageCountRPCReq;
import com.gkhy.safePlatform.incidentManage.rpc.api.model.dto.resp.IncidentManageCountDetailRPCResp;
import com.gkhy.safePlatform.incidentManage.rpc.api.model.dto.resp.IncidentManageCountRPCResp;
import com.gkhy.safePlatform.incidentManage.rpc.api.model.dto.resp.IncidentManageRPCResp;
import com.gkhy.safePlatform.incidentManage.service.AccidentCountService;
import com.gkhy.safePlatform.incidentManage.service.baseService.AccidentReportInfoService;
import com.gkhy.safePlatform.incidentManage.utils.TimeUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.print.DocFlavor;
import java.util.*;
import java.util.stream.Collectors;
@Service("accidentCountService")
public class AccidentCountServiceImpl implements AccidentCountService {
@@ -21,6 +35,141 @@
    @Autowired
    private AccidentReportInfoService accidentReportInfoService;
    @DubboReference(check = false)
    private AccountDepartmentService accountDepartmentService;
    @Override
    public SearchResultVO<IncidentManageRPCResp> getCountByDeptId(IncidentManageCountRPCReq query) {
        if (query.getYear() == null) {
            throw new AccidentException(AccidentResultCodes.YEAR_NULL);
        }
        IncidentManageRPCResp incidentManageCountRPCResp = new IncidentManageRPCResp();
        // 获取id对应的部门
        DepInfoRPCRespDTO depInfoRPCRespDTO = getDepInfoByDepId(query.getDeptId());
        incidentManageCountRPCResp.setDeptId(depInfoRPCRespDTO.getDepId());
        incidentManageCountRPCResp.setDeptName(depInfoRPCRespDTO.getDepName());
        incidentManageCountRPCResp.setDeptLevel(depInfoRPCRespDTO.getDepLevel());
        getSingleTimeData(incidentManageCountRPCResp, query);
        return new SearchResultVO<>(
                false,
                null,
                null,
                null,
                1L,
                incidentManageCountRPCResp,
                ResultCodes.OK
        );
    }
    @Override
    public SearchResultVO<List<IncidentManageRPCResp>> getCountByDeptIds(IncidentManageCountRPCReq query) {
        List<IncidentManageRPCResp> list = new ArrayList<>();
        // 获取该部门及其子部门的所有信息
        List<DepInfoRPCRespDTO> deptList = getDepListInfoByDepId(query.getDeptId());
        for (DepInfoRPCRespDTO depInfoRPCRespDTO : deptList) {
            IncidentManageRPCResp incidentManageCountRPCResp = new IncidentManageRPCResp();
            incidentManageCountRPCResp.setDeptId(depInfoRPCRespDTO.getDepId());
            incidentManageCountRPCResp.setDeptName(depInfoRPCRespDTO.getDepName());
            incidentManageCountRPCResp.setDeptLevel(depInfoRPCRespDTO.getDepLevel());
            getSingleTimeData(incidentManageCountRPCResp, query);
            list.add(incidentManageCountRPCResp);
        }
        return new SearchResultVO<>(
                false,
                null,
                null,
                null,
                (long) list.size(),
                list,
                ResultCodes.OK
        );
    }
    private void getSingleTimeData(IncidentManageRPCResp incidentManageRPCResp, IncidentManageCountRPCReq query) {
        List<IncidentManageCountRPCResp> resList = new ArrayList<>();
        List<AccidentReportCountRPC> accidentReportCountRPCList = new ArrayList<>();
        Long deptId = query.getDeptId();
        String startTime;
        String endTime;
        if (query.getMonth() == null) {
            // 查该年度12个月份的
            startTime = TimeUtils.getYearFirst(query.getYear());
            endTime = TimeUtils.getYearLast(query.getYear());
            accidentReportCountRPCList= accidentReportInfoService.getCountForRPCByDeptIdAndMonth(startTime, endTime, deptId);
        } else {
            // 查该年度指定月份的
            startTime = TimeUtils.getMonthFirst(query.getYear(), query.getMonth());
            endTime = TimeUtils.getMonthLast(query.getYear(), query.getMonth());
            accidentReportCountRPCList = accidentReportInfoService.getCountForRPCByDeptIdAndDay(startTime, endTime, deptId);
        }
        // 拼装第一层时间(去重)
        Set<String> timeSET = accidentReportCountRPCList.stream().map(AccidentReportCountRPC::getTime).collect(Collectors.toSet());
        // 拼装第一层时间(排序)
        List<String> timeList = timeSET.stream().sorted().collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(timeSET)) {
            for (String str : timeSET) {
                IncidentManageCountRPCResp incidentManageCountRPCResp = new IncidentManageCountRPCResp();
                incidentManageCountRPCResp.setTime(str);
                incidentManageCountRPCResp.setDetail(new ArrayList<>());
                resList.add(incidentManageCountRPCResp);
            }
        }
        // 拼装第二层事故详情
        if (!CollectionUtils.isEmpty(timeList)) {
            for (AccidentReportCountRPC accidentReportCountRPC : accidentReportCountRPCList) {
                for (IncidentManageCountRPCResp incidentManageCountRPCResp : resList) {
                    if (accidentReportCountRPC.getTime().equals(incidentManageCountRPCResp.getTime())) {
                        IncidentManageCountDetailRPCResp incidentManageCountDetailRPCResp = new IncidentManageCountDetailRPCResp();
                        incidentManageCountDetailRPCResp.setLevel(accidentReportCountRPC.getLevel());
                        incidentManageCountDetailRPCResp.setNum(accidentReportCountRPC.getNum());
                        incidentManageCountDetailRPCResp.setMinorInjuryNum(accidentReportCountRPC.getMinorInjuryNum());
                        incidentManageCountDetailRPCResp.setSeriousInjuryNum(accidentReportCountRPC.getSeriousInjuryNum());
                        incidentManageCountDetailRPCResp.setDeathNum(accidentReportCountRPC.getDeathNum());
                        incidentManageCountDetailRPCResp.setEconomicLoss(accidentReportCountRPC.getEconomicLoss());
                        incidentManageCountRPCResp.getDetail().add(incidentManageCountDetailRPCResp);
                    }
                }
            }
        }
        incidentManageRPCResp.setData(resList);
    }
    private DepInfoRPCRespDTO getDepInfoByDepId(Long deptId) {
        DepInfoRPCRespDTO dep = new DepInfoRPCRespDTO();
        ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(deptId);
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                dep = (DepInfoRPCRespDTO) rpcResult.getData();
            }
        } else {
            throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
        }
        return dep;
    }
    private List<DepInfoRPCRespDTO> getDepListInfoByDepId(Long deptId) {
        List<DepInfoRPCRespDTO> depList = new ArrayList<>();
        ResultVO<List<DepInfoRPCRespDTO>> rpcResult = accountDepartmentService.listDepAndSubDepByDepId(deptId);
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                depList = (List<DepInfoRPCRespDTO>) rpcResult.getData();
            }
        } else {
            throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
        }
        return depList;
    }
    @Override
    public ResultVO<List<AccidentReportCountRespDTO>> countAccidentReport(AccidentReportCountQuery query) {