package cn.qinys.platform.upms.api.controller;

import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.security.annotation.HasPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qinys.platform.upms.api.req.*;
import cn.qinys.platform.upms.api.resp.UserAccountInfoResp;
import cn.qinys.platform.upms.api.resp.UserDetailResp;
import cn.qinys.platform.upms.api.resp.UserPageResp;
import cn.qinys.platform.upms.api.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;


/**
 * 后台登录用户
 *
 * @author tanlie
 * Date: 2024/2/26 17:22
 */
@RestController
@RequestMapping("/upms/admin/user")
public class UserController {
    @Resource
    IUserService userService;

    /**
     * 管理员列表
     *
     * @return
     */

    @HasPermission({"upms.user"})
    @GetMapping("/page")
    public Result<Page<UserPageResp>> getUserPage(UserPageReq req) {
        Page<UserPageResp> resp = userService.getUserPage(req);
        return new Result<>(resp);
    }

    /**
     * 新增管理员
     *
     * @param req
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/create")
    public Result<String> createUser(@RequestBody @Valid UserCreateReq req) {
        userService.createUser(req);
        return new Result<>("操作成功");
    }

    /**
     * 新增管理员
     *
     * @param req
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/remove")
    public Result<String> removeUser(@RequestBody @Valid UserRemoveReq req) {
        userService.removeUser(req);
        return new Result<>("操作成功");
    }

    /**
     * 账号信息
     *
     * @return
     */
    @GetMapping("/account")
    public Result<UserAccountInfoResp> accountInfo(String id) {
        UserAccountInfoResp resp = userService.getAccountInfo(id);
        return new Result<>(resp);
    }

    /**
     * 用户角色新增
     *
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/role-add")
    public Result<String> userRoleAdd(@RequestBody @Valid UserRoleOperationReq req) {
        userService.userRoleAdd(req);
        return new Result<>("操作成功");
    }


    /**
     * 用户角色删除
     *
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/role-delete")
    public Result<String> userRoleDelete(@RequestBody @Valid UserRoleOperationReq req) {
        userService.userRoleDelete(req);
        return new Result<>("操作成功");
    }

    /**
     * 修改锁定状态
     *
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/change-lock")
    public Result<String> changeLock(@RequestBody @Valid UserChangeLockReq req) {
        userService.changeLock(req);
        return new Result<>("操作成功");
    }

    /**
     * 修改锁定状态
     *
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/reset-pwd")
    public Result<String> changePassword(@RequestBody @Valid UserResetPasswordReq req) {
        userService.resetPassword(req);
        return new Result<>("操作成功");
    }

    /**
     * 用户详情
     *
     * @return
     */
    @GetMapping("/detail/by-id")
    public Result<UserDetailResp> userDetailById(String id) {
        UserDetailResp resp = userService.userDetailById(id);
        return new Result<>(resp);
    }

    /**
     * 修改用户信息
     *
     * @return
     */
    @HasPermission({"upms.user"})
    @PostMapping("/detail/update")
    public Result<String> updateUserDetail(@RequestBody @Valid UserUpdateReq req) {
        userService.updateUser(req);
        return new Result<>("操作成功");
    }

}
