|
|
@@ -1,10 +1,22 @@
|
|
|
package com.fs.hisStore.service.impl;
|
|
|
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import com.fs.common.constant.LiveKeysConstant;
|
|
|
+import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.hisStore.domain.FsStoreProductAttrScrm;
|
|
|
+import com.fs.hisStore.domain.FsStoreProductAttrValueScrm;
|
|
|
import com.fs.hisStore.domain.FsStoreProductPurchaseLimitScrm;
|
|
|
+import com.fs.hisStore.domain.FsStoreProductScrm;
|
|
|
+import com.fs.hisStore.mapper.FsStoreProductAttrScrmMapper;
|
|
|
+import com.fs.hisStore.mapper.FsStoreProductAttrValueScrmMapper;
|
|
|
import com.fs.hisStore.mapper.FsStoreProductPurchaseLimitScrmMapper;
|
|
|
+import com.fs.hisStore.mapper.FsStoreProductScrmMapper;
|
|
|
import com.fs.hisStore.service.IFsStoreProductPurchaseLimitScrmService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -21,6 +33,16 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
|
|
|
@Autowired
|
|
|
private FsStoreProductPurchaseLimitScrmMapper fsStoreProductPurchaseLimitMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+ @Autowired
|
|
|
+ private FsStoreProductScrmMapper fsStoreProductScrmMapper;
|
|
|
+ @Autowired
|
|
|
+ private FsStoreProductAttrScrmMapper fsStoreProductAttrScrmMapper;
|
|
|
+ @Autowired
|
|
|
+ private FsStoreProductAttrValueScrmMapper fsStoreProductAttrValueScrmMapper;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 查询商品限购
|
|
|
*
|
|
|
@@ -119,19 +141,48 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
|
|
|
@Override
|
|
|
public int increasePurchaseLimit(Long productId, Long userId, Integer num)
|
|
|
{
|
|
|
- FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
|
|
|
- if (limit == null) {
|
|
|
- // 创建新记录
|
|
|
- limit = new FsStoreProductPurchaseLimitScrm();
|
|
|
- limit.setProductId(productId);
|
|
|
- limit.setUserId(userId);
|
|
|
- limit.setNum(num);
|
|
|
- return insertFsStoreProductPurchaseLimit(limit);
|
|
|
+ String cacheKey = String.format(LiveKeysConstant.PRODUCT_DETAIL_CACHE, productId);
|
|
|
+ Map<String, Object> cachedData = redisCache.getCacheObject(cacheKey);
|
|
|
+
|
|
|
+ FsStoreProductScrm product;
|
|
|
+ List<FsStoreProductAttrScrm> productAttr;
|
|
|
+ List<FsStoreProductAttrValueScrm> productValues;
|
|
|
+
|
|
|
+ if (cachedData != null) {
|
|
|
+ // 从缓存中获取数据
|
|
|
+ product = (FsStoreProductScrm) cachedData.get("product");
|
|
|
} else {
|
|
|
- // 更新现有记录
|
|
|
- limit.setNum(limit.getNum() + num);
|
|
|
- return updateFsStoreProductPurchaseLimit(limit);
|
|
|
+ // 缓存中没有,从数据库查询
|
|
|
+ product = fsStoreProductScrmMapper.selectFsStoreProductById(productId);
|
|
|
+ if(product==null){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ productAttr = fsStoreProductAttrScrmMapper.selectFsStoreProductAttrByProductId(productId);
|
|
|
+ productValues = fsStoreProductAttrValueScrmMapper.selectFsStoreProductAttrValueByProductId(productId);
|
|
|
+
|
|
|
+ // 将数据存入缓存
|
|
|
+ Map<String, Object> cacheData = new HashMap<>();
|
|
|
+ cacheData.put("product", product);
|
|
|
+ cacheData.put("productAttr", productAttr);
|
|
|
+ cacheData.put("productValues", productValues);
|
|
|
+ redisCache.setCacheObject(cacheKey, cacheData, LiveKeysConstant.PRODUCT_DETAIL_CACHE_EXPIRE, TimeUnit.SECONDS);
|
|
|
}
|
|
|
+ if (product != null && product.getPurchaseLimit() != null && product.getPurchaseLimit() > 0) {
|
|
|
+ FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
|
|
|
+ if (limit == null) {
|
|
|
+ // 创建新记录
|
|
|
+ limit = new FsStoreProductPurchaseLimitScrm();
|
|
|
+ limit.setProductId(productId);
|
|
|
+ limit.setUserId(userId);
|
|
|
+ limit.setNum(num);
|
|
|
+ return insertFsStoreProductPurchaseLimit(limit);
|
|
|
+ } else {
|
|
|
+ // 更新现有记录
|
|
|
+ limit.setNum(limit.getNum() + num);
|
|
|
+ return updateFsStoreProductPurchaseLimit(limit);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -145,19 +196,48 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
|
|
|
@Override
|
|
|
public int decreasePurchaseLimit(Long productId, Long userId, Integer num)
|
|
|
{
|
|
|
- FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
|
|
|
- if (limit != null) {
|
|
|
- int newNum = limit.getNum() - num;
|
|
|
- if (newNum <= 0) {
|
|
|
- // 如果数量为0或负数,删除记录
|
|
|
- return deleteFsStoreProductPurchaseLimitById(limit.getId());
|
|
|
- } else {
|
|
|
- // 更新数量
|
|
|
- limit.setNum(newNum);
|
|
|
- return updateFsStoreProductPurchaseLimit(limit);
|
|
|
+ String cacheKey = String.format(LiveKeysConstant.PRODUCT_DETAIL_CACHE, productId);
|
|
|
+ Map<String, Object> cachedData = redisCache.getCacheObject(cacheKey);
|
|
|
+
|
|
|
+ FsStoreProductScrm product;
|
|
|
+ List<FsStoreProductAttrScrm> productAttr;
|
|
|
+ List<FsStoreProductAttrValueScrm> productValues;
|
|
|
+
|
|
|
+ if (cachedData != null) {
|
|
|
+ // 从缓存中获取数据
|
|
|
+ product = (FsStoreProductScrm) cachedData.get("product");
|
|
|
+ } else {
|
|
|
+ // 缓存中没有,从数据库查询
|
|
|
+ product = fsStoreProductScrmMapper.selectFsStoreProductById(productId);
|
|
|
+ if(product==null){
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ productAttr = fsStoreProductAttrScrmMapper.selectFsStoreProductAttrByProductId(productId);
|
|
|
+ productValues = fsStoreProductAttrValueScrmMapper.selectFsStoreProductAttrValueByProductId(productId);
|
|
|
+
|
|
|
+ // 将数据存入缓存
|
|
|
+ Map<String, Object> cacheData = new HashMap<>();
|
|
|
+ cacheData.put("product", product);
|
|
|
+ cacheData.put("productAttr", productAttr);
|
|
|
+ cacheData.put("productValues", productValues);
|
|
|
+ redisCache.setCacheObject(cacheKey, cacheData, LiveKeysConstant.PRODUCT_DETAIL_CACHE_EXPIRE, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+ if (product != null && product.getPurchaseLimit() != null && product.getPurchaseLimit() > 0) {
|
|
|
+ FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
|
|
|
+ if (limit != null) {
|
|
|
+ int newNum = limit.getNum() - num;
|
|
|
+ if (newNum <= 0) {
|
|
|
+ // 如果数量为0或负数,删除记录
|
|
|
+ return deleteFsStoreProductPurchaseLimitById(limit.getId());
|
|
|
+ } else {
|
|
|
+ // 更新数量
|
|
|
+ limit.setNum(newNum);
|
|
|
+ return updateFsStoreProductPurchaseLimit(limit);
|
|
|
+ }
|
|
|
}
|
|
|
+ return 0;
|
|
|
}
|
|
|
- return 0;
|
|
|
+ return -1;
|
|
|
}
|
|
|
}
|
|
|
|