package com.cyksj.service.giftcard.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.GiftCardGoodsSkuMapper;
import com.cyksj.mapper.GoodsDonMapper;
import com.cyksj.mapper.GoodsDonSkuMapper;
import com.cyksj.model.entity.GiftCardGoodsSku;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.request.GiftCardGoodsSkuReq;
import com.cyksj.redis.RedisService;
import com.cyksj.service.giftcard.GiftCardGoodsSkuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/*
 *项目名: netflix
 *文件名: GiftCardGoodsSkuServiceImpl
 *创建者: JavaZou
 *创建时间:2023/8/29 18:19
 */
@Service
@RequiredArgsConstructor
public class GiftCardGoodsSkuServiceImpl extends ServiceImpl<GiftCardGoodsSkuMapper, GiftCardGoodsSku> implements GiftCardGoodsSkuService {
	private final GoodsDonSkuMapper goodsDonSkuMapper;

	private final GoodsDonMapper goodsDonMapper;

	private final RedisService redisService;

	@Override
	public void setOrUpdateGiftCardSku(GiftCardGoodsSku data) {
		if (data.getGoodsId() == null) {
			throw BusinessRuntimeException.getInstance("请选择对应的礼品卡平台");
		}
		GoodsDon goodsDon = goodsDonMapper.selectById(data.getGoodsId());
		if (goodsDon == null) {
			throw BusinessRuntimeException.getInstance("请选择对应的礼品卡平台");
		}
		if (goodsDon.getSpecialType() == null || goodsDon.getSpecialType() != GoodsDon.SpecialType.giftCard) {
			throw BusinessRuntimeException.getInstance("该{0}平台不是礼品卡特殊类型", goodsDon.getTitle());
		}
		BigDecimal cash = data.getCash();
		Integer gcType = data.getGcType();
		BigDecimal price = data.getPrice();
		if (price == null || price.compareTo(BigDecimal.ZERO) <= 0) {
			throw BusinessRuntimeException.getInstance("请输入正确的礼品卡价格");
		}
		Long gcSkuId = data.getGcSkuId();
		if (gcSkuId == null && cash == null) {
			throw BusinessRuntimeException.getInstance("请选择对应的礼品卡类型");
		}

		if (gcSkuId != null && cash != null) {
			throw BusinessRuntimeException.getInstance("只能选择一种礼品卡类型");
		}
		if (cash != null) {
			if (cash.compareTo(BigDecimal.ZERO) <= 0) {
				throw BusinessRuntimeException.getInstance("请输入正确的礼品卡价格");
			}
			gcType = 1;
		}
		//虚拟卡
		if (gcSkuId != null) {
			GoodsDonSku sku = goodsDonSkuMapper.selectById(gcSkuId);
			if (sku == null) {
				throw BusinessRuntimeException.getInstance("存在未有的虚拟卡规格,请重新选择");
			}
			if (!sku.getStatus()) {
				throw BusinessRuntimeException.getInstance("存在下架的虚拟卡规格,请重新选择");
			}
			data.setGcGoodsId(sku.getGoodsId());
		}
		data.setGcType(gcType);
		if (gcType != 1 && data.getId() == null) {
			GiftCardGoodsSku one = this.getOne(Wrappers.lambdaQuery(GiftCardGoodsSku.class)
					.eq(GiftCardGoodsSku::getGcSkuId, gcSkuId).eq(GiftCardGoodsSku::getDeleted, 1).last("limit 1"));
			if (one != null) {
				return;
			}
		}
		this.saveOrUpdate(data);
	}

	@Override
	public void setGiftCardSku(GiftCardGoodsSkuReq req) {
		List<GiftCardGoodsSku> giftCardGoodsSkus = req.getConfigs();
		if (CollUtil.isEmpty(giftCardGoodsSkus)) {
			throw BusinessRuntimeException.getInstance("请输入对应数据");
		}
		giftCardGoodsSkus.forEach(giftCardSku -> {
			giftCardSku.setGoodsId(req.getGoodsId());
			giftCardSku.setGcType(req.getGcType());
			setOrUpdateGiftCardSku(giftCardSku);
		});
	}

	/**
	 * 设置对应礼品卡平台 最低价格
	 */
	@Override
	public void setGiftCardGoodsStat(GoodsDon goodsDon) {
		//设置对应礼品卡平台 最低价格
		BigDecimal minPrice = goodsDon.getMinPrice();
		GiftCardGoodsSku min = this.getOne(Wrappers.lambdaQuery(GiftCardGoodsSku.class)
				.eq(GiftCardGoodsSku::getGoodsId, goodsDon.getId())
				.eq(GiftCardGoodsSku::getStatus, true)
				.eq(GiftCardGoodsSku::getDeleted, true)
				.orderByAsc(GiftCardGoodsSku::getPrice).last("limit 1"));
		if (min != null) {
			if (minPrice == null || min.getPrice().compareTo(minPrice) != 0) {
				goodsDon.setMinPrice(min.getPrice());
				goodsDonMapper.updateById(goodsDon);
				Set<String> keys = redisService.keys(RedisService.key.YH_HOME_PAGHE_CACHE.getName() + "*");
				redisService.del(keys.toArray(String[]::new));
			}
		}
	}
}
