package com.qs.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.admin.domain.Goods;
import com.qs.mp.admin.domain.GoodsSku;
import com.qs.mp.admin.domain.GoodsTagRel;
import com.qs.mp.admin.domain.vo.GoodsExportVO;
import com.qs.mp.admin.mapper.GoodsMapper;
import com.qs.mp.admin.service.IGoodsService;
import com.qs.mp.admin.service.IGoodsSkuService;
import com.qs.mp.admin.service.IGoodsTagRelService;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.system.service.id.BizIdGenerator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 商品库 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-07
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

	@Autowired
	private IGoodsService goodsService;

	@Autowired
	private IGoodsSkuService goodsSkuService;

	@Autowired
	private IGoodsTagRelService goodsTagRelService;

	@Override
	@Transactional
	public void saveGoods(Goods goods, List<GoodsSku> skuList, List<Long> tagIds) {
		boolean res = goodsService.save(goods);
		if(!res) {
			throw new ServiceException("保存商品失败，请联系管理员");
		}
		if(null != skuList && skuList.size() > 0) {
			for(GoodsSku goodsSku:skuList) {
				if(null != goodsSku) {
					goodsSku.setGoodsId(goods.getGoodsId());
					if(null == goodsSku.getOriginPrice()) {
						goodsSku.setOriginPrice(goodsSku.getExchangePrice());
					}
				}
			}
			boolean skuRes = goodsSkuService.saveBatch(skuList);
			if(!skuRes) {
				throw new ServiceException("保存sku失败，请联系管理员");
			}
		}

		if (!CollectionUtils.isEmpty(tagIds)) {
			List<GoodsTagRel> goodsTagRelList = new ArrayList<>();
			for (Long tagId : tagIds) {
				GoodsTagRel goodsTagRel = new GoodsTagRel();
				goodsTagRel.setGoodsId(goods.getGoodsId());
				goodsTagRel.setTagId(tagId);
				goodsTagRelList.add(goodsTagRel);
			}
			boolean tagRes = goodsTagRelService.saveBatch(goodsTagRelList);
			if (!tagRes) {
				throw new ServiceException("保存标签失败，请联系管理员");
			}
		}
	}

	@Override
	@Transactional
	public void updateGoods(Goods goods, List<GoodsSku> skuList, List<Long> tagIds) {
		boolean res = goodsService.updateById(goods);
		if(!res) {
			throw new ServiceException("请联系管理员");
		}

		// 多SKU
		goodsSkuService.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getGoodsId()));
		if(res && null != skuList && skuList.size() > 0) {
			for(GoodsSku goodsSku:skuList) {
				if(null != goodsSku) {
					goodsSku.setGoodsId(goods.getGoodsId());
					if(null == goodsSku.getOriginPrice()) {
						goodsSku.setOriginPrice(goodsSku.getExchangePrice());
					}
				}
			}
			boolean skuRes = goodsSkuService.saveBatch(skuList);
			if(!skuRes) {
				throw new ServiceException("请联系管理员");
			}
		}
		// 标签
		goodsTagRelService.remove(new LambdaQueryWrapper<GoodsTagRel>().eq(GoodsTagRel::getGoodsId, goods.getGoodsId()));
		if (!CollectionUtils.isEmpty(tagIds)) {
			List<GoodsTagRel> goodsTagRelList = new ArrayList<>();
			for (Long tagId : tagIds) {
				GoodsTagRel goodsTagRel = new GoodsTagRel();
				goodsTagRel.setGoodsId(goods.getGoodsId());
				goodsTagRel.setTagId(tagId);
				goodsTagRelList.add(goodsTagRel);
			}
			boolean tagRes = goodsTagRelService.saveBatch(goodsTagRelList);
			if (!tagRes) {
				throw new ServiceException("保存标签失败，请联系管理员");
			}
		}
	}

	@Override
	public List<GoodsExportVO> listGoodsExportVOByQueryWrapper(QueryWrapper<Goods> queryWrapper) {
		return getBaseMapper().listGoodsExportVOByQueryWrapper(queryWrapper);
	}
}
