package com.ruoyi.web.modules.farming.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=005, categoryType=8）。
 */
@Component
public class TechResourceCourseTopicValidator
{
    public static final String CATEGORY_PARENT_CODE = "005";

    public static final String CATEGORY_TYPE = "8";

    @Autowired
    private IBizInformationCategoryService bizInformationCategoryService;

    public void assertAllowed(String courseTopicCode)
    {
        if (StringUtils.isEmpty(courseTopicCode))
        {
            throw new ServiceException("课程主题不能为空");
        }
        if (!existsInCategoryChildren(courseTopicCode.trim()))
        {
            throw new ServiceException("课程主题无效，请选择资讯类别中的类型");
        }
    }

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