package org.example.service;

import org.example.config.CacheConstants;
import org.example.entity.PageResult;
import org.example.entity.Patient;
import org.example.mapper.PatientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 患者服务
 */
@Service
public class PatientService {

    @Autowired
    private PatientMapper patientMapper;

    /**
     * 获取页面初始化数据（合并查询，带缓存，支持分页）
     */
    public Map<String, Object> getInitData(int page, int pageSize) {
        Map<String, Object> data = new HashMap<>();
        data.put("deptList", patientMapper.findAllDeptCode());
        data.put("wardList", patientMapper.findWardsByDept(null));
        data.put("roomList", patientMapper.findRoomsByWard(null));
        data.put("bedList", patientMapper.findBedsByRoom(null));
        data.put("inpatientStatusList", patientMapper.findAllInpatientStatus());
        data.put("nurseLevelList", patientMapper.findAllNurseLevel());
        // 分页处理患者列表
        List<Patient> allPatients = patientMapper.findList(null, null, null, null, null, null, null, null, null, null);
        data.put("patientList", PageResult.of(allPatients, page, pageSize));
        return data;
    }

    /**
     * 查询患者列表
     */
    public List<Patient> findList(String name, String deptCode, String wardCode, String roomNo,
                                   String bedNo, String inpatientNo, String nurseLevel,
                                   String inpatientStatus, Date admissionDateStart, Date admissionDateEnd) {
        return patientMapper.findList(name, deptCode, wardCode, roomNo, bedNo, inpatientNo,
                nurseLevel, inpatientStatus, admissionDateStart, admissionDateEnd);
    }

    /**
     * 根据ID查询患者
     */
    @Cacheable(value = CacheConstants.CACHE_PATIENT, key = "'id:' + #id")
    public Patient findById(Long id) {
        return patientMapper.findById(id);
    }

    /**
     * 保存患者
     */
    @CacheEvict(value = {CacheConstants.CACHE_PATIENT, CacheConstants.CACHE_OPTIONS}, allEntries = true)
    public void save(Patient patient) {
        if (patient.getId() == null) {
            patientMapper.insert(patient);
        } else {
            patientMapper.update(patient);
        }
    }

    /**
     * 删除患者
     */
    @CacheEvict(value = {CacheConstants.CACHE_PATIENT, CacheConstants.CACHE_OPTIONS}, allEntries = true)
    public void deleteById(Long id) {
        patientMapper.deleteById(id);
    }

    /**
     * 获取所有不重复的所属科室
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:dept'")
    public List<String> findAllDeptCode() {
        return patientMapper.findAllDeptCode();
    }

    /**
     * 根据科室获取病区列表
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:ward:' + #deptCode")
    public List<String> findWardsByDept(String deptCode) {
        return patientMapper.findWardsByDept(deptCode);
    }

    /**
     * 根据病区获取房间号列表
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:room:' + #wardCode")
    public List<String> findRoomsByWard(String wardCode) {
        return patientMapper.findRoomsByWard(wardCode);
    }

    /**
     * 根据房间号获取床位号列表
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:bed:' + #roomNo")
    public List<String> findBedsByRoom(String roomNo) {
        return patientMapper.findBedsByRoom(roomNo);
    }
    
    /**
     * 获取所有不重复的在院状态
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:inpatientStatus'")
    public List<String> findAllInpatientStatus() {
        return patientMapper.findAllInpatientStatus();
    }
    
    /**
     * 获取所有不重复的护理级别
     */
    @Cacheable(value = CacheConstants.CACHE_OPTIONS, key = "'patient:nurseLevel'")
    public List<String> findAllNurseLevel() {
        return patientMapper.findAllNurseLevel();
    }
}
