/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.api.entity.User;
import com.tofly.base.entity.UserRole;
import com.tofly.base.service.UserRoleService;
import com.tofly.base.service.UserService;
import com.tofly.common.core.constant.CommonConstants;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 用户角色表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@AllArgsConstructor
@RequestMapping("/userrole")
//@ApiIgnore
//@Api(tags="用户角色接口")
public class UserRoleController {

    private final  UserRoleService userRoleService;
    @Autowired
    private UserService userService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param userRole 用户角色表
    * @return
    */
    @GetMapping("/page" )
    //@ApiOperation(value = "分页查询")
    public ResultRespone getUserRolePage(Page page, @ModelAttribute UserRole userRole) {
      return ResultRespone.success(userRoleService.page(page, Wrappers.query(userRole)));
    }


    /**
     * 通过id查询用户角色表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    //@ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(userRoleService.getById(id));
    }

    /**
     * 新增用户角色表
     * @param userRole 用户角色表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增用户角色" )
    @ApiOperation(tags = {"用户管理","角色管理"},value = "新增用户角色")
    @PostMapping
    public ResultRespone save(@RequestBody UserRole userRole) {
      return ResultRespone.success(userRoleService.save(userRole));
    }

   /* *//**
     * 修改用户角色表
     * @param userRole 用户角色表
     * @return ResultRespone
     *//*
    @ToFlyAppLog(title="修改用户角色表" )
    //@ApiOperation(value = "修改用户角色表")
    @PutMapping
    public ResultRespone updateById(@RequestBody UserRole userRole) {
      return ResultRespone.success(userRoleService.updateById(userRole));
    }*/

    /**
     * 通过id删除用户角色表
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除用户角色表" )
    //@ApiOperation(value = "通过ID删除用户角色表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(userRoleService.removeById(id));
    }

    /**
     * 修改用户角色表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改用户角色" )
    @ApiOperation(tags = {"用户管理","角色管理"},value = "新增、修改用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "roles", value = "用户角色，多个使用逗号(,)隔开",  dataType = "String")
    })
    @PutMapping
    public ResultRespone updateById(@ApiParam(hidden = true) @RequestBody Map map) {
        if(checkHandler(Long.parseLong((String)map.get("userid")))){
            return ResultRespone.failed("当前用户不能修改其他人创建的用户");
        }
        else {
            return ResultRespone.success(userRoleService.updateUserRole(Long.valueOf((String) map.get("userid")), (String) map.get("roles")));
        }
    }

    private boolean checkHandler(Long userid){

        if(CommonConstants.SUPER_ADMIN_ULEVEL.equals(SecurityUtils.getUser().getUserLevel())){
            return false;
        }
        if(SecurityUtils.getUserId().equals(userid)){
            return false;
        }

        User user=userService.getById(userid);

        if(SecurityUtils.getUserId().equals(user.getCreateUser())){
            return false;
        }else {
            if(CommonConstants.SUPER_ADMIN_ULEVEL.equals(user.getUserLevel())){
                return true;
            }else {
                return checkHandler(user.getCreateUser());
            }
        }


    }

}
