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.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.order.common.mapper.OrderRefundDetailMapper;
import com.bonus.core.order.common.model.OrderRefundDetail;
import com.bonus.core.order.common.service.OrderRefundDetailService;
import com.bonus.core.order.weight.vo.RefundDetailStateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.time.LocalDate;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrderRefundDetailServiceImpl extends ServiceImpl<OrderRefundDetailMapper, OrderRefundDetail> implements OrderRefundDetailService {
//   @Autowired
//   @Lazy
//   protected OrderDetailService orderDetailService;
//   @Autowired
//   @Lazy
//   protected OrderRefundService orderRefundService;

//   public OrderRefundDetail getById(Serializable id) {
//      return id == null ? null : (OrderRefundDetail)super.getById(id);
//   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void batchInsertOrderRefundDetail(List<OrderRefundDetail> orderRefundDetailList) {
      if (!CollUtil.isEmpty(orderRefundDetailList)) {
         super.saveBatch(orderRefundDetailList);
      }
   }

   public void updateRefundDetailQuantity(List<OrderRefundDetail> orderDetailList) {
      if (!CollUtil.isEmpty(orderDetailList)) {
         Iterator var2 = orderDetailList.iterator();

         while(var2.hasNext()) {
            OrderRefundDetail orderRefundDetail = (OrderRefundDetail)var2.next();
            ((OrderRefundDetailMapper)this.baseMapper).update(null, Wrappers.lambdaUpdate(OrderRefundDetail.class)
                    .set(orderRefundDetail.getQuantity() != null, OrderRefundDetail::getQuantity, orderRefundDetail.getQuantity())
                    .set(orderRefundDetail.getRealQuantity() != null, OrderRefundDetail::getRealQuantity, orderRefundDetail.getRealQuantity())
                    .set(orderRefundDetail.getRestoreQuantity() != null, OrderRefundDetail::getRestoreQuantity, orderRefundDetail.getRestoreQuantity())
                    .set(orderRefundDetail.getRefundAmount() != null, OrderRefundDetail::getRefundAmount, orderRefundDetail.getRefundAmount())
                    .set(orderRefundDetail.getRealRefundAmount() != null, OrderRefundDetail::getRealRefundAmount, orderRefundDetail.getRealRefundAmount())
                    .eq(OrderRefundDetail::getOrderRefundId, orderRefundDetail.getOrderRefundId()).eq(OrderRefundDetail::getDetailId, orderRefundDetail.getDetailId()));
         }

      }
   }
//
//   public void deleteByOrderRefundIds(List<Long> orderRefundIds) {
//      if (!CollUtil.isEmpty(orderRefundIds)) {
//         ((OrderRefundDetailMapper)this.baseMapper).delete((Wrapper)Wrappers.lambdaQuery(OrderRefundDetail.class).in(OrderRefundDetail::getOrderRefundId, orderRefundIds));
//      }
//   }
//
//   public List<OrderRefundDetail> listByOrderRefundId(Long orderRefundId) {
//      return ((OrderRefundDetailMapper)this.baseMapper).selectList((Wrapper)Wrappers.lambdaQuery(OrderRefundDetail.class).eq(OrderRefundDetail::getOrderRefundId, orderRefundId));
//   }
//
//   public List<OrderRefundDetail> listByOrderRefundIds(List<Long> orderRefundIds) {
//      return (List)(CollUtil.isEmpty(orderRefundIds) ? CollUtil.newArrayList(new OrderRefundDetail[0]) : ((OrderRefundDetailMapper)this.baseMapper).selectList((Wrapper)Wrappers.lambdaQuery(OrderRefundDetail.class).in(OrderRefundDetail::getOrderRefundId, orderRefundIds)));
//   }

   @Override
   public List<RefundDetailStateVO> listRefundDetailState(Long orderId, String macOrderId) {
      return ((OrderRefundDetailMapper)this.baseMapper).listRefundDetailState(orderId, macOrderId);
   }

//   public List<OrderRefundDetailVO> listOrderRefundDetailVO(List<Long> orderRefundIds, List<LocalDate> orderDates) {
//      if (CollUtil.isEmpty(orderRefundIds)) {
//         return CollUtil.newArrayList(new OrderRefundDetailVO[0]);
//      } else {
//         List<OrderRefundDetail> orderRefundDetails = ((OrderRefundDetailMapper)this.baseMapper).selectList((Wrapper)Wrappers.lambdaQuery(OrderRefundDetail.class).in(OrderRefundDetail::getOrderRefundId, orderRefundIds));
//         if (CollUtil.isEmpty(orderRefundDetails)) {
//            return CollUtil.newArrayList(new OrderRefundDetailVO[0]);
//         } else {
//            List<Long> detailIds = (List)orderRefundDetails.stream().map(OrderRefundDetail::getDetailId).collect(Collectors.toList());
//            Map<Long, OrderDetail> orderDetailMap = (Map)this.orderDetailService.listByDetailIds(detailIds, orderDates).stream().collect(Collectors.toMap(OrderDetail::getDetailId, Function.identity()));
//            List<OrderRefundDetailVO> voList = (List)orderRefundDetails.stream().map((s) -> {
//               return OrderRefundDetailVO.of(s, (OrderDetail)orderDetailMap.get(s.getDetailId()));
//            }).collect(Collectors.toList());
//            this.orderDetailService.fillOrderDetailImage(voList);
//            return voList;
//         }
//      }
//   }
}
