医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package cn.besteam.modules.medical.service.impl;
 
import cn.besteam.common.constant.DepartmentEnum;
import cn.besteam.common.constant.ErrorCode;
import cn.besteam.common.exception.BusinessException;
import cn.besteam.common.util.DateUtil;
import cn.besteam.common.util.RedisUtil;
import cn.besteam.common.util.oConvertUtils;
import cn.besteam.modules.medical.dto.MwWasteWaterReportDto;
import cn.besteam.modules.medical.dto.MwWasteWaterSeacherDto;
import cn.besteam.modules.medical.entity.MwDetectionIndex;
import cn.besteam.modules.medical.entity.MwEquipment;
import cn.besteam.modules.medical.entity.MwWasteWater;
import cn.besteam.modules.medical.mapper.MwWasteWaterMapper;
import cn.besteam.modules.medical.service.IMwDetectionIndexService;
import cn.besteam.modules.medical.service.IMwEquipmentService;
import cn.besteam.modules.medical.service.IMwWasteWaterService;
import cn.besteam.modules.medical.vo.DetectionIndexVo;
import cn.besteam.modules.medical.vo.MwCollectRecordVO;
import cn.besteam.modules.medical.vo.WasteWaterVo;
import cn.besteam.modules.report.dto.ReportDTO;
import cn.besteam.modules.report.vo.*;
import cn.besteam.modules.shiro.authc.util.EnvUtils;
import cn.besteam.modules.system.entity.SysDepartment;
import cn.besteam.modules.system.entity.SysDictItem;
import cn.besteam.modules.system.service.ISysDepartmentService;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * @Description: 废水
 * @author: besteam
 * @date:   2022-09-04
 * @version: V1.0
 */
@Service
public class MwWasteWaterServiceImpl extends ServiceImpl<MwWasteWaterMapper, MwWasteWater> implements IMwWasteWaterService {
 
 
 
    @Autowired
    private ISysDepartmentService sysDepartmentService;
 
    @Autowired
    private IMwEquipmentService mwEquipmentService;
 
