package com.gxwebsoft.shop.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gxwebsoft.common.system.entity.Dict;
import com.gxwebsoft.common.system.entity.DictData;
import com.gxwebsoft.shop.entity.Goods;
import com.gxwebsoft.shop.entity.GoodsSpec;
import com.gxwebsoft.shop.mapper.CartMapper;
import com.gxwebsoft.shop.service.CartService;
import com.gxwebsoft.shop.entity.Cart;
import com.gxwebsoft.shop.param.CartParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.vo.CartShopVo;
import com.gxwebsoft.shop.vo.CartVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 购物车Service实现
 *
 * @author 科技小王子
 * @since 2024-07-29 21:02:39
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

  @Override
  public PageResult<Cart> pageRel(CartParam param) {
    PageParam<Cart, CartParam> page = new PageParam<>(param);
    page.setDefaultOrder("create_time desc");
    List<Cart> list = baseMapper.selectPageRel(page, param);
    return new PageResult<>(list, page.getTotal());
  }

  @Override
  @Transactional(rollbackFor = {Exception.class})
  public CartVo listRel(CartParam param) {
    List<Cart> list = baseMapper.selectListRel(param);
    // 排序
    PageParam<Cart, CartParam> page = new PageParam<>();
    page.setDefaultOrder("create_time desc");
    final List<Cart> carts = page.sortRecords(list);
    System.out.println("carts = " + carts.size());
    // 整理数据
    final CartVo vo = new CartVo();
    // 计算单品总价
    for (Cart cart : carts) {
      cart.setTotalPrice(cart.getPrice().multiply(BigDecimal.valueOf(cart.getCartNum())));
    }
    // 计算购物车金额
    final Integer cartNum = carts.stream().map(Cart::getCartNum).reduce(Integer::sum).orElse(0);
    final Integer selectNums = carts.stream().filter(d -> d.getSelected().equals(true)).map(Cart::getCartNum).reduce(Integer::sum).orElse(0);
    final BigDecimal totalPrice = carts.stream().filter(d -> d.getSelected().equals(true)).map(Cart::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
    // 按店铺ID分组
    final Map<Integer, List<Cart>> collectCart = carts.stream().collect(Collectors.groupingBy(Cart::getMerchantId));
    final ArrayList<CartShopVo> shops = new ArrayList<>();
    if (MapUtil.isNotEmpty(collectCart)) {
      for (Integer shopId : collectCart.keySet()) {
        final CartShopVo shopVo = new CartShopVo();
        shopVo.setShopId(shopId);
        shopVo.setCarts(collectCart.get(shopId));
        if (ObjectUtil.isNotEmpty(collectCart.get(shopId).get(0))) {
          final Cart cart = collectCart.get(shopId).get(0);
          shopVo.setShopName(cart.getMerchantName());
          shopVo.setShopLogo(cart.getImage());
        }
        shops.add(shopVo);
      }
    }

    vo.setShops(shops);
    vo.setTotalPrice(totalPrice);
    vo.setTotalNums(Long.valueOf(cartNum));
    vo.setSelectNums(Long.valueOf(selectNums));
//    updateBatchById(carts);

    return vo;
  }

  @Override
  public Cart getByIdRel(Integer id) {
    CartParam param = new CartParam();
    param.setId(id);
    return param.getOne(baseMapper.selectListRel(param));
  }

  @Override
  public List<Cart> listRelMpj(CartParam param) {

    // 多表查询
    MPJLambdaWrapper<Cart> mpjLambdaWrapper = new MPJLambdaWrapper<>();
    mpjLambdaWrapper.selectAll(Cart.class)
      .select(Goods::getGoodsName)
      .select(Goods::getImage)
      .leftJoin(Goods.class,Goods::getGoodsId,Cart::getGoodsId).eq(Cart::getUserId,param.getUserId());

      // 查询选中状态
      if(param.getSelected() != null){
        mpjLambdaWrapper.eq(Cart::getSelected, param.getSelected());
      }
      // 商品类型 0商城 1预定
      if(param.getType() != null){
        mpjLambdaWrapper.eq(Cart::getType,param.getType());
      }
      // 是否为立即购买
      if(param.getIsNew() != null){
        mpjLambdaWrapper.eq(Cart::getIsNew,param.getIsNew());
      }
      // 指定cartId
      if(param.getId() != null){
        mpjLambdaWrapper.eq(Cart::getId,param.getId());
      }
    System.out.println("mpjLambdaWrapper = " + mpjLambdaWrapper);
    return baseMapper.selectJoinList(Cart.class, mpjLambdaWrapper);
  }

}
