package org.springblade.modules.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.common.enums.ZoneTypeEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.entity.GoodsSpec;
import org.springblade.modules.mall.entity.ShoppingCart;
import org.springblade.modules.mall.mapper.ShoppingCartMapper;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springblade.modules.mall.service.IGoodsSpecService;
import org.springblade.modules.mall.service.IShoppingCartService;
import org.springblade.modules.mall.vo.ShoppingCartVO;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author pangqijun
 * Date 2022/7/28
 * Description
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

	@Resource
	private IGoodsInfoService goodsInfoService;
	@Resource
	private IGoodsSpecService goodsSpecService;

	@Override
	public void add(ShoppingCart cart) {
		GoodsInfo goodsInfo = goodsInfoService.getById(cart.getGoodsInfoId());
		Assert.notNull(goodsInfo, "商品数据不存在");
		if (ZoneTypeEnum.CITY_PARTNER.getValue().equals(goodsInfo.getZoneType())) {
			throw new ServiceException("城市合伙人暂不支持添加到购物车");
		}
		ShoppingCart one = this.lambdaQuery().eq(ShoppingCart::getUserId, cart.getUserId()).eq(ShoppingCart::getGoodsInfoId, cart.getGoodsInfoId())
			.eq(ShoppingCart::getGoodsSpecId, cart.getGoodsSpecId()).one();
		if (one == null) {
			this.save(cart);
		} else {
			one.setGoodsNum(one.getGoodsNum() + cart.getGoodsNum());
			this.updateById(one);
		}
	}

	@Override
	public List<ShoppingCartVO> list(Query query) {
		List<ShoppingCartVO> cartVOS = this.baseMapper.selectPage(Condition.getPage(query), AuthUtil.getUserId());
		for (ShoppingCartVO vo : cartVOS) {
			vo.setCover(vo.getGoodsSpecImg());
			if (StrUtil.isBlank(vo.getGoodsSpecImg())) {
				vo.setCover(vo.getGoodsInfoImg());
			}

		}
		return cartVOS;
	}

	@Override
	public int increase(Long shoppingCartId) {
		ShoppingCart cart = this.getById(shoppingCartId);
		Assert.notNull(cart, "数据不存在");
		cart.setGoodsNum(cart.getGoodsNum() + 1);
		this.updateById(cart);
		return cart.getGoodsNum();
	}

	@Override
	public int reduce(Long shoppingCartId) {
		ShoppingCart cart = this.getById(shoppingCartId);
		Assert.notNull(cart, "数据不存在");
		if (cart.getGoodsNum() == 1) {
			throw new ServiceException("宝贝数量不能再减少了");
		}
		cart.setGoodsNum(cart.getGoodsNum() - 1);
		this.updateById(cart);
		return cart.getGoodsNum();
	}

	@Override
	public Integer quantity(Long userId) {
		return this.lambdaQuery().eq(ShoppingCart::getUserId, userId).count();
	}

	@Override
	public void clear(Long userId) {
		List<ShoppingCart> list = this.lambdaQuery().eq(ShoppingCart::getUserId, userId).list();
		for (ShoppingCart shoppingCart : list) {
			this.removeById(shoppingCart.getId());
		}
	}

	@Override
	public List<GoodsSpec> getGoodsSpec(Long goodsInfoId) {
		return goodsSpecService.lambdaQuery().eq(GoodsSpec::getGoodsId, goodsInfoId).eq(GoodsSpec::getIsSale, YesOrNoEnum.YES.getValue()).list();
	}

	@Override
	public void bindGoodsSpec(Long shoppingCartId, Long goodsSpecId) {
		ShoppingCart cart = this.getById(shoppingCartId);
		Assert.notNull(cart, "购物车数据不存在");
		cart.setGoodsSpecId(goodsSpecId);
		this.updateById(cart);
	}

	@Override
	public void remove(List<Long> shoppingCartIds) {
		this.removeByIds(shoppingCartIds);
	}
}
