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

import java.util.*;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.area.domain.AreaQuery;
import com.ruoyi.bst.area.domain.AreaVO;
import com.ruoyi.bst.article.domain.ArticleVO;
import com.ruoyi.bst.article.mapper.ArticleMapper;
import com.ruoyi.bst.article.service.ArticleService;
import com.ruoyi.bst.articleCategory.domain.enums.ArticleCategoryStatus;
import com.ruoyi.bst.device.utils.DeviceUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bst.articleCategory.mapper.ArticleCategoryMapper;
import com.ruoyi.bst.articleCategory.domain.ArticleCategory;
import com.ruoyi.bst.articleCategory.domain.ArticleCategoryVO;
import com.ruoyi.bst.articleCategory.domain.ArticleCategoryQuery;
import com.ruoyi.bst.articleCategory.service.ArticleCategoryService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@Service
public class ArticleCategoryServiceImpl implements ArticleCategoryService
{
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询文章分类
     *
     * @param id 文章分类主键
     * @return 文章分类
     */
    @Override
    public ArticleCategoryVO selectArticleCategoryById(Long id)
    {
        if (id == null) {
            return null;
        }
        ArticleCategoryQuery query = new ArticleCategoryQuery();
        query.setId(id);
        return this.selectOne(query);
    }

    public ArticleCategoryVO selectOne(ArticleCategoryQuery query){
            PageHelper.startPage(1, 1);
            List<ArticleCategoryVO> list = articleCategoryMapper.selectArticleCategoryList(query);
            return CollectionUtils.firstElement(list);
    }
    /**
     * 查询文章分类列表
     *
     * @param articleCategoryQuery 文章分类
     * @return 文章分类
     */
    @Override
    public List<ArticleCategoryVO> selectArticleCategoryList(ArticleCategoryQuery articleCategoryQuery)
    {
        return articleCategoryMapper.selectArticleCategoryList(articleCategoryQuery);
    }

    /**
     * 新增文章分类
     *
     * @param articleCategory 文章分类
     * @return 结果
     */
    @Override
    public int insertArticleCategory(ArticleCategory articleCategory)
    {
        //目标分类不允许子分类重名
        Boolean flag = articleCategoryMapper.checkByNameAndParetId(articleCategory);
        if (flag){
            ServiceUtil.assertion(true,"不允许插入重复分类");
        }

        if(articleCategory.getParentId()!= null) {
            ArticleCategoryVO vo = articleCategoryMapper.selectArticleCategoryById(articleCategory.getParentId());
            ServiceUtil.assertion(vo==null,"当前分类不存在");
            //
            if (vo.getParentId() != null && vo.getParentId()!=0) {
                Boolean hasParent = hasParent(articleCategory.getParentId());
                ServiceUtil.assertion(hasParent,"该分类下不允许再次分类");
            }
        }
        articleCategory.setCreateTime(DateUtils.getNowDate());
        return articleCategoryMapper.insertArticleCategory(articleCategory);
    }

    /**
     * 修改文章分类
     *
     * @param articleCategory 文章分类
     * @return 结果
     */
    @Override
    public int updateArticleCategory(ArticleCategory articleCategory)
    {
        ArticleCategoryVO vo = articleCategoryMapper.selectArticleCategoryById(articleCategory.getId());
        //检查是否需要分类校验
        if (vo != null && vo.getParentId().equals(articleCategory.getParentId())&&vo.getCategoryName().equals(articleCategory.getCategoryName())) {
            // 新增状态变更方向判断
            boolean isEnabling = vo.getCategoryStatus().equals(ArticleCategoryStatus.DISABLED.getCode())
                    && articleCategory.getCategoryStatus().equals(ArticleCategoryStatus.ENABLED.getCode());

            boolean isDisabling = vo.getCategoryStatus().equals(ArticleCategoryStatus.ENABLED.getCode())
                    && articleCategory.getCategoryStatus().equals(ArticleCategoryStatus.DISABLED.getCode());
            // 启用操作校验
            if (isEnabling) {
                if (articleCategory.getParentId() != null) {
                    ArticleCategoryVO parentVO = articleCategoryMapper.selectArticleCategoryById(articleCategory.getParentId());
                    ServiceUtil.assertion(parentVO.getCategoryStatus().equals(ArticleCategoryStatus.DISABLED.getCode()),
                            "该分类所属分类已禁用，无法启用该分类");
                }
            }
            // 禁用操作校验
            else if (isDisabling) {
                List<ArticleCategoryVO> childrenCategoryList = articleCategoryMapper.selectChildrenCategory(articleCategory.getId());
                boolean hasEnabledChild = childrenCategoryList.stream()
                        .anyMatch(child -> child.getCategoryStatus().equals(ArticleCategoryStatus.ENABLED.getCode()));
                ServiceUtil.assertion(hasEnabledChild, "当前分类下有启用的分类，不可禁用");
            }
            return articleCategoryMapper.updateArticleCategorySortAndStatusOnly(articleCategory);
        }
        //目标分类不允许子分类重名
        Boolean flag = articleCategoryMapper.checkByNameAndParetId(articleCategory);
        if (flag){
            ServiceUtil.assertion(true,"目标分类下已经存在相同名称的分类");
        }
        //只允许两次分类
        if(articleCategory.getParentId()!= null) {
            Boolean hasParent = hasParent(articleCategory.getParentId());
            ServiceUtil.assertion(hasParent,"该分类下不允许再次分类");
        }
        return articleCategoryMapper.updateArticleCategory(articleCategory);
    }


    private Boolean hasParent(Long id){
        if (id == 0){
            return false;
        }
        Long parentId = selectArticleCategoryById(id).getParentId();
        if (parentId != null){
            return true;
        }
        return false;
    }


    /**
     * 删除文章分类信息
     *
     * @param categoryId 文章分类主键
     * @return 结果
     */
    @Override
    public int deleteArticleCategoryById(Long categoryId)
    {
        // 如果当前分类有子分类，先删除子分类
        List<ArticleCategoryVO> childrenCategoryList = articleCategoryMapper.selectChildrenCategory(categoryId);
        ServiceUtil.assertion(!childrenCategoryList.isEmpty(),"请先删除当前分类下的子分类");
        // 如果当前分类下有文章，先删除文章
        List<ArticleVO> articleList = articleService.selectArticleByCategoryId(categoryId);
        ServiceUtil.assertion(!articleList.isEmpty(),"当前分类下存在文章，请先删除分类下的文章");
        return articleCategoryMapper.deleteArticleCategoryById(categoryId);
    }
}
