mitao
2024-12-17 ab7b8b5e702989d894620c41fbaa0e2d6904ecd5
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
package com.sinata.system.service.impl;
 
import cn.idev.excel.EasyExcel;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sinata.common.core.domain.entity.SysDictData;
import com.sinata.common.entity.PageDTO;
import com.sinata.common.utils.CollUtils;
import com.sinata.common.utils.DateUtils;
import com.sinata.common.utils.SecurityUtils;
import com.sinata.common.utils.StringUtils;
import com.sinata.system.domain.MwCollectRecord;
import com.sinata.system.domain.MwMicroEquipmentRecord;
import com.sinata.system.domain.MwMicroEquipmentRecordItem;
import com.sinata.system.domain.dto.MwMicroEquipmentRecordDTO;
import com.sinata.system.domain.query.MwMicroEquipmentRecordQuery;
import com.sinata.system.domain.query.MwMicroEquipmentStaticsQuery;
import com.sinata.system.domain.vo.MwCollectRecordVO;
import com.sinata.system.domain.vo.MwMicroEquipmentRecordVO;
import com.sinata.system.domain.vo.MwMicroEquipmentStaticsTitleVO;
import com.sinata.system.domain.vo.MwMicroEquipmentStaticsVO;
import com.sinata.system.enums.MedicalWasteStatusEnum;
import com.sinata.system.mapper.MwMicroEquipmentRecordMapper;
import com.sinata.system.service.ISysDictDataService;
import com.sinata.system.service.MwCollectRecordService;
import com.sinata.system.service.MwMicroEquipmentRecordItemService;
import com.sinata.system.service.MwMicroEquipmentRecordService;
import com.sinata.system.service.SysDepartmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 设备使用记录 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class MwMicroEquipmentRecordServiceImpl extends ServiceImpl<MwMicroEquipmentRecordMapper, MwMicroEquipmentRecord> implements MwMicroEquipmentRecordService {
    private final SysDepartmentService sysDepartmentService;
    private final MwCollectRecordService mwCollectRecordService;
    private final MwMicroEquipmentRecordItemService mwMicroEquipmentRecordItemService;
    private final ISysDictDataService sysDictDataService;
 
    /**
     * 设备使用记录分页列表
     *
     * @param query
     * @return
     */
    @Override
    public PageDTO<MwMicroEquipmentRecordVO> pageList(MwMicroEquipmentRecordQuery query) {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return PageDTO.empty(0L, 0L);
        }
        return PageDTO.of(baseMapper.pageList(new Page<>(query.getPageCurr(), query.getPageSize()), query, treeCode));
    }
 
    /**
     * 导出设备使用记录
     *
     * @param query
     * @param response
     */
    @Override
    public void export(MwMicroEquipmentRecordQuery query, HttpServletResponse response) throws IOException {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        if (StringUtils.isBlank(treeCode)) {
            return;
        }
        List<MwMicroEquipmentRecordVO> list = baseMapper.getList(query, treeCode);
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("微波设备使用记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MwCollectRecordVO.class).sheet("微波设备使用记录").doWrite(list);
    }
 
    /**
     * 新增使用记录
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(MwMicroEquipmentRecordDTO dto) {
        List<MwCollectRecord> collectRecordList = mwCollectRecordService.lambdaQuery().in(MwCollectRecord::getBoxId, dto.getIds())
                .eq(MwCollectRecord::getStatus, MedicalWasteStatusEnum.TEMPORARILY_STORED.getCode()).list();
        if (CollUtils.isNotEmpty(collectRecordList)) {
            MwMicroEquipmentRecord record = new MwMicroEquipmentRecord();
            record.setEquipmentId(dto.getEquipmentId());
            record.setOperatorId(SecurityUtils.getUserId());
            record.setRemark(dto.getRemark());
            record.setUseTime(new Date());
            record.setBoxNum(dto.getIds().size());
            record.setBagNum(collectRecordList.size());
            record.setTotalWeight(collectRecordList.stream().map(MwCollectRecord::getWeight).reduce(BigDecimal.ZERO, BigDecimal::add));
            save(record);
            List<MwMicroEquipmentRecordItem> itemList = new ArrayList<>();
            collectRecordList.forEach(collectRecord -> {
                //封装设备使用记录项
                MwMicroEquipmentRecordItem item = new MwMicroEquipmentRecordItem();
                item.setEquipmentRecordId(record.getId());
                item.setCollectRecordId(collectRecord.getId());
                itemList.add(item);
                //修改状态
                collectRecord.setStatus(MedicalWasteStatusEnum.DISPOSED.getCode());
            });
            //更新状态为已处置
            mwCollectRecordService.updateBatchById(collectRecordList);
            //批量新增设备使用记录项
            mwMicroEquipmentRecordItemService.saveBatch(itemList);
        }
    }
 
    /**
     * 处置分析表头
     *
     * @return
     */
    @Override
    public List<MwMicroEquipmentStaticsTitleVO> staticsTitle() {
        List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list();
 
        List<MwMicroEquipmentStaticsTitleVO> titles = new ArrayList<>();
        medicalWasteTypeList.forEach(item -> {
            MwMicroEquipmentStaticsTitleVO vo = new MwMicroEquipmentStaticsTitleVO();
            vo.setTitle(item.getDictLabel());
            MwMicroEquipmentStaticsTitleVO child1 = new MwMicroEquipmentStaticsTitleVO();
            child1.setTitle("总重量(KG)");
            MwMicroEquipmentStaticsTitleVO child2 = new MwMicroEquipmentStaticsTitleVO();
            child2.setTitle("总数量");
            vo.setChildren(Lists.newArrayList(child1, child2));
            titles.add(vo);
        });
 
        MwMicroEquipmentStaticsTitleVO total = new MwMicroEquipmentStaticsTitleVO();
        total.setTitle("小计");
        MwMicroEquipmentStaticsTitleVO child1 = new MwMicroEquipmentStaticsTitleVO();
        child1.setTitle("总重量(KG)");
        MwMicroEquipmentStaticsTitleVO child2 = new MwMicroEquipmentStaticsTitleVO();
        child2.setTitle("总数量");
        total.setChildren(Lists.newArrayList(child1, child2));
        titles.add(total);
        return titles;
    }
 
    /**
     * 处置分析数据
     *
     * @param query
     * @return
     */
    @Override
    public List<List<String>> getStaticsData(MwMicroEquipmentStaticsQuery query) {
        String treeCode = sysDepartmentService.getTreeCodeByDepartmentId(query.getDepartmentId());
        List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list();
        List<MwMicroEquipmentStaticsVO> staticsData = baseMapper.getStaticsData(query, treeCode);
        SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
        switch (query.getDateType()) {
            case 1:
                sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
            case 2:
                sdf = new SimpleDateFormat(DateUtils.YYYY_MM);
            case 3:
                sdf = new SimpleDateFormat(DateUtils.YYYY);
        }
        List<String> dateList = DateUtils.getDayBetween(query.getStartTime(), query.getEndTime(), query.getDateType());
        List<List<String>> result = new ArrayList<>();
        if (CollUtils.isNotEmpty(staticsData)) {
            for (String date : dateList) {
                List<String> list = new ArrayList<>();
                list.add(date);
                SimpleDateFormat finalSdf = sdf;
                for (SysDictData sysDictData : medicalWasteTypeList) {
                    BigDecimal weight = staticsData.stream().filter(e -> e.getWasteType().equals(sysDictData.getDictCode())
                            && finalSdf.format(e.getUseTime()).equals(date)
                    ).map(MwMicroEquipmentStaticsVO::getWeight).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).setScale(2, RoundingMode.HALF_UP);
                    long count = staticsData.stream().filter(e -> e.getWasteType().equals(sysDictData.getDictCode())
                            && finalSdf.format(e.getUseTime()).equals(date)).count();
                    list.add(weight.toString());
                    list.add(String.valueOf(count));
                }
                staticsData.stream().filter(e -> finalSdf.format(e.getUseTime()).equals(date)).findFirst().ifPresent(item -> {
                    list.add(item.getTotalWeight().toString());
                    list.add(String.valueOf(item.getBagNum()));
                });
                result.add(list);
            }
        }
        return result;
    }
 
    @Override
    public void staticsExport(MwMicroEquipmentStaticsQuery query, HttpServletResponse response) throws IOException {
 
        List<List<String>> head = head();
        System.out.println(JSONObject.toJSONString(head));
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("处置分析报表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        // 这里需要设置不关闭流
        EasyExcel.write(response.getOutputStream())
                .head(head)
                .autoCloseStream(Boolean.TRUE)
                .sheet("处置分析报表")
                .doWrite(getStaticsData(query));
    }
 
    private List<List<String>> head() {
        List<SysDictData> medicalWasteTypeList = sysDictDataService.lambdaQuery().eq(SysDictData::getDictType, "medical_waste_type").list();
        List<List<String>> headTitles = Lists.newArrayList();
        headTitles.add(Lists.newArrayList("日期"));
        medicalWasteTypeList.forEach(item -> {
            headTitles.add(Lists.newArrayList(item.getDictLabel(), "总重量(KG)"));
            headTitles.add(Lists.newArrayList(item.getDictLabel(), "总数量"));
        });
        headTitles.add(Lists.newArrayList("小计", "总重量(KG)"));
        headTitles.add(Lists.newArrayList("小计", "总数量"));
        return headTitles;
    }
}