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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.account.v3.mapper.AccTradeWalletDetailMapper;
import com.bonus.core.account.v3.model.AccTradeWalletDetail;
import com.bonus.core.account.v3.mq.send.model.RepAccMqDetailModel;
import com.bonus.core.account.v3.po.AccTradeWalletPO;
import com.bonus.core.account.v3.service.AccTradeWalletDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

@Service
public class AccTradeWalletDetailServiceImpl extends ServiceImpl<AccTradeWalletDetailMapper, AccTradeWalletDetail> implements AccTradeWalletDetailService {
   private static final Logger log = LoggerFactory.getLogger(AccTradeWalletDetailServiceImpl.class);


   @Override
   public List<AccTradeWalletDetail> listCustMonthAccSubsidyExpirationTrade(Long custId) {
      return ((AccTradeWalletDetailMapper)this.baseMapper).listCustMonthAccSubsidyExpirationTrade(custId, LocalDateTime.now(), LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).atTime(23, 59, 59));
   }

   @Override
   public int insertAccTradeWalletDetail(AccTradeWalletPO accTradeWalletPo) {
      AccTradeWalletDetail accTradeWalletDetail = new AccTradeWalletDetail();
      BeanUtil.copyProperties(accTradeWalletPo, accTradeWalletDetail, new String[0]);
      log.info("[账户交易明细]新增开始:{}", JSONUtil.toJsonStr(accTradeWalletDetail));
      int insert = ((AccTradeWalletDetailMapper)this.baseMapper).insert(accTradeWalletDetail);
      log.info("[账户交易明细]新增结束");
      return insert;
   }

   @Override
   public List<RepAccMqDetailModel> listAccTradeWalletDetailForSend(List<Long> tradeIdList) {
      return ((AccTradeWalletDetailMapper)this.baseMapper).listAccTradeWalletDetailForSend(tradeIdList);
   }

   @Override
   public List<Long> selectCustIdForSubsidyExpireTime(LocalDateTime now) {
      return ((AccTradeWalletDetailMapper)this.baseMapper).selectCustIdForSubsidyExpireTime(now);
   }
   @Override
   public void updateExpiredClearTradeUseAmount(Long id, BigDecimal useAmount) {
      ((AccTradeWalletDetailMapper)this.baseMapper).update(null,Wrappers.<AccTradeWalletDetail>lambdaUpdate()
              .setSql(true, "use_amount = use_amount + " + useAmount)
              .eq(AccTradeWalletDetail::getId, id));
   }


   @Override
   public AccTradeWalletDetail selectOneSubsidyExpirationTrade(Long custId) {
      LocalDateTime localDateTime = LocalDate.now().atTime(0, 0, 0);
      return ((AccTradeWalletDetailMapper)this.baseMapper).selectOneSubsidyExpirationTrade(custId, localDateTime);
   }

   @Override
   public List<AccTradeWalletDetail> queryAccTradeWalletDetailByLeOrdNo(Long orderId, List<Integer> tradeTypeList) {
      return ((AccTradeWalletDetailMapper)this.baseMapper).queryAccTradeWalletDetailByLeOrdNo(orderId, tradeTypeList);
   }


   @Override
   public List<AccTradeWalletDetail> queryAccTradeWalletDetailByOriginTradeId(Long originTradeId, Integer tradeType) {
      return ((AccTradeWalletDetailMapper)this.baseMapper).queryAccTradeWalletDetailByOriginTradeId(originTradeId, tradeType);
   }

}
