luoyisheng
2025-02-12 05628d269dff7ad4f2e9d3419b05b4e7e5768797
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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;
import org.springframework.stereotype.Service;
 
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;
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;
    private final MwDisposalRecordService mwDisposalRecordService;
    private final MwWarningRecordService mwWarningRecordService;
    private final MwTransitCarCollectPointService mwTransitCarCollectPointService;
 
    /**
     * 机构分布-获取机构列表
     *
     * @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);
    }
 
    /**
     * 获取大屏单位标签信息
     *
     * @param id
     * @return
     */
    public DepartmentTagInfoVO getTagInfo(Long id) {
        SysDepartment department = sysDepartmentService.getById(id);
        DepartmentTagInfoVO vo = new DepartmentTagInfoVO();
        if (Objects.isNull(department)) {
            throw new ServiceException("单位不存在");
        }
        if (department.getOrgType().equals(DepartmentEnum.MEDICAL_INSTITUTION.getCode())) {
            vo = mwCollectRecordService.getTagInfo(id);
        } else {
            vo = mwDisposalRecordService.getTagInfo(id);
        }
        return vo;
    }
    /**
     * 预警数据统计
     *
     * @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() {
        List<TodayMedicalWastePieVO> todayMedicalWastePieVOList = new ArrayList<>();
        List<Long> departmentIdList = getNanNingMedicalInsitutionIdList();
        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();
        //查询医废类型列表
        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(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());
    }
    /**
     * 各类型医废收集总量
     *
     * @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();
        vo.setWasteTypeList(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;
    }
 
    /**
     * 医疗机构收集情况
     *
     * @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);
    }
}