LocalBusDeviceRunningService.java 8.1 KB

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