package com.persagy.proxy.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.auth.service.impl.AbstractAuthServiceImpl;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.helper.SpringHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 不鉴权 实现类
 * 直接从head中获取上下文信息
 * @author Charlie Yu
 * @date 2021-11-02
 */
@Deprecated
public class NoneAuthServiceImpl extends AbstractAuthServiceImpl {

    @Override
    public void loginSuccess(HttpServletRequest request, HttpServletResponse response) {
        super.loginSuccess(request, response);
        loadContextByRequest(request);
    }

    @Override
    protected void loadContextByRequest(HttpServletRequest request) {
        AppContext context = AppContext.getContext();
        context.setGroupCode(request.getHeader("groupCode"));
        context.setProjectId(request.getHeader("projectId"));
        context.setAppId(request.getHeader("appId"));
        String userId = request.getHeader("userId");
        // 无用户时，默认为默认系统用户
        if(StrUtil.isBlank(userId)) {
            context.setAccountId(CommonConstant.DEFAULT_ID);
        }
        // 无集团编码时，默认为配置的集团编码
        String groupCode = request.getHeader("groupCode");
        if(StrUtil.isBlank(groupCode)) {
            context.setGroupCode(SpringHelper.getString("middleware.group.code"));
        }
        // 无项目id时,尝试从请求URL中获取
        if (StrUtil.isBlank(request.getHeader("projectId"))
                && StrUtil.isNotBlank(request.getParameter(CommonConstant.QUERY_PROJECTID))){
            context.setProjectId(request.getParameter(CommonConstant.QUERY_PROJECTID));
        }
    }
}
