|
|
@@ -1,5 +1,18 @@
|
|
|
package cn.tr.module.phototherapy.common.service.impl;
|
|
|
|
|
|
+import cn.tr.module.phototherapy.common.dto.UnbindDeviceDTO;
|
|
|
+import cn.tr.module.phototherapy.common.enums.DeviceStatusEnum;
|
|
|
+import cn.tr.module.phototherapy.common.enums.IsCurrentBindEnum;
|
|
|
+import cn.tr.module.phototherapy.common.enums.TherapyStatusEnum;
|
|
|
+import cn.tr.module.phototherapy.common.mapper.BusClinicMapper;
|
|
|
+import cn.tr.module.phototherapy.common.mapper.BusTherapyPlanMapper;
|
|
|
+import cn.tr.module.phototherapy.common.mapper.BusTherapyRecordMapper;
|
|
|
+import cn.tr.module.phototherapy.common.po.BusTherapyPlanPO;
|
|
|
+import cn.tr.module.phototherapy.common.po.BusTherapyRecordPO;
|
|
|
+import cn.tr.module.phototherapy.common.repository.BusClinicRepository;
|
|
|
+import cn.tr.module.phototherapy.common.repository.BusTherapyPlanRepository;
|
|
|
+import cn.tr.module.phototherapy.common.repository.BusTherapyRecordRepository;
|
|
|
+import cn.tr.module.phototherapy.common.vo.UnbindDeviceVO;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import cn.tr.module.phototherapy.common.po.BusClinicPO;
|
|
|
@@ -27,7 +40,14 @@ import cn.tr.core.exception.TRExcCode;
|
|
|
**/
|
|
|
@Service
|
|
|
public class BusDeviceServiceImpl extends ServiceImpl<BusDeviceRepository, BusDevicePO> implements IBusDeviceService {
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private BusClinicRepository busClinicRepository;
|
|
|
+ @Autowired
|
|
|
+ private BusDeviceRepository busDeviceRepository;
|
|
|
+ @Autowired
|
|
|
+ private BusTherapyRecordRepository busTherapyRecordRepository;
|
|
|
+ @Autowired
|
|
|
+ private BusTherapyPlanRepository busTherapyPlanRepository;
|
|
|
/**
|
|
|
* 根据条件查询设备表
|
|
|
* @param query 查询参数
|
|
|
@@ -103,4 +123,68 @@ public class BusDeviceServiceImpl extends ServiceImpl<BusDeviceRepository, BusDe
|
|
|
}
|
|
|
return this.removeByIds(ids);
|
|
|
};
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解绑设备
|
|
|
+ * @param source 解绑实体类
|
|
|
+ * @date 2026-01-23
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public UnbindDeviceVO unbindDevice(UnbindDeviceDTO source) {
|
|
|
+ UnbindDeviceVO vo = new UnbindDeviceVO();
|
|
|
+ try {
|
|
|
+ // 1. 封装数据库更新参数
|
|
|
+ source.setIsCurrentBind(IsCurrentBindEnum.HISTORY_BIND.getValue()); // 改为历史绑定
|
|
|
+ source.setBindEndTime(new Date()); // 绑定结束时间=当前时间
|
|
|
+ source.setDeviceStatus(DeviceStatusEnum.NoDevice.getValue()); // 设备状态改为“未使用”
|
|
|
+ source.setTherapyStatus(TherapyStatusEnum.NOT_STARTED.getValue()); // 治疗状态改为“未进行”
|
|
|
+
|
|
|
+ // 2. 执行数据库更新
|
|
|
+ // 更新bus_clinic(绑定关系表)
|
|
|
+ busClinicRepository.updateClinicForUnbind(source);
|
|
|
+ // 更新bus_device(设备表)
|
|
|
+ busDeviceRepository.updateDeviceForUnbind(source);
|
|
|
+ // 更新bus_therapy_record(治疗记录表)
|
|
|
+ //先查询患者最新治疗记录的当前状态
|
|
|
+ BusTherapyRecordPO latestTherapyRecord = busTherapyRecordRepository.selectLatestByPatientId(source.getPatientUniqueId());
|
|
|
+ if (latestTherapyRecord != null) {
|
|
|
+ Integer currentTherapyStatus = latestTherapyRecord.getTherapyStatus();
|
|
|
+ // 判断逻辑改为:只要状态≠2(未进行),就执行更新
|
|
|
+ if (!TherapyStatusEnum.NOT_STARTED.getValue().equals(currentTherapyStatus)) {
|
|
|
+ busTherapyRecordRepository.updateTherapyStatusForUnbind(source);
|
|
|
+ }
|
|
|
+ vo.setMessage("解除绑定成功");
|
|
|
+ } else {
|
|
|
+ // 无治疗记录,跳过更新
|
|
|
+ vo.setMessage("解除绑定成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 独立查询最新状态(不复用,直接查库)
|
|
|
+ // 3.1 查询绑定状态
|
|
|
+ BusClinicPO clinic = busClinicRepository.selectByPatientUniqueId(source.getPatientUniqueId());
|
|
|
+ IsCurrentBindEnum isCurrentBindEnum = IsCurrentBindEnum.getByValue(clinic.getIsCurrentBind());
|
|
|
+ vo.setIsCurrentBind(isCurrentBindEnum);
|
|
|
+
|
|
|
+ // 3.2 查询治疗状态
|
|
|
+ BusTherapyRecordPO therapyRecord = busTherapyRecordRepository.selectLatestByPatientId(source.getPatientUniqueId());
|
|
|
+ TherapyStatusEnum therapyStatusEnum = TherapyStatusEnum.getByValue(therapyRecord.getTherapyStatus());
|
|
|
+ vo.setTherapyStatus(therapyStatusEnum);
|
|
|
+
|
|
|
+ // 3.4 查询设备状态
|
|
|
+ BusDevicePO device = busDeviceRepository.selectByDeviceId(clinic.getDeviceId());
|
|
|
+ DeviceStatusEnum deviceStatusEnum = DeviceStatusEnum.getByValue(device.getDeviceStatus());
|
|
|
+ vo.setDeviceStatus(deviceStatusEnum);
|
|
|
+
|
|
|
+ // 4. 设置操作结果
|
|
|
+ vo.setSuccess(true);
|
|
|
+ vo.setBindEndTime(new Date());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 异常处理:回滚事务,返回失败信息
|
|
|
+ vo.setSuccess(false);
|
|
|
+ vo.setMessage("解除绑定失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
}
|