package com.cyksj.service.distribute.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.GoodsDonMapper;
import com.cyksj.mapper.manage.distribute.DistributePosterGoodsMapper;
import com.cyksj.model.entity.DistributePosterGoods;
import com.cyksj.model.entity.GoodsDon;
import com.cyksj.service.distribute.DistributePosterGoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 项目名: yhlxj
 * 文件名: DistributePosterGoodsServiceImp
 * 创建者: JavaZou
 * 创建时间:2024/6/24 15:42
 */
@Service
@RequiredArgsConstructor
public class DistributePosterGoodsServiceImp extends ServiceImpl<DistributePosterGoodsMapper, DistributePosterGoods> implements DistributePosterGoodsService {

	private final GoodsDonMapper goodsDonMapper;

	@Override
	public void saveOrUpdatePo(DistributePosterGoods distributePosterGoods) throws Exception {
		Assert.notEmpty(distributePosterGoods.getImg(), "海报不为空");

		Long goodsId = distributePosterGoods.getGoodsId();

		GoodsDon goodsDon = goodsDonMapper.selectById(goodsId);

		if (goodsDon.getIsSkuDistribute()) {
			if (StrUtil.isEmpty(distributePosterGoods.getSkuIds())) {
				throw BusinessRuntimeException.getInstance("请选择对应的规格");
			}
			distributePosterGoods.setSkuIds(Jsons.toJson(Jsons.parseList(distributePosterGoods.getSkuIds(), String.class)));
		} else {
			distributePosterGoods.setSkuIds(null);
		}

		this.saveOrUpdate(distributePosterGoods);
	}
}
