package com.ningdatech.kqapi.user.controller;


import com.ningdatech.basic.model.IdVo;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.kqapi.user.manage.UserInfoManage;
import com.ningdatech.kqapi.user.model.po.ChangeAccountStatusReq;
import com.ningdatech.kqapi.user.model.po.ModifyPasswordReq;
import com.ningdatech.kqapi.user.model.po.SaveUserReq;
import com.ningdatech.kqapi.user.model.po.UserListReq;
import com.ningdatech.kqapi.user.model.vo.LoginUserDetailVO;
import com.ningdatech.kqapi.user.model.vo.UserDetailVO;
import com.ningdatech.kqapi.user.model.vo.UserListVO;
import com.ningdatech.log.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Liuxinxin
 * @since 2022-11-23
 */
@RestController
@RequestMapping("/api/v1/user-info")
@Api(tags = {"用户信息-相关接口"})
@RequiredArgsConstructor
public class UserInfoController {

    private final UserInfoManage userInfoManage;

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存用户")
    @WebLog(value = "保存用户")
    @PreAuthorize("hasAuthority('SUPER_ADMIN')")
    public void saveUser(@Valid @RequestBody SaveUserReq request) {
        userInfoManage.saveUser(request);
    }

    @PostMapping(value = "/changeAccountStatus")
    @ApiOperation(value = "账号状态变更")
    @WebLog(value = "账号状态变更")
    @PreAuthorize("hasAuthority('SUPER_ADMIN')")
    public void changeAccountStatus(@Valid @RequestBody ChangeAccountStatusReq req) {
        userInfoManage.changeAccountStatus(req);
    }

    @GetMapping("detail/{userId}")
    @ApiOperation("获取用户详情")
    @WebLog(value = "获取用户详情")
    public UserDetailVO getUserDetail(@PathVariable Long userId) {
        return userInfoManage.getUserDetail(userId);
    }

    @DeleteMapping("/del")
    @ApiOperation("删除用户")
    @WebLog(value = "删除用户")
    @PreAuthorize("hasAuthority('SUPER_ADMIN')")
    public void deleteUser(@RequestBody IdVo<Long> req) {
        userInfoManage.deleteUser(req.getId());
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "用户列表筛选")
    @WebLog(value = "用户列表筛选")
    public PageVo<UserListVO> listUser(@Valid @RequestBody UserListReq request) {
        return userInfoManage.listUser(request);
    }

    @PostMapping(value = "/basic-info")
    @ApiOperation("获取用户基本信息")
    @WebLog(value = "获取用户基本信息")
    public LoginUserDetailVO loginUserDetail() {
        return userInfoManage.loginUserDetail();
    }

    @PostMapping("/kick-off/{userId}")
    @ApiOperation("踢用户下线测试接口")
    public void kickOff(@Valid @PathVariable Long userId) {
        userInfoManage.kickOff(userId);
    }

    @PostMapping("/password/mod")
    @ApiOperation("修改密码")
    @WebLog(value = "修改密码")
    @PreAuthorize("hasAuthority('SUPER_ADMIN')")
    public void changePassword(@Valid @RequestBody ModifyPasswordReq po) {
        userInfoManage.changePassword(po);
    }

    @PostMapping("/password/self")
    @ApiOperation("修改个人密码")
    @WebLog(value = "修改个人密码")
    public void changeSelfPassword(@Valid @RequestBody ModifyPasswordReq po) {
        userInfoManage.changeSelfPassword(po);
    }

}
