|
|
@@ -0,0 +1,188 @@
|
|
|
+package com.zskk.pacsonline.modules.report.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.zskk.pacsonline.modules.doctor.entity.Doctors;
|
|
|
+import com.zskk.pacsonline.modules.doctor.mapper.DoctorsMapper;
|
|
|
+import com.zskk.pacsonline.modules.report.entity.Report;
|
|
|
+import com.zskk.pacsonline.modules.report.entity.ReportTemp;
|
|
|
+import com.zskk.pacsonline.modules.report.mapper.RemoteApplicationMapper;
|
|
|
+import com.zskk.pacsonline.modules.report.mapper.ReportMapper;
|
|
|
+import com.zskk.pacsonline.modules.report.mapper.ReportTempMapper;
|
|
|
+import com.zskk.pacsonline.modules.report.service.ReportTempService;
|
|
|
+import com.zskk.pacsonline.modules.report.vo.ReportTempSaveVO;
|
|
|
+import com.zskk.pacsonline.security.LoginUser;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 报告暂存服务实现
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ReportTempServiceImpl implements ReportTempService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ReportTempMapper reportTempMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ReportMapper reportMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DoctorsMapper doctorsMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RemoteApplicationMapper remoteApplicationMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public ReportTemp saveTempReport(ReportTempSaveVO saveVO) {
|
|
|
+ String doctorId = getCurrentDoctorId();
|
|
|
+ if (StringUtils.isBlank(doctorId)) {
|
|
|
+ throw new RuntimeException("无法获取当前医生信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer isRemote = saveVO.getIsRemote();
|
|
|
+ if (isRemote == null) {
|
|
|
+ throw new RuntimeException("报告类型不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果没有报告ID,则自动生成报告
|
|
|
+ String reportId = StringUtils.defaultIfBlank(saveVO.getReportId(), null);
|
|
|
+ if (StringUtils.isBlank(reportId)) {
|
|
|
+ reportId = createReport(saveVO.getExamId(), isRemote);
|
|
|
+ }
|
|
|
+
|
|
|
+ String remoteApplicationId = null;
|
|
|
+ if (isRemote == 1) {
|
|
|
+ remoteApplicationId = getRemoteApplicationId(saveVO.getExamId());
|
|
|
+ }
|
|
|
+
|
|
|
+ ReportTemp draft = findDraft(reportId, doctorId);
|
|
|
+ Date now = new Date();
|
|
|
+ String reportResult = StringUtils.defaultIfBlank(saveVO.getReportResult(), "1");
|
|
|
+
|
|
|
+ if (draft == null) {
|
|
|
+ draft = new ReportTemp();
|
|
|
+ draft.setReportId(reportId);
|
|
|
+ draft.setExamId(saveVO.getExamId());
|
|
|
+ draft.setDoctorId(doctorId);
|
|
|
+ draft.setIsRemote(isRemote);
|
|
|
+ draft.setRemoteApplicationId(remoteApplicationId);
|
|
|
+ draft.setDescription(saveVO.getDescription());
|
|
|
+ draft.setImpression(saveVO.getImpression());
|
|
|
+ draft.setReportResult(reportResult);
|
|
|
+ draft.setCreateTime(now);
|
|
|
+ draft.setStatus(1);
|
|
|
+
|
|
|
+ int rows = reportTempMapper.insert(draft);
|
|
|
+ if (rows <= 0) {
|
|
|
+ throw new RuntimeException("暂存失败");
|
|
|
+ }
|
|
|
+ log.info("新增暂存报告,reportId={}, doctorId={}", reportId, doctorId);
|
|
|
+ } else {
|
|
|
+ draft.setExamId(saveVO.getExamId());
|
|
|
+ draft.setIsRemote(isRemote);
|
|
|
+ draft.setRemoteApplicationId(remoteApplicationId);
|
|
|
+ draft.setDescription(saveVO.getDescription());
|
|
|
+ draft.setImpression(saveVO.getImpression());
|
|
|
+ draft.setReportResult(reportResult);
|
|
|
+ draft.setCreateTime(now);
|
|
|
+ draft.setStatus(1);
|
|
|
+
|
|
|
+ int rows = reportTempMapper.updateById(draft);
|
|
|
+ if (rows <= 0) {
|
|
|
+ throw new RuntimeException("暂存失败");
|
|
|
+ }
|
|
|
+ log.info("更新暂存报告,reportId={}, doctorId={}", reportId, doctorId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return draft;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ReportTemp findDraft(String reportId, String doctorId) {
|
|
|
+ LambdaQueryWrapper<ReportTemp> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(ReportTemp::getReportId, reportId)
|
|
|
+ .eq(ReportTemp::getDoctorId, doctorId)
|
|
|
+ .eq(ReportTemp::getStatus, 1)
|
|
|
+ .last("LIMIT 1");
|
|
|
+ return reportTempMapper.selectOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createReport(String examId, Integer isRemote) {
|
|
|
+ Report report = new Report();
|
|
|
+ report.setExamId(examId);
|
|
|
+ String type = (isRemote != null && isRemote == 2) ? "1" : "2";
|
|
|
+ report.setType(type);
|
|
|
+
|
|
|
+ if (StringUtils.equals(type, "2")) {
|
|
|
+ report.setRemoteApplicationId(getRemoteApplicationId(examId));
|
|
|
+ }
|
|
|
+
|
|
|
+ report.setReportResult("1");
|
|
|
+ Date now = new Date();
|
|
|
+ report.setCreateTime(now);
|
|
|
+ report.setUpdatedTime(now);
|
|
|
+
|
|
|
+ int rows = reportMapper.insert(report);
|
|
|
+ if (rows <= 0) {
|
|
|
+ throw new RuntimeException("创建报告失败");
|
|
|
+ }
|
|
|
+ log.info("自动创建报告成功,reportId={}", report.getId());
|
|
|
+ return report.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getRemoteApplicationId(String examId) {
|
|
|
+ if (StringUtils.isBlank(examId)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String raId = remoteApplicationMapper.selectIdByExamId(examId);
|
|
|
+ if (StringUtils.isBlank(raId)) {
|
|
|
+ throw new RuntimeException("未找到对应的远程申请单");
|
|
|
+ }
|
|
|
+ return raId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getCurrentDoctorId() {
|
|
|
+ try {
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (authentication == null || authentication.getPrincipal() == null) {
|
|
|
+ log.warn("未找到登录用户信息");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Object principal = authentication.getPrincipal();
|
|
|
+ if (!(principal instanceof LoginUser)) {
|
|
|
+ log.warn("Principal 不是 LoginUser 类型: {}", principal.getClass().getName());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ LoginUser loginUser = (LoginUser) principal;
|
|
|
+ if (loginUser.getUser() == null || StringUtils.isBlank(loginUser.getUser().getId())) {
|
|
|
+ log.warn("LoginUser 中未找到用户信息");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String sysUserId = loginUser.getUser().getId();
|
|
|
+ LambdaQueryWrapper<Doctors> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Doctors::getUid, sysUserId).last("LIMIT 1");
|
|
|
+
|
|
|
+ Doctors doctor = doctorsMapper.selectOne(wrapper);
|
|
|
+ if (doctor == null) {
|
|
|
+ log.warn("未找到系统用户对应的医生信息,sysUserId={}", sysUserId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return doctor.getId();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取当前医生ID失败", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|