package com.yc.ship.module.trade.framework.common;

import cn.hutool.core.util.StrUtil;
import com.yc.ship.module.infra.api.config.ConfigApi;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.ota.api.dto.DistributorRespDTO;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * &#064;Description ：自定义参数工具
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/11 10:33
 */
@Component
public class ConfigUtils {


    @Resource
    private ConfigApi configApi;
    @Resource
    private OtaDistributorApi otaDistributorApi;

    private final static String CONFIG_CACHE_PREFIX = "config:plat:";

    @Cacheable(value = CONFIG_CACHE_PREFIX+"payTimeOut")
    public Integer getPayTimeOut(){
        String value = configApi.getPlatformConfigValueByKey("pay_time_out");
        return StrUtil.isBlank(value) ? 5 : Integer.parseInt(value);
    }

    @Cacheable(value = CONFIG_CACHE_PREFIX+"payTimeOut", key = "#otaId")
    public Integer getPayTimeOut(Long otaId){
        DistributorRespDTO respDTO = otaDistributorApi.getDistributorById(otaId);
        if(respDTO !=null && respDTO.getPayTimeOut()!=null){
            return respDTO.getPayTimeOut();
        }
        //默认
        return getPayTimeOut();
    }

    @Cacheable(value = CONFIG_CACHE_PREFIX+"confirmOrderTimeOut")
    public Integer getConfirmOrderTimeOut(){
        String value = configApi.getPlatformConfigValueByKey("confirm_order_time_out");
        return StrUtil.isBlank(value) ? 60 : Integer.parseInt(value);
    }

}