    @Autowired
    private IMwDetectionIndexService detectionIndexService;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Override
    public WasteWaterReportVo getWasteWaterReportList(MwWasteWaterReportDto dto) {
        WasteWaterReportVo vo = new WasteWaterReportVo();
        List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
        if (CollectionUtils.isEmpty(hospitalIdList)) {
            return vo;
        }
        dto.setHospitalIdList(hospitalIdList);
        List<MwDetectionIndex> wasteTypeList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().eq(MwDetectionIndex::getDelFlag,0));
        if (CollectionUtils.isEmpty(wasteTypeList)) {
            return vo;
        }
        vo.setLegend(wasteTypeList.stream().map(MwDetectionIndex::getName).collect(Collectors.toList()));
        List<WasteWateItemVo> list = this.baseMapper.getTime(dto);
        for(WasteWateItemVo wasteWateItemVo:list){
            wasteWateItemVo.setData(new ArrayList<>());
            for (MwDetectionIndex sysDictItem : wasteTypeList) {
                dto.setTime(wasteWateItemVo.getName());
                dto.setCode(sysDictItem.getCode());
                WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getList(dto);
                if(dto.getNumType()==1){
                    wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
                }else if(dto.getNumType()==2){
                    wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
                }else{
                    wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
                }
 
            }
        }
        vo.setList(list);
        return vo;
    }
 
    @Override
    public WasteWaterReportVo getWasteWaterList(MwWasteWaterReportDto dto) {
        WasteWaterReportVo vo = new WasteWaterReportVo();
 
        List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
        if (CollectionUtils.isEmpty(hospitalIdList)) {
            return vo;
        }
        dto.setHospitalIdList(hospitalIdList);
        List<MwDetectionIndex> wasteTypeList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().eq(MwDetectionIndex::getDelFlag,0));
        if (CollectionUtils.isEmpty(wasteTypeList)) {
            return vo;
        }
        vo.setLegend(wasteTypeList.stream().map(MwDetectionIndex::getName).collect(Collectors.toList()));
 
        List<WasteWateItemVo> list = this.baseMapper.getTime(dto);
        for(WasteWateItemVo wasteWateItemVo:list){
            wasteWateItemVo.setData(new ArrayList<>());
            for (MwDetectionIndex sysDictItem : wasteTypeList) {
                dto.setTime(wasteWateItemVo.getName());
                dto.setCode(sysDictItem.getCode());
                WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getList(dto);
                wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
                wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
                wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
                wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
 
            }
        }
        WasteWateItemVo wasteWateItemVo = new WasteWateItemVo();
        wasteWateItemVo.setName("最小值");
        wasteWateItemVo.setData(new ArrayList<>());
        for (MwDetectionIndex sysDictItem : wasteTypeList) {
            dto.setCode(sysDictItem.getCode());
            WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getMINList(dto);
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
 
        }
        list.add(wasteWateItemVo);
        wasteWateItemVo = new WasteWateItemVo();
        wasteWateItemVo.setName("最大值");
        wasteWateItemVo.setData(new ArrayList<>());
        for (MwDetectionIndex sysDictItem : wasteTypeList) {
            dto.setCode(sysDictItem.getCode());
            WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getMAXList(dto);
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
 
        }
        list.add(wasteWateItemVo);
        wasteWateItemVo = new WasteWateItemVo();
        wasteWateItemVo.setName("平均值");
        wasteWateItemVo.setData(new ArrayList<>());
        for (MwDetectionIndex sysDictItem : wasteTypeList) {
            dto.setCode(sysDictItem.getCode());
            WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getAVGList(dto);
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
 
        }
        list.add(wasteWateItemVo);
        wasteWateItemVo = new WasteWateItemVo();
        wasteWateItemVo.setName("累计值");
        wasteWateItemVo.setData(new ArrayList<>());
        for (MwDetectionIndex sysDictItem : wasteTypeList) {
            dto.setCode(sysDictItem.getCode());
            WasteWaterStsticVo wasteWaterStsticVo = this.baseMapper.getSUMList(dto);
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMinNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getAvgNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getMaxNum().setScale(2,RoundingMode.HALF_UP));
            wasteWateItemVo.getData().add(wasteWaterStsticVo.getTotalNum().setScale(2,RoundingMode.HALF_UP));
 
        }
        list.add(wasteWateItemVo);
        vo.setList(list);
        return vo;
    }
 
    @Override
    public IPage<WasteWaterVo> queryPageList(Integer pageNo, Integer pageSize, MwWasteWaterSeacherDto dto) {
 
        Page<WasteWaterVo> page = new Page<>(pageNo, pageSize);
        List<Integer> hospitalIdList = getSearchHospitalIdList(dto.getHospitalId());
        if (CollectionUtils.isEmpty(hospitalIdList)) {
            return page;
        }
        dto.setHospitalIdList(hospitalIdList);
        page =this.baseMapper.queryPageList(page,dto);
        List<WasteWaterVo> list = page.getRecords();
        for(WasteWaterVo wasteWaterVo:list){
            String[] ids = wasteWaterVo.getDetectionIndexId().toString().split(",");
            List<Integer> idList = new ArrayList<>();
            for(String id :ids){
                idList.add(Integer.parseInt(id));
            }
            List<MwDetectionIndex> detectionIndexList = detectionIndexService.list(new QueryWrapper<MwDetectionIndex>().lambda().in(MwDetectionIndex::getId,idList));
            List<DetectionIndexVo> detectionIndexVoList = new ArrayList<>();
            for(MwDetectionIndex mwDetectionIndex:detectionIndexList){
                DetectionIndexVo detectionIndexVo = new DetectionIndexVo();
                BeanUtils.copyProperties(mwDetectionIndex, detectionIndexVo);
                List<MwWasteWater> wasteWaterList = this.baseMapper.selectList(new QueryWrapper<MwWasteWater>().lambda().eq(MwWasteWater::getEquipmentnumber,wasteWaterVo.getCode()).eq(MwWasteWater::getCode,detectionIndexVo.getCode()).orderByDesc(MwWasteWater::getCreateTime));
                if(wasteWaterList!=null && wasteWaterList.size()>0){
                    MwWasteWater wasteWater = wasteWaterList.get(0);
                    if(Double.parseDouble(wasteWater.getAvgnum())>=Double.parseDouble(mwDetectionIndex.getMinNum()) && Double.parseDouble(wasteWater.getAvgnum())<=Double.parseDouble(mwDetectionIndex.getMaxNum())){
                        detectionIndexVo.setStatus(1);
                    }else{
                        detectionIndexVo.setStatus(2);
                    }
                    detectionIndexVo.setMwWasteWater(wasteWater);
                }
                detectionIndexVoList.add(detectionIndexVo);
            }
            wasteWaterVo.setDetectionIndexVoList(detectionIndexVoList);
        }
        page.setRecords(list);
        return page;
    }
 
    private List<Integer> getSearchHospitalIdList(Integer hospitalId) {
 
        if (hospitalId == null) {
            return sysDepartmentService.listMyHospital(EnvUtils.getUser().getId()).stream().map(SysDepartment::getId).collect(Collectors.toList());
        } else {
            SysDepartment sysDepartment = sysDepartmentService.getById(hospitalId);
            if (sysDepartment == null) {
                return new ArrayList<>();
            }
            List<SysDepartment> childrenList = sysDepartmentService.list(new QueryWrapper<SysDepartment>().lambda().likeRight(SysDepartment::getTreeCode, sysDepartment.getTreeCode()).eq(SysDepartment::getOrgType, DepartmentEnum.OrgTypeEnum.HOSPITAL.getType()));
            if (CollectionUtils.isEmpty(childrenList)) {
                return new ArrayList<>();
            }
            return childrenList.stream().map(SysDepartment::getId).collect(Collectors.toList());
        }
    }
 
    @Override
    public void exportReport(HttpServletResponse response, MwWasteWaterReportDto dto) throws Exception {
        String fileName = "";
        ExcelWriter writer = null;
        fileName = "汇总报表";
        writer = new ExcelWriter(exportDepartmentReport(dto, fileName), fileName);
        if (writer != null) {
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
            writer.flush(response.getOutputStream(), true);
            writer.close();
        }
    }
 
 
    private XSSFWorkbook exportDepartmentReport(MwWasteWaterReportDto dto, String sheetName)  throws Exception {
 
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WasteWaterReportVo vo = null;
 
        XSSFWorkbook workbook = new XSSFWorkbook();
        String cellName = "", title = "";
        vo = getWasteWaterList(dto);
        SysDepartment hospital = sysDepartmentService.getById(dto.getHospitalId());
        if (hospital == null) {
            throw new BusinessException(ErrorCode.ERR_500, "医院不存在");
        }
        cellName = "时间";
        title = hospital.getDepartName() + "废水汇总报表";
        XSSFSheet sheet = workbook.createSheet(sheetName);
        XSSFCellStyle cellStyle = createDefaultCellStyle(workbook);
        XSSFCellStyle rightCellStyle = createDefaultRightCellStyle(workbook);
        //
        int maxCellSize = 1 + vo.getLegend().size() * 4;
        XSSFRow titleRow = sheet.createRow(0);
        XSSFRow dateRow = sheet.createRow(1);
        XSSFRow firstRow = sheet.createRow(2);
        XSSFRow secondRow = sheet.createRow(3);
        int legendIndex = 0;
        for (int i = 0; i < maxCellSize; i++) {
            XSSFCell titleRowCell = titleRow.createCell(i);
            titleRowCell.setCellStyle(cellStyle);
            XSSFCell firstRowCell = firstRow.createCell(i);
            firstRowCell.setCellStyle(cellStyle);
            XSSFCell secondRowCell = secondRow.createCell(i);
            secondRowCell.setCellStyle(cellStyle);
            XSSFCell dateRowCell = dateRow.createCell(i);
            dateRowCell.setCellStyle(rightCellStyle);
            if (i == 0) {
                titleRowCell.setCellValue(title);
                firstRowCell.setCellValue(cellName);
            } else {
                if (i % 4 == 1) {
                    firstRowCell.setCellValue(vo.getLegend().get(legendIndex));
                    legendIndex++;
                    secondRowCell.setCellValue("最小值(mg/l)");
                } else if (i % 4 == 2) {
                    secondRowCell.setCellValue("平均值(mg/l)");
                } else if (i % 4 == 3) {
                    secondRowCell.setCellValue("最大值(mg/l)");
                } else {
                    secondRowCell.setCellValue("累计值(mg/l)");
                }
            }
            if (i == maxCellSize - 1) {
                dateRowCell.setCellValue("时间:" + DateUtil.format(df.parse(dto.getStartTime()), "yyyy年MM月dd日") + "至" + DateUtil.format(df.parse(dto.getEndTime()), "yyyy年MM月dd日"));
            }
        }
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxCellSize - 1));
        sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
        for (int i = 0; i < vo.getLegend().size(); i++) {
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 1 + i * 2, 2 + i * 2));
        }
        //
        if (CollectionUtils.isNotEmpty(vo.getList())) {
            for (int i = 0; i < vo.getList().size(); i++) {
                WasteWateItemVo itemVO = vo.getList().get(i);
                XSSFRow dataRow = sheet.createRow(i + 4);
                int dataIndex = 0;
                for (int k = 0; k < maxCellSize; k++) {
                    XSSFCell dataRowCell = dataRow.createCell(k);
                    dataRowCell.setCellStyle(cellStyle);
                    if (k == 0) {
                        dataRowCell.setCellValue(itemVO.getName());
                    } else {
                        if(itemVO.getName().equals("最小值")){
                            if(k%4==0){
                                dataRowCell.setCellValue("-");
                                dataIndex++;
                            }else{
                                dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
                                dataIndex++;
                            }
                        }else if(itemVO.getName().equals("最大值")){
                            if(k%4==0){
                                dataRowCell.setCellValue("-");
                                dataIndex++;
                            }else{
                                dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
                                dataIndex++;
                            }
                        }else if(itemVO.getName().equals("平均值")){
                            if(k%4==0){
                                dataRowCell.setCellValue("-");
                                dataIndex++;
                            }else{
                                dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
                                dataIndex++;
                            }
                        }else if(itemVO.getName().equals("累计值")){
                            if(k%4==0){
                                dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
                                dataIndex++;
                            }else{
                                dataRowCell.setCellValue("-");
                                dataIndex++;
                            }
                        }else {
                            dataRowCell.setCellValue(itemVO.getData().get(dataIndex).toString());
                            dataIndex++;
                        }
 
                    }
                }
            }
        }
        sheet.setColumnWidth(0, 6065);
 
        return workbook;
    }
 
    private XSSFCellStyle createDefaultCellStyle(XSSFWorkbook workbook) {
 
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);
        return cellStyle;
    }
 
    private XSSFCellStyle createDefaultLeftCellStyle(XSSFWorkbook workbook) {
 
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.LEFT);
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);
        return cellStyle;
    }
 
    private XSSFCellStyle createDefaultRightCellStyle(XSSFWorkbook workbook) {
 
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        XSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);
        cellStyle.setFont(font);
        return cellStyle;
    }
 
}