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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
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.constants.ApplyTypeEnum;
import com.bonus.canteen.core.order.common.constants.CheckStateEnum;
import com.bonus.canteen.core.order.common.mapper.OrderRefundMapper;
import com.bonus.canteen.core.order.common.model.OrderRefund;
import com.bonus.canteen.core.order.common.service.OrderRefundService;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, OrderRefund> implements OrderRefundService {
//   @Autowired
//   @Lazy
//   private OrderRefundDetailService orderRefundDetailService;
//
//   public OrderRefund getById(Serializable id) {
//      return id == null ? null : (OrderRefund)super.getById(id);
//   }

   public boolean existUnCheckOrder(Long orderId) {
      return this.baseMapper.exists(Wrappers
              .lambdaQuery(OrderRefund.class).eq(OrderRefund::getOrderId, orderId).eq(OrderRefund::getApplyType,
              ApplyTypeEnum.REFUND.getKey()).eq(OrderRefund::getCheckState, CheckStateEnum.WAIT.getKey()));
   }

   public boolean existRefundingOrder(Long orderId) {
      return this.baseMapper.exists(Wrappers
              .lambdaQuery(OrderRefund.class).eq(OrderRefund::getOrderId, orderId).eq(OrderRefund::getApplyType,
              ApplyTypeEnum.REFUND.getKey()).eq(OrderRefund::getCheckState, CheckStateEnum.REFUNDING.getKey()));
   }

   public void insertOrderRefundInfo(OrderRefund orderRefund) {
      this.save(orderRefund);
   }
//
//   public OrderRefund getOrderRefundById(Long orderRefundId) {
//      return this.getOrderRefundByIdOrMacRefundId(orderRefundId, (String)null);
//   }
//
//   public OrderRefund getOrderRefundByIdOrMacRefundId(Long orderRefundId, String macRefundId) {
//      return !LeNumUtil.isValidId(orderRefundId) && CharSequenceUtil.isEmpty(macRefundId) ? null : (OrderRefund)this.getOne(((LambdaQueryWrapper)Wrappers.lambdaQuery(OrderRefund.class).eq(LeNumUtil.isValidId(orderRefundId), OrderRefund::getOrderRefundId, orderRefundId)).eq(CharSequenceUtil.isNotEmpty(macRefundId), OrderRefund::getMacRefundId, macRefundId));
//   }
//
//   public OrderRefund listOrderRefundByRefundTradeId(Long refundTradeId) {
//      return ((OrderRefundMapper)this.baseMapper).getByRefundTradeId(refundTradeId);
//   }

   public void updateOrderRefundById(OrderRefund updateOrderRefund) {
      this.updateById(updateOrderRefund);
   }
//
//   public void updateCheckState(Long orderRefundId, Integer checkState, String checkResult) {
//      ((OrderRefundMapper)this.baseMapper).update((Object)null, (Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(OrderRefund.class).set(OrderRefund::getCheckState, checkState)).set(OrderRefund::getCheckResult, checkResult)).set(CheckStateEnum.UN_PASS.getKey().equals(checkState), OrderRefund::getRealRefundAmount, 0)).eq(OrderRefund::getOrderRefundId, orderRefundId));
//   }

   public BigDecimal getOrderRealRefundAmount(Long orderId) {
      BigDecimal realRefundAmount = ((OrderRefundMapper)this.baseMapper).getOrderRealRefundAmount(orderId, CheckStateEnum.PASS.getKey());
      return ObjectUtil.isNull(realRefundAmount) ? BigDecimal.ZERO : realRefundAmount;
   }
//
//   public void removeUnPassCorrectByMacOrderId(String macOrderId, Long detailId) {
//      if (!CharSequenceUtil.isEmpty(macOrderId)) {
//         List<Long> needRemoveRefundIds = ((OrderRefundMapper)this.baseMapper).listUnCheckCorrectRefundIds(macOrderId, detailId);
//         if (!CollUtil.isEmpty(needRemoveRefundIds)) {
//            ((OrderRefundMapper)this.baseMapper).delete((Wrapper)Wrappers.lambdaQuery(OrderRefund.class).in(OrderRefund::getOrderRefundId, needRemoveRefundIds));
//            this.orderRefundDetailService.deleteByOrderRefundIds(needRemoveRefundIds);
//         }
//      }
//   }

   @Override
   public List<OrderRefund> listByOrderIds(List<Long> orderIds, Integer applyType, Integer checkState) {
      return (List)(CollUtil.isEmpty(orderIds) ? CollUtil.newArrayList(new OrderRefund[0]) : ((OrderRefundMapper)this.baseMapper).selectList(((LambdaQueryWrapper<OrderRefund>)((LambdaQueryWrapper<OrderRefund>)Wrappers.lambdaQuery(OrderRefund.class).in(OrderRefund::getOrderId, orderIds)).eq(LeNumUtil.isValidId(applyType), OrderRefund::getApplyType, applyType)).eq(LeNumUtil.isValidId(checkState), OrderRefund::getCheckState, checkState)));
   }

}
