package org.example.service;

import org.example.config.CacheConstants;
import org.example.entity.Hospital;
import org.example.mapper.HospitalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 医院概况服务
 */
@Service
public class HospitalService {

    @Autowired
    private HospitalMapper hospitalMapper;

    /**
     * 获取医院信息
     */
    @Cacheable(value = CacheConstants.CACHE_HOSPITAL, key = "'info'")
    public Hospital getHospital() {
        return hospitalMapper.getHospital();
    }

    /**
     * 保存医院信息（新增或更新）
     */
    @CacheEvict(value = CacheConstants.CACHE_HOSPITAL, allEntries = true)
    public void saveHospital(Hospital hospital) {
        Hospital existing = hospitalMapper.getHospital();
        if (existing == null) {
            hospitalMapper.insert(hospital);
        } else {
            // 使用旧名称作为WHERE条件进行更新
            hospitalMapper.update(hospital, existing.getName());
        }
    }
}
