package com.sunda.spmsweb.usercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.service.ISpmsPermissionService;
import com.sunda.spmsweb.util.JWTUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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;

/**
 * <p>
 * spms权限表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/spmsPerm")
@Api(tags = "系统权限信息表", description = "系统权限信息表")
public class SpmsPermissionController {

    @Autowired
    ISpmsPermissionService iSpmsPermissionService;

    @RequestMapping("/findPermListByPage")
    @ApiOperation(value = "分页获取权限信息", notes = "分页获取权限信息。\n" +
            "{\n" +
            "\t\"description\": \"海外仓海运收货管理查询\",\n" +
            "\t\"perms\": oversea_getOverseaDeliveryNote,\n" +
            "\t\"firstMenuId\": 一级菜单ID-10,\n" +
            "\t\"secondMenuId\": 二级菜单ID-100001,\n" +
            "\t\t\t\"pageNo\": 1,\n" +
            "\t\t\t\"pageSize\": 20\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("spmsPerm-findPermListByPage")
    public ResponseResult findPermListByPage(@RequestBody JSONObject jsonObject){
        try {
        	if (!jsonObject.containsKey("pageNo") || !jsonObject.containsKey("pageSize")){
                return ResponseResult.error("请求参数错误");
            }
            return ResponseResult.success().add("spmsPermissionList", iSpmsPermissionService.findPermListByPage(jsonObject));
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/findPermList")
    @ApiOperation(value = "根据条件获取权限信息", notes = "根据条件获取权限信息。\n" +
    		"{\n" +
    		"\t\"description\": \"海外仓海运收货管理查询\",\n" +
    		"\t\"firstMenuId\": 一级菜单ID-10,\n" +
    		"\t\"secondMenuId\": 二级菜单ID-100001\n" +
    		"}", httpMethod = "POST")
    @RequiresPermissions("spmsPerm-findPermList")
    public ResponseResult findPermList(@RequestBody JSONObject jsonObject){
    	try {
    		return ResponseResult.success().add("spmsPermissionList", iSpmsPermissionService.findPermList(jsonObject));
    	}catch (Exception e){
    		return ResponseResult.error("请求数据失败").add("error", e.getMessage());
    	}
    }
    
    @RequestMapping("/getAllPerm")
    @ApiOperation(value = "获取全部菜单权限信息", notes = "获取全部菜单权限信息", httpMethod = "GET")
    @RequiresPermissions("spmsPerm-getAllPerm")
    public ResponseResult getAllPerm(){
    	try {
    		return ResponseResult.success().add("spmsPermissionList", iSpmsPermissionService.getAllPerm());
    	}catch (Exception e){
    		return ResponseResult.error("请求数据失败").add("error", e.getMessage());
    	}
    }
    
    @RequestMapping("/findPermListByRole")
	@ApiOperation(value = "角色分配权限时查询权限信息接口", notes = "角色分配权限时查询权限信息接口。\n" +
			"roleId 角色ID必填；\n" +
			"{\n" +
			"\t\t\t\"roleUuid\": \"角色的UUID\",\n" +
			"\t\t}", httpMethod = "POST")
    @RequiresPermissions("spmsPerm-findPermListByRole")
	public ResponseResult findPermListByRole(@RequestBody JSONObject jsonObject){
		try {
			if (!jsonObject.containsKey("roleUuid") || jsonObject.get("roleUuid") == null || jsonObject.get("roleUuid") == "") {
				return ResponseResult.error("roleUuid请求参数错误");
			}
			return ResponseResult.success().add("menuList", iSpmsPermissionService.getPermListByRoleId(jsonObject));
		}catch (Exception e){
			e.printStackTrace();
			return ResponseResult.error("请求数据失败").add("error", e.getMessage());
		}
	}

    @RequestMapping("/createSpmsPerm")
    @ApiOperation(value = "新增/更新权限信息", notes = "新增/更新权限信息。\n" +
            "permissionId 为空则进行新增数据；不为空则进行更新数据；\n" +
            "{\n" +
            "\t\"permissionId\": \"ff60bd7e73294417ab68bda207e2b5b0\",\n" +
            "\t\"description\": \"海外仓海运收货管理查询\",\n" +
            "\t\"perms\": oversea_getOverseaDeliveryNote,\n" +
            "\t\"firstMenuId\": 一级菜单ID-10,\n" +
            "\t\"secondMenuId\": 二级菜单ID-100001\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("spmsPerm-createSpmsPerm")
    public ResponseResult createSpmsPerm(@RequestBody JSONObject jsonObject){
        try {
        	String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSpmsPermissionService.createSpmsPerm(jsonObject, userId);
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
    
    @RequestMapping("/deleteSpmsPerm")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息。\n" +
            "permissionId 不能为空；\n" +
            "{\n" +
            "\t\"permissionId\": \"ff60bd7e73294417ab68bda207e2b5b0\"\n" +
            "}", httpMethod = "POST")
    @RequiresPermissions("spmsPerm-deleteSpmsPerm")
    public ResponseResult deleteSpmsPerm(@RequestBody JSONObject jsonObject){
        try {
        	String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iSpmsPermissionService.deleteSpmsPerm(jsonObject, userId);
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getPermsByUserId")
    @ApiOperation(value = "获取用户权限信息", notes = "获取用户权限信息。", httpMethod = "POST")
    public ResponseResult getPermsByUserId(String userId){
        try {
            return ResponseResult.success().add("userPerms", iSpmsPermissionService.getPermsByUserId(userId));
        }catch (Exception e){
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }


}
