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 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 mwCollectRecordList = mwCollectRecordService.getUnBoxingList(dto.getWarehouseId(), EnvUtils.getUser().getId()); if (CollectionUtils.isNotEmpty(mwCollectRecordList)) { //获取收集表ID列表 Set mwCollectRecordIdSet = mwCollectRecordList.stream().map(MwCollectRecord::getId).collect(Collectors.toSet()); //获取转运箱存在收集ID int count = mwBoxItemService.count(new QueryWrapper().lambda().in(MwBoxItem::getCollectRecordId, mwCollectRecordIdSet)); if (count > 0) { throw new BusinessException(ErrorCode.ERR_500, "医废已放入转运箱,请重新选择"); } //集合分组 Map> collect = mwCollectRecordList.stream().collect(groupingBy(MwCollectRecord::getWasteType)); log.info("集合分组:" + collect); for (Map.Entry> entry : collect.entrySet()) { List collectRecordList = entry.getValue(); List 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 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 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 mwCollectRecordList = mwCollectRecordService.list(new QueryWrapper()// .lambda()// .in(MwCollectRecord::getId, dto.getIdList())); if (CollectionUtils.isEmpty(mwCollectRecordList)) { throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空"); } //获取收集表医废类型 Set 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().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 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 mwCollectRecordList = mwCollectRecordService.list(new QueryWrapper()// .lambda()// .in(MwCollectRecord::getId, dto.getIdList())); if (CollectionUtils.isEmpty(mwCollectRecordList)) { throw new BusinessException(ErrorCode.ERR_500, "医废列表不能为空"); } //获取收集表废物类型 Set 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().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 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().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().lambda().eq(MwCollectRecord::getCode, sb.toString())); // if (mwCollectRecord == null) { // break; // } // count++; // sb = new StringBuilder(); // } return sb.toString(); } }