LocalBusDeviceRunningService.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.coffee.bus.service;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5. import com.coffee.bus.entity.BusPatientEntity;
  6. import com.coffee.bus.registry.device.DeviceOperator;
  7. import com.coffee.bus.registry.device.DeviceRegistry;
  8. import com.coffee.bus.registry.device.bean.DeviceCacheInfo;
  9. import com.coffee.bus.registry.patient.PatientOperator;
  10. import com.coffee.bus.registry.patient.PatientRegistry;
  11. import com.coffee.bus.registry.patient.bean.PatientCacheInfo;
  12. import com.coffee.bus.service.dto.UndoDeviceConfig;
  13. import com.coffee.bus.service.dto.DeviceShiftConfig;
  14. import com.coffee.bus.service.dto.ManualUndoConfig;
  15. import com.coffee.bus.entity.BusClinicEntity;
  16. import com.coffee.bus.entity.BusDeviceRunningEntity;
  17. import com.coffee.bus.entity.BusInfusionHistoryEntity;
  18. import com.coffee.bus.mapper.BusDeviceRunningMapper;
  19. import com.coffee.common.crud.BaseService;
  20. import com.coffee.common.exception.CustomException;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.context.annotation.Lazy;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. /**
  28. * @author lifang
  29. * @version 1.0.0
  30. * @ClassName LocalBusHospitalService.java
  31. * @Description TODO
  32. * @createTime 2022年03月19日 09:27:00
  33. */
  34. @Service
  35. public class LocalBusDeviceRunningService extends BaseService<BusDeviceRunningMapper, BusDeviceRunningEntity,String> {
  36. @Autowired
  37. @Lazy
  38. private LocalBusInfusionHistoryService infusionHistoryService;
  39. @Autowired
  40. @Lazy
  41. private PatientRegistry patientRegistry;
  42. @Autowired
  43. @Lazy
  44. private LocalBusPatientService patientService;
  45. @Autowired
  46. @Lazy
  47. private LocalBusClinicService clinicService;
  48. @Autowired
  49. @Lazy
  50. private DeviceRegistry deviceRegistry;
  51. public LocalBusDeviceRunningService(LocalBusInfusionHistoryService infusionHistoryService, PatientRegistry patientRegistry,LocalBusPatientService patientService, LocalBusClinicService clinicService) {
  52. this.infusionHistoryService = infusionHistoryService;
  53. this.patientRegistry = patientRegistry;
  54. this.patientService=patientService;
  55. this.clinicService=clinicService;
  56. }
  57. @Override
  58. public void validateBeforeSave(BusDeviceRunningEntity entity) {
  59. }
  60. @Override
  61. public void validateBeforeUpdate(BusDeviceRunningEntity entity) {
  62. }
  63. @Override
  64. public void validateBeforeDelete(String id) {
  65. }
  66. /**
  67. * 批量撤泵
  68. * @param undoConfigs 批量撤泵配置
  69. * @param finishClinic 撤泵后是否结束相关临床
  70. */
  71. @Transactional(rollbackFor = Exception.class)
  72. public void batchUndo(List<ManualUndoConfig> undoConfigs,boolean finishClinic){
  73. undoConfigs.forEach(undoConfig -> this.undo(undoConfig,false));
  74. //批量结束临床
  75. if(finishClinic){
  76. Map<String, List<ManualUndoConfig>> configsMap = undoConfigs.stream().collect(Collectors.groupingBy(ManualUndoConfig::getTenantId));
  77. configsMap.forEach((k,v)-> {
  78. clinicService.finish(v.stream().map(ManualUndoConfig::getPatientCode).collect(Collectors.toList()),v.get(0).getUndo().getUndoTime() ,k);
  79. });
  80. }
  81. }
  82. /**
  83. * 结束病患监控操作
  84. * @param manualUndoConfig
  85. * @param finishClinic 撤泵后是否结束相关临床
  86. */
  87. @Transactional(rollbackFor = Exception.class)
  88. public void undo(ManualUndoConfig manualUndoConfig,boolean finishClinic) {
  89. List<String> deviceIds = manualUndoConfig.getDeviceIds();
  90. if(CollectionUtil.isNotEmpty(deviceIds)){
  91. /****************将撤泵记录存入到泵的使用历史记录中***************/
  92. List<BusDeviceRunningEntity> devices = this.list(new QueryWrapper<BusDeviceRunningEntity>().lambda().in(BusDeviceRunningEntity::getDeviceId,deviceIds));
  93. if(CollectionUtil.isEmpty(devices)){
  94. return;
  95. }
  96. // //主泵撤除且没有结束临床
  97. // if (devices.stream().anyMatch(BusDeviceRunningEntity::getMaster)&&!finishClinic) {
  98. // throw new CustomException("主泵不可撤除,请在【结束管理】处操作");
  99. // }
  100. //对所有运行中的泵进行撤泵操作
  101. this.update(new UpdateWrapper<BusDeviceRunningEntity>().lambda().in(BusDeviceRunningEntity::getDeviceId,deviceIds).set(BusDeviceRunningEntity::getIsUndo,true));
  102. //无泵监护,不需要监护输注数据
  103. if(Boolean.TRUE.equals(manualUndoConfig.getMonitorType())){
  104. //输注结束,更新撤泵信息
  105. Set<String> infusionIds = devices.stream().map(BusDeviceRunningEntity::getInfusionId).collect(Collectors.toSet());
  106. UndoDeviceConfig undo = manualUndoConfig.getUndo();
  107. infusionHistoryService.update(new UpdateWrapper<BusInfusionHistoryEntity>().lambda()
  108. .in(BusInfusionHistoryEntity::getId,infusionIds)
  109. .set(BusInfusionHistoryEntity::getIsUndo,true)
  110. .set(BusInfusionHistoryEntity::getUndoBy,undo.getUndoBy())
  111. .set(BusInfusionHistoryEntity::getUndoTime,undo.getUndoTime())
  112. .set(BusInfusionHistoryEntity::getDestroyer,undo.getDestroyer())
  113. .set(BusInfusionHistoryEntity::getWitnesses,undo.getWitnesses()));
  114. //将缓存设置为已撤泵状态
  115. deviceIds.stream().map(deviceRegistry::getOperator)
  116. .forEach(operator->operator.setUndo(true));
  117. }
  118. }
  119. //结束临床
  120. if(finishClinic){
  121. clinicService.finish(Collections.singletonList(manualUndoConfig.getPatientCode()),manualUndoConfig.getTenantId());
  122. }
  123. }
  124. @Transactional(rollbackFor = Exception.class)
  125. public void updateClinicInfo(List<String> deviceIds, BusClinicEntity clinic) {
  126. this.update(new UpdateWrapper<BusDeviceRunningEntity>().lambda()
  127. .in(BusDeviceRunningEntity::getDeviceId,deviceIds)
  128. .set(BusDeviceRunningEntity::getClinicId,clinic.getId())
  129. .set(BusDeviceRunningEntity::getPatientName,clinic.getPatientName())
  130. .set(BusDeviceRunningEntity::getWard,clinic.getWard())
  131. .set(BusDeviceRunningEntity::getPatientSex,clinic.getPatientGender())
  132. .set(BusDeviceRunningEntity::getBedNo,clinic.getBedNo()));
  133. }
  134. /**
  135. * 切换主泵
  136. * @param shiftConfig 切换配置
  137. */
  138. @Transactional(rollbackFor = Exception.class)
  139. public void shift(DeviceShiftConfig shiftConfig) {
  140. PatientOperator<PatientCacheInfo> patientOperator = patientRegistry.getOperator(shiftConfig.getTenantId(), shiftConfig.getPatientCode());
  141. String bindDeviceId = patientOperator.getBindDeviceId();
  142. List<String> replicaDeviceIds = shiftConfig.getReplicaDeviceIds();
  143. String masterDeviceId = shiftConfig.getMasterDeviceId();
  144. if (masterDeviceId.equals(bindDeviceId)) {
  145. //主泵未发生切换
  146. return;
  147. }
  148. //切换副泵
  149. this.update(new UpdateWrapper<BusDeviceRunningEntity>().lambda()
  150. .in(BusDeviceRunningEntity::getDeviceId,replicaDeviceIds).set(BusDeviceRunningEntity::getMaster,false));
  151. //切换主泵
  152. this.update(new UpdateWrapper<BusDeviceRunningEntity>().lambda()
  153. .eq(BusDeviceRunningEntity::getDeviceId,masterDeviceId).set(BusDeviceRunningEntity::getMaster,true));
  154. DeviceOperator<DeviceCacheInfo> masterOperator = deviceRegistry.getOperator(masterDeviceId);
  155. String currentInfusionId = masterOperator.getInfusionId();
  156. //病患绑定主泵信息
  157. patientService.update(new UpdateWrapper<BusPatientEntity>().lambda()
  158. .eq(BusPatientEntity::getCode,shiftConfig.getPatientCode())
  159. .eq(BusPatientEntity::getTenantId,shiftConfig.getTenantId())
  160. .set(BusPatientEntity::getInfusionId,currentInfusionId));
  161. //刷新缓存信息
  162. replicaDeviceIds.stream().map(deviceRegistry::getOperator).forEach(deviceOperator->{
  163. deviceOperator.setMaster(false);
  164. //切换后也标注为暂时撤泵,即后续该泵发来的数据不再做主副泵切换的判断
  165. deviceOperator.setUndo(true);
  166. });
  167. masterOperator.setMaster(true);
  168. patientOperator.setBindDeviceId(shiftConfig.getMasterDeviceId());
  169. }
  170. }