package com.bonus.core.marketing.v2.api;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.constant.LeConstants;
import com.bonus.core.allocation.api.GlobalMetadataApi;
import com.bonus.core.common.enums.MetadataModelTypeEnum;
import com.bonus.core.common.redis.RedisUtil;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.marketing.v2.rule.pay.api.RulePayApi;
import com.bonus.core.marketing.v2.rule.pay.dto.RulePayComputeDTO;
import com.bonus.core.marketing.v2.rule.pay.dto.RulePayResultDTO;
import com.bonus.core.marketing.v2.rule.recharge.api.RuleRechargeApi;
import com.bonus.core.marketing.v2.rule.recharge.dto.RuleRechargeComputeDTO;
import com.bonus.core.marketing.v2.rule.recharge.dto.RuleRechargeResultDTO;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class MarketApi {
   private static final Logger log = LoggerFactory.getLogger(MarketApi.class);
   @Resource
   @Lazy
   private GlobalMetadataApi globalMetadataApi;
   @Resource
   private RuleRechargeApi ruleRechargeApi;
   @Resource
   private RulePayApi rulePayApi;
   public RuleRechargeResultDTO ruleRechargeCompute(RuleRechargeComputeDTO computeDTO) {
      log.info("【充值规则】入参：{}", JSONUtil.toJsonStr(computeDTO));
      if (Objects.isNull(computeDTO.getCustId())) {
         throw new ServiceException("人员ID不允许为空");
      } else {
         String value = this.globalMetadataApi.getAvailableValue(MetadataModelTypeEnum.MERCHANT, "ifUseRuleRecharge", (Long)null, (Long)null);
         boolean flag = CharSequenceUtil.equals(value, LeConstants.COMMON_YES.toString());
         if (!flag) {
            log.info("商户未配置充值规则");
            return new RuleRechargeResultDTO(computeDTO);
         } else {
            String key = String.format("yunshitang:%s:market:recharge:%s", TenantContextHolder.getTenantId(), computeDTO.getCustId());
            RLock lock = RedisUtil.getLock(key);
            lock.lock(300L, TimeUnit.SECONDS);

            RuleRechargeResultDTO var6;
            try {
               var6 = this.ruleRechargeApi.compute(computeDTO);
            } catch (Exception var10) {
               log.info("充值规则异常：", var10);
               throw new ServiceException(var10.getMessage());
            } finally {
               if (lock.isHeldByCurrentThread() && lock.isLocked()) {
                  lock.unlock();
               }
            }
            return var6;
         }
      }
   }

   public RulePayResultDTO rulePayCompute(RulePayComputeDTO computeDTO) {
      log.info("【扣款规则】入参：{}", JSONUtil.toJsonStr(computeDTO));
      String value = this.globalMetadataApi.getAvailableValue(MetadataModelTypeEnum.MERCHANT, "ifUseRulePay", (Long)null, (Long)null);
      boolean flag = CharSequenceUtil.equals(value, LeConstants.COMMON_YES.toString());
      if (!flag) {
         log.info("商户未配置扣款规则");
         return new RulePayResultDTO();
      } else {
         return this.rulePayApi.compute(computeDTO);
      }
   }
}
