package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Doctor;
import com.example.entity.DoctorPatient;
import com.example.entity.Patient;
import com.example.mapper.DoctorMapper;
import com.example.mapper.DoctorPatientMapper;
import com.example.mapper.PatientMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DoctorPatientService {

    @Resource
    private DoctorPatientMapper doctorPatientMapper;
    
    @Resource
    private DoctorMapper doctorMapper;
    
    @Resource
    private PatientMapper patientMapper;

    /**
     * 新增医生患者关系
     * @param doctorPatient 关系信息
     */
    public void add(DoctorPatient doctorPatient) {
        // 检查医生ID是否存在
        Doctor doctor = doctorMapper.selectById(doctorPatient.getDoctorId());
        if (ObjectUtil.isEmpty(doctor)) {
            throw new RuntimeException("医生不存在");
        }
        
        // 检查患者ID是否存在
        Patient patient = patientMapper.selectById(doctorPatient.getPatientId());
        if (ObjectUtil.isEmpty(patient)) {
            throw new RuntimeException("患者不存在");
        }
        
        // 检查关系是否已存在
        LambdaQueryWrapper<DoctorPatient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DoctorPatient::getDoctorId, doctorPatient.getDoctorId())
                .eq(DoctorPatient::getPatientId, doctorPatient.getPatientId())
                .eq(DoctorPatient::getStatus, "正常");
        
        if (doctorPatientMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("医生和患者关系已存在");
        }
        
        // 设置默认值
        doctorPatient.setRelationshipStart(new Date());
        doctorPatient.setStatus("正常");
        doctorPatient.setCreateTime(new Date());
        
        doctorPatientMapper.insert(doctorPatient);
    }
    
    /**
     * 更新医生患者关系
     * @param doctorPatient 关系信息
     */
    public void update(DoctorPatient doctorPatient) {
        DoctorPatient dbDoctorPatient = doctorPatientMapper.selectById(doctorPatient.getId());
        if (ObjectUtil.isEmpty(dbDoctorPatient)) {
            throw new RuntimeException("关系不存在");
        }
        
        doctorPatientMapper.updateById(doctorPatient);
    }
    
    /**
     * 删除医生患者关系
     * @param id 关系ID
     */
    public void delete(Integer id) {
        doctorPatientMapper.deleteById(id);
    }
    
    /**
     * 结束医生患者关系
     * @param id 关系ID
     */
    public void endRelationship(Integer id) {
        DoctorPatient doctorPatient = doctorPatientMapper.selectById(id);
        if (ObjectUtil.isEmpty(doctorPatient)) {
            throw new RuntimeException("关系不存在");
        }
        
        doctorPatient.setStatus("已结束");
        doctorPatientMapper.updateById(doctorPatient);
    }
    
    /**
     * 根据医生ID查询患者
     * @param doctorId 医生ID
     * @param name 患者姓名（可选）
     * @return 患者列表
     */
    public List<DoctorPatient> selectByDoctorId(Integer doctorId, String name) {
        return doctorPatientMapper.selectByDoctorId(doctorId, name);
    }
    
    /**
     * 根据患者ID查询医生
     * @param patientId 患者ID
     * @param name 医生姓名（可选）
     * @return 医生列表
     */
    public List<DoctorPatient> selectByPatientId(Integer patientId, String name) {
        return doctorPatientMapper.selectByPatientId(patientId, name);
    }
} 