luodangjia
2025-01-02 fc5cda9c324a91948dd964e91960623b41baf293
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.sinata.system.service.biz;
 
import cn.hutool.core.date.DateUtil;
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.SysDepartment;
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.service.ISysDictDataService;
import com.sinata.system.service.MwCollectRecordService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * @author mitao
 * @date 2025/1/1
 */
@Service
@RequiredArgsConstructor
public class ScreenService {
    private final SysDepartmentService sysDepartmentService;
    private final MwCollectRecordService mwCollectRecordService;
    private final ISysDictDataService sysDictDataService;
 
    /**
     * 机构分布-获取机构列表
     *
     * @return
     */
    public List<ScreenDepartmentVO> departmentList() {
        SysDepartment department = getNanNingDepartment();
        List<SysDepartment> list = sysDepartmentService.lambdaQuery().likeRight(SysDepartment::getTreeCode, department.getTreeCode()).in(SysDepartment::getOrgType, DepartmentEnum.MEDICAL_INSTITUTION.getCode(), DepartmentEnum.DISPOSAL_UNIT.getCode()).list();
        return BeanUtils.copyToList(list, ScreenDepartmentVO.class);
    }
 
    /**
     * 预警数据统计
     *
     * @return
     */
    public MedicalWasteStaticsVO medicalWasteStatics() {
        SysDepartment department = getNanNingDepartment();
        return mwCollectRecordService.queryMedicalWasteStatics(department.getTreeCode());
    }
 
    /**
     * 查询南宁市
     *
     * @return
     */
    @NotNull
    private SysDepartment getNanNingDepartment() {
        SysDepartment department = sysDepartmentService.lambdaQuery().eq(SysDepartment::getDepartmentName, "南宁市").one();
        if (Objects.isNull(department)) {
            throw new ServiceException("南宁市不存在!");
        }
        return department;
    }
 
    /**
     * 今日医废类型统计占比
     *
     * @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<>();
        if (CollUtils.isEmpty(departmentIdList)) {
            return todayMedicalWastePieVOList;
        }
        //查询收集记录
        List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery()
                .in(MwCollectRecord::getDepartmentId, departmentIdList).list();
        if (CollUtils.isEmpty(collectRecordList)) {
            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 -> {
            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)));
            return todayMedicalWastePieVO;
        }).collect(Collectors.toList());
 
        return todayMedicalWastePieVOList;
    }
 
    /**
     * 各类型医废收集总量
     *
     * @param type
     * @return
     */
    public TotalCollectWeightByTypeVO totalCollectWeightByType(Integer type) {
        SysDepartment nanNingDepartment = getNanNingDepartment();
        TotalCollectWeightByTypeVO vo = new TotalCollectWeightByTypeVO();
        Date startTime = DateUtils.getNowDate();
        Date endTime = DateUtils.getNowDate();
        if (type.equals(1)) {
            //获取最本周的日期
            vo.setDateList(DateUtils.getAllDatesOfCurrentWeek("MM-dd"));
            startTime = DateUtils.getFirstDayOfCurrentWeekAtMidnight();
        } else {
            //最近一个月的日期
            vo.setDateList(DateUtils.getAllDatesOfCurrentMonth("MM-dd"));
            startTime = DateUtils.getFirstDayOfCurrentMonthAtMidnight();
        }
        //查询医废类型
        List<SysDictDataVO> medicalWasteTypeList = sysDictDataService.medicalWasteTypeList();
 
        //查询南宁市下面的医疗机构
        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());
        if (CollUtils.isEmpty(departmentIdList)) {
            return vo;
        }
        //查询医废收集记录
        List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery()
                .in(MwCollectRecord::getDepartmentId, departmentIdList).between(MwCollectRecord::getCollectTime, startTime, endTime).list();
        if (CollUtils.isEmpty(collectRecordList)) {
            return vo;
        }
        //封装数据
        for (SysDictDataVO sysDictDataVO : medicalWasteTypeList) {
            List<BigDecimal> totalCollectWeightList = new ArrayList<>();
            for (String date : vo.getDateList()) {
                BigDecimal totalWeight = collectRecordList.stream().filter(record -> DateUtil.format(record.getCollectTime(), "MM-dd").equals(date)
                        && record.getWasteType().equals(sysDictDataVO.getDictCode())).map(MwCollectRecord::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
                totalCollectWeightList.add(totalWeight);
            }
            vo.getTotalCollectWeightList().add(totalCollectWeightList);
        }
        return vo;
    }
}