医院废弃物信息化管理平台
“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
package cn.besteam.modules.medical.service.impl;
 
import cn.besteam.common.constant.CommonConstant;
import cn.besteam.common.constant.ErrorCode;
import cn.besteam.common.constant.MedicalWasteEnum;
import cn.besteam.common.exception.BusinessException;
import cn.besteam.common.util.DateUtil;
import cn.besteam.modules.medical.dto.ApiMedicalBoxingDTO;
import cn.besteam.modules.medical.entity.MwBox;
import cn.besteam.modules.medical.entity.MwBoxItem;
import cn.besteam.modules.medical.entity.MwCollectRecord;
import cn.besteam.modules.medical.mapper.MwBoxMapper;
import cn.besteam.modules.medical.service.IMwBoxItemService;
import cn.besteam.modules.medical.service.IMwBoxService;
import cn.besteam.modules.medical.service.IMwCollectRecordService;
import cn.besteam.modules.medical.vo.ApiMedicalBoxingDetailsVO;
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.besteam.modules.system.service.ISysDictItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
import static java.util.stream.Collectors.groupingBy;
 
/**
 * @Description: 转运箱
 * @author: besteam
 * @date: 2020-04-25
 * @version: V1.0
 */
@Service
@Slf4j
public class MwBoxServiceImpl extends ServiceImpl<MwBoxMapper, MwBox> implements IMwBoxService {
 
    @Autowired
    private IMwCollectRecordService mwCollectRecordService;
 
    @Autowired
    private ISysDepartmentService sysDepartmentService;
 
    @Autowired
    private IMwBoxItemService mwBoxItemService;
 
    @Autowired
    private IMwCollectRecordService iMwCollectRecordService;
 
    @Autowired
    private ISysDictItemService sysDictItemService;
 
