luoyisheng
2025-03-25 b03d89e792a34c486f0afeab69e837f85d8cbe16
medicalWaste-system/src/main/java/com/sinata/system/service/biz/ScreenService.java
@@ -1,22 +1,34 @@
package com.sinata.system.service.biz;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.sinata.common.exception.ServiceException;
import com.sinata.common.utils.BeanUtils;
import com.sinata.common.utils.CollUtils;
import com.sinata.common.utils.DateUtils;
import com.sinata.system.domain.MedicalWasteStaticsVO;
import com.sinata.system.domain.MwCollectRecord;
import com.sinata.system.domain.MwTransitCarCollectPoint;
import com.sinata.system.domain.MwWarningRecord;
import com.sinata.system.domain.SysDepartment;
import com.sinata.system.domain.vo.CarDistributionVO;
import com.sinata.system.domain.vo.DepartmentTagInfoVO;
import com.sinata.system.domain.vo.MedicalInstitutionCollectListVO;
import com.sinata.system.domain.vo.MedicalWasteCollectVO;
import com.sinata.system.domain.vo.MwWarningRecordStaticsVO;
import com.sinata.system.domain.vo.MwWarningRecordVO;
import com.sinata.system.domain.vo.ScreenDepartmentVO;
import com.sinata.system.domain.vo.SysDictDataVO;
import com.sinata.system.domain.vo.TodayMedicalWastePieVO;
import com.sinata.system.domain.vo.TotalCollectWeightByTypeVO;
import com.sinata.system.enums.DepartmentEnum;
import com.sinata.system.enums.WarningTypeEnum;
import com.sinata.system.service.ISysDictDataService;
import com.sinata.system.service.MwCollectRecordService;
import com.sinata.system.service.MwDisposalRecordService;
import com.sinata.system.service.MwTransitCarCollectPointService;
import com.sinata.system.service.MwWarningRecordService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
@@ -25,6 +37,7 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -42,6 +55,8 @@
    private final MwCollectRecordService mwCollectRecordService;
    private final ISysDictDataService sysDictDataService;
    private final MwDisposalRecordService mwDisposalRecordService;
    private final MwWarningRecordService mwWarningRecordService;
    private final MwTransitCarCollectPointService mwTransitCarCollectPointService;
    /**
     * 机构分布-获取机构列表
@@ -62,7 +77,7 @@
     */
    public DepartmentTagInfoVO getTagInfo(Long id) {
        SysDepartment department = sysDepartmentService.getById(id);
        DepartmentTagInfoVO vo = null;
        DepartmentTagInfoVO vo = new DepartmentTagInfoVO();
        if (Objects.isNull(department)) {
            throw new ServiceException("单位不存在");
        }
@@ -103,11 +118,8 @@
     * @return
     */
    public List<TodayMedicalWastePieVO> todayMedicalWastePie() {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        //查询南宁市下面的医疗机构
        List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode()).eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode()).list();
        List<Long> departmentIdList = list.stream().map(SysDepartment::getId).collect(Collectors.toList());
        List<TodayMedicalWastePieVO> todayMedicalWastePieVOList = new ArrayList<>();
        List<Long> departmentIdList = getNanNingMedicalInsitutionIdList();
        if (CollUtils.isEmpty(departmentIdList)) {
            return todayMedicalWastePieVOList;
        }
