package com.bonus.core.pay.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.core.account.v3.service.AccTradeService;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.order.common.model.OrderInfo;
import com.bonus.core.order.common.service.OrderInfoService;
import com.bonus.core.pay.api.vo.TradeRecordParamVO;
import com.bonus.core.pay.common.constants.PayStateEnum;
import com.bonus.core.pay.common.constants.TradeTypeEnum;
import com.bonus.core.pay.common.model.TradeChannel;
import com.bonus.core.pay.common.model.TradeOrderRecharge;
import com.bonus.core.pay.common.model.TradeRecord;
import com.bonus.core.pay.common.service.TradeChannelService;
import com.bonus.core.pay.common.service.TradeRecordService;
import com.bonus.core.pay.common.service.impl.PayTradeServiceImpl;
import com.bonus.core.pay.common.vo.MobileNotifyVO;
import com.bonus.core.pay.common.vo.TradeChannelVO;
import com.bonus.i18n.I18n;
import org.apache.ibatis.annotations.Param;
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 org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TradeRecordApi {
   private static final Logger log = LoggerFactory.getLogger(TradeRecordApi.class);
   @Lazy
   @Resource
   protected TradeRecordService tradeRecordService;
   @Lazy
   @Resource
   protected OrderInfoService orderInfoService;

   public TradeRecordParamVO getParamVOByTradeId(Long tradeId) {
      TradeRecord tradeRecord = this.tradeRecordService.getTradeRecordById(tradeId);
      if (tradeRecord == null) {
         return null;
      } else {
         TradeRecordParamVO paramVO = TradeRecordParamVO.of(tradeRecord);
         if (TradeTypeEnum.isConsumeType(tradeRecord.getTradeType())) {
            List<OrderInfo> orderInfoList = this.orderInfoService.getOrderInfoListByTradeId(tradeRecord.getId());
            paramVO.fillOrderInfoList(orderInfoList);
         }

         return paramVO;
      }
   }
   public List<TradeOrderRecharge> listOrderRechargeByTradeId(Long tradeId) {
      return this.tradeRecordService.queryOrderRechargeTrade(tradeId);
   }
}
