package com.bonus.canteen.core.account.v3.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.account.v3.constants.*;
import com.bonus.canteen.core.account.v3.custom.AccRechargeCustomBusiness;
import com.bonus.canteen.core.account.v3.dto.AccWalletUpdateDTO;
import com.bonus.canteen.core.account.v3.metadata.enums.AccWalletIdEnum;
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.mq.send.AccSendMq;
import com.bonus.canteen.core.account.v3.mq.send.dto.RepAccMqDTO;
import com.bonus.canteen.core.account.v3.po.AccTradeAddPO;
import com.bonus.canteen.core.account.v3.po.AccTradeWalletPO;
import com.bonus.canteen.core.account.v3.po.AccWalletBalanceUpdatePO;
import com.bonus.canteen.core.account.v3.service.*;
import com.bonus.canteen.core.account.v3.utils.AccUtils;
import com.bonus.canteen.core.account.v3.web.dto.AccRechargeBackDTO;
import com.bonus.canteen.core.account.v3.web.vo.AccInfoVO;
import com.bonus.canteen.core.account.v3.web.vo.AccUpdateWalletBalanceVO;
import com.bonus.canteen.core.account.v3.web.vo.AccWalletInfoVO;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.common.redis.RedisUtil;
import com.bonus.canteen.core.common.utils.JacksonUtil;
import com.bonus.canteen.core.common.utils.LogUtil;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.customer.api.CustInfoApi;
import com.bonus.canteen.core.marketing.v2.api.MarketApi;
import com.bonus.canteen.core.marketing.v2.rule.recharge.dto.RuleRechargeComputeDTO;
import com.bonus.canteen.core.marketing.v2.rule.recharge.dto.RuleRechargeResultDTO;
import com.bonus.canteen.core.marketing.v2.rule.recharge.dto.RuleRechargeWalletDTO;
import com.bonus.canteen.core.pay.api.PayApi;
import com.bonus.canteen.core.pay.api.TradeRecordApi;
import com.bonus.canteen.core.pay.api.dto.UnifyPaySelectDTO;
import com.bonus.canteen.core.pay.api.dto.UnifyRefundDTO;
import com.bonus.canteen.core.pay.api.vo.UnifyPaySelectVO;
import com.bonus.canteen.core.pay.common.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.bonus.canteen.core.pay.common.constants.TradeTypeEnum;
import com.bonus.canteen.core.pay.common.model.TradeOrderRecharge;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import com.github.pagehelper.page.PageMethod;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class AccWalletServiceImpl implements AccWalletService {
   private static final Logger log = LoggerFactory.getLogger(AccWalletServiceImpl.class);
   @Autowired
   @Lazy
   private AccBusinessService accBusinessService;


   @Override
   public void accRechargeResultHandlerV2(AccRechargeBackDTO accRechargeBackDTO) {
      log.info("账户收到支付结果>入参:{}", JSONUtil.toJsonStr(accRechargeBackDTO));
      if (!AccUtils.getAccTradeLockByTradeId(accRechargeBackDTO.getAccTradeId())) {
         log.info("账户收到支付结果,已经有任务在执行{}", JSONUtil.toJsonStr(accRechargeBackDTO));
      } else {
         try {
            this.accBusinessService.accRechargeModifyAndUpdate(accRechargeBackDTO);
         } catch (Exception var6) {
            log.error("[账户收到支付结果]系统错误", var6);
         } finally {
            AccUtils.accTradeUnLockByTradeId(accRechargeBackDTO.getAccTradeId());
         }

      }
   }
}
