package com.ruoyi.web.modules.innovation.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=002）。
 */
@Component
public class TechProjectGuideTypeValidator
{
    public static final String CATEGORY_PARENT_CODE = "002";

    @Autowired
    private IBizInformationCategoryService bizInformationCategoryService;

    public void assertAllowed(String projectTypeCode)
    {
        TechProjectGuideValidation.validateProjectType(projectTypeCode);
        if (!existsInCategoryChildren(projectTypeCode))
        {
            throw new ServiceException("项目类型无效，请选择资讯类别中的类型");
        }
    }

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