package com.younike.admin.controller.system;

import com.younike.admin.utils.ShiroUtils;
import com.younike.common.bean.APIResult;
import com.younike.common.bean.ApiResultCode;
import com.younike.common.bean.ResultGenerator;
import com.younike.common.constant.UserConstants;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.dto.AjaxResult;
import com.younike.common.core.dto.Ztree;
import com.younike.common.core.page.TableResult;
import com.younike.service.dto.system.CheckboxDto;
import com.younike.service.pojo.SysMenu;
import com.younike.service.pojo.SysRole;
import com.younike.service.pojo.SysUser;
import com.younike.service.service.ISysMenuService;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    private String prefix = "system/menu";

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    public TableResult list(SysMenu menu) {
        Long userId = ShiroUtils.getUserId();
        startPage();
        List<SysMenu> menuList = menuService.selectMenuList(menu, userId);
        return getDataTable(menuList);
    }

    /**
     * 新增菜单
     */
    @PostMapping("")

    public APIResult addMenu(@RequestBody SysMenu menu) {
        //检查唯一性
        if (!menuService.checkMenuNameUnique(menu).equals(UserConstants.MENU_NAME_UNIQUE)) {
            return ResultGenerator.getFailResult("该菜单名称已存在");
        }
        if (menu.getRemark() != null && menu.getRemark().getBytes().length > 500) {
            return ResultGenerator.getFailResult("备注过长");
        }
        menuService.cleanUnrelatedParams(menu);
        menu.setCreateBy(ShiroUtils.getLoginName());
        menuService.insertMenu(menu);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 根据id查找菜单
     */
    @GetMapping(value = "/{menuId}")
    public SysMenu getMenu(@PathVariable Long menuId) {
        return menuService.selectMenuById(menuId);
    }

    /**
     * 更新菜单
     */
    @PutMapping("")

    public APIResult updateMenu(@RequestBody SysMenu menu) {
        //检查唯一性
        if (!menuService.checkMenuNameUnique(menu).equals(UserConstants.MENU_NAME_UNIQUE)) {
            return ResultGenerator.getFailResult("该菜单名称已存在");
        }
        if (menu.getRemark() != null && menu.getRemark().getBytes().length > 500) {
            return ResultGenerator.getFailResult("备注过长");
        }
        menuService.cleanUnrelatedParams(menu);
        menu.setUpdateBy(ShiroUtils.getLoginName());
        menuService.updateMenu(menu);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 删除菜单
     */
    @DeleteMapping(value = "/{menuId}")

    public APIResult deleteMenu(@PathVariable Long menuId) {
        try {
            menuService.deleteMenuById(menuId);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 获取角色下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu) {
        Long userId = ShiroUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载角色菜单列表树
     */
    @GetMapping("/roleMenuTreeData")
    public List<Ztree> roleMenuTreeData(SysRole role) {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.roleMenuTreeData(role, userId);
        return ztrees;
    }

    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/menuTreeData")
    public List<Ztree> menuTreeData() {
        Long userId = ShiroUtils.getUserId();
        List<Ztree> ztrees = menuService.menuTreeData(userId);
        return ztrees;
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        SysUser sysUser = ShiroUtils.getSysUser();
        List<SysMenu> menus = menuService.selectMenuList(sysUser.getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 2021-11-29 Another 获取 当前用户所拥有的物业管理平台一级菜单
     * @return 当前用户所拥有的物业管理平台一级菜单
     */
    @GetMapping("getFirstLevelMenu")
    public APIResult<List<CheckboxDto>> getMenu() {
        APIResult<List<CheckboxDto>> apiResult = new APIResult<>();
        Long userId = ShiroUtils.getUserId();
        apiResult.setResult(menuService.getFirstLevelMenu(userId));
        apiResult.setCode(ApiResultCode.Success.getCode());
        return apiResult;
    }

}
