|
|
@@ -0,0 +1,227 @@
|
|
|
+package work.baiyun.chronicdiseaseapp.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.FollowUpMapper;
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.UserInfoMapper;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.po.FollowUp;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.po.UserInfo;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.BaseQueryRequest;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.FollowUpResponse;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.CreateFollowUpRequest;
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.UpdateFollowUpRequest;
|
|
|
+import work.baiyun.chronicdiseaseapp.service.FollowUpService;
|
|
|
+import work.baiyun.chronicdiseaseapp.util.SecurityUtils;
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.PermissionGroup;
|
|
|
+import work.baiyun.chronicdiseaseapp.exception.CustomException;
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.ErrorCode;
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.FollowUpStatus;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class FollowUpServiceImpl implements FollowUpService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FollowUpMapper followUpMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserInfoMapper userInfoMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void createFollowUp(CreateFollowUpRequest request) {
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ FollowUp followUp = new FollowUp();
|
|
|
+ BeanUtils.copyProperties(request, followUp);
|
|
|
+ followUp.setPatientUserId(userId);
|
|
|
+ followUp.setStatus(FollowUpStatus.PENDING); // 默认状态为待确认
|
|
|
+ followUpMapper.insert(followUp);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateFollowUp(UpdateFollowUpRequest request) {
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ FollowUp followUp = followUpMapper.selectById(request.getId());
|
|
|
+
|
|
|
+ if (followUp == null) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_NOT_FOUND.getCode(),
|
|
|
+ "复诊记录不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 权限检查:患者只能修改自己的记录,医生只能修改分配给自己的记录
|
|
|
+ PermissionGroup role = SecurityUtils.getCurrentUserRole();
|
|
|
+ if (role == PermissionGroup.PATIENT &&
|
|
|
+ !followUp.getPatientUserId().equals(userId)) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_ACCESS_DENIED.getCode(),
|
|
|
+ "无权操作该复诊记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ if ((role == PermissionGroup.DOCTOR ||
|
|
|
+ role == PermissionGroup.SYS_ADMIN) &&
|
|
|
+ !followUp.getDoctorUserId().equals(userId)) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_ACCESS_DENIED.getCode(),
|
|
|
+ "无权操作该复诊记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新字段
|
|
|
+ if (request.getAppointmentTime() != null) {
|
|
|
+ followUp.setAppointmentTime(request.getAppointmentTime());
|
|
|
+ }
|
|
|
+ if (request.getStatus() != null) {
|
|
|
+ followUp.setStatus(FollowUpStatus.fromCode(request.getStatus()));
|
|
|
+ }
|
|
|
+ if (request.getNotes() != null) {
|
|
|
+ followUp.setNotes(request.getNotes());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果状态更新为已完成,则设置实际就诊时间
|
|
|
+ if (FollowUpStatus.COMPLETED.equals(followUp.getStatus())) {
|
|
|
+ followUp.setActualTime(LocalDateTime.now());
|
|
|
+ }
|
|
|
+
|
|
|
+ followUpMapper.updateById(followUp);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<FollowUpResponse> listFollowUps(BaseQueryRequest request) {
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ PermissionGroup role = SecurityUtils.getCurrentUserRole();
|
|
|
+
|
|
|
+ Page<FollowUp> page = new Page<>(request.getPageNum(), request.getPageSize());
|
|
|
+ LambdaQueryWrapper<FollowUp> wrapper = new LambdaQueryWrapper<>();
|
|
|
+
|
|
|
+ // 根据用户角色查询不同的数据
|
|
|
+ if (role == PermissionGroup.PATIENT) {
|
|
|
+ wrapper.eq(FollowUp::getPatientUserId, userId);
|
|
|
+ } else if (role == PermissionGroup.DOCTOR ||
|
|
|
+ role == PermissionGroup.SYS_ADMIN) {
|
|
|
+ wrapper.eq(FollowUp::getDoctorUserId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ wrapper.ge(request.getStartTime() != null, FollowUp::getCreateTime, request.getStartTime())
|
|
|
+ .le(request.getEndTime() != null, FollowUp::getCreateTime, request.getEndTime())
|
|
|
+ .orderByDesc(FollowUp::getCreateTime);
|
|
|
+
|
|
|
+ Page<FollowUp> result = followUpMapper.selectPage(page, wrapper);
|
|
|
+
|
|
|
+ // 批量查询用户信息
|
|
|
+ Set<Long> userIds = result.getRecords().stream()
|
|
|
+ .flatMap(r -> java.util.stream.Stream.of(r.getPatientUserId(), r.getDoctorUserId()))
|
|
|
+ .filter(id -> id != null)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ Map<Long, UserInfo> userInfoMap;
|
|
|
+ if (userIds.isEmpty()) {
|
|
|
+ userInfoMap = java.util.Collections.emptyMap();
|
|
|
+ } else {
|
|
|
+ List<UserInfo> userInfos = userInfoMapper.selectBatchIds(userIds);
|
|
|
+ userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
|
|
|
+ }
|
|
|
+
|
|
|
+ List<FollowUpResponse> responses = result.getRecords().stream().map(r -> {
|
|
|
+ FollowUpResponse resp = new FollowUpResponse();
|
|
|
+ BeanUtils.copyProperties(r, resp);
|
|
|
+ resp.setId(r.getId().toString());
|
|
|
+
|
|
|
+ // 设置用户昵称
|
|
|
+ UserInfo patient = userInfoMap.get(r.getPatientUserId());
|
|
|
+ if (patient != null) {
|
|
|
+ resp.setPatientNickname(patient.getNickname());
|
|
|
+ }
|
|
|
+
|
|
|
+ UserInfo doctor = userInfoMap.get(r.getDoctorUserId());
|
|
|
+ if (doctor != null) {
|
|
|
+ resp.setDoctorNickname(doctor.getNickname());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置状态码
|
|
|
+ if (r.getStatus() != null) {
|
|
|
+ resp.setStatus(r.getStatus().getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ return resp;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ Page<FollowUpResponse> responsePage = new Page<>();
|
|
|
+ responsePage.setRecords(responses);
|
|
|
+ responsePage.setCurrent(result.getCurrent());
|
|
|
+ responsePage.setSize(result.getSize());
|
|
|
+ responsePage.setTotal(result.getTotal());
|
|
|
+ responsePage.setPages(result.getPages());
|
|
|
+ return responsePage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<FollowUpResponse> listFollowUpsByPatient(Long patientUserId, BaseQueryRequest request) {
|
|
|
+ // 检查绑定关系
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ PermissionGroup role = SecurityUtils.getCurrentUserRole();
|
|
|
+
|
|
|
+ // 只有医生和管理员可以查询其他患者的复诊记录
|
|
|
+ if (role != PermissionGroup.DOCTOR &&
|
|
|
+ role != PermissionGroup.SYS_ADMIN) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_ACCESS_DENIED.getCode(),
|
|
|
+ "无权查询其他患者的复诊记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ Page<FollowUp> page = new Page<>(request.getPageNum(), request.getPageSize());
|
|
|
+ LambdaQueryWrapper<FollowUp> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(FollowUp::getPatientUserId, patientUserId)
|
|
|
+ .ge(request.getStartTime() != null, FollowUp::getCreateTime, request.getStartTime())
|
|
|
+ .le(request.getEndTime() != null, FollowUp::getCreateTime, request.getEndTime())
|
|
|
+ .orderByDesc(FollowUp::getCreateTime);
|
|
|
+
|
|
|
+ Page<FollowUp> result = followUpMapper.selectPage(page, wrapper);
|
|
|
+
|
|
|
+ List<FollowUpResponse> responses = result.getRecords().stream().map(r -> {
|
|
|
+ FollowUpResponse resp = new FollowUpResponse();
|
|
|
+ BeanUtils.copyProperties(r, resp);
|
|
|
+ resp.setId(r.getId().toString());
|
|
|
+ // 设置状态码
|
|
|
+ if (r.getStatus() != null) {
|
|
|
+ resp.setStatus(r.getStatus().getCode());
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ Page<FollowUpResponse> responsePage = new Page<>();
|
|
|
+ responsePage.setRecords(responses);
|
|
|
+ responsePage.setCurrent(result.getCurrent());
|
|
|
+ responsePage.setSize(result.getSize());
|
|
|
+ responsePage.setTotal(result.getTotal());
|
|
|
+ responsePage.setPages(result.getPages());
|
|
|
+ return responsePage;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteFollowUp(Long id) {
|
|
|
+ Long userId = SecurityUtils.getCurrentUserId();
|
|
|
+ FollowUp followUp = followUpMapper.selectById(id);
|
|
|
+
|
|
|
+ if (followUp == null) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_NOT_FOUND.getCode(),
|
|
|
+ "复诊记录不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只有患者本人才能删除自己的复诊记录
|
|
|
+ if (!followUp.getPatientUserId().equals(userId)) {
|
|
|
+ throw new CustomException(
|
|
|
+ ErrorCode.FOLLOW_UP_ACCESS_DENIED.getCode(),
|
|
|
+ "无权删除该复诊记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ followUpMapper.deleteById(id);
|
|
|
+ }
|
|
|
+}
|