package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.entity.Permission;
import com.judong.chuanyiserver.entity.Role;
import com.judong.chuanyiserver.entity.User;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.UserService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.RSAUtil;
import com.judong.chuanyiserver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取图形验证码
     *
     * @return
     */
    @GetMapping("/getCodeImage")
    public Result getCodeImage(boolean hasPubKey) {
        return userService.getCodeImage(hasPubKey);
    }

    /**
     * 获取公钥
     *
     * @return
     */
    @GetMapping("/getPublicKey")
    public Result getPublicKey() {
        return Result.ok(RSAUtil.PUBLIC_KEY);
    }

    /**
     * 前端用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/frontUserLogin")
    public Result frontUserLogin(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getUserName(), user.getPassword(), user.getUid(), user.getVerifyCode())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.frontUserLogin(user);
    }

    /**
     * 后台管理系统登录(只允许管理员登录)
     *
     * @param user
     * @return
     */
    @PostMapping("/backUserLogin")
    public Result backUserLogin(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getUserName(), user.getPassword(), user.getUid(), user.getVerifyCode())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.backUserLogin(user);
    }

    /**
     * 用户退出登录
     *
     * @return
     */
    @PostMapping("/userLoginOut")
    public Result userLoginOut() {
        return userService.userLoginOut();
    }

    /**
     * 添加用户
     *
     * @param userName
     * @param password
     * @return
     */
    @PostMapping("/addUser")
    @PermissionControl
    public Result addUser(String userName, String password) {
        if (Blank.isEmpty(userName, password)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.addUser(userName, password);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PermissionControl
    @PostMapping("/updateUser")
    public Result updateUser(User user) {
        if (Blank.isEmpty(user)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.updateUser(user);
    }

    /**
     * 管理员分页查询所有用户信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getUserPage")
    @PermissionControl
    public Result getUserPage(int page, int limit) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getUserPage(page, limit);
    }

    /**
     * 管理员可以查看任何用户的信息，不是管理员只能查看自己的信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserById")
    public Result getUserById(String userId) {
        if (Blank.isEmpty(userId)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getUserById(userId);
    }

    /**
     * 逻辑删除用户，锁定用户，解除用户锁定。只有管理员才能操作
     *
     * @param userId
     * @return
     */
    @PostMapping("/updateUserStateById")
    @PermissionControl
    public Result updateUserStateById(String userId, Integer userState) {
        if (Blank.isEmpty(userId, userState)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.updateUserStateById(userId, userState);
    }

    /**
     * 新增角色
     *
     * @param roleName
     * @return
     */
    @PostMapping("/addRole")
    @PermissionControl
    public Result addRole(String roleName) {
        if (Blank.isEmpty(roleName)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.addRole(roleName);
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @PostMapping("/updateRole")
    @PermissionControl
    public Result updateRole(Role role) {
        if (Blank.isEmpty(role)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.updateRole(role);
    }

    /**
     * 分配角色
     *
     * @param user
     * @return
     */
    @PostMapping("/assignRole")
    @PermissionControl
    public Result assignRole(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getUserId(), user.getRoleList())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.assignRole(user.getUserId(), user.getRoleList());
    }

    /**
     * 分页查询所有的角色信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getRoleListByPage")
    @PermissionControl
    public Result getRoleListByPage(Integer page, Integer limit) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getRoleListByPage(page, limit);
    }

    /**
     * 通过角色id查看角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getRoleById")
    @PermissionControl
    public Result getRoleById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getRoleById(id);
    }

    /**
     * 新增权限资源
     *
     * @param permission
     * @return
     */
    @PostMapping("/addPermission")
    @PermissionControl
    public Result addPermission(Permission permission) {
        if (Blank.isEmpty(permission)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.addPermission(permission);
    }

    /**
     * 修改权限
     *
     * @param permission
     * @return
     */
    @PostMapping("/updatePermission")
    @PermissionControl
    public Result updatePermission(Permission permission) {
        if (Blank.isEmpty(permission)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.updatePermission(permission);
    }

    /**
     * 通过id查询权限
     *
     * @param id
     * @return
     */
    @GetMapping("/getPermissionById")
    @PermissionControl
    public Result getPermissionById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getPermissionById(id);
    }

    /**
     * 分页查询权限
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getPermissionListByPage")
    @PermissionControl
    public Result getPermissionListByPage(Integer page, Integer limit) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.getPermissionListByPage(page, limit);
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @PostMapping("/deletePermissionById")
    @PermissionControl
    public Result deletePermissionById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.deletePermissionById(id);
    }

    /**
     * 给角色分配权限
     *
     * @param role
     * @return
     */
    @PostMapping("/assignPermission")
    @PermissionControl
    public Result assignPermission(@RequestBody Role role) {
        if (Blank.isEmpty(role, role.getId(), role.getPermissionList())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.assignPermission(role.getId(), role.getPermissionList());
    }
}
