package org.dromara.system.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.HospitalActivityDo;
import org.dromara.system.domain.bo.HospitalActivityBo;
import org.dromara.system.domain.vo.HospitalActivityVo;
import org.dromara.system.mapper.HospitalActivityMapper;
import org.dromara.system.service.IHospitalActivityService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author destiny
 * @description:
 * @date 2025/10/14 15:45
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HospitalActivityServiceImpl implements IHospitalActivityService {

    private final HospitalActivityMapper hospitalActivityMapper;

    // ✅ 中文 → 英文 映射（请根据实际数据库字段修改）
    private static final Map<String, String> DEPARTMENT_MAPPING = Map.of(
        "内科", "Internal Medicine",
        "外科", "Surgery",
        "急诊科", "Emergency Department",
        "妇产科", "Obstetrics & Gynecology",
        "儿科", "Pediatrics"
    );

    @Override
    public List<HospitalActivityVo> queryList(Map<String, Object> params) {
        String startDate = MapUtil.getStr(params, "startDate");
        String endDate = MapUtil.getStr(params, "endDate");
        String hospitalName = MapUtil.getStr(params, "hospitalName");
        String department = MapUtil.getStr(params, "department");
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)) {
            throw new IllegalArgumentException("startDate 和 endDate 不能为空");
        }
        // 验证日期格式
        if (!startDate.matches("\\d{4}-\\d{2}-\\d{2}") || !endDate.matches("\\d{4}-\\d{2}-\\d{2}")) {
            throw new IllegalArgumentException("startDate 和 endDate 必须为 yyyy-MM-dd 格式");
        }
        return hospitalActivityMapper.selectVoList(new LambdaQueryWrapper<HospitalActivityDo>()
            // 第一个参数代表条件，直接有条件为真，才添加当前筛选条件
            .eq(StringUtils.isNotBlank(hospitalName), HospitalActivityDo::getHospitalName, hospitalName)
            .eq(StringUtils.isNotBlank(department), HospitalActivityDo::getDepartment,
                StringUtils.isNotBlank(department) ? DEPARTMENT_MAPPING.getOrDefault(department, department): null)
            .between(HospitalActivityDo::getDate, startDate, endDate)
            .orderByAsc(HospitalActivityDo::getDate)
            .orderByAsc(HospitalActivityDo::getHospitalName)
        );

    }
}
