package com.microservices.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.constant.SecurityConstants;
import com.microservices.common.core.utils.ServletUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.security.service.IRequestCustomService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author otto
 */
@Service
@Primary
public class SystemRequestCustomServiceImpl implements IRequestCustomService {
    @Override
    public String processRequestDeptId(HttpServletRequest request) {
        String deptId = ServletUtils.getHeader(request, SecurityConstants.DEPT_ID);
        if (StringUtils.isNotEmpty(deptId)) {
            return deptId;
        } else {
            Object attributeObj = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
            JSONObject jsonObject = JSONObject.from(attributeObj);
            return jsonObject.getString("currentDeptId");
        }
    }
}
