package com.bonus.canteen.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.canteen.core.order.common.mapper.OrderDeliveryMapper;
import com.bonus.canteen.core.order.common.model.OrderDelivery;
import com.bonus.canteen.core.order.common.service.OrderDeliveryService;
import com.bonus.canteen.core.order.utils.LeOrderUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@Service
public class OrderDeliveryServiceImpl extends ServiceImpl<OrderDeliveryMapper, OrderDelivery> implements OrderDeliveryService {

   @Override
   public OrderDelivery getByOrderId(Long orderId, LocalDate orderDate) {
      return orderId == null ? null : this.baseMapper
              .selectOne(Wrappers.lambdaQuery(OrderDelivery.class)
                      .eq(OrderDelivery::getOrderId, orderId).eq(orderDate != null,
                      OrderDelivery::getOrderDate, orderDate).last("limit 1"));
   }

   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(OrderDelivery.class).in(OrderDelivery::getOrderId, orderIds).
                 between(OrderDelivery::getOrderDate, dateRange.get(0), dateRange.get(1)));
      }
   }

   @Transactional(
           rollbackFor = {Exception.class}
   )
   public void insertList(Collection<OrderDelivery> entityList) {
      if (!CollUtil.isEmpty(entityList)) {
         Iterator var2 = entityList.iterator();

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

      }
   }

   public List<OrderDelivery> listOrderDeliveryByOrderIds(List<Long> orderIds, Collection<LocalDate> orderDates) {
      if (CollUtil.isEmpty(orderIds)) {
         return CollUtil.newArrayList(new OrderDelivery[0]);
      } else {
         List<LocalDate> dateRange = LeOrderUtil.queryDateRange((Collection)orderIds, (Collection)orderDates);
         //return ((OrderDeliveryMapper)this.baseMapper).selectList((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(OrderDelivery.class)
         // .in(OrderDelivery::getOrderId, orderIds))
         // .between(OrderDelivery::getOrderDate, dateRange.get(0), dateRange.get(1)));
         List<OrderDelivery> result = ((OrderDeliveryMapper)this.baseMapper).selectList(Wrappers.lambdaQuery(OrderDelivery.class)
                                      .in(OrderDelivery::getOrderId, orderIds)
                                      .between(OrderDelivery::getOrderDate, dateRange.get(0), dateRange.get(1)));
         return result;
      }
   }
}
