package com.purui.prims.auth.authentication.rest;


import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.request.UserReq;
import com.purui.prims.auth.api.authentication.response.RoleResp;
import com.purui.prims.auth.api.authentication.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/auth/user")
@Slf4j
@Api(tags = "用户管理")
public class UserController {

    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "获取当前用户")
    @GetMapping("/getCurrentUser")
    public User getCurrentUser(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        return userService.getCurrentUser(httpServletRequest, httpServletResponse);
    }

    @GetMapping("/getUserNames")
    public List<String> getUserNames() {
        return userService.getUserNames();
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping("/getUserList")
    public List<User> getUserList() {
        return userService.getUserList();
    }

    @ApiOperation(value = "获取角色列表")
    @GetMapping("/getRoles")
    public List<RoleResp> getRoles() {
        return userService.getRoles();
    }

    @GetMapping("/getUserAllOrgId/{userName}")
    public Set<String> getUserAllOrgId(@PathVariable("userName") String userName) {
        return userService.getUserAllOrgId(userName);
    }

    @GetMapping("/getUserByOrg/{org}")
    public List<User> getUserByOrg(@PathVariable("org") String org) {
        return userService.getUserByOrg(org);
    }

    @ApiOperation(value = "创建用户")
    @PostMapping("/create")
    public void register(@RequestBody UserReq userCmd) {
        userService.register(userCmd);
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    public void update(@RequestBody UserReq userCmd) {
        userService.update(userCmd);
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public String login(@RequestBody UserReq userCmd, HttpServletRequest request) {
        return userService.login(userCmd, request);
    }

}
