// fs-company模块
package com.fs.core.config;

import com.fs.common.core.domain.entity.SysUser;
import com.fs.common.enums.DataScopeEnum;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.mapper.CompanyRoleMapper;
import com.fs.core.security.LoginUser;
import com.fs.core.security.SecurityUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * 登录上下文回调实现
 */
@Component
public class LoginContextCallback implements com.fs.common.config.LoginContextCallback, ApplicationContextAware {

    private static ApplicationContext applicationContext;
    /** 所有权限标识 */
    private static final String ALL_PERMISSION = "*:*:*";
    @Override
    public Long getUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser != null ? loginUser.getUser().getUserId() : null;
    }

    @Override
    public Long getCompanyUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CompanyUser user = loginUser.getUser();
        return user != null ? user.getUserId() : null;
    }

    @Override
    public String getUsername() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getCompanyUserName() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getUsername();
    }

    @Override
    public Long getCompanyId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return loginUser.getUser().getCompanyId();
    }

    @Override
    public Long getDeptId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CompanyUser user = loginUser.getUser();
        return user.getDeptId();
    }


    @Override
    public boolean hasPermission(String permission){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> permissions = loginUser.getPermissions();
        if(loginUser.getUser().isAdmin()){
            return true;
        }
        return permissions.contains(ALL_PERMISSION) || permissions.contains(StringUtils.trim(permission));
    }


    @Override
    public DataScopeEnum getDataScope(){
        CompanyRoleMapper companyRoleMapper = applicationContext.getBean(CompanyRoleMapper.class);
        List<String> dataScope = companyRoleMapper.queryCompanyUserDataScope(getCompanyUserId());
        if (dataScope == null || dataScope.isEmpty()) {
            return null;
        }
        Set<String> dataScopeSet = new TreeSet<>(dataScope);

        // 按优先级顺序检查
        for (DataScopeEnum scope : Arrays.asList(
                DataScopeEnum.ALL,
                DataScopeEnum.CUSTOM,
                DataScopeEnum.DEPARTMENT,
                DataScopeEnum.DEPARTMENT_AND_BELOW,
                DataScopeEnum.SELF_ONLY
        )) {
            if (dataScopeSet.contains(scope.getCode())) {
                return scope;
            }
        }
        return null;
    }




    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LoginContextCallback.applicationContext = applicationContext;
    }
}
