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

import java.util.Calendar;
import java.util.Date;
import com.ruoyi.web.modules.industrypolicy.domain.BizSubsidyProject;

/**
 * 执行状态自动推算（§2.2）。
 */
public final class SubsidyProjectStatusResolver
{
    private SubsidyProjectStatusResolver()
    {
    }

    /**
     * 仅计算执行状态，不修改实际完成日期。
     */
    public static int resolveExecStatus(Date projectStartDate, Date plannedEndDate, Date today)
    {
        Date s = truncateToDay(projectStartDate);
        Date e = truncateToDay(plannedEndDate);
        Date t = truncateToDay(today != null ? today : new Date());
        if (t.before(s))
        {
            return SubsidyProjectRules.EXEC_NOT_STARTED;
        }
        if (sameDay(t, e))
        {
            return SubsidyProjectRules.EXEC_COMPLETED;
        }
        if (t.before(e))
        {
            return SubsidyProjectRules.EXEC_IN_PROGRESS;
        }
        return SubsidyProjectRules.EXEC_IN_PROGRESS;
    }

    /**
     * manual_complete_flag=0 时按规则写 exec_status、actual_end_date。
     */
    public static void applyAutoResolution(BizSubsidyProject row, Date today)
    {
        if (row == null)
        {
            return;
        }
        if (row.getManualCompleteFlag() != null
                && row.getManualCompleteFlag() == SubsidyProjectRules.MANUAL_FLAG_LOCKED)
        {
            return;
        }
        Date t = truncateToDay(today != null ? today : new Date());
        int status = resolveExecStatus(row.getProjectStartDate(), row.getPlannedEndDate(), t);
        row.setExecStatus(status);
        if (status == SubsidyProjectRules.EXEC_COMPLETED)
        {
            if (row.getActualEndDate() == null)
            {
                row.setActualEndDate(t);
            }
        }
        else
        {
            row.setActualEndDate(null);
        }
    }

    public static boolean sameDay(Date a, Date b)
    {
        if (a == null || b == null)
        {
            return false;
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(truncateToDay(a));
        Calendar cb = Calendar.getInstance();
        cb.setTime(truncateToDay(b));
        return ca.get(Calendar.YEAR) == cb.get(Calendar.YEAR)
                && ca.get(Calendar.DAY_OF_YEAR) == cb.get(Calendar.DAY_OF_YEAR);
    }

    public static Date truncateToDay(Date date)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 保存时执行状态与手工锁定规则（§3.3.1）。
     */
    public static void applySaveExecRules(BizSubsidyProject row)
    {
        if (row.getExecStatus() != null && row.getExecStatus() == SubsidyProjectRules.EXEC_COMPLETED)
        {
            SubsidyProjectValidation.validateActualEndDateWhenCompleted(row.getActualEndDate());
            row.setManualCompleteFlag(SubsidyProjectRules.MANUAL_FLAG_LOCKED);
        }
        else
        {
            row.setManualCompleteFlag(SubsidyProjectRules.MANUAL_FLAG_AUTO);
            row.setActualEndDate(null);
            applyAutoResolution(row, new Date());
        }
    }
}
