package com.dgtly.web.controller.system;

import java.util.List;

import com.dgtly.system.domain.SysUserAuthorRel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.system.domain.SysUserOrderAuthor;
import com.dgtly.system.service.ISysUserOrderAuthorService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.common.core.domain.Ztree;

/**
 * 权限Controller
 * 
 * @author dgtly
 * @date 2022-03-22
 */
@Controller
@RequestMapping("/system/author")
public class SysUserOrderAuthorController extends BaseController
{
    private String prefix = "system/author";

    @Autowired
    private ISysUserOrderAuthorService sysUserOrderAuthorService;

    @RequiresPermissions("system:author:view")
    @GetMapping()
    public String author()
    {
        return prefix + "/author";
    }

    /**
     * 查询权限树列表
     */
    @RequiresPermissions("system:author:list")
    @PostMapping("/list")
    @ResponseBody
    public List<SysUserOrderAuthor> list(SysUserOrderAuthor sysUserOrderAuthor)
    {
        List<SysUserOrderAuthor> list = sysUserOrderAuthorService.selectSysUserOrderAuthorList(sysUserOrderAuthor);
        return list;
    }

    /**
     * 导出权限列表
     */
    @RequiresPermissions("system:author:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserOrderAuthor sysUserOrderAuthor)
    {
        List<SysUserOrderAuthor> list = sysUserOrderAuthorService.selectSysUserOrderAuthorList(sysUserOrderAuthor);
        ExcelUtil<SysUserOrderAuthor> util = new ExcelUtil<SysUserOrderAuthor>(SysUserOrderAuthor.class);
        return util.exportExcel(list, "author");
    }

    /**
     * 新增权限
     */
    @GetMapping(value = { "/add/{authorId}", "/add/" })
    public String add(@PathVariable(value = "authorId", required = false) Long authorId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(authorId))
        {
            mmap.put("sysUserOrderAuthor", sysUserOrderAuthorService.selectSysUserOrderAuthorById(authorId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存权限
     */
    @RequiresPermissions("system:author:add")
    @Log(title = "权限", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserOrderAuthor sysUserOrderAuthor)
    {
        return toAjax(sysUserOrderAuthorService.insertSysUserOrderAuthor(sysUserOrderAuthor));
    }

    /**
     * 修改权限
     */
    @GetMapping("/edit/{authorId}")
    public String edit(@PathVariable("authorId") Long authorId, ModelMap mmap)
    {
        SysUserOrderAuthor sysUserOrderAuthor = sysUserOrderAuthorService.selectSysUserOrderAuthorById(authorId);
        mmap.put("sysUserOrderAuthor", sysUserOrderAuthor);
        return prefix + "/edit";
    }

    /**
     * 修改保存权限
     */
    @RequiresPermissions("system:author:edit")
    @Log(title = "权限", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserOrderAuthor sysUserOrderAuthor)
    {
        return toAjax(sysUserOrderAuthorService.updateSysUserOrderAuthor(sysUserOrderAuthor));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:author:remove")
    @Log(title = "权限", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{authorId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("authorId") Long authorId)
    {
        return toAjax(sysUserOrderAuthorService.deleteSysUserOrderAuthorById(authorId));
    }

    @RequiresPermissions("system:author:edit")
    @Log(title = "修改用户权限", businessType = BusinessType.DELETE)
    @PostMapping("/tree/edit")
    @ResponseBody
    public AjaxResult editAuthor(SysUserAuthorRel item)
    {
        return toAjax(sysUserOrderAuthorService.insertRel(item));
    }

    /**
     * 查询用户权限树
     */
    @RequiresPermissions("system:author:config")
    @GetMapping("/tree/{userId}")
    @ResponseBody
    public List<Ztree> list(@PathVariable("userId") Long userId)
    {
        List<Ztree> list = sysUserOrderAuthorService.userAuthorTreeData(userId);
        return list;
    }

    /**
     * 选择权限树
     */
    @GetMapping(value = { "/selectAuthorTree/{authorId}", "/selectAuthorTree/" })
    public String selectAuthorTree(@PathVariable(value = "authorId", required = false) Long authorId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(authorId))
        {
            mmap.put("sysUserOrderAuthor", sysUserOrderAuthorService.selectSysUserOrderAuthorById(authorId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载权限树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = sysUserOrderAuthorService.selectSysUserOrderAuthorTree();
        return ztrees;
    }
}
