package com.ruoyi.web.modules.category.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.category.domain.BizInformationCategory;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryTreeVo;
import com.ruoyi.web.modules.category.mapper.BizInformationCategoryMapper;
import com.ruoyi.web.modules.category.service.IBizInformationCategoryService;
import com.ruoyi.web.modules.category.support.InformationCategoryConvert;
import com.ruoyi.web.modules.category.support.InformationCategoryRules;
import com.ruoyi.web.modules.category.support.InformationCategoryTreeBuilder;

/**
 * 资讯类别 Service 实现。
 */
@Service
public class BizInformationCategoryServiceImpl implements IBizInformationCategoryService
{
    @Autowired
    private BizInformationCategoryMapper bizInformationCategoryMapper;

    @Override
    public List<InformationCategoryTreeVo> selectCategoryTree(BizInformationCategory query)
    {
        BizInformationCategory criteria = query != null ? query : new BizInformationCategory();
        List<BizInformationCategory> rows = bizInformationCategoryMapper.selectVisibleActiveForTree(criteria);
        if (rows == null)
        {
            return new ArrayList<>();
        }
        return InformationCategoryTreeBuilder.build(rows);
    }

    @Override
    public List<InformationCategoryItemVo> selectChildren(String parentCode, String categoryType)
    {
        if (StringUtils.isEmpty(parentCode))
        {
            throw new ServiceException(InformationCategoryRules.MSG_PARENT_CODE_REQUIRED);
        }
        BizInformationCategory criteria = new BizInformationCategory();
        criteria.setParentCode(parentCode.trim());
        if (StringUtils.isNotEmpty(categoryType))
        {
            criteria.setCategoryType(categoryType.trim());
        }
        List<BizInformationCategory> rows = bizInformationCategoryMapper.selectChildrenByParent(criteria);
        return InformationCategoryConvert.toItemList(rows);
    }
}
