package com.bonus.core.order.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.order.common.mapper.OrderAmountChangeMapper;
import com.bonus.core.order.common.model.OrderAmountChange;
import com.bonus.core.order.common.service.OrderAmountChangeService;
import com.bonus.core.order.utils.LeOrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderAmountChangeServiceImpl extends ServiceImpl<OrderAmountChangeMapper, OrderAmountChange> implements OrderAmountChangeService {
   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void saveList(Collection<OrderAmountChange> entityList) {
      if (!CollUtil.isEmpty(entityList)) {
         Iterator var2 = entityList.iterator();

         while(var2.hasNext()) {
            OrderAmountChange entity = (OrderAmountChange)var2.next();
            boolean exists = this.baseMapper.exists(Wrappers.lambdaQuery(OrderAmountChange.class)
                    .eq(OrderAmountChange::getChangeId, entity.getChangeId()).eq(OrderAmountChange::getOrderDate,
                    entity.getOrderDate()));
            if (exists) {
               this.baseMapper.update(entity, Wrappers
                       .lambdaUpdate(OrderAmountChange.class).eq(OrderAmountChange::getChangeId, entity.getChangeId())
                       .eq(OrderAmountChange::getOrderDate, entity.getOrderDate()));
            } else {
               this.baseMapper.insert(entity);
            }
         }

      }
   }

   public void deleteByOrderIds(List<Long> orderIds, Collection<LocalDate> orderDates) {
      if (!CollUtil.isEmpty(orderIds)) {
         List<LocalDate> dateRange = LeOrderUtil.queryDateRange(orderIds, orderDates);
         this.baseMapper.delete(Wrappers.lambdaQuery(OrderAmountChange.class)
                 .in(OrderAmountChange::getOrderId, orderIds).between(OrderAmountChange::getOrderDate, dateRange.get(0),
                 dateRange.get(1)));
      }
   }

   public List<OrderAmountChange> listByOrderIds(List<Long> orderIds, Collection<LocalDate> orderDates) {
      if (CollUtil.isEmpty(orderIds)) {
         CollUtil.newArrayList(new Object[0]);
      }

      List<LocalDate> dateRange = LeOrderUtil.queryDateRange((Collection)orderIds, (Collection)orderDates);
      return (this.baseMapper).selectList(Wrappers.lambdaQuery(OrderAmountChange.class)
              .in(OrderAmountChange::getOrderId, orderIds).between(OrderAmountChange::getOrderDate,
              dateRange.get(0), dateRange.get(1)));
   }
}
