CmsGoodsDonSkuServiceImpl.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.cyksj.service.mange.goods;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import com.cyksj.common.exception.BusinessRuntimeException;
  7. import com.cyksj.common.util.Jsons;
  8. import com.cyksj.mapper.*;
  9. import com.cyksj.mapper.shop.YhShopGoodsRateMapper;
  10. import com.cyksj.mapper.shop.YhShopGoodsSkuMapper;
  11. import com.cyksj.mapper.shop.YhShopSkuSysTakeDownRecordMapper;
  12. import com.cyksj.model.entity.*;
  13. import com.cyksj.service.mange.CmsGoodsDonSkuService;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.dao.DuplicateKeyException;
  17. import org.springframework.stereotype.Service;
  18. import java.math.BigDecimal;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import java.util.concurrent.atomic.AtomicBoolean;
  23. /**
  24. * @author chan
  25. * @date 2022/9/27 5:43 PM
  26. */
  27. @Slf4j
  28. @RequiredArgsConstructor
  29. @Service
  30. public class CmsGoodsDonSkuServiceImpl extends ServiceImpl<GoodsDonSkuMapper,GoodsDonSku> implements CmsGoodsDonSkuService {
  31. private final GoodsDiscountPlusPurchaseRelationMapper discountPlusPurchaseRelationMapper;
  32. private final GoodsDonSkuMapper skuMapper;
  33. private final ChatgptUserMapper chatgptUserMapper;
  34. private final YhShopGoodsRateMapper yhShopGoodsRateMapper;
  35. private final YhShopGoodsSkuMapper yhShopGoodsSkuMapper;
  36. private final YhShopSkuSysTakeDownRecordMapper yhShopSkuSysTakeDownRecordMapper;
  37. private final UpgradePackageMapper upgradePackageMapper;
  38. private final GoodsDonMapper goodsDonMapper;
  39. @Override
  40. public void handleDiscountPurchaseGoods(GoodsDonSku sku, List<GoodsDiscountPlusPurchaseRelation> discountPlusPurchaseGoods) {
  41. Long skuId = sku.getId();
  42. Long goodsId = sku.getGoodsId();
  43. List<GoodsDiscountPlusPurchaseRelation> dbDiscountPlusGoodsList = discountPlusPurchaseRelationMapper.selectList(Wrappers.lambdaQuery(GoodsDiscountPlusPurchaseRelation.class)
  44. .eq(GoodsDiscountPlusPurchaseRelation::getSkuId, skuId));
  45. if (CollUtil.isEmpty(dbDiscountPlusGoodsList) && CollUtil.isNotEmpty(discountPlusPurchaseGoods)) {
  46. discountPlusPurchaseGoods.forEach(discountGoods -> {
  47. checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
  48. });
  49. sku.setIsDp(true);
  50. return;
  51. }
  52. //优惠加购为空
  53. if (CollUtil.isEmpty(discountPlusPurchaseGoods)) {
  54. discountPlusPurchaseRelationMapper.delete(Wrappers.lambdaQuery(GoodsDiscountPlusPurchaseRelation.class)
  55. .eq(GoodsDiscountPlusPurchaseRelation::getSkuId, skuId));
  56. sku.setIsDp(false);
  57. return;
  58. }
  59. //新增或更新
  60. sku.setIsDp(true);
  61. Map<Long, GoodsDiscountPlusPurchaseRelation> skuIdMap = new ConcurrentHashMap<>();
  62. discountPlusPurchaseGoods.forEach(discountGoods -> {
  63. AtomicBoolean is_exist = new AtomicBoolean(false);
  64. if (skuIdMap.get(discountGoods.getDisSid()) == null) {
  65. skuIdMap.put(discountGoods.getDisSid(), discountGoods);
  66. }
  67. dbDiscountPlusGoodsList.forEach(db_exist->{
  68. //存在
  69. if (db_exist.getDisSid().equals(discountGoods.getDisSid())) {
  70. is_exist.set(true);
  71. discountGoods.setId(db_exist.getId());
  72. checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
  73. }
  74. });
  75. //不存在新增
  76. if (!is_exist.get()) {
  77. checkPurchaseParamsAndSaveOrUpdate(goodsId, skuId, discountGoods);
  78. }
  79. });
  80. //去除数据库已被删除的
  81. dbDiscountPlusGoodsList.forEach(dbDiscountPlusGoods -> {
  82. GoodsDiscountPlusPurchaseRelation exists = skuIdMap.get(dbDiscountPlusGoods.getDisSid());
  83. //不存在
  84. if (exists == null) {
  85. discountPlusPurchaseRelationMapper.deleteById(dbDiscountPlusGoods.getId());
  86. }
  87. });
  88. }
  89. @Override
  90. public void updateMirrorCarUserNum(GoodsDonSku sku) {
  91. if (sku.getIsCar() != null && sku.getIsCar()) {
  92. Long id = sku.getId();
  93. GoodsDonSku dbSku = skuMapper.selectById(id);
  94. Integer gptLimitNum = sku.getGptLimitNum();
  95. Long gptLimitTime = sku.getGptLimitTime();
  96. Integer dbGptLimitNum = dbSku.getGptLimitNum();
  97. Long dbGptLimitTime = dbSku.getGptLimitTime();
  98. if (gptLimitNum == null || gptLimitNum < 0) {
  99. throw BusinessRuntimeException.getInstance("请输入正确的限制次数");
  100. }
  101. if (gptLimitTime == null || gptLimitTime < 0) {
  102. throw BusinessRuntimeException.getInstance("请输入正确的限制时间");
  103. }
  104. if (!ObjectUtil.equal(gptLimitNum, dbGptLimitNum) || !ObjectUtil.equal(gptLimitTime, dbGptLimitTime)) {
  105. //更新该规格镜像车队的用户次数 时间
  106. chatgptUserMapper.updateChatGptCarUserNumAndLimitTime(sku.getId(), gptLimitNum, gptLimitTime);
  107. }
  108. }
  109. }
  110. @Override
  111. public void setBelowPriceShopSkuDown(Long skuId, BigDecimal newPrice) {
  112. YhShopGoodsRate yhShopGoodsRate = yhShopGoodsRateMapper.selectOne(Wrappers.lambdaQuery(YhShopGoodsRate.class)
  113. .eq(YhShopGoodsRate::getSkuId, skuId)
  114. .last("limit 1"));
  115. if (yhShopGoodsRate != null) {
  116. BigDecimal differPrice = newPrice.multiply(BigDecimal.valueOf(yhShopGoodsRate.getRate()).divide(BigDecimal.valueOf(100)));
  117. BigDecimal minPrice = newPrice.subtract(differPrice);
  118. BigDecimal maxPrice = newPrice.add(differPrice);
  119. //不在设置的价格区间店铺 统统下架
  120. //记录那些店铺规格被下架
  121. List<YhShopGoodsSku> yhShopGoodsSkus = yhShopGoodsSkuMapper.getBelowPriceShopSku(skuId, minPrice, maxPrice);
  122. if (CollUtil.isNotEmpty(yhShopGoodsSkus)) {
  123. yhShopSkuSysTakeDownRecordMapper.batchInsert(yhShopGoodsSkus, minPrice);
  124. Integer update = yhShopGoodsSkuMapper.setBelowPriceShopSkuDown(skuId, minPrice, maxPrice);
  125. log.info("变更skuId:{}规格价格,店铺数量:{}低于价格区间:{}-{}下架", skuId, update, minPrice, maxPrice);
  126. }
  127. }
  128. }
  129. @Override
  130. public void handleGptPackageSku(GoodsDonSku dbSku, GoodsDonSku sku) throws Exception {
  131. Long gptLimitTime = dbSku.getGptLimitTime();
  132. Integer gptLimitNum = dbSku.getGptLimitNum();
  133. BigDecimal price = dbSku.getPrice();
  134. BigDecimal newPrice = sku.getPrice();
  135. //GPT镜像套餐包关联
  136. if (sku.getGoodsId() == 18 && sku.getIsMirror()) {
  137. //同步升级规格的价格
  138. if (gptLimitNum != sku.getGptLimitNum() || gptLimitTime != sku.getGptLimitTime() || price.compareTo(newPrice) != 0) {
  139. if (price.compareTo(newPrice) != 0) {
  140. //本身是需升级的规格 同步套餐价格
  141. UpgradePackage upgradePackageSelfBind = upgradePackageMapper.selectOne(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + sku.getId() + "\"')")
  142. .eq(UpgradePackage::getStatus, Boolean.TRUE)
  143. .eq(UpgradePackage::getGptType, 0)
  144. .eq(UpgradePackage::getDeleted, Boolean.TRUE)
  145. .last("limit 1"));
  146. if (upgradePackageSelfBind != null) {
  147. Long upgradeSkuId = upgradePackageSelfBind.getUpgradeSkuId();
  148. GoodsDonSku upgradeSku = skuMapper.selectById(upgradeSkuId);
  149. if (upgradeSku != null && upgradeSku.getPrice().compareTo(sku.getPrice()) > 0) {
  150. upgradePackageSelfBind.setPrice(upgradeSku.getPrice().subtract(sku.getPrice()));
  151. upgradePackageMapper.updateById(upgradePackageSelfBind);
  152. }
  153. }
  154. }
  155. //被绑定升级套餐
  156. List<UpgradePackage> toUpgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class)
  157. .eq(UpgradePackage::getUpgradeSkuId, sku.getId()));
  158. if (CollUtil.isNotEmpty(toUpgradePackages)) {
  159. toUpgradePackages.forEach(toUpgradePackage->{
  160. if (price.compareTo(sku.getPrice()) != 0) {
  161. Long skuId = null;
  162. try {
  163. skuId = Jsons.parseList(toUpgradePackage.getSkuIds(), Long.class).get(0);
  164. //需升级的规格
  165. GoodsDonSku oriSku = skuMapper.selectById(skuId);
  166. if (oriSku == null) {
  167. return;
  168. }
  169. BigDecimal differ = sku.getPrice().subtract(oriSku.getPrice());
  170. if (differ.compareTo(BigDecimal.ZERO) > 0) {
  171. toUpgradePackage.setPrice(differ);
  172. }
  173. } catch (Exception e) {
  174. }
  175. }
  176. toUpgradePackage.setTime(sku.getGptLimitTime());
  177. toUpgradePackage.setNum(sku.getGptLimitNum());
  178. upgradePackageMapper.updateById(toUpgradePackage);
  179. });
  180. }
  181. }
  182. }
  183. }
  184. public void checkPurchaseParamsAndSaveOrUpdate(Long goodsId, Long skuId, GoodsDiscountPlusPurchaseRelation discountGoods) {
  185. Long disSid = discountGoods.getDisSid();
  186. if (disSid == null) {
  187. throw BusinessRuntimeException.getInstance("请选择对应优惠加购规格");
  188. }
  189. if (skuId.equals(disSid)) {
  190. return;
  191. }
  192. BigDecimal disPrice = discountGoods.getDisPrice();
  193. GoodsDonSku sku = skuMapper.selectOne(Wrappers.lambdaQuery(GoodsDonSku.class)
  194. .eq(GoodsDonSku::getId, disSid).last("limit 1"));
  195. if (sku == null) {
  196. throw BusinessRuntimeException.getInstance("优惠加购规格不存在");
  197. }
  198. if (!sku.getStatus()) {
  199. GoodsDon goodsDon = goodsDonMapper.selectById(sku.getGoodsId());
  200. throw BusinessRuntimeException.getInstance("{0}优惠加购规格未上架", goodsDon.getTitle() + sku.getSpecVal());
  201. }
  202. if (disPrice == null || disPrice.compareTo(BigDecimal.ZERO) <= 0) {
  203. throw BusinessRuntimeException.getInstance("请输入正确的优惠加购价格");
  204. }
  205. discountGoods.setGoodsId(goodsId);
  206. discountGoods.setSkuId(skuId);
  207. discountGoods.setDisGid(sku.getGoodsId());
  208. if (discountGoods.getId() != null) {
  209. discountPlusPurchaseRelationMapper.updateById(discountGoods);
  210. return;
  211. }
  212. try {
  213. discountPlusPurchaseRelationMapper.insert(discountGoods);
  214. } catch (DuplicateKeyException e) {
  215. }
  216. }
  217. }