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.bonus.core.order.common.mapper.OrderShoppingCartMapper;
import com.bonus.core.order.common.model.OrderShoppingCart;
import com.bonus.core.order.common.service.OrderShoppingCartService;
import com.bonus.core.order.mobile.dto.OrderShoppingSearchDTO;
import com.bonus.core.order.utils.LeNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderShoppingCartServiceImpl implements OrderShoppingCartService {
   @Autowired
   @Lazy
   protected OrderShoppingCartMapper baseMapper;



   @Override
   public List<OrderShoppingCart> listByCust(OrderShoppingSearchDTO param) {
      return this.baseMapper.selectList(((LambdaQueryWrapper<OrderShoppingCart>)((LambdaQueryWrapper) Wrappers
              .lambdaQuery(OrderShoppingCart.class).eq(OrderShoppingCart::getCustId, param.getCustId())
              .eq(LeNumUtil.isValidId(param.getOrderType()), OrderShoppingCart::getOrderType, param.getOrderType()))
              .apply(CollUtil.isNotEmpty(param.getCanteenIdList()),"canteen_id IN (" + param.getCanteenIdList().stream().map(String::valueOf).collect(Collectors.joining(", ")) + ")"))
              .eq(LeNumUtil.isValidId(param.getStallId()), OrderShoppingCart::getStallId, param.getStallId()).orderByAsc(OrderShoppingCart::getShoppingCartId));
   }

   @Override
   public void plusQuantity(Long shoppingCartId, Integer quantity) {
      this.baseMapper.update(null, Wrappers.lambdaUpdate(OrderShoppingCart.class)
              .setSql(true, "quantity = quantity + " + quantity)
              .eq(OrderShoppingCart::getShoppingCartId, shoppingCartId));
   }

   @Override
   public void insert(OrderShoppingCart entity) {
      this.baseMapper.insert(entity);
   }

   @Override
   public void updateQuantity(Long shoppingCartId, Integer quantity) {
      this.baseMapper.update(null, Wrappers.lambdaUpdate(OrderShoppingCart.class).eq(OrderShoppingCart::getShoppingCartId, shoppingCartId).set(OrderShoppingCart::getQuantity, quantity));
   }

   @Override
   public void removeByIds(Collection<Long> shoppingCartIds) {
      if (!CollUtil.isEmpty(shoppingCartIds)) {
         this.baseMapper.deleteBatchIds(shoppingCartIds);
      }
   }

   @Override
   public void removeByCust(Long custId, Integer orderType, List<Long> canteenId) {
      if (LeNumUtil.isValidId(custId)) {
         this.baseMapper.delete(Wrappers.lambdaQuery(OrderShoppingCart.class).eq(OrderShoppingCart::getCustId, custId).in(CollUtil.isNotEmpty(canteenId), OrderShoppingCart::getCanteenId, canteenId).eq(LeNumUtil.isValidId(orderType), OrderShoppingCart::getOrderType, orderType));
      }
   }

   public List<OrderShoppingCart> listByIds(List<Long> shoppingCartIds) {
      return (CollUtil.isEmpty(shoppingCartIds) ? CollUtil.newArrayList(new OrderShoppingCart[0]) :
              this.baseMapper.selectList(Wrappers.lambdaQuery(OrderShoppingCart.class)
                      .in(CollUtil.isNotEmpty(shoppingCartIds), OrderShoppingCart::getShoppingCartId, shoppingCartIds)));
   }
}
