package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.base.api.entity.SysUser;
import com.tofly.base.service.SysUserService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.auth.innerCall;
import com.tofly.common.oauth.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 * @date 2019/9/6  17:02
 */
@RestController
@Slf4j
@RequestMapping("/user")
@AllArgsConstructor
public class SysUserController {
    private SysUserService sysUserService;

    @innerCall
    @GetMapping("/info/{username}")
    public ResultRespone getInfo(@PathVariable("username") String username){
        log.info("base:通过用户名从数据库找信息..:"+username);
        SysUser sysUser=sysUserService.getOne(Wrappers.<SysUser>lambdaQuery()
        .eq(SysUser::getUsername,username));
        log.info("base:从数据库找到用户信息..:"+username+":"+sysUser);
        if(sysUser==null){
            return ResultRespone.failed("用户信息为空"+username);
        }
        return ResultRespone.success(sysUserService.getUserInfo(sysUser));
    }



    @GetMapping("/role")
    public ResultRespone getRole(){
        return ResultRespone.success(SecurityUtils.getRoles());

    }

    public ResultRespone updateLoginStatus(){
        return null;
    }

    @PreAuthorize("hasRole('/user/test')")
    @GetMapping("/test")
    public ResultRespone test(){
        return ResultRespone.success(11111111);
    }
}
