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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.booking.domain.AppBookingResourceQuery;

/**
 * 预约资源列表查询参数规范化。
 */
public final class AppBookingResourceQuerySupport
{
    private AppBookingResourceQuerySupport()
    {
    }

    public static AppBookingResourceQuery normalize(AppBookingResourceQuery query)
    {
        AppBookingResourceQuery effective = query != null ? query : new AppBookingResourceQuery();
        if (StringUtils.isEmpty(effective.getResourceType()))
        {
            throw new ServiceException("资源类型不能为空");
        }
        effective.setResourceType(effective.getResourceType().trim());
        if (!AppointmentResourceRules.isAllowedResourceType(effective.getResourceType()))
        {
            throw new ServiceException("资源类型无效");
        }
        effective.setResourceName(AppointmentResourceRules.normalizeResourceName(effective.getResourceName()));
        if (!AppointmentResourceRules.isValidWeekday(effective.getWeekday()))
        {
            throw new ServiceException("周几参数无效");
        }
        effective.setPageNum(resolvePageNum(effective.getPageNum()));
        effective.setPageSize(resolvePageSize(effective.getPageSize()));
        return effective;
    }

    static int resolvePageNum(Integer pageNum)
    {
        if (pageNum == null || pageNum < 1)
        {
            return AppointmentResourceRules.DEFAULT_PAGE_NUM;
        }
        return pageNum;
    }

    static int resolvePageSize(Integer pageSize)
    {
        if (pageSize == null || pageSize < 1)
        {
            return AppointmentResourceRules.DEFAULT_PAGE_SIZE;
        }
        return Math.min(pageSize, AppointmentResourceRules.MAX_PAGE_SIZE);
    }
}
