package com.cpop.system.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.constant.Constants;
import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.system.business.entity.Menu;
import com.cpop.system.business.entity.Role;
import com.cpop.system.business.service.LoginService;
import com.cpop.system.business.service.MenuService;
import com.cpop.system.business.vo.LoginUserInfoVo;
import com.cpop.system.business.vo.MenuRouteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import com.cpop.core.utils.SpringUtils;
import com.cpop.system.framework.ws.server.WebSocketServer;
import com.cpop.system.business.service.RoleService;

import static com.cpop.system.business.entity.table.MenuTableDef.MENU;
import static com.cpop.system.business.entity.table.RoleMenuTableDef.ROLE_MENU;
import static com.cpop.system.business.entity.table.RoleTableDef.ROLE;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.compress.archivers.dump.DumpArchiveEntry.PERMISSION;
import java.util.HashSet;

/**
 * @author DB
 * @createTime 2023/09/10 11:19
 * @description
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Autowired
    private MenuService menuService;

    /**
     * @author LOST.yuan
     * @Description 获取用户信息
     * @date 23:01 2022/9/26
     * @return {@link String}
     **/
    @Override
    public LoginUserInfoVo getUserInfo() {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getInstance().getLoginUser();
        // 获取申请员工信息
        JSONObject loginStaffInfo = SecurityUtils.getInstance().getLoginUserInfo();
        return new LoginUserInfoVo().setUserId(loginUser.getUserId())
            .setUsername(loginUser.getUsername())
            .setRealName(loginStaffInfo.getString("name"))
            .setAvatar(loginStaffInfo.getString("avatar"))
            .setPermissions(loginUser.getPermissions())
            .setRoles(Collections.singleton("Cpop"))
            .setHomePath("");
    }

    /**
     * 获取权限码
     * @author DB
     * @since 2023-11-30 14:32:57
     * @version 1.0.0
     */
    @Override
    public Set<String> getPermCode() {
        // 获取当前登录用户信息
        JSONObject loginUser = SecurityUtils.getInstance().getLoginUserInfo();

        if (StringUtils.equals(Constants.SUPER_ADMIN, loginUser.getString("userName"))) {
            Set<String> permCodes = new HashSet<>();
            permCodes.add("*:*:*");
            return permCodes;
        } else {
            List<Menu> menuPermissions = SpringUtils.getBean(MenuService.class)
                .queryChain()
                .select(MENU.PERMISSION)
                .leftJoin(ROLE_MENU)
                .on(ROLE_MENU.MENU_ID.eq(MENU.ID))
                .leftJoin(ROLE)
                .on(ROLE.ID.eq(ROLE_MENU.ROLE_ID))
                .where(ROLE.ID.eq(loginUser.getString("roleId")))
                .and(MENU.TYPE.in(1, 2))
                .and(MENU.PERMISSION.isNotNull())
                .list();
            return menuPermissions.stream().map(Menu::getPermission).collect(Collectors.toSet());
        }

    }

    /**
     * 获取菜单列表
     */
    @Override
    public List<MenuRouteVo> getSysMenuList() {
        return menuService.getSysMenuList();
    }
}
