package com.ozs.web.controller.system;

import com.ozs.common.constant.Constants;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.domain.entity.SysMenu;
import com.ozs.common.core.domain.entity.SysUser;
import com.ozs.common.core.domain.model.LoginBody;
import com.ozs.common.core.domain.model.LoginUser;
import com.ozs.common.core.redis.RedisCache;
import com.ozs.common.utils.SecurityUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.stateSecrets.SymmetryTest;
import com.ozs.framework.web.service.SysLoginService;
import com.ozs.framework.web.service.SysPermissionService;
import com.ozs.framework.web.service.TokenService;
import com.ozs.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Slf4j
@RestController
//@RequestMapping("/app")
@RequestMapping("")
public class SysLoginController extends BaseController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String decrypt = SymmetryTest.decrypt(loginBody.getPassword(), "1234567890abcdef");
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), decrypt, loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        LoginUser loginedUser = tokenService.getLoginedUserByToken(token);
        if (loginedUser != null) {
            ajax.put("userId", loginedUser.getUserId());
            ajax.put("username", loginedUser.getUsername());
            ajax.put("avatar", loginedUser.getUser() != null ? loginedUser.getUser().getAvatar() : "");
        }
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/loginApp")
    public AjaxResult loginApp(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginApp(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        LoginUser loginedUser = tokenService.getLoginedUserByTokenApp(token);
        if (loginedUser != null) {
            ajax.put("userId", loginedUser.getUserId());
            ajax.put("username", loginedUser.getUsername());
            ajax.put("avatar", loginedUser.getUser() != null ? loginedUser.getUser().getAvatar() : "");
        }
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().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() {
        Long userId = SecurityUtils.getId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
