患者根据医生编号完成绑定和解绑接口

发布于:2025-04-13 ⋅ 阅读:(90) ⋅ 点赞:(0)

医疗系统接口文档

一、Controller 层

1. InstitutionDoctorController

医疗机构和医生相关的控制器,提供机构查询、医生查询、绑定解绑医生等功能。

@RestController
@RequestMapping("/institution-doctor")
public class InstitutionDoctorController {

    @Autowired
    private InstitutionService institutionService;

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private ClientService clientService;

    @GetMapping("/listInstitution/{institutionCategoryId}")
    public Result<List<Institution>> getInstitutionList(@PathVariable Long institutionCategoryId) {
        List<Institution> institutionList = institutionService.getInstitutionByInstitutionCategoryId(institutionCategoryId);
        return Result.success(institutionList);
    }

    @GetMapping("/listDoctor")
    public Result<List<User>> getDoctorList(@RequestParam String institution) {
        List<User> doctorList = doctorService.getDoctorByInstitutionName(institution);
        return Result.success(doctorList);
    }

    @PatchMapping("/bindDoctorByDoctorNumber")
    public Result<Boolean> bindDoctorByDoctorNumber(@PathVariable BindDoctorDto bindDoctorDto){
        boolean result = clientService.bindDoctorByDoctorNumber(bindDoctorDto);
        return Result.success(result);
    }

    @PatchMapping("/unbindDoctorByDoctorNumber")
    public Result<Boolean> unbindDoctorByDoctorNumber(@PathVariable BindDoctorDto bindDoctorDto){
        boolean result = clientService.unbindDoctorByDoctorNumber(bindDoctorDto);
        return Result.success(result);
    }

    @GetMapping("/getDoctorMsg/{doctorNumber}")
    public Result<UserVO> getDoctorMsg(@RequestParam int doctorNumber){
        UserVO doctorMsg = doctorService.getDoctorMsg(doctorNumber);
        return Result.success(doctorMsg);
    }
}

二、Service 接口

1. DoctorService

医生服务接口,提供获取医生信息的方法。

public interface DoctorService extends IService<User> {
    List<User> getDoctorByInstitutionName(String institution);

    UserVO getDoctorMsg(int doctorNumber);
}

2. ClientService

客户服务接口,提供客户与医生绑定和解绑的功能。

public interface ClientService extends IService<Client> {
    boolean bindDoctorByDoctorNumber(BindDoctorDto bindDoctorDto);

    boolean unbindDoctorByDoctorNumber(BindDoctorDto bindDoctorDto);
}

3. InstitutionService

医疗机构服务接口,提供获取机构列表的功能。

public interface InstitutionService extends IService<Institution>{
    List<Institution> getInstitutionByInstitutionCategoryId(Long institutionCategoryId);
}

三、Service 实现类

1. DoctorServiceImpl

@Service
public class DoctorServiceImpl extends ServiceImpl<UserMapper, User> implements DoctorService {

    @Autowired
    private UserMapper userMapper;

    private static final String Institution = "institution";
    @Override
    public List<User> getDoctorByInstitutionName(String institution) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Institution,institution);
        return userMapper.selectList(queryWrapper);
    }
    @Override
    public UserVO getDoctorMsg(int doctorNumber) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNumber, doctorNumber);
        // 查询数据库
        User doctor = userMapper.selectOne(queryWrapper);
        if (doctor == null) {
            throw new BusinessException(
                    InstitutionDoctorEnum.DOCTOR_NOT_EXIST.getCode(),
                    InstitutionDoctorEnum.DOCTOR_NOT_EXIST.getMessage());
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(doctor, userVO);
        return userVO;
    }
}

2. ClientServiceImpl

