package com.jeequan.jeepay.agent.ctrl.anon;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.DBOEMConfig;
import com.jeequan.jeepay.core.service.ISysConfigService;
import com.jeequan.jeepay.core.utils.JsonKit;
import com.jeequan.jeepay.db.entity.AgentConfig;
import com.jeequan.jeepay.service.impl.AgentConfigService;
import com.jeequan.jeepay.service.impl.SysAdvertConfigService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 网站信息ctrl
 *
 * @author terrfly
 * @date 2022/2/14 20:56
 */
@RestController
@RequestMapping("/api/anon/siteInfos")
public class SiteInfoController extends CommonCtrl {

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private SysAdvertConfigService advertConfigService;
    @Autowired
    private AgentConfigService agentConfigService;

    @GetMapping(value = "")
    public ApiRes getSiteInfos() throws BizException {

        JSONObject result = new JSONObject();
        result.put("sysConfig", JsonKit.newJson("httpMsgIsEncrypt", SysConfigService.HTTP_MSG_IS_ENCRYPT));
        DBOEMConfig dboemConfig = sysConfigService.getOemConfig();
        //获取域名
        String headerDomainName = getdomainName();
        if (headerDomainName != null && headerDomainName.contains("//")) {
            int startIndex = headerDomainName.indexOf("//") + 2; // 获取第一个 "//" 后的位置
            int endIndex = headerDomainName.indexOf("/", startIndex); // 获取从 startIndex 开始的第一个 "/"
            if (endIndex != -1) {
                String domainName = headerDomainName.substring(startIndex, endIndex);
                result.put("domainName", domainName);
                AgentConfig agentConfig = agentConfigService.getOne(AgentConfig.gw().eq(AgentConfig::getGroupKey, "agentSiteConfig").eq(AgentConfig::getConfigVal, domainName).last("limit 1"));
                if (agentConfig == null) {
                    //该域名没配置，取总后台数据
                    result.put("siteInfo", dboemConfig);
                    return ApiRes.ok(result);
                }
                //获取商户域名
                AgentConfig mchDomainName = agentConfigService.getOne(AgentConfig.gw().eq(AgentConfig::getGroupKey, "agentSiteConfig").eq(AgentConfig::getConfigKey, "mchSiteUrl").eq(AgentConfig::getAgentNo, agentConfig.getAgentNo()));
                if (mchDomainName != null) {
                    result.put("mchDomainName", mchDomainName.getConfigVal());
                }
                //获取服务商域名
                AgentConfig agentDomainName = agentConfigService.getOne(AgentConfig.gw().eq(AgentConfig::getGroupKey, "agentSiteConfig").eq(AgentConfig::getConfigKey, "agentSiteUrl").eq(AgentConfig::getAgentNo, agentConfig.getAgentNo()));
                if (mchDomainName != null) {
                    result.put("agentDomainName", agentDomainName.getConfigVal());
                }
                AgentConfig oemConfig = agentConfigService.getOne(AgentConfig.gw().eq(AgentConfig::getConfigKey, "oemConfig").eq(AgentConfig::getAgentNo, agentConfig.getAgentNo()));
                if (oemConfig == null) {
                    //贴牌数据为空，取总后台数据
                    result.put("siteInfo", dboemConfig);
                    return ApiRes.ok(result);
                }
                result.put("siteInfo", JSONObject.parseObject(oemConfig.getConfigVal()));

            }
        }
        return ApiRes.ok(result);

    }

}
