package com.railway.web.controller.system;

import com.railway.common.constant.Constants;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.domain.entity.SysMenu;
import com.railway.common.core.domain.entity.SysUser;
import com.railway.common.core.domain.model.LoginBody;
import com.railway.common.utils.SecurityUtils;
import com.railway.framework.web.service.SysLoginService;
import com.railway.framework.web.service.SysPermissionService;
import com.railway.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Set;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录验证
 *
 * @author railway
 */
@Api(tags = "权限认证 - 登录验证")
@RestController
public class SysLoginController {

  private final SysLoginService loginService;
  private final ISysMenuService menuService;
  private final SysPermissionService permissionService;

  public SysLoginController(SysLoginService loginService,
      ISysMenuService menuService,
      SysPermissionService permissionService) {
    this.loginService = loginService;
    this.menuService = menuService;
    this.permissionService = permissionService;
  }

  /**
   * 登录方法
   *
   * @param loginBody 登录信息
   * @return 结果
   */
  @ApiOperation(value = "登录", notes = "登录方法", response = AjaxResult.class)
  @PostMapping("/login")
  public AjaxResult login(@RequestBody LoginBody loginBody) {
    AjaxResult ajax = AjaxResult.success();
    // 生成令牌
    String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(),
        loginBody.getCode(),
        loginBody.getUuid());
    ajax.put(Constants.TOKEN, token);
    return ajax;
  }

  /**
   * 获取用户信息
   *
   * @return 用户信息
   */
  @ApiOperation(value = "获取用户信息", notes = "获取用户信息", response = AjaxResult.class)
  @GetMapping("/getInfo")
  public AjaxResult getInfo() {
    SysUser user = SecurityUtils.getLoginUser().getUser();
    // 角色集合
    Set<String> roles = permissionService.getRolePermission(user);
    // 权限集合
    Set<String> permissions = permissionService.getMenuPermission(user);
    // 菜单集合
    Set<String> pages = menuService.selectMenuPagesByUserId(user.getUserId());
    // 按钮集合
    Set<String> bnts = menuService.selectMenuBtnsByUserId(user.getUserId());
    AjaxResult ajax = AjaxResult.success();
    ajax.put("user", user);
    ajax.put("roles", roles);
    ajax.put("pages", pages);
    ajax.put("btns", bnts);
    ajax.put("permissions", permissions);
    return ajax;
  }

  /**
   * 获取路由信息
   *
   * @return 路由信息
   */
  @GetMapping("getRouters")
  public AjaxResult getRouters() {
    Long userId = SecurityUtils.getUserId();
    List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
    return AjaxResult.success(menuService.buildMenus(menus));
  }
}
