GiftCardGoodsSkuServiceImpl.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.cyksj.service.giftcard.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.cyksj.common.exception.BusinessRuntimeException;
  5. import com.cyksj.mapper.GiftCardGoodsSkuMapper;
  6. import com.cyksj.mapper.GoodsDonMapper;
  7. import com.cyksj.mapper.GoodsDonSkuMapper;
  8. import com.cyksj.model.entity.GiftCardGoodsSku;
  9. import com.cyksj.model.entity.GoodsDon;
  10. import com.cyksj.model.entity.GoodsDonSku;
  11. import com.cyksj.model.request.GiftCardGoodsSkuReq;
  12. import com.cyksj.service.giftcard.GiftCardGoodsSkuService;
  13. import lombok.RequiredArgsConstructor;
  14. import org.springframework.dao.DuplicateKeyException;
  15. import org.springframework.stereotype.Service;
  16. import java.math.BigDecimal;
  17. import java.util.List;
  18. /*
  19. *项目名: netflix
  20. *文件名: GiftCardGoodsSkuServiceImpl
  21. *创建者: JavaZou
  22. *创建时间:2023/8/29 18:19
  23. */
  24. @Service
  25. @RequiredArgsConstructor
  26. public class GiftCardGoodsSkuServiceImpl extends ServiceImpl<GiftCardGoodsSkuMapper, GiftCardGoodsSku> implements GiftCardGoodsSkuService {
  27. private final GoodsDonSkuMapper goodsDonSkuMapper;
  28. private final GoodsDonMapper goodsDonMapper;
  29. @Override
  30. public void setOrUpdateGiftCardSku(GiftCardGoodsSku data) {
  31. if (data.getGoodsId() == null) {
  32. throw BusinessRuntimeException.getInstance("请选择对应的礼品卡平台");
  33. }
  34. GoodsDon goodsDon = goodsDonMapper.selectById(data.getGoodsId());
  35. if (goodsDon == null) {
  36. throw BusinessRuntimeException.getInstance("请选择对应的礼品卡平台");
  37. }
  38. if (goodsDon.getSpecialType() == null || goodsDon.getSpecialType() != GoodsDon.SpecialType.giftCard) {
  39. throw BusinessRuntimeException.getInstance("该{0}平台不是礼品卡特殊类型", goodsDon.getTitle());
  40. }
  41. BigDecimal cash = data.getCash();
  42. Integer gcType = data.getGcType();
  43. BigDecimal price = data.getPrice();
  44. if (price == null || price.compareTo(BigDecimal.ZERO) == 0) {
  45. throw BusinessRuntimeException.getInstance("请输入正确的礼品卡配置价格");
  46. }
  47. Long gcSkuId = data.getGcSkuId();
  48. if (gcSkuId == null && cash == null) {
  49. throw BusinessRuntimeException.getInstance("请选择对应的礼品卡类型");
  50. }
  51. if (gcSkuId != null && cash != null) {
  52. throw BusinessRuntimeException.getInstance("只能选择一种礼品卡类型");
  53. }
  54. //虚拟卡
  55. if (cash != null) {
  56. if (cash.compareTo(BigDecimal.ZERO) == 0) {
  57. throw BusinessRuntimeException.getInstance("请输入正确的礼品卡现金类型金额");
  58. }
  59. gcType = 1;
  60. }
  61. if (gcSkuId != null) {
  62. GoodsDonSku sku = goodsDonSkuMapper.selectById(gcSkuId);
  63. if (sku == null) {
  64. throw BusinessRuntimeException.getInstance("存在未有的虚拟卡规格,请重新选择");
  65. }
  66. if (!sku.getStatus()) {
  67. throw BusinessRuntimeException.getInstance("存在下架的虚拟卡规格,请重新选择");
  68. }
  69. gcType = 2;
  70. data.setGcGoodsId(sku.getGoodsId());
  71. }
  72. data.setGcType(gcType);
  73. try {
  74. this.saveOrUpdate(data);
  75. } catch (DuplicateKeyException e) {
  76. }
  77. }
  78. @Override
  79. public void setGiftCardSku(GiftCardGoodsSkuReq req) {
  80. List<GiftCardGoodsSku> giftCardGoodsSkus = req.getConfigs();
  81. if (CollUtil.isEmpty(giftCardGoodsSkus)) {
  82. throw BusinessRuntimeException.getInstance("请输入对应数据");
  83. }
  84. giftCardGoodsSkus.forEach(giftCardSku -> {
  85. giftCardSku.setGoodsId(req.getGoodsId());
  86. setOrUpdateGiftCardSku(giftCardSku);
  87. });
  88. }
  89. }