package com.bonus.core.account.v3.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.bonus.core.account.v3.api.dto.AccOrdBaseDTO;
import com.bonus.core.account.v3.api.dto.AccOrdRefundDTO;
import com.bonus.core.account.v3.mq.send.AccSendMq;
import com.bonus.core.account.v3.service.AccPayService;
import com.bonus.core.account.v3.web.dto.AccOrdMixPayResultDTO;
import com.bonus.core.account.v3.web.vo.AccOrdBasicsVO;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.account.v3.mq.send.dto.RepAccMqDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class AccPayBusinessApi {
   private static final Logger log = LoggerFactory.getLogger(AccPayBusinessApi.class);
   @Resource
   @Lazy
   private AccPayService accPayService;
   @Resource
   @Lazy
   private AccSendMq accSendMq;

   public AccOrdBasicsVO accPayForOffline(AccOrdBaseDTO accOrdOfflineDTO) {
      AccOrdBasicsVO accOrdBasicsVO = this.accPayService.accPayForOffline(accOrdOfflineDTO);
      log.info("[离线订单]账户支付结果_{}", JSONUtil.toJsonStr(accOrdBasicsVO));
      this.sendAccSendMq(accOrdOfflineDTO, accOrdBasicsVO.getAccTradeId());
      return accOrdBasicsVO;
   }

   public AccOrdBasicsVO accOrdForRefund(AccOrdRefundDTO accOrdRefundDTO) {
      AccOrdBasicsVO accOrdForRefund = this.accPayService.accOrdForRefund(accOrdRefundDTO);
      log.info("[订单退款支付]账户结果,tradeId_{}", JSONUtil.toJsonStr(accOrdForRefund));
      this.sendAccSendMq(accOrdRefundDTO, accOrdForRefund.getAccTradeId());
      return accOrdForRefund;
   }

   private void sendAccSendMq(AccOrdBaseDTO accOrdBaseDTO, Long tradeId) {
      if (ObjectUtil.isNotNull(tradeId)) {
         RepAccMqDTO repAccMqDTO = new RepAccMqDTO();
         BeanUtil.copyProperties(accOrdBaseDTO, repAccMqDTO, new String[0]);
         repAccMqDTO.setTradeId(tradeId);
         List<RepAccMqDTO> sendList = Collections.singletonList(repAccMqDTO);
         this.accSendMq.accTradeSendHandle(TenantContextHolder.getTenantId(), sendList);
      }
   }

   public List<Integer> getWalletPayDefaultOrder() {
      return this.accPayService.getWalletPayDefaultOrder();
   }

   public AccOrdBasicsVO accPayForMixPay(AccOrdBaseDTO accPayMixPayDTO) {
      AccOrdBasicsVO accOrdMixVO = this.accPayService.accPayForMixPay(accPayMixPayDTO);
      log.info("[订单混合支付]账户支付结果_{}", JSONUtil.toJsonStr(accOrdMixVO));
      return accOrdMixVO;
   }

   public AccOrdBasicsVO accPayForOnline(AccOrdBaseDTO accOrdOnlineDTO) {
      AccOrdBasicsVO accOrdBasicsVO = this.accPayService.accPayForOnline(accOrdOnlineDTO);
      log.info("[在线订单]账户支付结果_{}", JSONUtil.toJsonStr(accOrdBasicsVO));
      this.sendAccSendMq(accOrdOnlineDTO, accOrdBasicsVO.getAccTradeId());
      return accOrdBasicsVO;
   }

   public AccOrdBasicsVO accPayForMixPayResult(AccOrdMixPayResultDTO accPayMixPayResultDTO) {
      AccOrdBasicsVO accOrdMixVO = this.accPayService.accPayForMixPayResult(accPayMixPayResultDTO);
      log.info("[订单混合支付回调]交易成功后结果_{}", JSONUtil.toJsonStr(accOrdMixVO));
      this.sendAccSendMq(accPayMixPayResultDTO, accOrdMixVO.getAccTradeId());
      return accOrdMixVO;
   }
}
