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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.category.service.IBizInformationCategoryService;

/**
 * 饲喂标准类型须为资讯类别下级（parentCode=001, categoryType=2）。
 */
@Component
public class FeedingStandardTypeValidator
{
    public static final String CATEGORY_PARENT_CODE = "001";

    public static final String CATEGORY_TYPE = "2";

    @Autowired
    private IBizInformationCategoryService bizInformationCategoryService;

    public void assertAllowed(String standardTypeCode)
    {
        FeedingStandardValidation.validateStandardType(standardTypeCode);
        if (!existsInCategoryChildren(standardTypeCode))
        {
            throw new ServiceException("标准类型无效，请选择资讯类别中的类型");
        }
    }

    private boolean existsInCategoryChildren(String standardTypeCode)
    {
        String code = standardTypeCode.trim();
        for (InformationCategoryItemVo item : bizInformationCategoryService.selectChildren(
                CATEGORY_PARENT_CODE, CATEGORY_TYPE))
        {
            if (item != null && code.equals(StringUtils.trim(item.getCode())))
            {
                return true;
            }
        }
        return false;
    }
}
