package com.ruoyi.web.controller.ss;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SmArticleClassify;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ss.articleClassify.domain.SmArticleClassifyQuery;
import com.ruoyi.ss.articleClassify.service.ISmArticleClassifyService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/article/classify")
public class SmArticleClassifyController extends BaseController
{
    @Autowired
    private ISmArticleClassifyService smArticleClassifyService;

    /**
     * 获取分类列表
     */
    @GetMapping("/list")
    public AjaxResult list(SmArticleClassifyQuery asArticleClassify)
    {
        List<SmArticleClassify> asArticleClassifies = smArticleClassifyService.selectClassifyList(asArticleClassify);
        return success(asArticleClassifies);
    }

    /**
     * 查询分类列表（排除节点）
     */
    @GetMapping("/list/exclude/{classifyId}")
    public AjaxResult excludeChild(@PathVariable(value = "classifyId", required = false) Long classifyId)
    {
        List<SmArticleClassify> articleClassifies = smArticleClassifyService.selectClassifyList(new SmArticleClassifyQuery());
        articleClassifies.removeIf(d -> d.getClassifyId().intValue() == classifyId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), classifyId + ""));
        return success(articleClassifies);
    }

    /**
     * 根据分类编号获取详细信息
     */
    @GetMapping(value = "/{classifyId}")
    public AjaxResult getInfo(@PathVariable Long classifyId)
    {
        smArticleClassifyService.checkClassifyDataScope(classifyId);
        return success(smArticleClassifyService.selectClassifyById(classifyId));
    }

    /**
     * 新增分类
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SmArticleClassify dept)
    {
        if (!smArticleClassifyService.checkClassifyNameUnique(dept))
        {
            return error("新增分类'" + dept.getClassifyName() + "'失败，分类名称已存在");
        }
        dept.setCreateBy(getUsername());
        return toAjax(smArticleClassifyService.insertClassify(dept));
    }

    /**
     * 修改分类
     */
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SmArticleClassify articleClassify)
    {
        Long classifyId = articleClassify.getClassifyId();
        smArticleClassifyService.checkClassifyDataScope(classifyId);
        if (!smArticleClassifyService.checkClassifyNameUnique(articleClassify))
        {
            return error("修改分类'" + articleClassify.getClassifyName() + "'失败，分类名称已存在");
        }
        else if (articleClassify.getParentId().equals(classifyId))
        {
            return error("修改分类'" + articleClassify.getClassifyName() + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, articleClassify.getStatus()) && smArticleClassifyService.selectNormalChildrenClassifyById(classifyId) > 0)
        {
            return error("该分类包含未停用的子分类！");
        }
        articleClassify.setUpdateBy(getUsername());
        return toAjax(smArticleClassifyService.updateClassify(articleClassify));
    }

    /**
     * 删除分类
     */
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classifyId}")
    public AjaxResult remove(@PathVariable Long classifyId)
    {
        if (smArticleClassifyService.hasChildByclassifyId(classifyId))
        {
            return warn("存在下级分类,不允许删除");
        }
        if (smArticleClassifyService.checkClassifyExistArticle(classifyId))
        {
            return warn("分类存在文章,不允许删除");
        }
        if (smArticleClassifyService.checkIsBuiltSystem(classifyId))
        {
            return warn("系统内置分类，不允许删除");
        }
        smArticleClassifyService.checkClassifyDataScope(classifyId);
        return toAjax(smArticleClassifyService.deleteClassifyById(classifyId));
    }
}
