package com.microservices.pms.common.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.security.service.IRequestCustomService;
import com.microservices.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.mapper.PmsEnterpriseMapper;
import org.springframework.beans.factory.annotation.Autowired;
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 PmsRequestCustomServiceImpl implements IRequestCustomService {
    @Autowired
    private PmsEnterpriseMapper pmsEnterpriseMapper;
    @Override
    public String processRequestDeptId(HttpServletRequest request) {
        Object attributeObj = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        JSONObject jsonObject = JSONObject.from(attributeObj);
        String enterpriseIdentifier = jsonObject.getString("enterpriseIdentifier");
        if (StringUtils.isEmpty(enterpriseIdentifier)) {
            return null;
        }
        PmsEnterprise pmsEnterprise
                = pmsEnterpriseMapper.selectPmsEnterpriseByIdentifier(enterpriseIdentifier);
        if (pmsEnterprise == null) {
            return null;
        }
        return pmsEnterprise.getDeptId().toString();
    }
}
