package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.annotation.WebLog;
import com.judong.chuanyiserver.entity.User;
import com.judong.chuanyiserver.enums.ModelEnum;
import com.judong.chuanyiserver.enums.OperationEnum;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.enums.ServerEnum;
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")
    @WebLog(ServerEnum = ServerEnum.CLIENT, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.LOGIN)
    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")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.LOGIN)
    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")
    @WebLog(ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.LOGOUT)
    public Result userLoginOut() {
        return userService.userLoginOut();
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.ADD)
    @PermissionControl
    public Result addUser(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getUserName(), user.getPassword(), user.getUserType())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.addUser(user);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PermissionControl
    @PostMapping("/updateUser")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.UPDATE)
    public Result updateUser(@RequestBody 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
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.SELECT)
    public Result getUserPage(int page, int limit, String userName, Integer state, Integer userType) {
        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, userName, state, userType);
    }

    /**
     * 管理员可以查看任何用户的信息，不是管理员只能查看自己的信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserById")
    @WebLog(ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.SELECT)
    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")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.UPDATE)
    @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 user
     * @return
     */
    @PostMapping("/deleteUserById")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.DELETE)
    @PermissionControl
    public Result deleteUserById(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getUserId())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return userService.deleteUserById(user.getUserId());
    }

    /**
     * 用户分配角色
     *
     * @param user
     * @return
     */
    @PostMapping("/assignRole")
    @WebLog(ServerEnum = ServerEnum.SERVER, ModelEnum = ModelEnum.USER, OperationEnum = OperationEnum.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());
    }

}
