package com.xunmei.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.domain.R;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.common.core.web.controller.BaseController;
import com.xunmei.common.security.annotation.InnerAuth;
import com.xunmei.system.api.domain.User;
import com.xunmei.system.api.model.LoginUser;
import com.xunmei.system.dto.user.UserPageDto;
import com.xunmei.system.service.IPermissionService;
import com.xunmei.system.service.IUserService;
import com.xunmei.system.service.impl.UserServiceImpl;
import com.xunmei.system.vo.user.UserPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IPermissionService permissionService;

    /**
     * 获取当前用户信息
     * @param username 用户名
     * @return
     */
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<LoginUser> info(@PathVariable("username") String username){
        User user = userService.selectUserByUserName(username);
        if(StringUtils.isNull(user)){
            return R.fail("用户名或密码错误");
        }

        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(user);

        //角色集合
        Set<String> roles = permissionService.getRolePermission(user);

        //权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);

        loginUser.setRoles(roles);
        loginUser.setPermissions(permissions);

        return R.ok(loginUser);
    }

    @InnerAuth
    @GetMapping("/page")
    public R<Page<UserPageVo>> page(@RequestBody UserPageDto request){
        Page<UserPageVo> userPage = userService.selectPageData(request);


        return R.ok(userPage);
    }
}
