package com.cpop.core.strategy.login;

import com.cpop.common.constant.Constants;
import com.cpop.common.utils.StringUtils;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.base.entity.Permission;
import com.cpop.core.base.entity.loginInfo.OamStaffLoginInfo;
import com.cpop.core.base.enums.InitRoleEnum;
import com.cpop.core.base.enums.UserType;
import com.cpop.core.base.table.SysUser;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowUtil;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author DB
 * @createTime 2023/11/08 9:35
 * @description Oam登陆策略
 */
@Component
public class OamLoginStrategy implements LoginStrategy{

    /**
     * @descriptions 获取登陆用户信息
     * @author DB
     * @date 2023/11/08 9:34
     * @param sysUser 系统用户
     * @param credentials 登陆相关凭着
     * @return: com.cpop.core.base.entity.LoginUser
     */
    @Override
    public LoginUser getLoginUserInfo(SysUser sysUser, Map<String, Object> credentials) {
        //构建登陆员工信息
        OamStaffLoginInfo staffLoginInfo = BeanUtils.mapToClass(sysUser, OamStaffLoginInfo.class);
        staffLoginInfo.setUserId(sysUser.getId());
        //员工不是超级管理员
        if (!staffLoginInfo.getUserName().equals(Constants.SUPER_ADMIN)) {
            Row row = DbChain.table("cp_oam_staff")
                    .select()
                    .where("user_id = ?", staffLoginInfo.getUserId())
                    .one();
            staffLoginInfo.setDeptId(row.getString("deptId"));
            staffLoginInfo.setRoleId(row.getString("roleId"));
            staffLoginInfo.setName(row.getString("name"));
            staffLoginInfo.setStaffType(row.getInt("staffType"));
            staffLoginInfo.setId(row.getString("id"));
        }else {
            staffLoginInfo.setName(Constants.SUPER_ADMIN);
            staffLoginInfo.setId(Constants.SUPER_ADMIN_ID);
        }
        LoginUser loginUser = new LoginUser(staffLoginInfo, staffLoginInfo.getId(), getPermissionSet(sysUser.getUserName(), staffLoginInfo.getRoleId()));
        loginUser.setUserId(sysUser.getId())
                .setUserType(UserType.OAM_USER)
                .setUserName(sysUser.getUserName())
                .setLoginTime(System.currentTimeMillis())
                .setStatus(sysUser.getStatus());
        return loginUser;
    }

    /**
     * @descriptions 获取权限列表
     * @author DB
     * @date 2023/11/08 9:37
     * @param username 用户名
     * @param roleId 角色id
     * @return: java.util.Set<java.lang.String>
     */
    private Set<String> getPermissionSet(String username, String roleId) {
        //获取权限
        Set<String> permissionSet = new HashSet<>();
        if (Constants.SUPER_ADMIN.equals(username) || StringUtils.equals(InitRoleEnum.SUPER_OAM_ROLE.getId(), roleId)) {
            permissionSet.add(Constants.ALL_PERMISSION);
        } else {
            //查询员工信息
            List<Row> list = DbChain.table("cp_sys_menu")
                    .select("pom.permission")
                    .from("cp_sys_menu").as("pom")
                    .leftJoin("cp_sys_role_menu").as("porm").on("porm.menu_id = pom.id")
                    .where("pom.type in (1,2)")
                    .and("porm.role_id = ?", roleId)
                    .and("pom.permission is not null")
                    .list();
            if (!list.isEmpty()) {
                List<Permission> permissions = RowUtil.toEntityList(list, Permission.class);
                permissionSet = permissions.stream().map(Permission::getPermission).collect(Collectors.toSet());
            }
        }
        return permissionSet;
    }
}