@@ -118,21 +130,33 @@
            return todayMedicalWastePieVOList;
        }
        int total = collectRecordList.size();
        // 分组并统计每个 wasteTypeStr 对应的记录数
        Map<String, Long> resultMap = collectRecordList.stream()
                .collect(Collectors.groupingBy(MwCollectRecord::getWasteTypeStr, Collectors.counting()));
        todayMedicalWastePieVOList = resultMap.entrySet().stream().map(entry -> {
        //查询医废类型列表
        List<SysDictDataVO> sysDictDataVOS = sysDictDataService.medicalWasteTypeList();
        // 分组并统计每个 wasteType 对应的记录数
        Map<Long, Long> resultMap = collectRecordList.stream()
                .collect(Collectors.groupingBy(MwCollectRecord::getWasteType, Collectors.counting()));
        todayMedicalWastePieVOList = sysDictDataVOS.stream().map(sysDictDataVO -> {
            Long count = resultMap.getOrDefault(sysDictDataVO.getDictCode(), 0L);
            TodayMedicalWastePieVO todayMedicalWastePieVO = new TodayMedicalWastePieVO();
            todayMedicalWastePieVO.setMedicalWasteStr(entry.getKey());
            todayMedicalWastePieVO.setCount(entry.getValue().intValue());
            todayMedicalWastePieVO.setProportion(BigDecimal.valueOf(entry.getValue()).divide(BigDecimal.valueOf(total), RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
            todayMedicalWastePieVO.setMedicalWasteStr(sysDictDataVO.getDictLabel());
            todayMedicalWastePieVO.setCssClass(sysDictDataVO.getCssClass());
            todayMedicalWastePieVO.setCount(count.intValue());
            todayMedicalWastePieVO.setProportion(BigDecimal.valueOf(count).divide(BigDecimal.valueOf(total), 4, RoundingMode.FLOOR).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.FLOOR));
            return todayMedicalWastePieVO;
        }).collect(Collectors.toList());
        return todayMedicalWastePieVOList;
    }
    /**
     * 南宁市医疗单位id列表
     *
     * @return
     */
    public List<Long> getNanNingMedicalInsitutionIdList() {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode()).eq(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode()).list();
        return list.stream().map(SysDepartment::getId).collect(Collectors.toList());
    }
    /**
     * 各类型医废收集总量
     *
@@ -155,7 +179,7 @@
        }
        //查询医废类型
        List<SysDictDataVO> medicalWasteTypeList = sysDictDataService.medicalWasteTypeList();
        vo.setWasteTypeList(medicalWasteTypeList);
        //查询南宁市下面的医疗机构
        List<SysDepartment> list = sysDepartmentService.lambdaQuery()
                .likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode())
@@ -183,5 +207,176 @@
        return vo;
    }
    /**
     * 医疗机构收集情况
     *
     * @return
     */
    public List<MedicalInstitutionCollectListVO> medicalInstitutionCollectList() {
        // 获取南宁市医疗机构ID列表
        List<Long> nanNingMedicalInstitutionIdList = getNanNingMedicalInsitutionIdList();
        if (CollUtils.isEmpty(nanNingMedicalInstitutionIdList)) {
            return CollUtils.emptyList();
        }
        // 查询收集记录
        List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery()
                .in(MwCollectRecord::getDepartmentId, nanNingMedicalInstitutionIdList)
                .list();
        if (CollUtils.isEmpty(collectRecordList)) {
            return CollUtils.emptyList();
        }
        // 查询医废类型
        List<SysDictDataVO> medicalWasteTypeList = sysDictDataService.medicalWasteTypeList();
        // 根据医院分组
        Map<String, List<MwCollectRecord>> recordGroupByDepartment = collectRecordList.stream()
                .collect(Collectors.groupingBy(MwCollectRecord::getHospitalName));
        // 构建结果列表
        return recordGroupByDepartment.entrySet().stream()
                .map(entry -> buildMedicalInstitutionCollectVO(entry.getKey(), entry.getValue(), medicalWasteTypeList))
                .collect(Collectors.toList());
    }
    /**
     * 构建医疗机构收集情况视图对象
     *
     * @param hospitalName
     * @param records
     * @param medicalWasteTypeList
     * @return
     */
    private MedicalInstitutionCollectListVO buildMedicalInstitutionCollectVO(String hospitalName, List<MwCollectRecord> records, List<SysDictDataVO> medicalWasteTypeList) {
        // 医院基础信息
        MedicalInstitutionCollectListVO vo = new MedicalInstitutionCollectListVO();
        vo.setHospitalName(hospitalName);
        // 根据医废类型分组
        Map<Long, List<MwCollectRecord>> collectRecordGroupByWasteType = records.stream()
                .collect(Collectors.groupingBy(MwCollectRecord::getWasteType));
        // 构建医废类型统计信息
        List<MedicalWasteCollectVO> medicalWasteList = medicalWasteTypeList.stream()
                .map(sysDictDataVO -> buildMedicalWasteCollectVO(sysDictDataVO, collectRecordGroupByWasteType))
                .collect(Collectors.toList());
        vo.setMedicalWasteList(medicalWasteList);
        return vo;
    }
    /**
     * 封装医废类型统计信息
     *
     * @param sysDictDataVO
     * @param collectRecordGroupByWasteType
     * @return
     */
    private MedicalWasteCollectVO buildMedicalWasteCollectVO(SysDictDataVO sysDictDataVO, Map<Long, List<MwCollectRecord>> collectRecordGroupByWasteType) {
        MedicalWasteCollectVO medicalWasteCollectVO = new MedicalWasteCollectVO();
        medicalWasteCollectVO.setWasteTypeStr(sysDictDataVO.getDictLabel());
        // 获取当前类型的医废记录
        List<MwCollectRecord> mwCollectRecords = collectRecordGroupByWasteType.getOrDefault(sysDictDataVO.getDictCode(), CollUtils.emptyList());
        medicalWasteCollectVO.setCssClass(sysDictDataVO.getCssClass());
        medicalWasteCollectVO.setBagNum(mwCollectRecords.size());
        medicalWasteCollectVO.setWeight(mwCollectRecords.stream()
                .map(MwCollectRecord::getWeight)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        return medicalWasteCollectVO;
    }
    /**
     * 预警记录统计
     *
     * @return
     */
    public List<MwWarningRecordStaticsVO> warningRecordStaticsList() {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        //查询南宁的所有单位
        List<SysDepartment> list = sysDepartmentService.lambdaQuery()
                .likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode())
                .in(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode()).list();
        if (CollUtils.isEmpty(list)) {
            return CollUtils.emptyList();
        }
        List<Long> departmentIds = list.stream().map(SysDepartment::getId).collect(Collectors.toList());
        List<MwWarningRecord> warningRecordList = mwWarningRecordService.lambdaQuery().in(MwWarningRecord::getDepartmentId, departmentIds).list();
        if (CollUtils.isEmpty(warningRecordList)) {
            return CollUtils.emptyList();
        }
        return Arrays.stream(WarningTypeEnum.values()).map(item -> buildWarningRecordStaticsVO(item, warningRecordList)).collect(Collectors.toList());
    }
    /**
     * 构建预警记录统计视图对象
     *
     * @param item
     * @param warningRecordList
     * @return
     */
    private MwWarningRecordStaticsVO buildWarningRecordStaticsVO(WarningTypeEnum item, List<MwWarningRecord> warningRecordList) {
        MwWarningRecordStaticsVO vo = new MwWarningRecordStaticsVO();
        vo.setType(item.getCode());
        vo.setTypeName(item.getDesc());
        vo.setCount(warningRecordList.stream().filter(record -> record.getType().equals(item.getCode())).count());
        return vo;
    }
    /**
     * 根据预警类型查询预警详情
     *
     * @param type
     * @return
     */
    public List<MwWarningRecordVO> queryWarningRecordDetailByType(Integer type) {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        //查询南宁市所有单位信息
        List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, nanNingDepartment.getTreeCode()).list();
        if (CollUtils.isEmpty(list)) {
            return CollUtils.emptyList();
        }
        List<Long> departmentIds = list.stream().map(SysDepartment::getId).collect(Collectors.toList());
        List<MwWarningRecord> warningRecordList = mwWarningRecordService.lambdaQuery().in(MwWarningRecord::getDepartmentId, departmentIds).eq(MwWarningRecord::getType, type).list();
        return BeanUtils.copyToList(warningRecordList, MwWarningRecordVO.class);
    }
    /**
     * 车辆分布
     *
     * @return
     */
    public List<CarDistributionVO> queryCarDistribution() {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        //查询车辆分布列表
        List<CarDistributionVO> carDistributionVOS = mwTransitCarCollectPointService.queryCarListByTreeCode(nanNingDepartment.getTreeCode());
        if (CollUtils.isNotEmpty(carDistributionVOS)) {
            for (CarDistributionVO carDistributionVO : carDistributionVOS) {
                MwTransitCarCollectPoint point = mwTransitCarCollectPointService.lambdaQuery().eq(MwTransitCarCollectPoint::getCarId, carDistributionVO.getId()).eq(MwTransitCarCollectPoint::getTransitDate, DateUtil.format(DateUtil.date(), DatePattern.NORM_DATE_PATTERN)).last("LIMIT 1").one();
                if (Objects.nonNull(point)) {
                    String pointList = point.getPointList();
                    List<Long> pointIdList = JSONArray.parseArray(pointList, Long.class);
                    if (CollUtils.isNotEmpty(pointIdList)) {
                        //查询最后一个点位
                        SysDepartment department = sysDepartmentService.getById(pointIdList.get(pointIdList.size() - 1));
                        carDistributionVO.setLatitude(department.getLatitude());
                        carDistributionVO.setLongitude(department.getLongitude());
                    }
                }
            }
        }
        return carDistributionVOS;
    }
    /**
     * 车辆分布详情
     *
     * @param id
     * @return
     */
    public CarDistributionVO queryCarDistributionDetail(Long id) {
        return mwTransitCarCollectPointService.queryCarDistributionDetail(id);
    }
}