package com.flossom.system.controller;

import com.flossom.common.core.constant.UserConstants;
import com.flossom.common.core.domain.entity.SysDept;
import com.flossom.common.core.domain.entity.SysTag;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import com.flossom.common.security.utils.SecurityUtils;
import com.flossom.system.service.ISysDeptService;
import com.flossom.system.service.ISysTagService;
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 flossom
 */
@RestController
@RequestMapping("/tagInfo")
public class SysTagController extends BaseController
{
    @Autowired
    private ISysTagService tagService;

    /**
     * 获取标签列表
     */
    @RequiresPermissions("system:dept:list")
    @GetMapping("/list")
    public AjaxResult list(SysTag sysTag)
    {
        List<SysTag> tags = tagService.selectDeptList(sysTag);
        return success(tags);
    }

    /**
     * 查询标签列表（排除节点）
     */
    @RequiresPermissions("system:tagInfo:list")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
    {
        List<SysTag> tags = tagService.selectDeptList(new SysTag());
        tags.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return success(tags);
    }

    /**
     * 根据标签编号获取详细信息
     */
    @RequiresPermissions("system:tagInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        tagService.checkDeptDataScope(id);
        return success(tagService.selectDeptById(id));
    }

    /**
     * 新增标签
     */
    @RequiresPermissions("system:tagInfo:add")
    @Log(title = "标签管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysTag tag)
    {
        if (!tagService.checkDeptNameUnique(tag))
        {
            return error("新增标签'" + tag.getTagName() + "'失败，标签名称已存在");
        }
        tag.setCreateBy(SecurityUtils.getUsername());
        return toAjax(tagService.insertDept(tag));
    }

    /**
     * 修改标签
     */
    @RequiresPermissions("system:tagInfo:edit")
    @Log(title = "标签管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysTag tag)
    {
        Long id = tag.getId();
        tagService.checkDeptDataScope(id);
        if (!tagService.checkDeptNameUnique(tag))
        {
            return error("修改标签'" + tag.getTagName() + "'失败，标签名称已存在");
        }
        else if (tag.getParentId().equals(id))
        {
            return error("修改标签'" + tag.getTagName() + "'失败，上级标签不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, tag.getStatus()) && tagService.selectNormalChildrenDeptById(id) > 0)
        {
            return error("该标签包含未停用的子标签！");
        }
        tag.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(tagService.updateDept(tag));
    }

    /**
     * 删除标签
     */
    @RequiresPermissions("system:dept:remove")
    @Log(title = "标签管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (tagService.hasChildByDeptId(id))
        {
            return warn("存在下级标签,不允许删除");
        }
//        if (tagService.checkDeptExistUser(id))
//        {
//            return warn("标签存在用户,不允许删除");
//        }
        tagService.checkDeptDataScope(id);
        return toAjax(tagService.deleteDeptById(id));

    }

    /**
     * 获取标签树列表
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/tagTree")
    public AjaxResult tagTree(SysTag tag)
    {
        return success(tagService.selectTagTreeList(tag));
    }
}
