package com.fs.web.controller.system;

import java.util.List;
import java.util.Set;

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.core.security.LoginBody;
import com.fs.core.security.LoginUser;
import com.fs.core.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;
import com.fs.common.constant.Constants;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.entity.SysMenu;
import com.fs.common.core.domain.entity.SysUser;
import com.fs.common.utils.ServletUtils;
import com.fs.core.web.service.SysLoginService;
import com.fs.core.web.service.SysPermissionService;
import com.fs.core.web.service.TokenService;
import com.fs.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 
 */
@RestController
public class SysLoginController
{
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody)
    {

        try{

            // 生成令牌
            String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                    loginBody.getUuid());
            //写入用户TOKEN
            redisCache.setCacheObject("token-pc:"+loginBody.getUsername(),token);
            return R.ok().put(Constants.TOKEN, token);
        }
        catch (Exception e){
            return R.error(e.getMessage());
        }



    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId(), SecurityUtils.isAdmin(user.getUserId()));
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
