package com.cyksj.service.mange.vip.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.mapper.vip.VipGoodsSkuMapper;
import com.cyksj.model.entity.VipGoodsSku;
import com.cyksj.service.mange.vip.VipCommonService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj2
 * 文件名: VipCommonServiceImpl
 * 创建者: JavaZou
 * 创建时间:2025/5/14 17:01
 */
@Service
@RequiredArgsConstructor
public class VipCommonServiceImpl implements VipCommonService {
	private final VipGoodsSkuMapper vipGoodsSkuMapper;

	@Override
	public void setVipGoodsSku(List<VipGoodsSku> vipGoodsSkus) {
		List<Long> collect = vipGoodsSkus.stream().map(VipGoodsSku::getSkuId).collect(Collectors.toList());
		//清除
		vipGoodsSkuMapper.delete(Wrappers.lambdaUpdate(VipGoodsSku.class)
				.notIn(VipGoodsSku::getSkuId, collect));

		vipGoodsSkus.forEach(ygs->{
			Long id = ygs.getId();
			if (id == null) {
				try {
					vipGoodsSkuMapper.insert(ygs);
				} catch (DuplicateKeyException e) {
				}
				return;
			}
			vipGoodsSkuMapper.updateById(ygs);
		});
	}
}
