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

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.article.service.ArticleService;
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.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
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 org.springframework.transaction.support.TransactionTemplate;

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

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private TransactionTemplate transactionTemplate;
    
    /**
     * 查询文章
     *
     * @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,"该分类已被禁用");
        }

        Integer result = transactionTemplate.execute(status -> {

            int insert = articleMapper.insertArticle(article);
            ServiceUtil.assertion(insert != 1,"新增文章失败");
            ArticleVO vo = this.selectArticleById(article.getId());

            ArticleQuery articleQuery = new ArticleQuery();
            articleQuery.setId(vo.getId());
            articleQuery.setCode(vo.getCode());
            ArticleVO articleVO = articleMapper.selectArticleByCode(articleQuery);
            ServiceUtil.assertion(articleVO != null,"该指南已存在");
            return insert;
        });
        return result == null ? 0:result;
    }

    /**
     * 修改文章
     *
     * @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());
        ArticleQuery articleQuery = new ArticleQuery();
        articleQuery.setId(article.getId());
        articleQuery.setCode(article.getCode());
        ArticleVO articleVO = articleMapper.selectArticleByCode(articleQuery);
        ServiceUtil.assertion(articleVO != null,"该指南已存在");
        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 List<ArticleVO> selectArticleByCategoryId(Long categoryId) {
        return articleMapper.selectChildrenById(categoryId);
    }

    @Override
    public ArticleVO selectArticleByCode(String code, boolean scope) {
        if (code == null) {
            return null;
        }

        ArticleQuery query = new ArticleQuery();
        query.setCode(code);
        query.setScope(scope);
        return this.selectOne(query);
    }

    private ArticleVO selectOne(ArticleQuery query) {
        PageHelper.startPage(1, 1);
        List<ArticleVO> list = articleMapper.selectArticleList(query);
        return CollectionUtils.firstElement(list);
    }
}
