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

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.cpop.core.constant.Constants;
import com.cpop.system.business.entity.Menu;
import com.cpop.system.business.entity.StoreSign;
import com.cpop.system.business.service.LoginService;
import com.cpop.system.business.service.MenuService;
import com.cpop.system.business.service.StoreSignService;
import com.cpop.system.business.vo.LoginUserInfoVo;
import com.cpop.system.business.vo.MenuRouteVo;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import com.cpop.core.utils.SpringUtils;

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 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() {
        SaSession session = StpUtil.getSession();
        return new LoginUserInfoVo()
                .setId((String) session.get("id"))
                .setUserId((String) session.get("userId"))
                .setUsername((String) session.get("username"))
                .setRealName((String) session.get("name"))
                .setAvatar((String) session.get("avatar"))
                .setPermissions(StpUtil.getPermissionList());
    }

    /**
     * 获取权限码
     * @author DB
     * @since 2023-11-30 14:32:57
     * @version 1.0.0
     */
    @Override
    public Set<String> getPermCode() {
        SaSession session = StpUtil.getSession();
        if (StringUtils.equals(Constants.SUPER_ADMIN, session.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(session.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();
    }
}
