package com.sunda.spmsweb.usercontroller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.service.ISpmsMenuService;
import com.sunda.spmsweb.util.JWTUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2023-05-25
 */
@RestController
@RequestMapping("/spmsMenu")
@Api(tags = "系统菜单表", description = "系统菜单表接口")
public class SpmsMenuController {
	
	@Autowired
	ISpmsMenuService iSpmsMenuService;
	
	@RequestMapping("/findMenuListByPage")
    @ApiOperation(value = "分页获取系统菜单信息", notes = "分页获取系统菜单信息。\n" +
    		"{\n" +
            "\t\t\t\"menuName\": \"菜单名称\",\n" +
            "\t\t\t\"menuType\": \"0-一级菜单，1-二级菜单\",\n" +
            "\t\t\t\"pageNo\": 1,\n" +
            "\t\t\t\"pageSize\": 20\n" +
            "\t\t}", httpMethod = "POST")
	@RequiresPermissions("spmsMenu-findMenuListByPage")
    public ResponseResult findMenuListByPage(@RequestBody JSONObject jsonObject){
    	try {
    		if (!jsonObject.containsKey("pageNo") || !jsonObject.containsKey("pageSize")){
                return ResponseResult.error("请求参数错误");
            }
    		return ResponseResult.success().add("menuList", iSpmsMenuService.getMenuListByParam(jsonObject));
    	}catch (Exception e){
    		e.printStackTrace();
    		return ResponseResult.error("请求数据失败").add("error", e.getMessage());
    	}
    }
	
	@RequestMapping("/findMenuList")
	@ApiOperation(value = "获取系统菜单信息", notes = "获取系统菜单信息。\n" +
			"{\n" +
			"\t\t\t\"menuName\": \"菜单名称\",\n" +
			"\t\t\t\"parentId\": \"父菜单ID\",\n" +
			"\t\t\t\"menuType\": \"0-一级菜单，1-二级菜单\"\n" +
			"\t\t}", httpMethod = "POST")
	@RequiresPermissions("spmsMenu-findMenuList")
	public ResponseResult findMenuList(@RequestBody JSONObject jsonObject){
		try {
			return ResponseResult.success().add("menuList", iSpmsMenuService.getMenuList(jsonObject));
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/findMenuListByRole")
	@ApiOperation(value = "角色分配菜单时查询菜单信息接口", notes = "角色分配菜单时查询菜单信息接口。\n" +
			"roleId 角色ID必填；\n" +
			"{\n" +
			"\t\t\t\"roleUuid\": \"角色的UUID\",\n" +
			"\t\t}", httpMethod = "POST")
	@RequiresPermissions("spmsMenu-findMenuListByRole")
	public ResponseResult findMenuListByRole(@RequestBody JSONObject jsonObject){
		try {
			if (!jsonObject.containsKey("roleUuid") || jsonObject.get("roleUuid") == null || jsonObject.get("roleUuid") == "") {
				return ResponseResult.error("roleUuid请求参数错误");
			}
			return ResponseResult.success().add("menuList", iSpmsMenuService.getMenuListByRoleId(jsonObject));
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/createMenu")
	@ApiOperation(value = "创建或修改菜单信息", notes = "创建或修改菜单信息。\n" +
			"menuId 为空则新增；menuId 不为空则更新菜单信息；\n" +
			"parentId 为空则为一级菜单；parentId 不为空则为二级菜单；\n" +
			"{\n" +
			"\t\t\t\"menuId\": \"\",\n" +
			"\t\t\t\"parentId\": \"\",\n" +
			"\t\t\t\"menuName\": \"菜单名称\",\n" +
			"\t\t\t\"menuEnName\": \"菜单英文名称\",\n" +
			"\t\t\t\"url\": \"路由\",\n" +
			"\t\t\t\"componentUrl\": \"路由别名\",\n" +
			"\t\t\t\"sortNo\": \"菜单排序\",\n" +
			"\t\t\t\"icon\": \"菜单图标\",\n" +
			"\t\t\t\"menuType\": \"0-一级菜单，1-二级菜单\"\n" +
			"\t\t}", httpMethod = "POST")
	@RequiresPermissions("spmsMenu-createMenu")
	public ResponseResult createMenu(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iSpmsMenuService.createMenu(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	@RequestMapping("/deleteMenu")
	@ApiOperation(value = "删除菜单信息", notes = "删除菜单信息。\n" +
			"menuId 不能为空为空则新增；menuType 不能为空；\n" +
			"{\n" +
			"\t\t\t\"menuId\": \"\",\n" +
			"\t\t\t\"menuType\": \"0-一级菜单，1-二级菜单\"\n" +
			"\t\t}", httpMethod = "POST")
	@RequiresPermissions("spmsMenu-deleteMenu")
	public ResponseResult deleteMenu(@RequestBody JSONObject jsonObject){
		try {
			String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
			return iSpmsMenuService.deleteMenu(jsonObject, userId);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}
	
	
}
