package com.ruoyi.os.service.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.healthcare.HealthcareOsPatientType;
import com.ruoyi.common.enums.healthcare.HealthcareOsSex;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.os.domain.StudentInfo;
import com.ruoyi.os.mapper.StudentInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.os.mapper.HealthcareOsPatientInfoMapper;
import com.ruoyi.os.domain.HealthcareOsPatientInfo;
import com.ruoyi.os.service.IHealthcareOsPatientInfoService;

/**
 * 患者信息Service业务层处理
 * <p>
 * <<<<<<< HEAD
 *
 * @author 蒋松霖
 * =======
 * @author 何顺
 * >>>>>>> cf33b39dcb79314a02d35b6116058d8b46856eeb
 * @date 2024-09-17
 */
@Service
public class HealthcareOsPatientInfoServiceImpl implements IHealthcareOsPatientInfoService {
    @Autowired
    private HealthcareOsPatientInfoMapper healthcareOsPatientInfoMapper;

    @Autowired
    public StudentInfoMapper studentInfoMapper;


    /**
     * 查询患者信息
     *
     * @param id 患者信息主键
     * @return 患者信息
     */
    @Override
    public HealthcareOsPatientInfo selectOsPatientInfoById(Long id) {
        return healthcareOsPatientInfoMapper.selectOsPatientInfoById(id);
    }

    /**
     * 查询患者信息
     *
     * @param studentId 患者信息主键
     * @return 患者信息
     */
    @Override
    public HealthcareOsPatientInfo selectOsPatientInfoByStudentId(String studentId) {
        return healthcareOsPatientInfoMapper.selectOsPatientInfoByStudentId(studentId);
    }

    /**
     * 查询患者信息列表
     *
     * @param osPatientInfo 患者信息
     * @return 患者信息
     */
    @Override
    public List<HealthcareOsPatientInfo> selectOsPatientInfoList(HealthcareOsPatientInfo osPatientInfo) {
        return healthcareOsPatientInfoMapper.selectOsPatientInfoList(osPatientInfo);
    }

    /**
     * 新增患者信息
     *
     * @param osPatientInfo 患者信息
     * @return 结果
     */
    @Override
    public int insertOsPatientInfo(HealthcareOsPatientInfo osPatientInfo) {
        // 判断 学号 和 工号 是否 重复
        HealthcareOsPatientInfo os = new HealthcareOsPatientInfo();
        os.setStudentId(osPatientInfo.getStudentId());
        if (healthcareOsPatientInfoMapper.selectOsPatientInfoList(os).size() > 0) {
            throw new RuntimeException("学号或工号已存在");
        }
        //  添加 时间
        osPatientInfo.setCreateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        osPatientInfo.setCreateBy(loginUser.getUsername());
        return healthcareOsPatientInfoMapper.insertOsPatientInfo(osPatientInfo);
    }

    /**
     * 修改患者信息
     *
     * @param osPatientInfo 患者信息
     * @return 结果
     */
    @Override
    public int updateOsPatientInfo(HealthcareOsPatientInfo osPatientInfo) {
        // 修改 时间
        osPatientInfo.setUpdateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        osPatientInfo.setUpdateBy(loginUser.getUsername());
        return healthcareOsPatientInfoMapper.updateOsPatientInfo(osPatientInfo);
    }

    /**
     * 批量删除患者信息
     *
     * @param ids 需要删除的患者信息主键
     * @return 结果
     */
    @Override
    public int deleteOsPatientInfoByIds(Long[] ids) {
        return healthcareOsPatientInfoMapper.deleteOsPatientInfoByIds(ids);
    }

    /**
     * 删除患者信息信息
     *
     * @param id 患者信息主键
     * @return 结果
     */
    @Override
    public int deleteOsPatientInfoById(Long id) {
        return healthcareOsPatientInfoMapper.deleteOsPatientInfoById(id);
    }


    /**
     * 获取 学生 信息 列表
     */
    public List<HealthcareOsPatientInfo> getStudentInfoList(StudentInfo studentInfo) {
        List<HealthcareOsPatientInfo> list = new ArrayList<>();
        List<StudentInfo> studentInfoList = studentInfoMapper.selectStudentInfoList(studentInfo);
        // 进行 数据 的 赋值
        for (int i = 0; i < studentInfoList.size(); i++) {
            HealthcareOsPatientInfo patientInfo = new HealthcareOsPatientInfo();
            // 就诊人类型    HealthcareOsPatientType
            patientInfo.setPatientType(HealthcareOsPatientType.OS_USAGE);
            // 学号
            patientInfo.setStudentId(studentInfoList.get(i).getStuNo());
            // 姓名
            patientInfo.setName(studentInfoList.get(i).getStuName());
            // 性别(进行处理）
            patientInfo.setGender(Objects.equals(studentInfoList.get(i).getGender(), HealthcareOsSex.SRS_VSI_MALE) ? HealthcareOsSex.SYS_MALE : HealthcareOsSex.SYS_FEMALE);
            // 年龄
            patientInfo.setAge(studentInfoList.get(i).getAge());
            // 专业
            patientInfo.setMajor(studentInfoList.get(i).getMajorName());
            // 班级
            patientInfo.setClassInfo(studentInfoList.get(i).getClassName());
            // 电话
            patientInfo.setPhone(studentInfoList.get(i).getPhone());
            //  辅导员
            patientInfo.setCounselor(studentInfoList.get(i).getTeacherName());
            // 学院
            patientInfo.setDepartment(studentInfoList.get(i).getDeptName());
            // 身份证
            patientInfo.setIdCard(studentInfoList.get(i).getIdCard());
            // ?????????????????????????????????????????? patientId
            HealthcareOsPatientInfo ensured = ensurePatientInfo(patientInfo);
            if (ensured != null) {
                patientInfo.setId(ensured.getId());
            }


            list.add(patientInfo);
        }
        return list;
    }

    @Override
    public List<HealthcareOsPatientInfo> getTeachingStaffInfo(HealthcareOsPatientInfo patientInfo) {
        List<HealthcareOsPatientInfo> list = studentInfoMapper.getTeachingStaffInfo(patientInfo);
        if (list == null || list.isEmpty()) {
            return list;
        }
        list.forEach(item -> {
            HealthcareOsPatientInfo ensured = ensurePatientInfo(item);
            if (ensured != null) {
                item.setId(ensured.getId());
            }
        });
        return list;
    }


    private HealthcareOsPatientInfo ensurePatientInfo(HealthcareOsPatientInfo patientInfo) {
        if (patientInfo == null || patientInfo.getStudentId() == null || patientInfo.getStudentId().isEmpty()) {
            return null;
        }
        HealthcareOsPatientInfo existing = healthcareOsPatientInfoMapper
                .selectOsPatientInfoByStudentId(patientInfo.getStudentId());
        if (existing != null) {
            return existing;
        }
        insertOsPatientInfo(patientInfo);
        return patientInfo;
    }

}
