package com.ruoyi.bst.article.service.impl;

import java.util.List;

import com.ruoyi.bst.area.service.AreaService;
import com.ruoyi.bst.article.service.ArticleService;
import com.ruoyi.bst.articleCategory.domain.ArticleCategoryQuery;
import com.ruoyi.bst.articleCategory.domain.ArticleCategoryVO;
import com.ruoyi.bst.articleCategory.domain.enums.ArticleCategoryStatus;
import com.ruoyi.bst.articleCategory.service.ArticleCategoryService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.article.mapper.ArticleMapper;
import com.ruoyi.bst.article.domain.Article;
import com.ruoyi.bst.article.domain.ArticleVO;
import com.ruoyi.bst.article.domain.ArticleQuery;
import com.ruoyi.bst.article.service.ArticleService;

import javax.print.ServiceUI;

/**
 * 文章Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class ArticleServiceImpl implements ArticleService
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCategoryService articleCategoryService;
    
    /**
     * 查询文章
     *
     * @param id 文章主键
     * @return 文章
     */
    @Override
    public ArticleVO selectArticleById(Long id)
    {
        return articleMapper.selectArticleById(id);
    }

    /**
     * 查询文章列表
     *
     * @param article 文章
     * @return 文章
     */
    @Override
    public List<ArticleVO> selectArticleList(ArticleQuery article)
    {
        return articleMapper.selectArticleList(article);
    }

    /**
     * 新增文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int insertArticle(Article article)
    {
        ArticleCategoryVO articleCategoryVO = articleCategoryService.selectArticleCategoryById(article.getCategoryId());
        ServiceUtil.assertion(articleCategoryVO ==null,"当前分类不存在");
        if (articleCategoryVO.getCategoryStatus().equals(ArticleCategoryStatus.DISABLED.getCode())){
            ServiceUtil.assertion(true,"该分类已被禁用");
        }
        article.setCreateTime(DateUtils.getNowDate());
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章
     *
     * @param article 文章
     * @return 结果
     */
    @Override
    public int updateArticle(Article article)
    {
        ArticleCategoryVO articleCategoryVO = articleCategoryService.selectArticleCategoryById(article.getCategoryId());
        ServiceUtil.assertion(articleCategoryVO ==null,"当前分类不存在");
        if (articleCategoryVO.getCategoryStatus().equals(ArticleCategoryStatus.DISABLED.getCode())){
            ServiceUtil.assertion(true,"该分类已被禁用");
        }
        article.setUpdateTime(DateUtils.getNowDate());
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章
     *
     * @param ids 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Long[] ids)
    {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章信息
     *
     * @param id 文章主键
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id)
    {
        return articleMapper.deleteArticleById(id);
    }


    @Override
    public ArticleVO selectArticleByCode(ArticleCategoryQuery articleCategoryQuery) {
        return articleMapper.selectArticleByCode(articleCategoryQuery);
    }

    @Override
    public List<ArticleVO> selectArticleByCategoryId(Long categoryId) {
        return articleMapper.selectChildrenById(categoryId);
    }
}
