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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bonus.canteen.core.account.v3.api.dto.AccRechargeSumApiDTO;
import com.bonus.canteen.core.account.v3.api.vo.AccRechargeSumApiVO;
import com.bonus.canteen.core.account.v3.api.vo.AccTradeOrderBalanceVO;
import com.bonus.canteen.core.account.v3.api.vo.AccTradeOrderWalletPayVO;
import com.bonus.canteen.core.account.v3.model.AccTrade;
import com.bonus.canteen.core.account.v3.model.AccTradeWalletDetail;
import com.bonus.canteen.core.account.v3.service.AccTradeService;
import com.bonus.canteen.core.account.v3.service.AccTradeWalletDetailService;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AccTradeApi {
   private static final Logger log = LoggerFactory.getLogger(AccTradeApi.class);
   @Resource
   @Lazy
   private AccTradeService accTradeService;
   @Autowired
   @Lazy
   private AccTradeWalletDetailService accTradeWalletDetailService;


   public AccRechargeSumApiVO getAccRechargeSum(AccRechargeSumApiDTO accRechargeSumApiDTO) {
      return this.accTradeService.getAccRechargeSum(accRechargeSumApiDTO);
   }

   public Map<Long, AccTradeOrderBalanceVO> listOrderAccPayDetail(List<Long> leOrdNos) {
      //List<AccTrade> accTradeList = this.accTradeService.list((Wrapper) Wrappers.lambdaQuery(AccTrade.class)
      // .select(new SFunction[]{AccTrade::getId, AccTrade::getLeOrdNo, AccTrade::getWalletBalTotal})
      // .in(AccTrade::getLeOrdNo, leOrdNos));
      List<AccTrade> accTradeList = this.accTradeService.list(Wrappers.lambdaQuery(AccTrade.class)
                                          .select(AccTrade::getId, AccTrade::getLeOrdNo, AccTrade::getWalletBalTotal)
                                          .in(AccTrade::getLeOrdNo, leOrdNos));
      if (CollUtil.isEmpty(accTradeList)) {
         return MapUtil.empty();
      } else {
         Map<Long, List<AccTrade>> leOrdNo_accTrade = (Map)accTradeList.stream().collect(Collectors.groupingBy(AccTrade::getLeOrdNo));
         Map<Long, List<AccTradeWalletDetail>> accTradeId_walletDetail = this.accTradeWalletDetailService.queryAccTradeWalletDetailByTradeIdList(accTradeList.stream().map(AccTrade::getId).collect(Collectors.toList()));
         Map<Long, AccTradeOrderBalanceVO> resultMap = MapUtil.newHashMap();
         leOrdNo_accTrade.keySet().forEach((leOrdNo) -> {
            AccTrade accTrade = (AccTrade)((List)leOrdNo_accTrade.get(leOrdNo)).get(0);
            List<AccTradeOrderWalletPayVO> walletPayVOList = accTradeId_walletDetail.get(accTrade.getId()).stream().map((item) -> {
               return (new AccTradeOrderWalletPayVO()).setWalletId(item.getWalletId()).setAmount(item.getAmount()).setWalletBal(item.getWalletBal());
            }).collect(Collectors.toList());
            resultMap.put(leOrdNo, (new AccTradeOrderBalanceVO()).setAccTradeId(accTrade.getId()).setLeOrdNo(accTrade.getLeOrdNo()).setWalletBalTotal(accTrade.getWalletBalTotal()).setWalletPayDetail(walletPayVOList));
         });
         return resultMap;
      }
   }

}
