package com.ruoyi.web.modules.epidemic.support;

import java.util.Calendar;
import java.util.Date;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.epidemic.domain.BizEpidemicReport;
import com.ruoyi.web.modules.epidemic.domain.dto.EpidemicReportRelieveRequest;

/**
 * 疫情上报入参校验。
 */
public final class EpidemicReportValidation
{
    private EpidemicReportValidation()
    {
    }

    public static void trimFieldsForSave(BizEpidemicReport row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getAffectedLocationTrack() != null)
        {
            row.setAffectedLocationTrack(row.getAffectedLocationTrack().trim());
        }
        if (row.getEpidemicName() != null)
        {
            row.setEpidemicName(row.getEpidemicName().trim());
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateReportDate(Date reportDate)
    {
        if (reportDate == null)
        {
            throw new ServiceException("上报日期不能为空");
        }
    }

    public static void validateTransmissionRoute(Integer transmissionRoute)
    {
        if (!EpidemicReportRules.isTransmissionRouteValid(transmissionRoute))
        {
            throw new ServiceException("传播途径须为1～4（1调运传入2本地扩散3环境感染4其他未知）");
        }
    }

    public static void validateEpidemicType(Integer epidemicType)
    {
        if (!EpidemicReportRules.isEpidemicTypeValid(epidemicType))
        {
            throw new ServiceException("疫病类型须为1～4（1病毒性2细菌性3寄生虫4其他）");
        }
    }

    public static void validateRiskLevel(Integer riskLevel)
    {
        if (!EpidemicReportRules.isRiskLevelValid(riskLevel))
        {
            throw new ServiceException("风险等级须为1～3（1高危2中危3低危）");
        }
    }

    public static void validateAffectedLocationTrack(String track)
    {
        if (StringUtils.isEmpty(track) || track.length() < 1 || track.length() > 500)
        {
            throw new ServiceException("涉疫点位/轨迹须为1～500个字符");
        }
    }

    public static void validateEpidemicName(String epidemicName)
    {
        if (StringUtils.isEmpty(epidemicName) || epidemicName.length() < 1 || epidemicName.length() > 50)
        {
            throw new ServiceException("疫病名称须为1～50个字符");
        }
    }

    public static void validateRowForSave(BizEpidemicReport row)
    {
        trimFieldsForSave(row);
        validateReportDate(row.getReportDate());
        validateTransmissionRoute(row.getTransmissionRoute());
        validateAffectedLocationTrack(row.getAffectedLocationTrack());
        validateEpidemicType(row.getEpidemicType());
        validateEpidemicName(row.getEpidemicName());
        validateRiskLevel(row.getRiskLevel());
    }

    public static void validateRelieveRequest(EpidemicReportRelieveRequest request, Date reportDate)
    {
        if (request == null || request.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        if (request.getReleaseDate() == null)
        {
            throw new ServiceException("解除日期不能为空");
        }
        validateReleaseDateRange(reportDate, request.getReleaseDate());
    }

    public static void validateReleaseDateRange(Date reportDate, Date releaseDate)
    {
        if (reportDate == null || releaseDate == null)
        {
            throw new ServiceException("上报日期或解除日期不能为空");
        }
        Calendar report = Calendar.getInstance();
        report.setTime(reportDate);
        truncateToDay(report);
        Calendar release = Calendar.getInstance();
        release.setTime(releaseDate);
        truncateToDay(release);
        if (release.before(report))
        {
            throw new ServiceException("解除日期不能早于上报日期");
        }
        Calendar today = Calendar.getInstance();
        truncateToDay(today);
        if (release.after(today))
        {
            throw new ServiceException("解除日期不能为未来日期");
        }
    }

    private static void truncateToDay(Calendar cal)
    {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
    }
}
