/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.ShopCartDto;
import com.yami.shop.bean.app.dto.ShopCartItemDto;
import com.yami.shop.bean.app.param.ChangeShopCartParam;
import com.yami.shop.bean.app.param.OrderItemParam;
import com.yami.shop.bean.app.param.ShopCartParam;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.event.ShopCartEvent;
import com.yami.shop.bean.model.Basket;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.Sku;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.CacheManagerUtil;
import com.yami.shop.dao.BasketMapper;
import com.yami.shop.service.BasketService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.SkuService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lgh on 2018/10/18.
 */
@Service
@AllArgsConstructor
public class BasketServiceImpl extends ServiceImpl<BasketMapper, Basket> implements BasketService {

    private final BasketMapper basketMapper;

    private final CacheManagerUtil cacheManagerUtil;

    private final ApplicationContext applicationContext;

    private final SkuService skuService;

    private final ShopDetailService shopDetailService;

    private final ProductService productService;

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void deleteShopCartItemsByBasketIds(String userId, List<Long> basketIds) {
        basketMapper.deleteShopCartItemsByBasketIds(userId, basketIds);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void addShopCartItem(ChangeShopCartParam param, String userId) {
        Basket basket = new Basket();
        basket.setBasketCount(param.getCount());
        basket.setBasketDate(new Date());
        basket.setProdId(param.getProdId());
        basket.setShopId(param.getShopId());
        basket.setChannelId(param.getChannelId());
        basket.setUserId(userId);
        basket.setSkuId(param.getSkuId());
//        basket.setDistributionCardNo(param.getDistributionCardNo());
        basketMapper.insert(basket);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#basket.userId")
    public void updateShopCartItem(Basket basket) {
        basketMapper.updateById(basket);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void deleteAllShopCartItems(String userId) {
        basketMapper.deleteAllShopCartItems(userId);
    }

//    @Override
//    public List<ShopCartItemDto> getShopCartItems(Long channelId,String userId) {
//        // 在这个类里面要调用这里的缓存信息，并没有使用aop，所以不使用注解
//
//        List<ShopCartItemDto> shopCartItemDtoList = basketMapper.getShopCartItems(userId);
//        for (ShopCartItemDto shopCartItemDto : shopCartItemDtoList) {
//            shopCartItemDto.setProductTotalAmount(Arith.mul(shopCartItemDto.getProdCount(), shopCartItemDto.getPrice()));
//            shopCartItemDto.setWeight(shopCartItemDto.getWeight());
//            shopCartItemDto.setWeightUnit(shopCartItemDto.getWeightUnit());
//        }
//        return shopCartItemDtoList;
//    }

    @Override
    public List<ShopCartItemDto> getShopCartItems(String userId, Long channelId) {
        // 在这个类里面要调用这里的缓存信息，并没有使用aop，所以不使用注解
        List<ShopCartItemDto> shopCartItemDtoList = basketMapper.getShopCartItemsByPlatform(userId, channelId);
        for (ShopCartItemDto shopCartItemDto : shopCartItemDtoList) {
            shopCartItemDto.setProductTotalAmount(Arith.mul(shopCartItemDto.getProdCount(), shopCartItemDto.getPrice()));
            shopCartItemDto.setWeight(shopCartItemDto.getWeight());
            shopCartItemDto.setWeightUnit(shopCartItemDto.getWeightUnit());
        }
        return shopCartItemDtoList;
    }

    @Override
    public List<ShopCartItemDto> getShopCartExpiryItems(String userId) {
        return basketMapper.getShopCartExpiryItems(userId);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void cleanExpiryProdList(String userId) {
        basketMapper.cleanExpiryProdList(userId);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void updateBasketByShopCartParam(String userId, List<ShopCartParam> shopCartParams) {
        basketMapper.updateDiscountItemId(userId, shopCartParams);
    }

    @Override
//    @CacheEvict(cacheNames = "ShopCartItems", key = "#userId")
    public void removeShopCartItemsCacheByUserId(String userId) {

    }

    @Override
    public List<String> listUserIdByProdId(Long prodId) {
        return basketMapper.listUserIdByProdId(prodId);
    }

    @Override
    public List<ShopCartDto> getShopCarts(List<ShopCartItemDto> shopCartItems, Long shopId1) {
        // 根据店铺ID划分item
        Map<Long, List<ShopCartItemDto>> shopCartMap = shopCartItems.stream().collect(Collectors.groupingBy(ShopCartItemDto::getShopId));
        if (shopId1 != null && shopId1 > 0) {
            List<ShopCartDto> shopCartDtos2 = Lists.newArrayList();
            //获取店铺的所有商品项
            List<ShopCartItemDto> shopCartItemDtoList2 = shopCartMap.get(shopId1);
            if (shopCartItemDtoList2 != null) {
                // 构建每个店铺的购物车信息
                ShopCartDto shopCart2 = new ShopCartDto();
                //店铺信息
                shopCart2.setShopId(shopId1);
                shopCart2.setShopName(shopCartItemDtoList2.get(0).getShopName());
                applicationContext.publishEvent(new ShopCartEvent(shopCart2, shopCartItemDtoList2));
                // 自营店的购物车位置是放在第一的
                if (Objects.equals(1L, shopId1)) {
                    shopCartDtos2.add(0, shopCart2);
                } else {
                    shopCartDtos2.add(shopCart2);
                }
            }
            return shopCartDtos2;
        }
        // 返回一个店铺的所有信息
        List<ShopCartDto> shopCartDtos = Lists.newArrayList();
        for (Long shopId : shopCartMap.keySet()) {
            //获取店铺的所有商品项
            List<ShopCartItemDto> shopCartItemDtoList = shopCartMap.get(shopId);
            // 构建每个店铺的购物车信息
            ShopCartDto shopCart = new ShopCartDto();
            //店铺信息
            shopCart.setShopId(shopId);
            shopCart.setShopName(shopCartItemDtoList.get(0).getShopName());
            applicationContext.publishEvent(new ShopCartEvent(shopCart, shopCartItemDtoList));
            // 自营店的购物车位置是放在第一的
            if (Objects.equals(1L, shopId)) {
                shopCartDtos.add(0, shopCart);
            } else {
                shopCartDtos.add(shopCart);
            }
        }

        return shopCartDtos;
    }

    @Override
    public List<ShopCartItemDto> getShopCartItemsByOrderItems(List<Long> basketId, OrderItemParam orderItem, String userId,Long channelId) {
        if (orderItem == null && CollectionUtil.isEmpty(basketId)) {
            return Collections.emptyList();
        }
        // 当立即购买时，没有提交的订单是没有购物车信息的
        if (CollectionUtil.isEmpty(basketId) && orderItem != null) {
            return Collections.singletonList(getShopCartItem(orderItem));
        }

        List<ShopCartItemDto> dbShopCartItems = getShopCartItems(userId,channelId);

        // 返回购物车选择的商品信息
        List<ShopCartItemDto> collect = dbShopCartItems
                .stream()
                .filter(shopCartItemDto -> basketId.contains(shopCartItemDto.getBasketId()))
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public ShopCartItemDto getShopCartItem(OrderItemParam orderItem) {
        //Integer platform, Long shopId
        Sku sku = skuService.getSkuBySkuId(orderItem.getSkuId(), orderItem.getChannelId(), orderItem.getShopId());
        if (sku == null) {
            throw new RuntimeException("订单包含无法识别的商品");
        }
        Product prod = productService.getProductByProdId(orderItem.getProdId());
        if (prod == null) {
            throw new RuntimeException("订单包含无法识别的商品");
        }

        // 拿到购物车的所有item
        ShopCartItemDto shopCartItemDto = new ShopCartItemDto();
        shopCartItemDto.setBasketId(-1L);
        shopCartItemDto.setSkuId(orderItem.getSkuId());
        shopCartItemDto.setProdCount(orderItem.getProdCount());
        shopCartItemDto.setProdId(orderItem.getProdId());
        if (Objects.equals(prod.getProdType(), ProdType.PROD_TYPE_SCORE.value())) {
            shopCartItemDto.setScorePrice(sku.getSkuScore() * orderItem.getProdCount());
        }
        shopCartItemDto.setSkuName(sku.getSkuName());
        shopCartItemDto.setWeight(sku.getWeight());
        shopCartItemDto.setWeightUnit(sku.getWeightUnit());
        shopCartItemDto.setCategoryId(prod.getCategoryId());
        shopCartItemDto.setPic(StrUtil.isBlank(sku.getPic()) ? prod.getPic() : sku.getPic());
        shopCartItemDto.setProdName(sku.getProdName());
        shopCartItemDto.setProductTotalAmount(Arith.mul(sku.getPrice(), orderItem.getProdCount()));
        shopCartItemDto.setPrice(sku.getPrice());
        shopCartItemDto.setDistributionCardNo(orderItem.getDistributionCardNo());
        shopCartItemDto.setBasketDate(new Date());
        if (orderItem.getShopId() == 0) {
            shopCartItemDto.setShopId(0L);
            shopCartItemDto.setShopName(Constant.PLATFORM_SHOP_NAME);
        } else {
            ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(orderItem.getShopId());
            shopCartItemDto.setShopId(shopDetail.getShopId());
            shopCartItemDto.setShopName(shopDetail.getShopName());
        }
        return shopCartItemDto;
    }
}
