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

import java.util.Date;
import com.ruoyi.web.modules.diagnosis.domain.BizMedicalResource;

/**
 * 列表/详情展示用发布三态（未发布 / 已发布 / 已下架）。
 */
public final class MedicalResourcePublishStatusResolver
{
    private MedicalResourcePublishStatusResolver()
    {
    }

    public static void applyDisplayStatus(BizMedicalResource row)
    {
        if (row == null)
        {
            return;
        }
        int display = resolveDisplayCode(row.getPublishStatus(), row.getOfflineTime());
        row.setDisplayPublishStatus(display);
        row.setDisplayPublishStatusName(resolveDisplayName(display));
        row.setCanEdit(MedicalResourceRules.canEdit(row.getPublishStatus()));
        row.setCanOffline(MedicalResourceRules.canOffline(row.getPublishStatus()));
        row.setCanPublish(MedicalResourceRules.canPublish(row.getPublishStatus()));
        row.setCanDelete(MedicalResourceRules.canDelete(row.getPublishStatus()));
        row.setCanAssignAccount(canAssignAccount(row));
    }

    public static int resolveDisplayCode(Integer publishStatus, Date offlineTime)
    {
        if (MedicalResourceRules.isPublished(publishStatus))
        {
            return MedicalResourceRules.QUERY_PUBLISH_PUBLISHED;
        }
        if (offlineTime != null)
        {
            return MedicalResourceRules.QUERY_PUBLISH_OFFLINE;
        }
        return MedicalResourceRules.QUERY_PUBLISH_UNPUBLISHED;
    }

    public static String resolveDisplayName(int displayCode)
    {
        switch (displayCode)
        {
            case MedicalResourceRules.QUERY_PUBLISH_PUBLISHED:
                return "已发布";
            case MedicalResourceRules.QUERY_PUBLISH_OFFLINE:
                return "已下架";
            default:
                return "未发布";
        }
    }

    private static boolean canAssignAccount(BizMedicalResource row)
    {
        return MedicalResourceRules.canAssignAccount(row.getResourceType())
                && row.getAccountAssigned() != null
                && row.getAccountAssigned() == MedicalResourceRules.ACCOUNT_ASSIGNED_NO
                && MedicalResourceRules.canEdit(row.getPublishStatus());
    }
}
