package com.tmerclub.cloud.order.service.impl;

import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.common.bean.PaySettlementConfig;
import com.tmerclub.cloud.common.cache.constant.OrderCacheNames;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.order.service.OrderAllinpayService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author TRACK
 */
@Service
public class OrderAllinpayServiceImpl implements OrderAllinpayService {

    @DubboReference
    private ConfigFeignClient configFeignClient;

    @Override
    @Cacheable(cacheNames = OrderCacheNames.ALLINPAY_CONFIG_KEY, key = "'0'")
    public boolean getIsAllinpay() {
        // 获取是否开启通联支付
        ServerResponseEntity<String> response = configFeignClient.getConfig(Constant.PAY_SETTLEMENT_CONFIG);
        if (!response.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        PaySettlementConfig paySettlementConfig = Json.parseObject(response.getData(), PaySettlementConfig.class);
        return Objects.equals(paySettlementConfig.getPaySettlementType(), 1);
    }

    @Override
    @CacheEvict(cacheNames = OrderCacheNames.ALLINPAY_CONFIG_KEY, key = "'0'")
    public void deleteAllinpayCache() {
    }
}
