package cn.besteam.modules.medical.service.impl; import cn.besteam.common.constant.CommonConstant; import cn.besteam.common.constant.DepartmentEnum; 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.*; import cn.besteam.modules.medical.entity.*; import cn.besteam.modules.medical.mapper.MwCollectRecordMapper; import cn.besteam.modules.medical.service.*; import cn.besteam.modules.medical.vo.*; import cn.besteam.modules.report.dto.DisposalMonthlyReportSumDto; import cn.besteam.modules.report.dto.DisposalMonthlyReportVoItemDto; import cn.besteam.modules.report.vo.DisposalMonthlyReportVoItemVO; import cn.besteam.modules.report.vo.RankStatisticsVO; import cn.besteam.modules.shiro.authc.util.EnvUtils; import cn.besteam.modules.system.biz.SysDictBiz; import cn.besteam.modules.system.entity.SysDepartment; import cn.besteam.modules.system.entity.SysDict; import cn.besteam.modules.system.entity.SysDictItem; import cn.besteam.modules.system.entity.SysUser; import cn.besteam.modules.system.service.ISysDepartmentService; import cn.besteam.modules.system.service.ISysDictItemService; import cn.besteam.modules.system.service.ISysDictService; import cn.besteam.modules.system.service.ISysUserService; import cn.besteam.modules.system.vo.SysDictItemVO; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.collect.Lists; 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 javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** * @Description: APP-版本表 * @author: besteam * @date: 2020-04-25 * @version: V1.0 */ @Service @Slf4j public class MwCollectRecordServiceImpl extends ServiceImpl implements IMwCollectRecordService { @Autowired private ISysUserService sysUserService; @Autowired private ISysDepartmentService sysDepartmentService; @Autowired private ISysDictItemService sysDictItemService; @Autowired private IMwTransitConfigService mwTransitConfigService; @Autowired private IMwCheckoutRecordService mwCheckoutRecordService; @Autowired private IMwCheckoutRecordItemService mwCheckoutRecordItemService; @Autowired private ISysDictService sysDictService; @Autowired private SysDictBiz sysDictBiz; @Autowired private IMwWarningConfigService mwWarningConfigService; @Autowired private IMwWarningRecordService mwWarningRecordService; @Autowired private IMwTransitConfigService transitConfigService; @Transactional @Override public Integer addCollect(MwCollectRecordSaveDTO dto) { SysDepartment department = sysDepartmentService.getSysDepartmentById(dto.getDepartmentId()); SysDepartment hospital = sysDepartmentService.getHospital(department.getParentId()); MwCollectRecord collectRecord = getOne(new QueryWrapper().lambda().eq(MwCollectRecord::getCode, dto.getCode())); if (collectRecord != null) { if (collectRecord.getHospitalId() == hospital.getId() && collectRecord.getCreateTime().getTime() == dto.getCreateTime().getTime() && collectRecord.getWeight() == dto.getWeight()) { log.error("新增医废收集错误,code重复.请求参数:{}", JSON.toJSONString(dto)); return -1; } return -1; } if (BigDecimal.ZERO.compareTo(dto.getWeight()) >= 0) { throw new BusinessException(ErrorCode.ERR_500, "医废重量必须大于0"); } if (new BigDecimal("100").compareTo(dto.getWeight()) < 0) { throw new BusinessException(ErrorCode.ERR_500, "医废重量超出最大值100"); } if (department == null) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } if (!department.getOrgType().equals(DepartmentEnum.OrgTypeEnum.OFFICE.getType())) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } if (department.getIsWarehouse().equals(1)) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } SysUser departmentUser = null; if (dto.getDepartmentUserId() != null) { departmentUser = sysUserService.getById(dto.getDepartmentUserId()); if (departmentUser == null) { throw new BusinessException(ErrorCode.ERR_500, "科室人员不存在"); } } if (hospital == null) { throw new BusinessException(ErrorCode.ERR_500, "查询不到科室所在医院"); } if (dto.getCreateBy() == null) { dto.setCreateBy(EnvUtils.getUser().getId()); } SysUser creator = sysUserService.getById(dto.getCreateBy()); if (creator == null) { throw new BusinessException(ErrorCode.ERR_500, "收集人员不存在"); } SysDepartment creatorDepartment = sysDepartmentService.getMyDepartment(creator.getId()); if (creatorDepartment == null) { throw new BusinessException(ErrorCode.ERR_500, "收集人员不属于此医院"); } if (!creatorDepartment.getTreeCode().startsWith(hospital.getTreeCode())) { throw new BusinessException(ErrorCode.ERR_500, "收集人员不属于此医院"); } SysDictItem wasteType = sysDictItemService.getById(dto.getWasteType()); if (wasteType == null) { throw new BusinessException(ErrorCode.ERR_500, "废物类型不存在"); } // 移动端传入的收集时间如果超出当前时间20分钟则报错 if (dto.getCreateTime().after(DateUtils.addMinutes(new Date(), 20))) { throw new BusinessException(ErrorCode.ERR_500, "收集时间错误"); } MwCollectRecord mwCollectRecord = new MwCollectRecord(); BeanUtils.copyProperties(dto, mwCollectRecord); mwCollectRecord.setBoxWeight(dto.getWeight()); mwCollectRecord.setStoreWeight(dto.getWeight()); mwCollectRecord.setCheckoutWeight(dto.getWeight()); mwCollectRecord.setWasteTypeStr(wasteType.getItemText()); mwCollectRecord.setHospitalId(hospital.getId()); mwCollectRecord.setHospitalName(hospital.getDepartName()); mwCollectRecord.setDepartmentName(department.getDepartName()); mwCollectRecord.setDepartmentUserName(departmentUser != null ? departmentUser.getName() : ""); if (StringUtils.isBlank(mwCollectRecord.getCode())) { mwCollectRecord.setCode(generateCode(hospital.getOrgCode(), department.getOrgCode(), dto.getCreateTime(), wasteType.getItemValue())); } mwCollectRecord.setCreateBy(creator.getId()); mwCollectRecord.setCreator(creator.getName()); mwCollectRecord.setOfficePersonnelSignature(dto.getOfficePersonnelSignature()); mwCollectRecord.setCreateTime(new Date()); save(mwCollectRecord); // if (dto.getDepartmentUserId() == null) { MwWarningRecord warningRecord = new MwWarningRecord(); warningRecord.setHospitalId(mwCollectRecord.getHospitalId()); warningRecord.setHospitalName(mwCollectRecord.getHospitalName()); warningRecord.setOfficeId(mwCollectRecord.getDepartmentId()); warningRecord.setOfficeName(mwCollectRecord.getDepartmentName()); warningRecord.setType(MedicalWasteEnum.WarningConfigTypeEnum.ILLEGAL_HANDOVER.getType()); warningRecord.setTypeName(MedicalWasteEnum.WarningConfigTypeEnum.ILLEGAL_HANDOVER.getName()); warningRecord.setWarningTime(new Date()); warningRecord.setMessage("医疗废物违规交接"); warningRecord.setWarningTargetId(mwCollectRecord.getId()); warningRecord.setWarningTargetName(mwCollectRecord.getCode()); warningRecord.setStatus(MedicalWasteEnum.WarningRecordStatusEnum.WAIT_AUDIT.getStatus()); warningRecord.setWasteTypeName(mwCollectRecord.getWasteTypeStr()); warningRecord.setWeight(mwCollectRecord.getWeight()); mwWarningRecordService.save(warningRecord); } return mwCollectRecord.getId(); } @Transactional(rollbackFor = Exception.class) @Override public boolean addCollectBatch(List list) { Set codeSet = list.stream().map(MwCollectRecordSaveDTO::getCode).collect(Collectors.toSet()); if (codeSet.size() != list.size()) { log.error("批量新增收集列表,医废编码重复:{}", JSON.toJSONString(list)); // throw new BusinessException(ErrorCode.ERR_500, "医废编码重复"); } for (MwCollectRecordSaveDTO dto : list) { try { addCollect(dto); } catch (Exception e) { log.error("批量新增收集失败:数据为{},{}", JSON.toJSONString(dto), e.getMessage()); } } return true; } @Override public boolean delete(Integer id) { MwCollectRecord collectRecord = getById(id); if (collectRecord == null) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } collectRecord.setDelFlag(1); collectRecord.setDeleteTime(new Date()); return updateById(collectRecord); } @Override public boolean deleteByCode(String code) { MwCollectRecord collectRecord = getOne(new QueryWrapper().lambda().eq(MwCollectRecord::getCode, code)); if (collectRecord == null) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } collectRecord.setDelFlag(1); collectRecord.setDeleteTime(new Date()); return updateById(collectRecord); } @Override public boolean restore(Integer id) { MwCollectRecord collectRecord = getById(id); if (collectRecord == null) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } collectRecord.setDelFlag(0); return updateById(collectRecord); } @Transactional(rollbackFor = Exception.class) @Override public boolean storeCollect(MwCollectRecordStoreDTO dto) { MwCollectRecord collectRecord = getOne(new QueryWrapper().lambda().eq(MwCollectRecord::getCode, dto.getCode())); if (collectRecord == null) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } SysDepartment hospital = sysDepartmentService.getById(collectRecord.getHospitalId()); if (hospital == null) { throw new BusinessException(ErrorCode.ERR_500, "医院不存在"); } if (!collectRecord.getStatus().equals(MedicalWasteEnum.MedicalWasteStatusEnum.COLLECT.getStatus())) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } 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, "暂存间不存在"); } if (!warehouse.getTreeCode().startsWith(hospital.getTreeCode())) { throw new BusinessException(ErrorCode.ERR_500, "暂存间不属于此医院"); } collectRecord.setStoreUser(EnvUtils.getUser().getName()); collectRecord.setStoreUserId(EnvUtils.getUser().getId()); collectRecord.setWarehouseId(warehouse.getId()); collectRecord.setWarehouseName(warehouse.getDepartName()); collectRecord.setStoreTime(new Date()); collectRecord.setStatus(MedicalWasteEnum.MedicalWasteStatusEnum.STORE.getStatus()); // if (dto.getWeight() != null) { collectRecord.setStoreWeight(dto.getWeight()); collectRecord.setBoxWeight(dto.getWeight()); collectRecord.setCheckoutWeight(dto.getWeight()); BigDecimal subtract = dto.getWeight().subtract(collectRecord.getWeight()); if (subtract.compareTo(BigDecimal.ZERO) > 0) { MwWarningConfig storeWeightConfig = mwWarningConfigService.getOne(new QueryWrapper().lambda().eq(MwWarningConfig::getType, MedicalWasteEnum.WarningConfigTypeEnum.STORE_WEIGHT.getType())); if (subtract.compareTo(storeWeightConfig.getDefaultValue()) > 0 && storeWeightConfig.getDefaultValue().compareTo(BigDecimal.ZERO) > 0) { MwWarningRecord warningRecord = new MwWarningRecord(); warningRecord.setHospitalId(collectRecord.getHospitalId()); warningRecord.setHospitalName(collectRecord.getHospitalName()); warningRecord.setOfficeId(collectRecord.getDepartmentId()); warningRecord.setOfficeName(collectRecord.getDepartmentName()); warningRecord.setType(MedicalWasteEnum.WarningConfigTypeEnum.STORE_WEIGHT.getType()); warningRecord.setTypeName(MedicalWasteEnum.WarningConfigTypeEnum.STORE_WEIGHT.getName()); warningRecord.setWarningTime(new Date()); warningRecord.setMessage("医疗废物入库重量不符"); warningRecord.setWarningTargetId(collectRecord.getId()); warningRecord.setWarningTargetName(collectRecord.getCode()); warningRecord.setStatus(MedicalWasteEnum.WarningRecordStatusEnum.WAIT_AUDIT.getStatus()); warningRecord.setWasteTypeName(collectRecord.getWasteTypeStr()); warningRecord.setWeight(collectRecord.getStoreWeight()); mwWarningRecordService.save(warningRecord); } } else { subtract = subtract.abs(); MwWarningConfig leekConfig = mwWarningConfigService.getOne(new QueryWrapper().lambda().eq(MwWarningConfig::getType, MedicalWasteEnum.WarningConfigTypeEnum.LEEK.getType())); if (subtract.compareTo(leekConfig.getDefaultValue()) > 0 && StringUtils.isNotEmpty(leekConfig.getWasteTypes()) && leekConfig.getDefaultValue().compareTo(BigDecimal.ZERO) > 0) { List wasteTypeList = Arrays.asList(leekConfig.getWasteTypes().split(",")); if (wasteTypeList.contains(String.valueOf(collectRecord.getWasteType()))) { MwWarningRecord warningRecord = new MwWarningRecord(); warningRecord.setHospitalId(collectRecord.getHospitalId()); warningRecord.setHospitalName(collectRecord.getHospitalName()); warningRecord.setOfficeId(collectRecord.getDepartmentId()); warningRecord.setOfficeName(collectRecord.getDepartmentName()); warningRecord.setType(MedicalWasteEnum.WarningConfigTypeEnum.LEEK.getType()); warningRecord.setTypeName(MedicalWasteEnum.WarningConfigTypeEnum.LEEK.getName()); warningRecord.setWarningTime(new Date()); warningRecord.setMessage("医疗废物可能出现泄漏"); warningRecord.setWarningTargetId(collectRecord.getId()); warningRecord.setWarningTargetName(collectRecord.getCode()); warningRecord.setStatus(MedicalWasteEnum.WarningRecordStatusEnum.WAIT_AUDIT.getStatus()); warningRecord.setWasteTypeName(collectRecord.getWasteTypeStr()); warningRecord.setWeight(collectRecord.getStoreWeight()); mwWarningRecordService.save(warningRecord); } } MwWarningConfig damagedConfig = mwWarningConfigService.getOne(new QueryWrapper().lambda().eq(MwWarningConfig::getType, MedicalWasteEnum.WarningConfigTypeEnum.DAMAGED.getType())); if (subtract.compareTo(damagedConfig.getDefaultValue()) > 0 && StringUtils.isNotEmpty(damagedConfig.getWasteTypes()) && damagedConfig.getDefaultValue().compareTo(BigDecimal.ZERO) > 0) { List wasteTypeList = Arrays.asList(damagedConfig.getWasteTypes().split(",")); if (wasteTypeList.contains(String.valueOf(collectRecord.getWasteType()))) { MwWarningRecord warningRecord = new MwWarningRecord(); warningRecord.setHospitalId(collectRecord.getHospitalId()); warningRecord.setHospitalName(collectRecord.getHospitalName()); warningRecord.setOfficeId(collectRecord.getDepartmentId()); warningRecord.setOfficeName(collectRecord.getDepartmentName()); warningRecord.setType(MedicalWasteEnum.WarningConfigTypeEnum.DAMAGED.getType()); warningRecord.setTypeName(MedicalWasteEnum.WarningConfigTypeEnum.DAMAGED.getName()); warningRecord.setWarningTime(new Date()); warningRecord.setMessage("医疗废物可能出现破损"); warningRecord.setWarningTargetId(collectRecord.getId()); warningRecord.setWarningTargetName(collectRecord.getCode()); warningRecord.setStatus(MedicalWasteEnum.WarningRecordStatusEnum.WAIT_AUDIT.getStatus()); warningRecord.setWasteTypeName(collectRecord.getWasteTypeStr()); warningRecord.setWeight(collectRecord.getStoreWeight()); mwWarningRecordService.save(warningRecord); } } } } boolean flag = updateById(collectRecord); //处理入库超时、入库前遗失预警 mwWarningRecordService.handleStoreWarning(collectRecord); return flag; } @Transactional(rollbackFor = Exception.class) @Override public boolean storeCollectBatch(List list) { for (MwCollectRecordStoreDTO dto : list) { storeCollect(dto); } return true; } private List 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 childrenList = sysDepartmentService.list(new QueryWrapper().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 IPage queryPageCollectList(Integer pageNo, Integer pageSize, MwCollectRecordOpsDTO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); if (Objects.nonNull(dto.getType())) { dto.setWasteTypes(MedicalWasteEnum.WASTE_TYPE_CATEGORY_MAP.get(dto.getType())); } IPage result = baseMapper.queryPageCollectList(page, dto); return result; } @Override public List queryPageCollectList(MwCollectRecordOpsDTO dto) { List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return null; } dto.setHospitalIdList(hospitalIdList); if (Objects.nonNull(dto.getType())) { dto.setWasteTypes(MedicalWasteEnum.WASTE_TYPE_CATEGORY_MAP.get(dto.getType())); } List list = baseMapper.queryPageCollectList(dto); return list; } @Override public IPage queryPageStoreList(Integer pageNo, Integer pageSize, MwCollectRecordOpsDTO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); IPage result = baseMapper.queryPageStoreList(page, dto); return result; } @Override public List queryPageStoreList(MwCollectRecordOpsDTO dto) { List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return null; } dto.setHospitalIdList(hospitalIdList); List list = baseMapper.queryPageStoreList(dto); return list; } @Override public IPage queryPageCheckoutList(Integer pageNo, Integer pageSize, MwCollectRecordOpsDTO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); IPage result = baseMapper.queryPageCheckoutList(page, dto); return result; } @Override public List queryPageCheckoutList(MwCollectRecordOpsDTO dto) { List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return null; } dto.setHospitalIdList(hospitalIdList); List list = baseMapper.queryPageCheckoutList(dto); return list; } @Override public IPage queryPageSupplyList(Integer pageNo, Integer pageSize, MwCollectRecordOpsDTO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); IPage result = baseMapper.queryPageSupplyList(page, dto); return result; } @Override public List queryPageSupplyList(MwCollectRecordOpsDTO dto) { List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return null; } dto.setHospitalIdList(hospitalIdList); List list = baseMapper.queryPageSupplyList(dto); return list; } @Override public IPage queryPageDeleteList(Integer pageNo, Integer pageSize, MwCollectRecordOpsDTO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); IPage result = baseMapper.queryPageDeleteList(page, dto); return result; } @Override public List queryPageDeleteList(MwCollectRecordOpsDTO dto) { List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return null; } dto.setHospitalIdList(hospitalIdList); List list = baseMapper.queryPageDeleteList(dto); return list; } @Override public boolean auditSupplement(MwCollectRecordAuditDTO dto) { MwCollectRecord collectRecord = getById(dto.getId()); if (collectRecord == null) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } if (!collectRecord.getAuditStatus().equals(MedicalWasteEnum.MedicalWasteAuditStatusEnum.WAIT_AUDIT.getStatus())) { throw new BusinessException(ErrorCode.MW_COLLECT_RECORD_NOT_FOUND); } collectRecord.setAuditStatus(dto.getAuditStatus()); return updateById(collectRecord); } @Transactional(rollbackFor = Exception.class) @Override public boolean supply(MwCollectRecordSupplyDTO dto) { if (BigDecimal.ZERO.compareTo(dto.getWeight()) >= 0) { throw new BusinessException(ErrorCode.ERR_500, "医废重量必须大于0"); } SysUser departmentUser = sysUserService.getById(dto.getDepartmentUserId()); if (departmentUser == null) { throw new BusinessException(ErrorCode.ERR_500, "科室人员不存在"); } SysDepartment department = sysDepartmentService.getById(dto.getDepartmentId()); if (department == null) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } if (!department.getOrgType().equals(DepartmentEnum.OrgTypeEnum.OFFICE.getType())) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } if (department.getIsWarehouse().equals(1)) { throw new BusinessException(ErrorCode.ERR_500, "科室不存在"); } SysDepartment hospital = sysDepartmentService.getHospital(department.getParentId()); if (hospital == null) { throw new BusinessException(ErrorCode.ERR_500, "查询不到科室所在医院"); } SysDictItem wasteType = sysDictItemService.getById(dto.getWasteType()); if (wasteType == null) { throw new BusinessException(ErrorCode.ERR_500, "废物类型不存在"); } SysUser creator = sysUserService.getById(dto.getCreateBy()); if (creator == null) { throw new BusinessException(ErrorCode.ERR_500, "收集人员不存在"); } if (dto.getCreateTime().after(new Date())) { throw new BusinessException(ErrorCode.ERR_500, "收集时间错误"); } MwCollectRecord mwCollectRecord = new MwCollectRecord(); BeanUtils.copyProperties(dto, mwCollectRecord); mwCollectRecord.setBoxWeight(dto.getWeight()); mwCollectRecord.setStoreWeight(dto.getWeight()); mwCollectRecord.setCheckoutWeight(dto.getWeight()); mwCollectRecord.setWasteTypeStr(wasteType.getItemText()); mwCollectRecord.setHospitalId(hospital.getId()); mwCollectRecord.setHospitalName(hospital.getDepartName()); mwCollectRecord.setDepartmentName(department.getDepartName()); mwCollectRecord.setDepartmentUserName(departmentUser.getName()); mwCollectRecord.setSupplyTime(new Date()); mwCollectRecord.setCreator(creator.getName()); mwCollectRecord.setAuditStatus(MedicalWasteEnum.MedicalWasteAuditStatusEnum.WAIT_AUDIT.getStatus()); mwCollectRecord.setSource(MedicalWasteEnum.MedicalWasteSourceEnum.OPS.getType()); if (StringUtils.isEmpty(hospital.getCustomNumber())) { mwCollectRecord.setCode(generateCode(hospital.getOrgCode(), department.getOrgCode(), dto.getCreateTime(), wasteType.getItemValue())); } else { //0967 006 210104085048 1 Random random = new Random(); int ran = random.nextInt(10); mwCollectRecord.setCode(generateCodeNew(hospital.getCustomNumber(), dto.getCreateTime(), ran, wasteType.getItemValue())); } save(mwCollectRecord); // if (!dto.getStatus().equals(MedicalWasteEnum.MedicalWasteStatusEnum.COLLECT.getStatus())) { mwCollectRecord.setStoreTime(dto.getCreateTime()); mwCollectRecord.setStoreUserId(dto.getCreateBy()); mwCollectRecord.setStoreUser(creator.getName()); // SysDepartment warehouse = sysDepartmentService.getById(dto.getWarehouseId()); if (warehouse == null) { throw new BusinessException(ErrorCode.ERR_500, "暂存间不存在"); } if (!warehouse.getTreeCode().startsWith(hospital.getTreeCode())) { throw new BusinessException(ErrorCode.ERR_500, "暂存间不属于此医院"); } mwCollectRecord.setWarehouseId(warehouse.getId()); mwCollectRecord.setWarehouseName(warehouse.getDepartName()); } if (dto.getStatus().equals(MedicalWasteEnum.MedicalWasteStatusEnum.CHECKOUT.getStatus())) { MwTransitConfig mwTransitConfig = mwTransitConfigService.getById(dto.getTransitConfigId()); if (mwTransitConfig == null) { throw new BusinessException(ErrorCode.ERR_500, "出库配置不存在"); } mwCollectRecord.setCheckoutTime(dto.getCreateTime()); // MwCheckoutRecord mwCheckoutRecord = new MwCheckoutRecord(); mwCheckoutRecord.setCreateTime(dto.getCreateTime()); mwCheckoutRecord.setCreateBy(dto.getCreateBy()); mwCheckoutRecord.setCreator(creator.getName()); mwCheckoutRecord.setHospitalId(hospital.getId()); mwCheckoutRecord.setHospitalName(hospital.getDepartName()); mwCheckoutRecord.setTransitConfigId(dto.getTransitConfigId()); mwCheckoutRecord.setTransitCompany(mwTransitConfig.getTransitCompany()); mwCheckoutRecord.setLicensePlate(mwTransitConfig.getLicensePlate()); mwCheckoutRecord.setEngineDriver(mwTransitConfig.getEngineDriver()); mwCheckoutRecordService.save(mwCheckoutRecord); // MwCheckoutRecordItem mwCheckoutRecordItem = new MwCheckoutRecordItem(); mwCheckoutRecordItem.setCheckoutRecordId(mwCheckoutRecord.getId()); mwCheckoutRecordItem.setCollectRecordId(mwCollectRecord.getId()); mwCheckoutRecordItemService.save(mwCheckoutRecordItem); } return updateById(mwCollectRecord); } @Override public MwCollectRecordVO getRecordDetail(Integer id) { return baseMapper.getRecordDetail(id); } private String generateCode(String hospitalCode, String departmentCode, Date createTime, String wasteType) { String currentDate = DateUtil.format(createTime, "yyMMddHHmmss"); StringBuilder sb = new StringBuilder(); // int count = 0; // while (true) { sb.append(hospitalCode).append(departmentCode).append(currentDate).append(wasteType); // MwCollectRecord mwCollectRecord = getOne(new QueryWrapper().lambda().eq(MwCollectRecord::getCode, sb.toString())); // if (mwCollectRecord == null) { // break; // } // count++; // sb = new StringBuilder(); // } 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(); } /** * 获取此次出库操作医废收集类型统计 * * @param idList * @param wasteTypeStr * @param status * @param delFlag * @param auditStatus * @return */ @Override public ApiMedicalStatisticsVO getStatistic(List idList, String wasteTypeStr, Integer status, Integer delFlag, Integer auditStatus) { return baseMapper.getStatistic(idList, wasteTypeStr, status, delFlag, auditStatus); } /** * 统计-收集统计 * * @param dto * @return */ @Override public ApiStatisticsVO collectStatistics(ApiStatisticsDTO dto, HttpServletRequest request) { if (dto.getStatus() == null || dto.getStatus() <= 0) { throw new BusinessException(ErrorCode.ERR_500, "统计类型不能为空"); } SysDict sysDict = sysDictService.getOne(new QueryWrapper().lambda().eq(SysDict::getDictCode, "MEDICAL_WASTE_TYPE")); if (sysDict == null) { return null; } //获取字典所有医废类型 List wasteTypeList = sysDictBiz.getSysDictListByDict(sysDict.getId()); if (CollectionUtils.isEmpty(wasteTypeList)) { return null; } //获取当前用户收集表所有科室 QueryWrapper queryWrapper = new QueryWrapper<>(); String accessToken = request.getHeader("X-Access-Token"); SysUser sysUser = sysUserService.getByAppToken(accessToken); queryWrapper.lambda()// // .eq(MwCollectRecord::getStatus, dto.getStatus())// .eq(MwCollectRecord::getCreateBy, EnvUtils.getUser().getId())// .eq(MwCollectRecord::getDelFlag, CommonConstant.DEL_FLAG_0)// .eq(MwCollectRecord::getAuditStatus, MedicalWasteEnum.MedicalWasteAuditStatusEnum.PASS.getStatus()) .eq(sysUser.getHospitalId() != null, MwCollectRecord::getHospitalId, sysUser.getHospitalId()); if (dto.getStartTime() != null) { if (dto.getStatus().equals(1)) { queryWrapper.lambda().ge(MwCollectRecord::getCreateTime, dto.getStartTime()); } else if (dto.getStatus().equals(2)) { queryWrapper.lambda().ge(MwCollectRecord::getStoreTime, dto.getStartTime()); } else { queryWrapper.lambda().ge(MwCollectRecord::getCheckoutTime, dto.getStartTime()); } } if (dto.getEndTime() != null) { if (dto.getStatus().equals(1)) { queryWrapper.lambda().le(MwCollectRecord::getCreateTime, dto.getEndTime()); } else if (dto.getStatus().equals(2)) { queryWrapper.lambda().le(MwCollectRecord::getStoreTime, dto.getEndTime()); } else { queryWrapper.lambda().le(MwCollectRecord::getCheckoutTime, dto.getEndTime()); } } List collectRecordList = list(queryWrapper); if (CollectionUtils.isEmpty(collectRecordList)) { return null; } ApiStatisticsVO vo = new ApiStatisticsVO(); // 部门统计列表 List voList = new ArrayList<>(); // Set departmentSet = collectRecordList.stream().map(MwCollectRecord::getDepartmentId).collect(Collectors.toSet()); for (Integer departmentId : departmentSet) { SysDepartment sysDepartment = sysDepartmentService.getById(departmentId); if (sysDepartment == null) { continue; } ApiDepNameStatisticsVO departmentStatisticsVO = new ApiDepNameStatisticsVO(); departmentStatisticsVO.setNumberTotal(0); departmentStatisticsVO.setWeightTotal(BigDecimal.ZERO); departmentStatisticsVO.setDepartmentName(sysDepartment.getDepartName()); List statisticsList = new ArrayList<>(); for (SysDictItemVO wasteTypeVO : wasteTypeList) { ApiMedicalStatisticsVO wasteTypeStatisticsVO = new ApiMedicalStatisticsVO(); wasteTypeStatisticsVO.setWasteTypeStr(wasteTypeVO.getItemText()); wasteTypeStatisticsVO.setCount(0); wasteTypeStatisticsVO.setWeightSum(BigDecimal.ZERO); for (MwCollectRecord record : collectRecordList) { if (record.getDepartmentId().equals(departmentId) && record.getWasteType().equals(wasteTypeVO.getId())) { wasteTypeStatisticsVO.setWeightSum(wasteTypeStatisticsVO.getWeightSum().add(record.getWeight())); wasteTypeStatisticsVO.setCount(wasteTypeStatisticsVO.getCount() + 1); } } statisticsList.add(wasteTypeStatisticsVO); } departmentStatisticsVO.setNumberTotal(statisticsList.stream().mapToInt(ApiMedicalStatisticsVO::getCount).sum()); departmentStatisticsVO.setWeightTotal(statisticsList.stream().map(ApiMedicalStatisticsVO::getWeightSum).reduce(BigDecimal.ZERO, BigDecimal::add)); departmentStatisticsVO.setStatisticsList(statisticsList); voList.add(departmentStatisticsVO); } vo.setDepNameStatisticsList(voList); // ApiDepNameStatisticsVO totalStatisticsVO = new ApiDepNameStatisticsVO(); totalStatisticsVO.setDepartmentName("合计"); totalStatisticsVO.setNumberTotal(0); totalStatisticsVO.setWeightTotal(BigDecimal.ZERO); List statisticsList = new ArrayList<>(); for (SysDictItemVO wasteTypeVO : wasteTypeList) { ApiMedicalStatisticsVO wasteTypeStatisticsVO = new ApiMedicalStatisticsVO(); wasteTypeStatisticsVO.setWasteTypeStr(wasteTypeVO.getItemText()); wasteTypeStatisticsVO.setCount(0); wasteTypeStatisticsVO.setWeightSum(BigDecimal.ZERO); for (MwCollectRecord record : collectRecordList) { if (record.getWasteType().equals(wasteTypeVO.getId())) { wasteTypeStatisticsVO.setWeightSum(wasteTypeStatisticsVO.getWeightSum().add(record.getWeight())); wasteTypeStatisticsVO.setCount(wasteTypeStatisticsVO.getCount() + 1); } } statisticsList.add(wasteTypeStatisticsVO); } totalStatisticsVO.setNumberTotal(statisticsList.stream().mapToInt(ApiMedicalStatisticsVO::getCount).sum()); totalStatisticsVO.setWeightTotal(statisticsList.stream().map(ApiMedicalStatisticsVO::getWeightSum).reduce(BigDecimal.ZERO, BigDecimal::add)); totalStatisticsVO.setStatisticsList(statisticsList); voList.add(totalStatisticsVO); return vo; } /** * 统计-入库和出库统计 * * @param dto * @return */ @Override public List storeAndCheckoutStatistics(ApiStatisticsDTO dto, HttpServletRequest request) { if (dto.getStatus() == null || dto.getStatus() <= 0) { throw new BusinessException(ErrorCode.ERR_500, "统计类型不能为空"); } List apiStatisticsList = new ArrayList<>(); SysDict sysDict = sysDictService.getOne(new QueryWrapper().lambda().eq(SysDict::getDictCode, "MEDICAL_WASTE_TYPE")); if (sysDict == null) { return apiStatisticsList; } String accessToken = request.getHeader("X-Access-Token"); SysUser sysUser = sysUserService.getByAppToken(accessToken); //获取字典所有医废类型 Set wasteTypeStrSet = sysDictBiz.getSysDictListByDict(sysDict.getId()).stream().map(SysDictItemVO::getItemText).collect(Collectors.toSet()); if (CollectionUtils.isNotEmpty(wasteTypeStrSet)) { if (dto.getStatus().equals(CommonConstant.STORE_STATISTICS_WAY)) { apiStatisticsList = baseMapper.getStoreStatistics(sysUser.getHospitalId(), dto, wasteTypeStrSet, EnvUtils.getUser().getId(), CommonConstant.DEL_FLAG_0, MedicalWasteEnum.MedicalWasteAuditStatusEnum.PASS.getStatus()); } if (dto.getStatus().equals(CommonConstant.CHECKOUT_STATISTICS_WAY)) { apiStatisticsList = baseMapper.getCheckoutStatistics(sysUser.getHospitalId(), dto, wasteTypeStrSet, EnvUtils.getUser().getId(), CommonConstant.DEL_FLAG_0, MedicalWasteEnum.MedicalWasteAuditStatusEnum.PASS.getStatus()); } } return apiStatisticsList; } @Override public List getUnBoxingList(Integer warehouseId, Integer userId) { return baseMapper.getUnBoxingList(warehouseId, userId); } @Override public List getUnBoxingListNew(Integer warehouseId, Integer userId, Integer hospitalId) { return baseMapper.getUnBoxingListNew(warehouseId, userId, hospitalId); } @Override public List getUnBoxingListTotal(Integer warehouseId, Integer userId, Integer hospitalId) { List mwCollectRecordList = baseMapper.getUnBoxingListTotal(warehouseId, userId, hospitalId); List mwCollectRecordListRe = new ArrayList<>(); for (MwCollectRecord mwCollectRecord : mwCollectRecordList) { Boolean ok = true; for (MwCollectRecord mwCollectRecord1 : mwCollectRecordListRe) { if (mwCollectRecord.getBoxCode().equals(mwCollectRecord1.getBoxCode())) { ok = false; } } if (ok) { mwCollectRecordListRe.add(mwCollectRecord); } } return mwCollectRecordListRe; } @Override public IPage getRankStatisticsList(Integer pageNo, Integer pageSize, List departmentIdList, Date startTime, Date endTime, String typeName) { Page pageParam = new Page<>(pageNo, pageSize); return baseMapper.getRankStatisticsList(pageParam, departmentIdList, startTime, endTime, typeName); } @Override public IPage getRegionStatisticsList(Integer pageNo, Integer pageSize, List departmentIdList, Date startTime, Date endTime) { Page pageParam = new Page<>(pageNo, pageSize); return baseMapper.getRegionStatisticsList(pageParam, departmentIdList, startTime, endTime); } @Override public List getDepartmentLastCollectList() { return baseMapper.getDepartmentLastCollectList(); } @Override public List getHospitalLastCollectList() { return baseMapper.getHospitalLastCollectList(); } @Override public NonCollectMedicalWasteVO getDepartmentLastCollect(String officeIds) { return baseMapper.getDepartmentLastCollect(officeIds); } @Override public List getOfficeRankStatistics(List departmentIdList, Date startTime, Date endTime) { return baseMapper.getOfficeRankStatistics(departmentIdList, startTime, endTime); } @Override public IPage getCollectTrajectory(Integer pageNo, Integer pageSize, MwCollectCollectTrajectoryParamVO dto) { Page page = new Page<>(pageNo, pageSize); List hospitalIdList = getSearchHospitalIdList(dto.getHospitalId()); if (CollectionUtils.isEmpty(hospitalIdList)) { return page; } dto.setHospitalIdList(hospitalIdList); List trajectoryVOS = baseMapper.getCollectTrajectory((pageNo - 1) * pageSize, pageSize, dto); for (MwCollectCollectTrajectoryVO mwCollectCollectTrajectoryVO : trajectoryVOS) { List list = mwCollectCollectTrajectoryVO.getDetailList(); List list_ = new ArrayList<>(); for (Integer i = 0; i < list.size(); i++) { //同一个科室的,一个小时内的数据不能要 Boolean ok = true; if (i > 0) { MwCollectCollectTrajectoryDetailVO vo = list.get(i - 1); MwCollectCollectTrajectoryDetailVO vo1 = list.get(i); long d = (vo.getCreateTime().getTime() - vo1.getCreateTime().getTime()) / 1000; if (vo.getDepartmentName().equals(vo1.getDepartmentName()) && d < 3600) { ok = false; } } if (ok) { list_.add(list.get(i)); } } mwCollectCollectTrajectoryVO.setDetailList(list_); } page.setRecords(trajectoryVOS); page.setTotal(baseMapper.getCollectTrajectoryCount(dto)); return page; } @Override public List> getMedicalStatistics(MwCollectCollectTrajectoryParamVO dto) { return this.baseMapper.getMedicalStatistics(dto); } @Override public List getDisposalMonthlyReport(Integer departmentId, Date startTime, Date endTime, String keyWord, List dictTypeId) { SysDepartment department = sysDepartmentService.getById(departmentId); if (department == null) { throw new BusinessException(ErrorCode.ERR_500, "区域不存在"); } if (!department.getOrgType().equals(DepartmentEnum.OrgTypeEnum.AREA.getType())) { throw new BusinessException(ErrorCode.ERR_500, "区域不存在"); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String start = ""; String end = ""; if (null != startTime && null != endTime) { start = sdf.format(startTime); end = sdf.format(endTime); } List keyWords = null; if (org.springframework.util.StringUtils.hasLength(keyWord)) { String[] split = keyWord.split("、"); keyWords = Arrays.asList(split); } List hospitalList = sysDepartmentService.list(new QueryWrapper()// .lambda()// .likeRight(SysDepartment::getTreeCode, department.getTreeCode())// .eq(SysDepartment::getOrgType, DepartmentEnum.OrgTypeEnum.HOSPITAL.getType()));// List ids = hospitalList.stream().map(SysDepartment::getId).collect(Collectors.toList()); List records = this.baseMapper.getDisposalMonthlyReport(departmentId, start, end, keyWords, dictTypeId,ids); List list = new ArrayList<>(); if (CollUtil.isEmpty(records)) { return list; } records.forEach(x -> { DisposalMonthlyReportVoItemVO vo = new DisposalMonthlyReportVoItemVO(); vo.setKey(x.getId()); vo.setName(x.getName()); List bg = new ArrayList<>(); //集中收集基层数量 bg.add(x.getNum1()); //暂存点数量 bg.add(x.getNum2()); vo.setData(bg); list.add(vo); }); if (CollUtil.isNotEmpty(list)) { //List ids = list.stream().map(DisposalMonthlyReportVoItemVO::getKey).distinct().collect(Collectors.toList()); String last = ""; if (keyWords!=null&&CollUtil.isNotEmpty(keyWords)) { last = " and ("; List likeList = new ArrayList<>(); keyWords.forEach(k -> likeList.add(" transit_company like '%" + k + "%' ")); String likeSql = String.join(" or ", likeList); last = last.concat(likeSql).concat(")"); } List configList = transitConfigService.lambdaQuery() .in(MwTransitConfig::getHospitalId, ids) .last(StrUtil.isNotBlank(last), last) .list(); //产生量 List douS = this.baseMapper.getDisposalMonthlyReportSum(ids, start, end, 1, null); //转移量 List douZ = this.baseMapper.getDisposalMonthlyReportSum(ids, start, end, 2, null); //库存量 List douC = this.baseMapper.getDisposalMonthlyReportSum(ids, start, end, 3, null); Map> map = configList.stream().collect(Collectors.groupingBy(MwTransitConfig::getHospitalId)); Map> douSMap = douS.stream().collect(Collectors.groupingBy(DisposalMonthlyReportSumDto::getHospitalId)); Map> douZMap = douZ.stream().collect(Collectors.groupingBy(DisposalMonthlyReportSumDto::getHospitalId)); Map> douCMap = douC.stream().collect(Collectors.groupingBy(DisposalMonthlyReportSumDto::getHospitalId)); list.forEach(dto -> { List mapList = map.get(dto.getKey()); if (CollUtil.isNotEmpty(mapList)) { List collect = mapList.stream().map(MwTransitConfig::getTransitCompany).distinct().collect(Collectors.toList()); dto.setCheckoutRecord(collect); } List douSList = buildBigDecimalList(douSMap.get(dto.getKey()), dictTypeId); dto.getData().addAll(douSList); List douZList = buildBigDecimalList(douZMap.get(dto.getKey()), dictTypeId); dto.getData().addAll(douZList); List douCList = buildBigDecimalList(douCMap.get(dto.getKey()), dictTypeId); dto.getData().addAll(douCList); }); } List foot = new ArrayList<>(); list.forEach(datum -> { List data1 = datum.getData(); for (int i = 0; i < data1.size(); i++) { if (foot.size() != (dictTypeId.size() * 3 + 2)) { BigDecimal bigDecimal = new BigDecimal("0"); bigDecimal = bigDecimal.add(data1.get(i)); foot.add(bigDecimal); } else { BigDecimal bigDecimal = foot.get(i); bigDecimal = bigDecimal.add(data1.get(i)); foot.set(i, bigDecimal); } } }); DisposalMonthlyReportVoItemVO f = new DisposalMonthlyReportVoItemVO(); f.setName("合计"); f.setData(foot); list.add(f); return list; } /** * 构建数据 * * @param records 数据记录 * @param dictList 字典ids集合 * @return 数据列表 */ private List buildBigDecimalList(List records, List dictList) { List list = new ArrayList<>(); if (CollUtil.isNotEmpty(records)) { dictList.forEach(dict -> { DisposalMonthlyReportSumDto sum = records.stream().filter(x -> x.getWasteType().equals(dict)).findFirst().orElse(null); if (sum != null) { list.add(sum.getWeight().setScale(2, RoundingMode.HALF_EVEN)); } else { list.add(BigDecimal.ZERO); } }); } else { dictList.forEach(dict -> { list.add(BigDecimal.ZERO); }); } return list; } @Override public Double getTotalNonCheckout(List departmentIdList, List wasteTypes) { return baseMapper.getTotalNonCheckout(departmentIdList, wasteTypes); } }