@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientService {

    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PatientMapper patientMapper;

    private static final String NUMBER = "number";

    private static final String UUID = "uuid";

    private static final int DEFAULT_DOCTOR_NUMBER = 887375;


    // 绑定医生
    @Override
    public boolean bindDoctorByDoctorNumber(BindDoctorDto bindDoctorDto){
        String clientUuid = bindDoctorDto.getUuid();
        int doctorNumber = bindDoctorDto.getDoctorNumber();

        // 1. 查询医生
        User doctor = userMapper.selectOne(
                new QueryWrapper<User>().eq(NUMBER, doctorNumber)
        );
        if (doctor == null) {
            throw new BusinessException(
                    InstitutionDoctorEnum.DOCTOR_NOT_EXIST.getCode(),
                    InstitutionDoctorEnum.DOCTOR_NOT_EXIST.getMessage());
        }

        // 2. 查询 client
        Client client = clientMapper.selectOne(
                new QueryWrapper<Client>().eq(UUID, clientUuid)
        );
        if (client == null) {
            throw new BusinessException(
                    InstitutionDoctorEnum.CLIENT_NOT_EXIST.getCode(),
                    InstitutionDoctorEnum.CLIENT_NOT_EXIST.getMessage());
        }

        // 3. 查询 patient
        Patient patient = patientMapper.selectOne(
                new QueryWrapper<Patient>().eq(UUID, clientUuid)
        );
        if (patient == null) {
            throw new BusinessException(
                    InstitutionDoctorEnum.Patient_NOT_EXIST.getCode(),
                    InstitutionDoctorEnum.Patient_NOT_EXIST.getMessage());
        }

        // 4. 校验当前医生是否可覆盖
        Integer currentDoctorNumber = patient.getDoctorNumber();
        if (currentDoctorNumber != null) {
            User currentDoctor = userMapper.selectOne(
                    new QueryWrapper<User>().eq(NUMBER, currentDoctorNumber)
            );
            if (currentDoctor != null && currentDoctor.getRole() != 1) {
                throw new BusinessException(
                        InstitutionDoctorEnum.DOCTOR_ALREADY_BOUND.getCode(),
                        InstitutionDoctorEnum.DOCTOR_ALREADY_BOUND.getMessage()
                );
            }
        }

        // 5. 更新绑定
        patient.setDoctorNumber(doctorNumber);
        int update = patientMapper.update(
                patient,
                new UpdateWrapper<Patient>().eq(UUID, patient.getUuid())
        );

        if (update <= 0) {
            throw new BusinessException(
                    InstitutionDoctorEnum.BIND_UPDATE_FAILED.getCode(),
                    InstitutionDoctorEnum.BIND_UPDATE_FAILED.getMessage()
            );
        }

        return true;
    }

    @Override
    public boolean unbindDoctorByDoctorNumber(BindDoctorDto bindDoctorDto) {
        String clientUuid = bindDoctorDto.getUuid();

        Patient patient = patientMapper.selectOne(
                new QueryWrapper<Patient>().eq(UUID, clientUuid)
        );

        if (patient == null) {
            throw new BusinessException(
                    InstitutionDoctorEnum.Patient_NOT_EXIST.getCode(),
                    InstitutionDoctorEnum.Patient_NOT_EXIST.getMessage()
            );
        }

        patient.setDoctorNumber(DEFAULT_DOCTOR_NUMBER);

        int update = patientMapper.update(
                patient,
                new UpdateWrapper<Patient>().eq(UUID, clientUuid)
        );

        if (update <= 0) {
            throw new BusinessException(
                    InstitutionDoctorEnum.BIND_UPDATE_FAILED.getCode(),
                    InstitutionDoctorEnum.BIND_UPDATE_FAILED.getMessage()
            );
        }

        return true;
    }
}

3. InstitutionServiceImpl

@Service
public class InstitutionServiceImpl extends ServiceImpl<InstitutionMapper, Institution> implements InstitutionService {

    @Autowired
    private InstitutionMapper institutionMapper;

    @Autowired
    private UserMapper userMapper;

    public static final String CATEGORY_ID = "category_id";


    @Override
    public List<Institution> getInstitutionByInstitutionCategoryId(Long institutionCategoryId) {
        // 使用QueryWrapper构建查询条件
        QueryWrapper<Institution> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CATEGORY_ID, institutionCategoryId);
        // 查询符合条件的所有机构
        return institutionMapper.selectList(queryWrapper);
    }
}

四、枚举类

InstitutionDoctorEnum

定义了医生和机构相关的业务异常枚举。

public enum InstitutionDoctorEnum {

    DOCTOR_NOT_EXIST(4031,"医生不存在"),
    CLIENT_NOT_EXIST(4032,"患者不存在"),
    Patient_NOT_EXIST(4033,"患者未绑定,请前往绑定"),
    DOCTOR_ALREADY_BOUND(4034,"用户已绑定医生"),
    BIND_UPDATE_FAILED(4035,"其它绑定错误");


    private Integer code;

    private String message;

    InstitutionDoctorEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

五、接口功能说明

  1. getInstitutionList:根据机构分类ID获取机构列表
  2. getDoctorList:根据机构名称获取该机构的医生列表
  3. bindDoctorByDoctorNumber:通过医生编号为用户绑定医生
  4. unbindDoctorByDoctorNumber:解除用户与医生的绑定关系
  5. getDoctorMsg:根据医生编号获取医生详细信息

六、接口调用示例

1. 获取机构列表

GET /institution-doctor/listInstitution/1

2. 获取医生列表

GET /institution-doctor/listDoctor?institution=某医院

3. 绑定医生

PATCH /institution-doctor/bindDoctorByDoctorNumber
请求体: {"uuid": "用户UUID", "doctorNumber": 12345}

4. 解绑医生

PATCH /institution-doctor/unbindDoctorByDoctorNumber
请求体: {"uuid": "用户UUID", "doctorNumber": 12345}

5. 获取医生信息

GET /institution-doctor/getDoctorMsg/12345

其它问题思考:

  1. pathVariable和requestparam的使用场景
  2. 唯一标识在用户量不大的情况下使用INT和String哪个效率高
  3. 用uuid完全代替id是否合理