package com.inspect.system.controller;

import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.domain.SysMenu;
import com.inspect.system.service.ISysMenuService;

import java.util.List;

import org.springframework.validation.annotation.Validated;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/menu"})
public class SysMenuController extends BaseController {
    private final ISysMenuService menuService;

    public SysMenuController(ISysMenuService menuService) {
        this.menuService = menuService;
    }

    @RequiresPermissions({"system:menu:list"})
    @GetMapping({"/list"})
    public AjaxResult list(SysMenu menu) {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = this.menuService.selectMenuList(menu, userId);
        return AjaxResult.success(menus);
    }

    @RequiresPermissions({"system:menu:query"})
    @GetMapping({"/{menuId}"})
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(this.menuService.selectMenuById(menuId));
    }

    @GetMapping({"/treeselect"})
    public AjaxResult treeSelect(SysMenu menu) {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = this.menuService.selectMenuList(menu, userId);
        return AjaxResult.success(this.menuService.buildMenuTreeSelect(menus));
    }

    @GetMapping({"/filterTreeselect"})
    public AjaxResult filterTreeSelect(SysMenu menu) {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = this.menuService.selectMenuListAndFilter(menu, userId);
        return AjaxResult.success(this.menuService.buildMenuTreeSelect(menus));
    }

    @GetMapping({"/roleMenuTreeselect/{roleId}"})
    public AjaxResult roleMenuTreeSelect(@PathVariable("roleId") Long roleId) {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = this.menuService.selectMenuList(userId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", this.menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", this.menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    @RequiresPermissions({"system:menu:add"})
    @Log(
            title = "菜单管理",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMenu menu) {
        if ("1".equals(this.menuService.checkMenuNameUnique(menu))) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if ("0".equals(menu.getIsFrame()) && !StringUtils.isHttp(menu.getPath())) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else {
            menu.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.menuService.insertMenu(menu));
        }
    }

    @RequiresPermissions({"system:menu:edit"})
    @Log(
            title = "菜单管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysMenu menu) {
        if ("1".equals(this.menuService.checkMenuNameUnique(menu))) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if ("0".equals(menu.getIsFrame()) && !StringUtils.isHttp(menu.getPath())) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return AjaxResult.error("修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        } else {
            menu.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.menuService.updateMenu(menu));
        }
    }

    @RequiresPermissions({"system:menu:remove"})
    @Log(
            title = "菜单管理",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{menuId}"})
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        return this.menuService.hasChildByMenuId(menuId) ? AjaxResult.error("存在子菜单,不允许删除") : (this.menuService.checkMenuExistRole(menuId) ? AjaxResult.error("菜单已分配,不允许删除") : this.toAjax(this.menuService.deleteMenuById(menuId)));
    }

    @GetMapping({"getRouters"})
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = this.menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(this.menuService.buildMenus(menus));
    }
}
