|
|
@@ -1,13 +1,25 @@
|
|
|
package com.cyksj.service.mange.goods;
|
|
|
|
|
|
+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.GoodsDiscountPlusPurchaseRelationMapper;
|
|
|
import com.cyksj.mapper.GoodsDonSkuMapper;
|
|
|
+import com.cyksj.model.entity.GoodsDiscountPlusPurchaseRelation;
|
|
|
import com.cyksj.model.entity.GoodsDonSku;
|
|
|
import com.cyksj.service.mange.CmsGoodsDonSkuService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
/**
|
|
|
* @author chan
|
|
|
* @date 2022/9/27 5:43 PM
|
|
|
@@ -16,4 +28,94 @@ import org.springframework.stereotype.Service;
|
|
|
@RequiredArgsConstructor
|
|
|
@Service
|
|
|
public class CmsGoodsDonSkuServiceImpl extends ServiceImpl<GoodsDonSkuMapper,GoodsDonSku> implements CmsGoodsDonSkuService {
|
|
|
+ private final GoodsDiscountPlusPurchaseRelationMapper discountPlusPurchaseRelationMapper;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper skuMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void handleDiscountPurchaseGoods(GoodsDonSku sku, List<GoodsDiscountPlusPurchaseRelation> discountPlusPurchaseGoods) {
|
|
|
+ Long skuId = sku.getId();
|
|
|
+ Long goodsId = sku.getGoodsId();
|
|
|
+ List<GoodsDiscountPlusPurchaseRelation> dbDiscountPlusGoodsList = discountPlusPurchaseRelationMapper.selectList(Wrappers.lambdaQuery(GoodsDiscountPlusPurchaseRelation.class)
|
|
|
+ .eq(GoodsDiscountPlusPurchaseRelation::getSkuId, skuId));
|
|
|
+ if (CollUtil.isEmpty(dbDiscountPlusGoodsList)) {
|
|
|
+ discountPlusPurchaseGoods.forEach(discountGoods -> {
|
|
|
+ checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
|
|
|
+ });
|
|
|
+ sku.setIsDp(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //优惠加购为空
|
|
|
+ if (CollUtil.isEmpty(discountPlusPurchaseGoods)) {
|
|
|
+ discountPlusPurchaseRelationMapper.delete(Wrappers.lambdaQuery(GoodsDiscountPlusPurchaseRelation.class)
|
|
|
+ .eq(GoodsDiscountPlusPurchaseRelation::getSkuId, skuId));
|
|
|
+ sku.setIsDp(false);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增或更新
|
|
|
+ sku.setIsDp(true);
|
|
|
+ Map<Long, GoodsDiscountPlusPurchaseRelation> skuIdMap = new ConcurrentHashMap<>();
|
|
|
+ discountPlusPurchaseGoods.forEach(discountGoods -> {
|
|
|
+ AtomicBoolean is_exist = new AtomicBoolean(false);
|
|
|
+ if (skuIdMap.get(discountGoods.getDisGid()) == null) {
|
|
|
+ skuIdMap.put(discountGoods.getDisGid(), discountGoods);
|
|
|
+ }
|
|
|
+ dbDiscountPlusGoodsList.forEach(db_exist->{
|
|
|
+ //存在
|
|
|
+ if (db_exist.getDisGid().equals(discountGoods.getDisGid())) {
|
|
|
+ is_exist.set(true);
|
|
|
+ discountGoods.setId(db_exist.getId());
|
|
|
+ checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //不存在新增
|
|
|
+ if (!is_exist.get()) {
|
|
|
+ checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //去除数据库已被删除的
|
|
|
+ dbDiscountPlusGoodsList.forEach(dbDiscountPlusGoods -> {
|
|
|
+ GoodsDiscountPlusPurchaseRelation exists = skuIdMap.get(dbDiscountPlusGoods.getDisGid());
|
|
|
+ //不存在
|
|
|
+ if (exists == null) {
|
|
|
+ discountPlusPurchaseRelationMapper.deleteById(dbDiscountPlusGoods.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkPurchaseParamsAndSaveOrUpdate(Long goodsId, Long skuId, GoodsDiscountPlusPurchaseRelation discountGoods) {
|
|
|
+ Long disSid = discountGoods.getDisSid();
|
|
|
+ if (disSid == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请选择对应优惠加购规格");
|
|
|
+ }
|
|
|
+ if (skuId.equals(disSid)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ BigDecimal disPrice = discountGoods.getDisPrice();
|
|
|
+ GoodsDonSku sku = skuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
|
|
|
+ .eq(GoodsDonSku::getId, disSid).last("limit 1"));
|
|
|
+ if (sku == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("规格不存在");
|
|
|
+ }
|
|
|
+ if (!sku.getStatus()) {
|
|
|
+ throw BusinessRuntimeException.getInstance("{0}规格未上架", sku.getSpecVal());
|
|
|
+ }
|
|
|
+ if (disPrice == null || disPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("请输入正确的优惠加购价格");
|
|
|
+ }
|
|
|
+ discountGoods.setGoodsId(goodsId);
|
|
|
+ discountGoods.setSkuId(skuId);
|
|
|
+ discountGoods.setDisGid(sku.getGoodsId());
|
|
|
+ if (discountGoods.getId() != null) {
|
|
|
+ discountPlusPurchaseRelationMapper.updateById(discountGoods);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ discountPlusPurchaseRelationMapper.insert(discountGoods);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|