    /**
     * 自动装箱
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean autoBoxing(ApiMedicalBoxingDTO dto) {
 
        if (dto.getWarehouseId() == null || dto.getWarehouseId() <= 0) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间ID不能为空");
        }
        SysDepartment warehouse = sysDepartmentService.getById(dto.getWarehouseId());
        if (warehouse == null) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        if (warehouse.getIsWarehouse().equals(0)) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        //根据暂存间ID获取未装箱清单列表
        List<MwCollectRecord> mwCollectRecordList = mwCollectRecordService.getUnBoxingList(dto.getWarehouseId(), EnvUtils.getUser().getId());
        if (CollectionUtils.isNotEmpty(mwCollectRecordList)) {
            //获取收集表ID列表
            Set<Integer> mwCollectRecordIdSet = mwCollectRecordList.stream().map(MwCollectRecord::getId).collect(Collectors.toSet());
            //获取转运箱存在收集ID
            int count = mwBoxItemService.count(new QueryWrapper<MwBoxItem>().lambda().in(MwBoxItem::getCollectRecordId, mwCollectRecordIdSet));
            if (count > 0) {
                throw new BusinessException(ErrorCode.ERR_500, "医废已放入转运箱,请重新选择");
            }
            //集合分组
            Map<Integer, List<MwCollectRecord>> collect = mwCollectRecordList.stream().collect(groupingBy(MwCollectRecord::getWasteType));
            log.info("集合分组:" + collect);
            for (Map.Entry<Integer, List<MwCollectRecord>> entry : collect.entrySet()) {
                List<MwCollectRecord> collectRecordList = entry.getValue();
                List<MwCollectRecord> toBoxingCollectRecordList = new ArrayList<>();
                for (MwCollectRecord mwCollectRecord : collectRecordList) {
                    toBoxingCollectRecordList.add(mwCollectRecord);
                    if (toBoxingCollectRecordList.size() == 10) {
                        dealAutoBoxing(toBoxingCollectRecordList);
                        toBoxingCollectRecordList.clear();
                    }
                }
                if (CollectionUtils.isNotEmpty(toBoxingCollectRecordList)) {
                    dealAutoBoxing(toBoxingCollectRecordList);
                    toBoxingCollectRecordList.clear();
                }
            }
        }
        return true;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public boolean dealAutoBoxing(List<MwCollectRecord> toBoxingCollectRecordList) {
 
        SysDepartment hospital = sysDepartmentService.getById(toBoxingCollectRecordList.get(0).getHospitalId());
        MwBox mwBox = new MwBox();
        mwBox.setHospitalId(toBoxingCollectRecordList.get(0).getHospitalId());
        //获取装运箱编码
        mwBox.setCode(generateCode(mwBox, hospital.getOrgCode(), toBoxingCollectRecordList.get(0).getWasteType()));
        save(mwBox);
        List<MwBoxItem> mwBoxItemList = new ArrayList<>();
        toBoxingCollectRecordList.forEach(collectRecordId -> {
            MwBoxItem mwBoxItem = new MwBoxItem();
            mwBoxItem.setBoxId(mwBox.getId());
            mwBoxItem.setCollectRecordId(collectRecordId.getId());
            mwBoxItemList.add(mwBoxItem);
        });
        //批量新增
        mwBoxItemService.saveBatch(mwBoxItemList);
        toBoxingCollectRecordList.forEach(mwCollectRecord -> {
            mwCollectRecord.setBoxTime(mwBox.getCreateTime());
        });
        //批量更新收集装箱时间
        mwCollectRecordService.updateBatchById(toBoxingCollectRecordList);
        return true;
    }
 
 
    @Override
    public ApiMedicalBoxingDetailsVO boxingAndPrintingNew(ApiMedicalBoxingDTO dto) {
        ApiMedicalBoxingDetailsVO vo = boxingAndPrintingNewDetail(dto);
        //先入库操作
        iMwCollectRecordService.storeCollectBatch(dto.getRecordList());
        return  vo;
    }
 
    /**
     * 手动装箱并打印
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiMedicalBoxingDetailsVO boxingAndPrintingNewDetail(ApiMedicalBoxingDTO dto) {
 
        if (CollectionUtils.isEmpty(dto.getIdList())) {
            throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空");
        }
        if (dto.getWarehouseId() == null || dto.getWarehouseId() <= 0) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间ID不能为空");
        }
        //暂存间
        SysDepartment warehouse = sysDepartmentService.getById(dto.getWarehouseId());
        if (warehouse == null) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        if (warehouse.getIsWarehouse().equals(0)) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        //医院
        SysDepartment hospital = sysDepartmentService.getHospital(warehouse.getParentId());
        if (hospital == null) {
            throw new BusinessException(ErrorCode.DEPARTMENT_NOT_FOUND);
        }
        ApiMedicalBoxingDetailsVO boxingAndPrintingDetails = new ApiMedicalBoxingDetailsVO();
        List<MwCollectRecord> mwCollectRecordList = mwCollectRecordService.list(new QueryWrapper<MwCollectRecord>()//
                .lambda()//
                .in(MwCollectRecord::getId, dto.getIdList()));
        if (CollectionUtils.isEmpty(mwCollectRecordList)) {
            throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空");
        }
        //获取收集表医废类型
        Set<Integer> wasteTypeSet = mwCollectRecordList.stream().map(MwCollectRecord::getWasteType).collect(Collectors.toSet());
        if (wasteTypeSet.size() > 1) {
            throw new BusinessException(ErrorCode.ERR_500, "废物类型不同,请重新选择");
        }
        //获取转运箱存在收集ID
        int count = mwBoxItemService.count(new QueryWrapper<MwBoxItem>().lambda().in(MwBoxItem::getCollectRecordId, dto.getIdList()));
        if (count > 0) {
            throw new BusinessException(ErrorCode.ERR_500, "医废已放入转运箱,请重新选择");
        }
        Integer wasteType=0;
        //
        for (MwCollectRecord mwCollectRecord : mwCollectRecordList) {
            wasteType = mwCollectRecord.getWasteType();
            /*if (!mwCollectRecord.getWarehouseId().equals(warehouse.getId())) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "不在此暂存间,请重新选择");
            }*/
            if (!mwCollectRecord.getStatus().equals(MedicalWasteEnum.MedicalWasteStatusEnum.COLLECT.getStatus())) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "状态错误,请重新选择");
            }
            if (mwCollectRecord.getDelFlag().equals(CommonConstant.DEL_FLAG_1)) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "已删除,请重新选择");
            }
        }
        MwBox mwBox = new MwBox();
        mwBox.setHospitalId(hospital.getId());
        //新增芯片ID,由APP端直接上传
        mwBox.setChipId(dto.getChipId());
        if(StringUtils.isEmpty(hospital.getCustomNumber())){
            //获取装运箱编码
            mwBox.setCode(generateCode(mwBox, hospital.getOrgCode(), wasteTypeSet.iterator().next()));
        }else {
            Random random = new Random();
            int ran = random.nextInt(10);
            SysDictItem sysDictItem = sysDictItemService.getById(wasteType);
            mwBox.setCode(generateCodeNew(hospital.getCustomNumber(), new Date(), ran, sysDictItem.getItemValue()));
        }
        save(mwBox);
        List<MwBoxItem> mwBoxItemList = new ArrayList<>();
        dto.getIdList().forEach(collectRecordId -> {
            MwBoxItem mwBoxItem = new MwBoxItem();
            mwBoxItem.setBoxId(mwBox.getId());
            mwBoxItem.setCollectRecordId(collectRecordId);
            mwBoxItemList.add(mwBoxItem);
        });
        //批量新增
        mwBoxItemService.saveBatch(mwBoxItemList);
        //批量更新医废的装箱时间
        for (MwCollectRecord mwCollectRecord : mwCollectRecordList) {
            mwCollectRecord.setBoxTime(mwBox.getCreateTime());
        }
        mwCollectRecordService.updateBatchById(mwCollectRecordList);
        //自动装箱打印时间
        BeanUtils.copyProperties(mwBox, boxingAndPrintingDetails);
        boxingAndPrintingDetails.setBoxTime(mwBox.getCreateTime());
        return boxingAndPrintingDetails;
    }
 
    /**
     * 手动装箱并打印
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiMedicalBoxingDetailsVO boxingAndPrinting(ApiMedicalBoxingDTO dto) {
 
        if (CollectionUtils.isEmpty(dto.getIdList())) {
            throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空");
        }
        if (dto.getWarehouseId() == null || dto.getWarehouseId() <= 0) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间ID不能为空");
        }
        //暂存间
        SysDepartment warehouse = sysDepartmentService.getById(dto.getWarehouseId());
        if (warehouse == null) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        if (warehouse.getIsWarehouse().equals(0)) {
            throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在");
        }
        //医院
        SysDepartment hospital = sysDepartmentService.getHospital(warehouse.getParentId());
        if (hospital == null) {
            throw new BusinessException(ErrorCode.DEPARTMENT_NOT_FOUND);
        }
        ApiMedicalBoxingDetailsVO boxingAndPrintingDetails = new ApiMedicalBoxingDetailsVO();
        List<MwCollectRecord> mwCollectRecordList = mwCollectRecordService.list(new QueryWrapper<MwCollectRecord>()//
                .lambda()//
                .in(MwCollectRecord::getId, dto.getIdList()));
        if (CollectionUtils.isEmpty(mwCollectRecordList)) {
            throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空");
        }
        //获取收集表废物类型
        Set<Integer> wasteTypeSet = mwCollectRecordList.stream().map(MwCollectRecord::getWasteType).collect(Collectors.toSet());
        if (wasteTypeSet.size() > 1) {
            throw new BusinessException(ErrorCode.ERR_500, "废物类型不同,请重新选择");
        }
        //获取转运箱存在收集ID
        int count = mwBoxItemService.count(new QueryWrapper<MwBoxItem>().lambda().in(MwBoxItem::getCollectRecordId, dto.getIdList()));
        if (count > 0) {
            throw new BusinessException(ErrorCode.ERR_500, "医废已放入转运箱,请重新选择");
        }
        //
        for (MwCollectRecord mwCollectRecord : mwCollectRecordList) {
            if (!mwCollectRecord.getWarehouseId().equals(warehouse.getId())) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "不在此暂存间,请重新选择");
            }
            if (!mwCollectRecord.getStatus().equals(MedicalWasteEnum.MedicalWasteStatusEnum.STORE.getStatus())) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "状态错误,请重新选择");
            }
            if (mwCollectRecord.getDelFlag().equals(CommonConstant.DEL_FLAG_1)) {
                throw new BusinessException(ErrorCode.ERR_500, "医废" + mwCollectRecord.getCode() + "已删除,请重新选择");
            }
        }
        MwBox mwBox = new MwBox();
        mwBox.setHospitalId(hospital.getId());
        //获取装运箱编码
        mwBox.setCode(generateCode(mwBox, hospital.getOrgCode(), wasteTypeSet.iterator().next()));
        //新增芯片ID,由APP端直接上传
        mwBox.setChipId(dto.getChipId());
        save(mwBox);
        List<MwBoxItem> mwBoxItemList = new ArrayList<>();
        dto.getIdList().forEach(collectRecordId -> {
            MwBoxItem mwBoxItem = new MwBoxItem();
            mwBoxItem.setBoxId(mwBox.getId());
            mwBoxItem.setCollectRecordId(collectRecordId);
            mwBoxItemList.add(mwBoxItem);
        });
        //批量新增
        mwBoxItemService.saveBatch(mwBoxItemList);
        //批量更新医废的装箱时间
        for (MwCollectRecord mwCollectRecord : mwCollectRecordList) {
            mwCollectRecord.setBoxTime(mwBox.getCreateTime());
        }
        mwCollectRecordService.updateBatchById(mwCollectRecordList);
        //自动装箱打印时间
        BeanUtils.copyProperties(mwBox, boxingAndPrintingDetails);
        boxingAndPrintingDetails.setBoxTime(mwBox.getCreateTime());
        return boxingAndPrintingDetails;
    }
 
    /**
     * 获取装运箱编码
     *
     * @param mwBox
     * @param hospitalCode
     * @param wasteType
     * @return
     */
    private String generateCode(MwBox mwBox, String hospitalCode, Integer wasteType) {
 
        Random random = new Random();
        int ran = random.nextInt(10);
        SysDictItem sysDictItem = sysDictItemService.getById(wasteType);
        Date createTime = new Date();
        StringBuilder sb = new StringBuilder();
        while (true) {
            String currentDate = DateUtil.format(createTime, "yyyyMMddHHmmss");
            sb.append(hospitalCode).append(currentDate).append(ran).append(sysDictItem.getItemValue());
            MwBox mwBoxEntity = getOne(new QueryWrapper<MwBox>().lambda().eq(MwBox::getCode, sb.toString()));
            if (mwBoxEntity == null) {
                break;
            }
            createTime = DateUtils.addSeconds(createTime, 1);
            sb = new StringBuilder();
        }
        mwBox.setCreateTime(createTime);
        return sb.toString();
    }
 
    private String generateCodeNew(String hospitalCode,  Date createTime,Integer random,String wasteType) {
 
        Long currentDate = createTime.getTime()/1000;
        StringBuilder sb = new StringBuilder();
        // int count = 0;
        // while (true) {
        sb.append(hospitalCode).append(currentDate).append(random).append(wasteType);
        // MwCollectRecord mwCollectRecord = getOne(new QueryWrapper<MwCollectRecord>().lambda().eq(MwCollectRecord::getCode, sb.toString()));
        //     if (mwCollectRecord == null) {
        //         break;
        //     }
        //     count++;
        //     sb = new StringBuilder();
        // }
        return sb.toString();
    }
 
}