package com.hs.admin.controller;

import com.hs.admin.common.Result;
import com.hs.admin.common.annotations.CurrentUser;
import com.hs.admin.common.annotations.Token;
import com.hs.admin.model.SysRole;
import com.hs.admin.model.SysUser;
import com.hs.admin.model.reqmodel.AddUserReq;
import com.hs.admin.model.reqmodel.UpdateUserReq;
import com.hs.admin.model.reqmodel.UserReq;
import com.hs.admin.model.respmodel.SysMenuList;
import com.hs.admin.model.respmodel.SysUserList;
import com.hs.admin.service.SysMenuService;
import com.hs.admin.service.SysUserRoleRsService;
import com.hs.admin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户API", produces = "produces", consumes = "consumes", protocols = "protocols")
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserRoleRsService sysUserRoleRsService;
    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("all")
    @Token
    @ApiOperation("查询用户列表")
    public Object getAll(@RequestBody UserReq.GetUserReq user) throws Exception {
        List<SysUserList> list = userService.getAll(user);
        return Result.success(list);
    }

    @PostMapping("info")
    @Token
    @ApiOperation("查询用户详细信息")
    public Object getById(@RequestBody UserReq.InfoUserReq user) throws Exception {
        SysUserList info = userService.info(user.getUserId());
        return Result.success(info);
    }

    @PostMapping("add")
    @Token
    @ApiOperation("添加用户")
    public Object addUser(@ApiIgnore @CurrentUser Integer userId, @Valid @RequestBody AddUserReq user) throws Exception {
        SysUser sysUser = userService.findByLoginName(user.getUserCode());
        if (sysUser != null) {
            return Result.error("该账号已存在！");
        }
        boolean result = userService.addUser(Long.valueOf(userId), user);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("delete")
    @Token
    @ApiOperation("删除用户")
    public Object deleteUser(@RequestBody UserReq.DeleteUserReq user) throws Exception {
        boolean result = userService.deleteUser(user);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("update")
    @Token
    @ApiOperation("修改用户")
    public Object updateUser(@Valid @RequestBody UpdateUserReq user) throws Exception {
        SysUserList reqUser = userService.info(user.getUserId());
        SysUser codeUser = userService.findByLoginName(user.getUserCode());
        if (reqUser != null) {
            if (codeUser != null && !codeUser.getUserCode().equals(reqUser.getUserCode())) {
                return Result.error("该用户已存在");
            }
        }
        boolean result = userService.updateUser(user);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("roles")
    @Token
    @ApiOperation("查询用户角色")
    public Object getRoleListByUser(@RequestBody UserReq.InfoUserReq user) throws Exception {
        List<SysRole> sysRoleList = sysUserRoleRsService.getRoleListByUser(user.getUserId());
        return Result.success(sysRoleList);
    }

    @PostMapping("menus")
    @Token
    @ApiOperation("查询用户菜单")
    public Object getMenuByUser(@RequestBody UserReq.InfoUserReq user) throws Exception {
        List<SysMenuList> sysMenuList = sysMenuService.getMenu(user.getUserId());
        return Result.success(sysMenuList);
    }

}
