package com.usoftchina.bi.server.controller.user;

import com.usoftchina.bi.auth.annotation.Auth;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.server.model.po.User;
import com.usoftchina.bi.server.model.pojo.annotation.CheckToken;
import com.usoftchina.bi.server.model.pojo.annotation.Log;
import com.usoftchina.bi.server.model.vo.configVo.LoginInfo;
import com.usoftchina.bi.server.model.vo.configVo.UserGroupInfo;
import com.usoftchina.bi.server.model.vo.configVo.UserGroupSetInfo;
import com.usoftchina.bi.server.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "用户相关接口")
public class UserController {
    @Autowired
    UserService userService;

    /*
    登录(生成token)
     */
    @ApiOperation(value = "登录", notes = "登录", response = RepEntity.class)
    @PostMapping("/login")
    public RepEntity login(@RequestBody LoginInfo body){
        return userService.login(body);
    }

    /*
    更新用户信息
     */
    @ApiOperation(value = "更新用户信息", notes = "更新用户信息", response = RepEntity.class)
    @Auth(user = "admin")
    @CheckToken
    @PostMapping("/updateUser")
    public RepEntity updateUser(@RequestHeader String token, @RequestBody User body){
        return userService.updateUser(body);
    }

    /*
    查询用户列表
     */
    @ApiOperation(value = "查询用户列表", notes = "查询用户列表", response = RepEntity.class)
    @Auth(user = "admin")
    @PostMapping("/getUserList")
    public RepEntity getUserList(){
        return userService.getUserList();
    }

    /*
    查询匹配用户
     */
    @ApiOperation(value = "查询匹配用户", notes = "查询匹配用户", response = RepEntity.class)
    @GetMapping("/getNameList")
    public RepEntity getNameList(@RequestParam("condition") String condition, @RequestParam(value = "count", defaultValue = "20") Long count){
        return userService.getNameList(condition, count);
    }

    /*
    创建用户组
     */
    @ApiOperation(value = "创建用户组", notes = "创建用户组", response = RepEntity.class)
    @Auth(user = "admin")
    @Log(module = "用户管理", name = "#body.userGroupName", token = "#token")
    @CheckToken
    @PostMapping("/createUserGroup")
    public RepEntity saveUserGroup(@RequestHeader String token, @RequestBody UserGroupInfo body){
        return userService.createUserGroup(body);
    }

    /*
    更新用户组
     */
    @ApiOperation(value = "更新用户组", notes = "更新用户组", response = RepEntity.class)
    @Auth(user = "admin")
    @Log(module = "用户管理", name = "#body.userGroupName", token = "#token")
    @CheckToken
    @PostMapping("/updateUserGroup")
    public RepEntity updateUserGroup(@RequestHeader String token, @RequestBody UserGroupInfo body){
        return userService.updateUserGroup(body);
    }

    /*
    查询用户组列表
     */
    @ApiOperation(value = "查询用户组列表", notes = "查询用户组列表", response = RepEntity.class)
    @Auth(user = "admin")
    @PostMapping("/getUserGroupList")
    public RepEntity getUserGroupList(){
        return userService.getUserGroup();
    }

    /*
    删除用户组
     */
    @ApiOperation(value = "删除用户组", notes = "删除用户组", response = RepEntity.class)
    @Auth(user = "admin")
    @CheckToken
    @PostMapping("/delUserGroup/{id}")
    public RepEntity delUserGroup(@RequestHeader String token, @PathVariable("id") Long id){
        return userService.delUserGroup(id);
    }

    /*
    用户组添加用户
     */
    @ApiOperation(value = "用户组添加用户", notes = "用户组添加用户", response = RepEntity.class)
    @Auth(user = "admin")
    @PostMapping("/setUserInto")
    @CheckToken
    public RepEntity setUserInto(@RequestHeader String token, @RequestBody UserGroupSetInfo body){
        return userService.setUserInto(body);
    }

    /*
    查询用户组下的用户
     */
    @ApiOperation(value = "查询用户组下的用户", notes = "查询用户组下的用户", response = RepEntity.class)
    @Auth(user = "admin")
    @PostMapping("/getUserInGroup")
    public RepEntity getUserInGroup(@RequestBody int body){
        return userService.getUserInGroup(body);
    }

    /*
    删除用户组下的用户
     */
    @ApiOperation(value = "删除用户组下的用户", notes = "删除用户组下的用户", response = RepEntity.class)
    @Auth(user = "admin")
    @PostMapping("/delUserInGroup")
    @CheckToken
    public RepEntity delUserInGroup(@RequestHeader String token, @RequestBody UserGroupSetInfo body){
        return userService.delUserInGroup(body);
    }
}
