package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.service.IWyBreweryMenuService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WyBreweryMenu;
import com.ruoyi.common.core.domain.entity.WyBreweryUser;
import com.ruoyi.common.core.domain.model.BreweryLoginUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
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;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Api(tags = "品牌方登录验证")
@RestController
@RequestMapping("/brewery")
public class BreweryLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private IWyBreweryMenuService wyBreweryMenuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

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

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @ApiOperation("品牌方获取用户信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        BreweryLoginUser loginUser = SecurityUtils.getBreweryLoginUser();
        WyBreweryUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getBreweryRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getBreweryMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshBreweryToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @ApiOperation("品牌方获取路由信息")
    @GetMapping("/getRouters")
    public AjaxResult getRouters()
    {
        BreweryLoginUser loginUser = SecurityUtils.getBreweryLoginUser();
        WyBreweryUser user = loginUser.getUser();
        Long userId = user.getUserId();
          List<WyBreweryMenu> menus = wyBreweryMenuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(wyBreweryMenuService.buildMenus(menus));
    }
}
