Compare commits
4 Commits
c7a203441f
...
9de78c669b
Author | SHA1 | Date |
---|---|---|
|
9de78c669b | |
|
51049930ec | |
|
2155eeab10 | |
|
ba01e230ef |
|
@ -84,4 +84,13 @@ public interface HashCache <K,Hk,HV> extends DecorationKey<K> {
|
|||
* @param hashKey hash键
|
||||
*/
|
||||
public void remove(K key,Hk hashKey);
|
||||
|
||||
/**
|
||||
* 判断redis中的hashKey是否存在
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
* @return
|
||||
*/
|
||||
public boolean hashKey(K key , Hk hashKey);
|
||||
|
||||
}
|
||||
|
|
|
@ -21,8 +21,14 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public Long get(K key) {
|
||||
return this.redisService.getCacheObject(encode(key));
|
||||
public Long get (K key) {
|
||||
Long cacheValue = this.redisService.getCacheObject(encode(key));
|
||||
if (cacheValue==null){
|
||||
Long data = getData(key);
|
||||
cacheValue=data==null?0L:data;
|
||||
this.redisService.setCacheObject(encode(key),cacheValue);
|
||||
}
|
||||
return cacheValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,54 +36,64 @@ public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K
|
|||
* @param key
|
||||
*/
|
||||
@Override
|
||||
public Long increment(K key) {
|
||||
return this.increment(key,1L);
|
||||
public Long increment (K key) {
|
||||
return this.increment(key, 1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自减
|
||||
*
|
||||
自减
|
||||
* @param key
|
||||
*/
|
||||
@Override
|
||||
public Long decrement(K key) {
|
||||
return this.decrement(key,1L);
|
||||
public Long decrement (K key) {
|
||||
return this.decrement(key, 1L);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加数值
|
||||
*
|
||||
* @param key
|
||||
* @param number
|
||||
*/
|
||||
@Override
|
||||
public Long increment(K key, Long number) {
|
||||
public Long increment (K key, Long number) {
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue==null){
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data=data==null?0L:data;
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.increment(encode(key),number);
|
||||
return redisService.increment(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少数值
|
||||
*
|
||||
* @param key
|
||||
* @param number
|
||||
*/
|
||||
@Override
|
||||
public Long decrement(K key, Long number) {
|
||||
public Long decrement (K key, Long number) {
|
||||
Long numberValue = redisService.getCacheObject(encode(key));
|
||||
if (numberValue==null){
|
||||
if (numberValue == null){
|
||||
Long data = getData(key);
|
||||
data=data==null?0L:data;
|
||||
redisService.setCacheObject(encode(key),data);
|
||||
data = data == null ? 0L : data;
|
||||
redisService.setCacheObject(encode(key), data);
|
||||
}
|
||||
return redisService.decrement(encode(key),number);
|
||||
return redisService.decrement(encode(key), number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
*
|
||||
* @param key ID
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode(K key) {
|
||||
return keyPre()+key;
|
||||
public String encode (K key) {
|
||||
return keyPre() + key;
|
||||
}
|
||||
|
||||
public abstract Long getData(K key);
|
||||
|
|
|
@ -103,6 +103,17 @@ public abstract class HashCacheAbs<K,HK,HV> implements HashCache<K,HK,HV>{
|
|||
redisService.deleteCacheMapValue(encode(key),encodeHashKey(hashKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断redis中的hashKey是否存在
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
|
||||
@Override
|
||||
public boolean hashKey(K key, HK hashKey) {
|
||||
return redisService.hasKey(encode(key),encodeHashKey(hashKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 原始数据转编码数据
|
||||
* @param dataMap 原始数据
|
||||
|
|
|
@ -20,4 +20,9 @@ public class ServiceNameConstants {
|
|||
* 文件服务的serviceid
|
||||
*/
|
||||
public static final String FILE_SERVICE = "muyu-file";
|
||||
|
||||
/**
|
||||
* 商品服务
|
||||
*/
|
||||
public static final String PRODUCT_SERVICE="muyu-product";
|
||||
}
|
||||
|
|
|
@ -39,6 +39,10 @@ public class Result<T> implements Serializable {
|
|||
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> buildCode(int code, String msg, T data){
|
||||
return restResult(data, code, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> success () {
|
||||
return restResult(null, SUCCESS, null);
|
||||
}
|
||||
|
@ -71,8 +75,6 @@ public class Result<T> implements Serializable {
|
|||
return restResult(null, code, msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <T> Result<T> warn () {
|
||||
return restResult(null, WARN, null);
|
||||
}
|
||||
|
|
|
@ -221,6 +221,15 @@ public class RedisService {
|
|||
return opsForHash.get(key, hKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断redis中的hashKey是否存在
|
||||
* @param key redis键
|
||||
* @param hashKey hash键
|
||||
*/
|
||||
public Boolean hasKey (final String key,final String hashKey) {
|
||||
return redisTemplate.opsForHash().hasKey(key,hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多个Hash中的数据
|
||||
*
|
||||
|
|
|
@ -66,7 +66,7 @@ public class GlobalExceptionHandler {
|
|||
public Result handleServiceException (ServiceException e, HttpServletRequest request) {
|
||||
log.error(e.getMessage(), e);
|
||||
Integer code = e.getCode();
|
||||
return StringUtils.isNotNull(code) ? Result.error(code, e.getMessage()) : Result.error(e.getMessage());
|
||||
return StringUtils.isNotNull(code) ? Result.buildCode(code, e.getMessage(),null) : Result.error(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -23,10 +23,10 @@
|
|||
<artifactId>muyu-common-cache</artifactId>
|
||||
|
||||
</dependency>
|
||||
<!--商品模块 common依赖-->
|
||||
<!--商品模块 远程调用 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-product-common</artifactId>
|
||||
<artifactId>muyu-product-remote</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -1,10 +1,58 @@
|
|||
package com.muyu.product.cache;
|
||||
|
||||
import com.muyu.cache.abs.AtomicSequenceCacheAbs;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuStockData;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* ProjectSkuStockCache
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/1
|
||||
*/
|
||||
public class ProjectSkuStockCache {
|
||||
@Service
|
||||
public class ProjectSkuStockCache extends AtomicSequenceCacheAbs<SkuStockKey>{
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuStockData projectSkuStockData;
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
* @return key前缀
|
||||
*/
|
||||
@Override
|
||||
public String keyPre() {
|
||||
return "project:sku:stock:";
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param skuStockKey ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encode(SkuStockKey skuStockKey){
|
||||
return keyPre()+skuStockKey.getProjectId()+":"+skuStockKey.getSku();
|
||||
}
|
||||
@Override
|
||||
public Long getData(SkuStockKey key) {
|
||||
return projectSkuStockData.getData(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public SkuStockKey decode(String redisKey) {
|
||||
String[] split = redisKey.replace(keyPre(), "").split(":");
|
||||
return SkuStockKey.builder()
|
||||
.projectId(Convert.toLong(split[0]))
|
||||
.sku(split[1])
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.muyu.product.cache;
|
||||
|
||||
import com.muyu.cache.abs.CacheAbs;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.product.cache.datasource.RuleCacheData;
|
||||
import com.muyu.product.cache.model.RuleCacheModel;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
* RuleInfoCache
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
public class RuleInfoCache extends CacheAbs<Long, RuleCacheModel> {
|
||||
|
||||
@Autowired
|
||||
private RuleCacheData ruleCacheData;
|
||||
@Override
|
||||
public String keyPre() {
|
||||
return "rule:info";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long decode(String redisKey) {
|
||||
return Convert.toLong(redisKey.replace(keyPre(),""));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RuleCacheModel getData(Long key) {
|
||||
return ruleCacheData.getRuleCacheModel(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RuleCacheModel defaultValue() {
|
||||
return new RuleCacheModel();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.muyu.product.cache.datasource;
|
||||
|
||||
import com.muyu.product.cache.model.RuleCacheModel;
|
||||
|
||||
/**
|
||||
* RuleCacheData
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
public interface RuleCacheData {
|
||||
public RuleCacheModel getRuleCacheModel(Long ruleId);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.product.cache.datasource.impl;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.cache.datasource.ProjectInfoData;
|
||||
import com.muyu.product.domain.ProjectInfo;
|
||||
import com.muyu.product.remote.RemoteProjectInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* ProjectInfoDataRemoteImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Service
|
||||
public class ProjectInfoDataRemoteImpl implements ProjectInfoData{
|
||||
@Autowired
|
||||
private RemoteProjectInfoService remoteProjectInfoService;
|
||||
@Override
|
||||
public ProjectInfo getData(Long key) {
|
||||
Result<ProjectInfo> projectInfoResult = remoteProjectInfoService.getInfo(key);
|
||||
if (Result.isError(projectInfoResult)){
|
||||
return new ProjectInfo();
|
||||
}
|
||||
return projectInfoResult.getData();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.muyu.product.cache.datasource.impl;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuData;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.remote.RemoteProjectSkuService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* ProjectSkuDataRemoteImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Service
|
||||
public class ProjectSkuDataRemoteImpl implements ProjectSkuData{
|
||||
@Autowired
|
||||
private RemoteProjectSkuService remoteProjectSkuService;
|
||||
@Override
|
||||
public Map<String, ProjectSkuInfo> getData(Long projectId) {
|
||||
Result<List<ProjectSkuInfo>> projectListResult =
|
||||
remoteProjectSkuService.listByProjectId(projectId);
|
||||
if (Result.isError(projectListResult)){
|
||||
return new HashMap<>();
|
||||
}
|
||||
List<ProjectSkuInfo> projectSkuInfoList = projectListResult.getData();
|
||||
|
||||
return projectSkuInfoList.stream()
|
||||
.collect(Collectors.toMap(ProjectSkuInfo::getSku,projectSkuInfo -> projectSkuInfo));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectSkuInfo getData(Long projectId, String projectSku) {
|
||||
Result<ProjectSkuInfo> projectSkuInfoResult = remoteProjectSkuService.getInfoByProjectIdAndSku(
|
||||
projectId, projectSku
|
||||
);
|
||||
if (Result.isError(projectSkuInfoResult)){
|
||||
return new ProjectSkuInfo();
|
||||
}
|
||||
return projectSkuInfoResult.getData();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.muyu.product.cache.datasource.impl;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuStockData;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.remote.RemoteProjectSkuService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: sku库存实现类
|
||||
* @Date 2024-4-2 上午 10:53
|
||||
*/
|
||||
@Service
|
||||
public class ProjectSkuStockDataRemoteImpl implements ProjectSkuStockData {
|
||||
|
||||
@Autowired
|
||||
private RemoteProjectSkuService remoteProjectSkuService;
|
||||
|
||||
@Override
|
||||
public Long getData (SkuStockKey key) {
|
||||
Result<ProjectSkuInfo> result
|
||||
= remoteProjectSkuService.getInfoByProjectIdAndSku(key.getProjectId(), key.getSku());
|
||||
if (Result.isSuccess(result)){
|
||||
ProjectSkuInfo projectSkuInfo = result.getData();
|
||||
if (!Objects.isNull(projectSkuInfo)){
|
||||
return projectSkuInfo.getStock();
|
||||
}
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.muyu.product.cache.datasource.impl;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.cache.datasource.RuleCacheData;
|
||||
import com.muyu.product.cache.model.RuleAttrCacheModel;
|
||||
import com.muyu.product.cache.model.RuleCacheModel;
|
||||
import com.muyu.product.domain.RuleAttrInfo;
|
||||
import com.muyu.product.domain.RuleInfo;
|
||||
import com.muyu.product.remote.RemoteRuleAttrService;
|
||||
import com.muyu.product.remote.RemoteRuleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* RuleCacheDataRemoteImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Service
|
||||
public class RuleCacheDataRemoteImpl implements RuleCacheData{
|
||||
@Autowired
|
||||
private RemoteRuleService remoteRuleService;
|
||||
|
||||
@Autowired
|
||||
private RemoteRuleAttrService remoteRuleAttrService;
|
||||
@Override
|
||||
public RuleCacheModel getRuleCacheModel(Long ruleId) {
|
||||
Result<RuleInfo> ruleInfoResult = remoteRuleService.getInfo(ruleId);
|
||||
Result<List<RuleAttrInfo>> ruleAttrResult = remoteRuleAttrService.getInfoByRuleId(ruleId);
|
||||
if (Result.isError(ruleInfoResult)||Result.isError(ruleAttrResult)){
|
||||
return new RuleCacheModel();
|
||||
}
|
||||
RuleInfo ruleInfo = ruleInfoResult.getData();
|
||||
List<RuleAttrInfo> ruleAttrInfoList = ruleAttrResult.getData();
|
||||
List<RuleAttrCacheModel> attrCacheModelList = ruleAttrInfoList.stream()
|
||||
.map(RuleAttrCacheModel::ruleAttrBuild)
|
||||
.toList();
|
||||
|
||||
return RuleCacheModel.builder()
|
||||
.ruleName(ruleInfo.getName())
|
||||
.ruleAttrModelList(attrCacheModelList)
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.muyu.product.cache.model;
|
||||
|
||||
import com.muyu.product.domain.RuleAttrInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* RuleAttrCacheModel
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RuleAttrCacheModel {
|
||||
|
||||
/**
|
||||
* 类目名称
|
||||
*/
|
||||
private String attrName;
|
||||
|
||||
/**
|
||||
* 规格值
|
||||
*/
|
||||
private List<String> attrValueList;
|
||||
|
||||
public static RuleAttrCacheModel ruleAttrBuild(RuleAttrInfo ruleAttrInfo){
|
||||
return RuleAttrCacheModel.builder()
|
||||
.attrName(ruleAttrInfo.getName())
|
||||
.attrValueList(Arrays.stream(ruleAttrInfo.getAttrValue().split(",")).toList())
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.product.cache.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* RuleCacheModel
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class RuleCacheModel {
|
||||
|
||||
/**
|
||||
* 规格名称
|
||||
*/
|
||||
private String ruleName;
|
||||
|
||||
/**
|
||||
* 规格缓存模型
|
||||
*/
|
||||
private List<RuleAttrCacheModel> ruleAttrModelList;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
com.muyu.product.cache.ProjectInfoCache
|
||||
com.muyu.product.cache.ProjectSkuCache
|
||||
com.muyu.product.cache.ProjectSkuStockCache
|
||||
com.muyu.product.cache.RuleInfoCache
|
||||
com.muyu.product.cache.datasource.impl.RuleCacheDataRemoteImpl
|
||||
com.muyu.product.cache.datasource.impl.ProjectInfoDataRemoteImpl
|
||||
com.muyu.product.cache.datasource.impl.ProjectSkuDataRemoteImpl
|
||||
com.muyu.product.cache.datasource.impl.ProjectSkuStockDataRemoteImpl
|
|
@ -1,35 +0,0 @@
|
|||
{
|
||||
"品牌":
|
||||
{
|
||||
"id"
|
||||
"品牌中间表"
|
||||
}
|
||||
"轮播图"
|
||||
"名称":
|
||||
"价格"
|
||||
"商品规格"{
|
||||
"规格详情表"
|
||||
}
|
||||
}
|
||||
|
||||
商品信息 商品品类 品类信息
|
||||
|
||||
{
|
||||
"商品品类":[
|
||||
"品类id":
|
||||
"品类名称"
|
||||
]
|
||||
"商品轮播图":
|
||||
"商品名称":
|
||||
"商品价格":
|
||||
"商品规格":[
|
||||
{
|
||||
"规格id"
|
||||
"规格值"
|
||||
}
|
||||
]
|
||||
"商品数量"
|
||||
"商品图片"
|
||||
自由属性:[]
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.product.remote;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.ProjectInfo;
|
||||
import com.muyu.product.remote.factory.RemoteProjectInfoFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格远程调用业务层
|
||||
* @Date 2024-4-7 上午 10:58
|
||||
*/
|
||||
@FeignClient(
|
||||
contextId = "remoteProjectInfoService",
|
||||
value = ServiceNameConstants.PRODUCT_SERVICE,
|
||||
fallbackFactory = RemoteProjectInfoFactory.class,
|
||||
path = "/info"
|
||||
)
|
||||
public interface RemoteProjectInfoService {
|
||||
/**
|
||||
* 获取商品信息详细信息
|
||||
*/
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result<ProjectInfo> getInfo(@PathVariable("id") Long id) ;
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.muyu.product.remote;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.remote.factory.RemoteProjectSkuFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 远程调用业务层
|
||||
* @Date 2024-4-7 上午 10:58
|
||||
*/
|
||||
@FeignClient(
|
||||
contextId = "remoteProjectSkuService",
|
||||
value = ServiceNameConstants.PRODUCT_SERVICE,
|
||||
fallbackFactory = RemoteProjectSkuFactory.class,
|
||||
path = "/sku"
|
||||
)
|
||||
public interface RemoteProjectSkuService {
|
||||
|
||||
/**
|
||||
* 通过商品ID和SKU获取SKU信息
|
||||
* @param projectId 商品ID
|
||||
* @param projectSku 商品SKU
|
||||
* @return 商品SKU信息
|
||||
*/
|
||||
@GetMapping("/info/{projectId}/{projectSku}")
|
||||
public Result<ProjectSkuInfo> getInfoByProjectIdAndSku(@PathVariable("projectId") Long projectId,
|
||||
@PathVariable("projectSku") String projectSku);
|
||||
/**
|
||||
* 通过商品ID和SKU获取SKU信息
|
||||
* @param projectId 商品ID
|
||||
* @return 商品SKU信息
|
||||
*/
|
||||
@GetMapping("/list/{projectId}")
|
||||
public Result<List<ProjectSkuInfo>> listByProjectId(@PathVariable("projectId") Long projectId);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.muyu.product.remote;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.RuleAttrInfo;
|
||||
import com.muyu.product.remote.factory.RemoteRuleAttrFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格信息远程调用业务层
|
||||
* @Date 2024-4-7 上午 10:58
|
||||
*/
|
||||
@FeignClient(
|
||||
contextId = "remoteRuleAttrService",
|
||||
value = ServiceNameConstants.PRODUCT_SERVICE,
|
||||
fallbackFactory = RemoteRuleAttrFactory.class,
|
||||
path = "/ruleAttr"
|
||||
)
|
||||
public interface RemoteRuleAttrService {
|
||||
/**
|
||||
* 获取规格详情详细信息
|
||||
*/
|
||||
@GetMapping(value = "/list/ruleId/{ruleId}")
|
||||
public Result<List<RuleAttrInfo>> getInfoByRuleId(@PathVariable("ruleId") Long id);
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.muyu.product.remote;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.RuleInfo;
|
||||
import com.muyu.product.remote.factory.RemoteRuleFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格远程调用业务层
|
||||
* @Date 2024-4-7 上午 10:58
|
||||
*/
|
||||
@FeignClient(
|
||||
contextId = "remoteRuleService",
|
||||
value = ServiceNameConstants.PRODUCT_SERVICE,
|
||||
fallbackFactory = RemoteRuleFactory.class,
|
||||
path = "/rule"
|
||||
)
|
||||
public interface RemoteRuleService {
|
||||
/**
|
||||
* 获取商品规格详细信息
|
||||
*/
|
||||
@GetMapping(value = "/{id}")
|
||||
public Result<RuleInfo> getInfo(@PathVariable("id") Long id);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.product.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.ProjectInfo;
|
||||
import com.muyu.product.remote.RemoteProjectInfoService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格远程调熔断器
|
||||
* @Date 2024-4-7 上午 10:59
|
||||
*/
|
||||
public class RemoteProjectInfoFactory implements FallbackFactory<RemoteProjectInfoService> {
|
||||
@Override
|
||||
public RemoteProjectInfoService create (Throwable cause) {
|
||||
return new RemoteProjectInfoService() {
|
||||
|
||||
/**
|
||||
* 获取商品规格详细信息
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
public Result<ProjectInfo> getInfo (Long id) {
|
||||
return Result.error(cause.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.product.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.remote.RemoteProjectSkuService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 远程调熔断器
|
||||
* @Date 2024-4-7 上午 10:59
|
||||
*/
|
||||
public class RemoteProjectSkuFactory implements FallbackFactory<RemoteProjectSkuService> {
|
||||
@Override
|
||||
public RemoteProjectSkuService create (Throwable cause) {
|
||||
return new RemoteProjectSkuService() {
|
||||
@Override
|
||||
public Result<ProjectSkuInfo> getInfoByProjectIdAndSku (Long projectId, String projectSku) {
|
||||
return Result.error(cause.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过商品ID和SKU获取SKU信息
|
||||
*
|
||||
* @param projectId 商品ID
|
||||
*
|
||||
* @return 商品SKU信息
|
||||
*/
|
||||
@Override
|
||||
public Result<List<ProjectSkuInfo>> listByProjectId (Long projectId) {
|
||||
return Result.error(cause.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.product.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.RuleAttrInfo;
|
||||
import com.muyu.product.remote.RemoteRuleAttrService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格详情远程调熔断器
|
||||
* @Date 2024-4-7 上午 10:59
|
||||
*/
|
||||
public class RemoteRuleAttrFactory implements FallbackFactory<RemoteRuleAttrService> {
|
||||
@Override
|
||||
public RemoteRuleAttrService create (Throwable cause) {
|
||||
return new RemoteRuleAttrService() {
|
||||
|
||||
/**
|
||||
* 获取规格详情详细信息
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
public Result<List<RuleAttrInfo>> getInfoByRuleId (Long id) {
|
||||
return Result.error(cause.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.product.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.product.domain.RuleInfo;
|
||||
import com.muyu.product.remote.RemoteRuleService;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 规格远程调熔断器
|
||||
* @Date 2024-4-7 上午 10:59
|
||||
*/
|
||||
public class RemoteRuleFactory implements FallbackFactory<RemoteRuleService> {
|
||||
@Override
|
||||
public RemoteRuleService create (Throwable cause) {
|
||||
return new RemoteRuleService() {
|
||||
|
||||
/**
|
||||
* 获取商品规格详细信息
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
@Override
|
||||
public Result<RuleInfo> getInfo (Long id) {
|
||||
return Result.error(cause.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
com.muyu.product.remote.factory.RemoteProjectSkuFactory
|
||||
com.muyu.product.remote.factory.RemoteRuleAttrFactory
|
||||
com.muyu.product.remote.factory.RemoteRuleFactory
|
||||
com.muyu.product.remote.factory.RemoteProjectInfoFactory
|
|
@ -4,16 +4,18 @@ import com.muyu.product.cache.datasource.ProjectInfoData;
|
|||
import com.muyu.product.domain.ProjectInfo;
|
||||
import com.muyu.product.service.ProjectInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 缓存数据获取 ProjectInfoDataImpl
|
||||
*
|
||||
* @author LeYang
|
||||
* on 2024/3/27
|
||||
* @author DongZl
|
||||
* @description: 缓存数据获取
|
||||
* @Date 2024-3-27 下午 03:37
|
||||
*/
|
||||
@Service
|
||||
public class ProjectInfoDataImpl implements ProjectInfoData {
|
||||
@Primary
|
||||
public class ProjectInfoDataImpl implements ProjectInfoData {
|
||||
|
||||
@Autowired
|
||||
private ProjectInfoService projectInfoService;
|
||||
|
||||
|
@ -23,7 +25,7 @@ public class ProjectInfoDataImpl implements ProjectInfoData {
|
|||
* @return 缓存对象
|
||||
*/
|
||||
@Override
|
||||
public ProjectInfo getData(Long key){
|
||||
public ProjectInfo getData (Long key) {
|
||||
return projectInfoService.getById(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.muyu.product.cache.datasource.ProjectSkuData;
|
|||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.service.ProjectSkuInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -12,26 +13,42 @@ import java.util.Map;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* ProjectSkuDataImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/1
|
||||
* @author DongZl
|
||||
* @description: 商品SKU实现类
|
||||
* @Date 2024-4-1 上午 11:38
|
||||
*/
|
||||
@Service
|
||||
public class ProjectSkuDataImpl implements ProjectSkuData{
|
||||
@Primary
|
||||
public class ProjectSkuDataImpl implements ProjectSkuData {
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuInfoService projectSkuInfoService;
|
||||
@Override
|
||||
public Map<String, ProjectSkuInfo> getData(Long projectId) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId,projectId);
|
||||
List<ProjectSkuInfo> projectSkuInfoList = projectSkuInfoService.list(queryWrapper);
|
||||
|
||||
return projectSkuInfoList.stream().collect(Collectors.toMap(ProjectSkuInfo::getSku,projectSkuInfo -> projectSkuInfo));
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param projectId 商品ID
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<String, ProjectSkuInfo> getData (Long projectId) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId, projectId);
|
||||
List<ProjectSkuInfo> projectSkuInfoList = projectSkuInfoService.list(queryWrapper);
|
||||
return projectSkuInfoList.stream()
|
||||
.collect(Collectors.toMap(ProjectSkuInfo::getSku, projectSkuInfo -> projectSkuInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param projectId 商品ID
|
||||
* @param projectSku 商品SKU
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public ProjectSkuInfo getData(Long projectId, String projectSku) {
|
||||
return null;
|
||||
public ProjectSkuInfo getData (Long projectId, String projectSku) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId, projectId);
|
||||
queryWrapper.eq(ProjectSkuInfo::getSku, projectSku);
|
||||
return projectSkuInfoService.getOne(queryWrapper);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,30 +1,28 @@
|
|||
package com.muyu.product.cache.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.muyu.product.cache.datasource.ProjectSkuStockData;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.product.service.ProjectSkuInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* sku库存实体类 ProjectSkuStockDataImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
* @author DongZl
|
||||
* @description: sku库存实现类
|
||||
* @Date 2024-4-2 上午 10:53
|
||||
*/
|
||||
@Service
|
||||
public class ProjectSkuStockDataImpl implements ProjectSkuStockData{
|
||||
@Primary
|
||||
public class ProjectSkuStockDataImpl implements ProjectSkuStockData {
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuInfoService projectSkuInfoService;
|
||||
|
||||
@Override
|
||||
public Long getData(SkuStockKey key) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId,key.getProjectId());
|
||||
queryWrapper.eq(ProjectSkuInfo::getSku,key.getSku());
|
||||
ProjectSkuInfo projectSkuInfo = projectSkuInfoService.getOne(queryWrapper);
|
||||
public Long getData (SkuStockKey key) {
|
||||
ProjectSkuInfo projectSkuInfo = projectSkuInfoService.getInfoByProjectIdAndSku(key.getProjectId(), key.getSku());
|
||||
return projectSkuInfo.getStock();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,16 @@ public class ProjectSkuInfoController extends BaseController {
|
|||
return getDataTable(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过商品ID获取SKU集合
|
||||
* @param projectId 商品ID
|
||||
* @return 商品SKU集合
|
||||
*/
|
||||
@GetMapping("/list/{projectId}")
|
||||
public Result<List<ProjectSkuInfo>> listByProjectId(@PathVariable("projectId") Long projectId){
|
||||
return Result.success(projectSkuInfoService.listByProjectId(projectId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出商品SKU列表
|
||||
*/
|
||||
|
@ -75,6 +85,11 @@ public class ProjectSkuInfoController extends BaseController {
|
|||
return Result.success(projectSkuInfoService.getById(id));
|
||||
}
|
||||
|
||||
@GetMapping("/info/{projectId}/{projectSku}")
|
||||
public Result<ProjectSkuInfo> getInfoByProjectIdAndSku(@PathVariable("projectId") Long projectId,
|
||||
@PathVariable("projectSku") String projectSku){
|
||||
return Result.success(projectSkuInfoService.getInfoByProjectIdAndSku(projectId,projectSku));
|
||||
}
|
||||
/**
|
||||
* 新增商品SKU
|
||||
*/
|
||||
|
|
|
@ -75,6 +75,13 @@ public class RuleAttrInfoController extends BaseController {
|
|||
return Result.success(ruleAttrInfoService.getById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取规格详情详细信息
|
||||
*/
|
||||
@GetMapping(value = "/list/ruleId/{ruleId}")
|
||||
public Result<List<RuleAttrInfo>> getInfoByRuleId(@PathVariable("ruleId") Long ruleId){
|
||||
return Result.success(ruleAttrInfoService.getInfoByRuleId(ruleId));
|
||||
}
|
||||
/**
|
||||
* 新增规格详情
|
||||
*/
|
||||
|
|
|
@ -19,4 +19,7 @@ public interface ProjectSkuInfoService extends IService<ProjectSkuInfo> {
|
|||
*/
|
||||
public List<ProjectSkuInfo> list(ProjectSkuInfo projectSkuInfo);
|
||||
|
||||
ProjectSkuInfo getInfoByProjectIdAndSku(Long projectId, String projectSku);
|
||||
|
||||
List<ProjectSkuInfo> listByProjectId(Long projectId);
|
||||
}
|
||||
|
|
|
@ -19,4 +19,5 @@ public interface RuleAttrInfoService extends IService<RuleAttrInfo> {
|
|||
*/
|
||||
public List<RuleAttrInfo> list(RuleAttrInfo ruleAttrInfo);
|
||||
|
||||
List<RuleAttrInfo> getInfoByRuleId(Long ruleId);
|
||||
}
|
||||
|
|
|
@ -58,4 +58,20 @@ public class ProjectSkuInfoServiceImpl extends ServiceImpl<ProjectSkuInfoMapper,
|
|||
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectSkuInfo getInfoByProjectIdAndSku(Long projectId, String projectSku) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId,projectId);
|
||||
queryWrapper.eq(ProjectSkuInfo::getSku,projectSku);
|
||||
return this.getOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProjectSkuInfo> listByProjectId(Long projectId) {
|
||||
LambdaQueryWrapper<ProjectSkuInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ProjectSkuInfo::getProjectId,projectId);
|
||||
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,4 +50,12 @@ public class RuleAttrInfoServiceImpl extends ServiceImpl<RuleAttrInfoMapper, Rul
|
|||
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RuleAttrInfo> getInfoByRuleId(Long ruleId) {
|
||||
LambdaQueryWrapper<RuleAttrInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(RuleAttrInfo::getRuleId,ruleId);
|
||||
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>muyu-shop-cart-cache</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- 商品服务公共依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-common</artifactId>
|
||||
</dependency>
|
||||
<!-- 缓存基准依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-common-cache</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,100 @@
|
|||
package com.muyu.shop.cart.cache;
|
||||
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey ;
|
||||
import com.muyu.shop.cart.cache.sourcedata.CartData;
|
||||
import com.muyu.cache.abs.HashCacheAbs;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CartCache
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
@Component
|
||||
public class CartCache extends HashCacheAbs<Long, CartHashKey, CartInfo> {
|
||||
@Autowired
|
||||
private CartData cartData;
|
||||
|
||||
/**
|
||||
* key前缀
|
||||
* @return key前缀
|
||||
*/
|
||||
@Override
|
||||
public String keyPre() {
|
||||
return "cart:info:";
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public Long decode(String redisKey) {
|
||||
return Convert.toLong(redisKey.replace(keyPre(),""));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码
|
||||
* @param hashKey ID
|
||||
* @return 键
|
||||
*/
|
||||
@Override
|
||||
public String encodeHashKey(CartHashKey hashKey){
|
||||
return hashKey.getProjectId()+":"+hashKey.getProjectSku();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码
|
||||
* @param redisHashKey 数据库键
|
||||
* @return ID
|
||||
*/
|
||||
@Override
|
||||
public CartHashKey decodeHashKey(String redisHashKey) {
|
||||
String[] split = redisHashKey.split(":");
|
||||
return CartHashKey.builder()
|
||||
.projectId(Convert.toLong(split[0]))
|
||||
.projectSku(split[1])
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData(Long key) {
|
||||
return cartData.getData(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CartInfo getData(Long key, CartHashKey hashKey) {
|
||||
return cartData.getData(key,hashKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> defaultValue() {
|
||||
throw new ServiceException("购物车无数据", Result.SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CartInfo defaultHashValue() {
|
||||
throw new ServiceException("购物车无数据",Result.SUCCESS);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.muyu.shop.cart.cache;
|
||||
|
||||
import com.muyu.cache.abs.CacheAbs;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* RuleCache
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/8
|
||||
*/
|
||||
@Component
|
||||
public class RuleCache<K,V> extends CacheAbs<K,V>{
|
||||
|
||||
|
||||
@Override
|
||||
public V getData(K key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V defaultValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String keyPre() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K decode(String redisKey) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.muyu.shop.cart.cache.key;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 购物车HashKey
|
||||
* @Date 2024-4-2 上午 11:25
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CartHashKey {
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long projectId;
|
||||
|
||||
/**
|
||||
* 商品SKU
|
||||
*/
|
||||
private String projectSku;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.shop.cart.cache.sourcedata;
|
||||
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CartData
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
public interface CartData {
|
||||
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Map<CartHashKey, CartInfo> getData(Long key);
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取1hash值
|
||||
* @param key 缓存键
|
||||
* @param hashKey hash值
|
||||
* @return hash值
|
||||
*/
|
||||
public CartInfo getData(Long key,CartHashKey hashKey);
|
||||
}
|
|
@ -82,9 +82,7 @@ public class CartInfo extends BaseEntity {
|
|||
return CartInfo.builder()
|
||||
.projectId(cartInfoSaveReq.getProjectId())
|
||||
.projectSku(cartInfoSaveReq.getProjectSku())
|
||||
.userId(cartInfoSaveReq.getUserId())
|
||||
.num(cartInfoSaveReq.getNum())
|
||||
.isSelected(cartInfoSaveReq.getIsSelected())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package com.muyu.shop.cart.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CartSkuModel
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/7
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CartSkuModel {
|
||||
|
||||
/**
|
||||
* 购物车id
|
||||
*/
|
||||
private Long cartInfoId;
|
||||
|
||||
/**
|
||||
* 图片
|
||||
*/
|
||||
private String images;
|
||||
|
||||
/**
|
||||
* 商品ID
|
||||
*/
|
||||
private Long projectId;
|
||||
|
||||
/**
|
||||
* 商品名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 商品SKU价格
|
||||
*/
|
||||
private BigDecimal price;
|
||||
|
||||
/**
|
||||
* Sku规格集合
|
||||
*/
|
||||
private List<SkuRuleModel> skuRuleList;
|
||||
|
||||
/**
|
||||
* 购物车数量
|
||||
*/
|
||||
private Long num;
|
||||
|
||||
/**
|
||||
* 库存
|
||||
*/
|
||||
private Long stock;
|
||||
|
||||
/**
|
||||
* 购物车小计
|
||||
*/
|
||||
private BigDecimal subtotal;
|
||||
|
||||
/**
|
||||
* 是否选中
|
||||
*/
|
||||
private String isSelected;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.muyu.shop.cart.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* SkuRuleModel
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/7
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SkuRuleModel {
|
||||
|
||||
/**
|
||||
* 规格名称
|
||||
*/
|
||||
private String ruleName;
|
||||
|
||||
/**
|
||||
* 规格属性
|
||||
*/
|
||||
private String ruleValue;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.muyu.shop.cart.domain.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* StatisticsCartModel
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/7
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class StatisticsCartModel {
|
||||
|
||||
/**
|
||||
* 商品总数
|
||||
*/
|
||||
private Long total;
|
||||
|
||||
/**
|
||||
* 选择总数
|
||||
*/
|
||||
private Long selectedTotal;
|
||||
|
||||
/**
|
||||
* 商品总价
|
||||
*/
|
||||
private BigDecimal priceTotal;
|
||||
|
||||
/**
|
||||
* 实际总价
|
||||
*/
|
||||
private BigDecimal actualTotal;
|
||||
}
|
|
@ -23,10 +23,6 @@ public class CartInfoSaveReq extends BaseEntity {
|
|||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 购物车id */
|
||||
|
||||
@ApiModelProperty(name = "购物车id", value = "购物车id")
|
||||
private Long id;
|
||||
|
||||
/** 商品ID */
|
||||
|
||||
|
@ -38,19 +34,12 @@ public class CartInfoSaveReq extends BaseEntity {
|
|||
@ApiModelProperty(name = "SKU", value = "SKU")
|
||||
private String projectSku;
|
||||
|
||||
/** 用户ID */
|
||||
|
||||
@ApiModelProperty(name = "用户ID", value = "用户ID")
|
||||
private Long userId;
|
||||
|
||||
/** 数量 */
|
||||
|
||||
@ApiModelProperty(name = "数量", value = "数量")
|
||||
private Long num;
|
||||
|
||||
/** 是否选中 */
|
||||
|
||||
@ApiModelProperty(name = "是否选中", value = "是否选中")
|
||||
private String isSelected;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.muyu.shop.cart.domain.resp;
|
||||
|
||||
import com.muyu.shop.cart.domain.model.CartSkuModel;
|
||||
import com.muyu.shop.cart.domain.model.StatisticsCartModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CartDetailResp
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/7
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class CartDetailResp {
|
||||
|
||||
/**
|
||||
* 购物车商品集合
|
||||
*/
|
||||
private List<CartSkuModel> cartSkuList;
|
||||
|
||||
/**
|
||||
* 购物车统计
|
||||
*/
|
||||
private StatisticsCartModel statisticsCartModel;
|
||||
}
|
|
@ -24,6 +24,21 @@
|
|||
<artifactId>muyu-shop-cart-common</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart-cache</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
<!-- 商品模块 缓存 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-product-cache</artifactId>
|
||||
</dependency>
|
||||
<!-- 商品模块 远程调用 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-product-remote</artifactId>
|
||||
</dependency>
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.shop.cart.controller;
|
|||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.muyu.shop.cart.domain.resp.CartDetailResp;
|
||||
import io.swagger.annotations.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
@ -75,6 +76,11 @@ public class CartInfoController extends BaseController {
|
|||
return Result.success(cartInfoService.getById(id));
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/detail")
|
||||
public Result<CartDetailResp> detail(){
|
||||
return Result.success(cartInfoService.detail());
|
||||
}
|
||||
/**
|
||||
* 新增购物车
|
||||
*/
|
||||
|
@ -83,7 +89,7 @@ public class CartInfoController extends BaseController {
|
|||
@PostMapping
|
||||
@ApiOperation("新增购物车")
|
||||
public Result<String> add(@RequestBody CartInfoSaveReq cartInfoSaveReq) {
|
||||
return toAjax(cartInfoService.save(CartInfo.saveBuild(cartInfoSaveReq)));
|
||||
return toAjax(cartInfoService.add(CartInfo.saveBuild(cartInfoSaveReq)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.muyu.shop.cart.service;
|
|||
import java.util.List;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.shop.cart.domain.resp.CartDetailResp;
|
||||
|
||||
/**
|
||||
* 购物车Service接口
|
||||
|
@ -19,4 +20,12 @@ public interface CartInfoService extends IService<CartInfo> {
|
|||
*/
|
||||
public List<CartInfo> list(CartInfo cartInfo);
|
||||
|
||||
CartDetailResp detail();
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
* @param cartInfo 购物车信息
|
||||
* @return 是否添加成功
|
||||
*/
|
||||
boolean add(CartInfo cartInfo);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,32 @@
|
|||
package com.muyu.shop.cart.service.impl;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.muyu.common.core.exception.ServiceException;
|
||||
import com.muyu.common.core.text.Convert;
|
||||
import com.muyu.common.core.utils.ObjUtils;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.product.cache.ProjectInfoCache;
|
||||
import com.muyu.product.cache.ProjectSkuCache;
|
||||
import com.muyu.product.cache.ProjectSkuStockCache;
|
||||
import com.muyu.product.cache.RuleInfoCache;
|
||||
import com.muyu.product.cache.key.SkuStockKey;
|
||||
import com.muyu.product.cache.model.RuleAttrCacheModel;
|
||||
import com.muyu.product.cache.model.RuleCacheModel;
|
||||
import com.muyu.product.domain.ProjectInfo;
|
||||
import com.muyu.product.domain.ProjectSkuInfo;
|
||||
import com.muyu.shop.cart.cache.CartCache ;
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.domain.model.CartSkuModel;
|
||||
import com.muyu.shop.cart.domain.model.SkuRuleModel;
|
||||
import com.muyu.shop.cart.domain.model.StatisticsCartModel;
|
||||
import com.muyu.shop.cart.domain.resp.CartDetailResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.muyu.shop.cart.mapper.CartInfoMapper;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
|
@ -14,13 +37,28 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||
/**
|
||||
* 购物车Service业务层处理
|
||||
*
|
||||
* @author muyu
|
||||
* @author DongZeLiang
|
||||
* @date 2024-03-29
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo> implements CartInfoService {
|
||||
|
||||
@Autowired
|
||||
private CartCache cartCache;
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuStockCache projectSkuStockCache;
|
||||
|
||||
@Autowired
|
||||
private ProjectSkuCache projectSkuCache;
|
||||
|
||||
@Autowired
|
||||
private ProjectInfoCache projectInfoCache;
|
||||
|
||||
@Autowired
|
||||
private RuleInfoCache ruleInfoCache;
|
||||
|
||||
/**
|
||||
* 查询购物车列表
|
||||
*
|
||||
|
@ -52,10 +90,135 @@ public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo>
|
|||
queryWrapper.eq(CartInfo::getIsSelected, cartInfo.getIsSelected());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param cartInfo 购物车信息
|
||||
*
|
||||
* @return 是否添加成功
|
||||
*/
|
||||
@Override
|
||||
public boolean add (CartInfo cartInfo) {
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
CartHashKey cartHashKey = CartHashKey.builder()
|
||||
.projectId(cartInfo.getProjectId())
|
||||
.projectSku(cartInfo.getProjectSku())
|
||||
.build();
|
||||
SkuStockKey skuStockKey = SkuStockKey.builder()
|
||||
.projectId(cartInfo.getProjectId())
|
||||
.sku(cartInfo.getProjectSku())
|
||||
.build();
|
||||
Long skuStock = Convert.toLong(projectSkuStockCache.get(skuStockKey), -1L);
|
||||
if (cartCache.hashKey(userId, cartHashKey)){
|
||||
// 取出来修改
|
||||
CartInfo cartInfoCache = cartCache.get(userId, cartHashKey);
|
||||
cartInfoCache.setNum( cartInfoCache.getNum() + cartInfo.getNum());
|
||||
if (skuStock < cartInfoCache.getNum()){
|
||||
throw new ServiceException("当前库存不足");
|
||||
}
|
||||
LambdaUpdateWrapper<CartInfo> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
updateWrapper.set(CartInfo::getNum, cartInfoCache.getNum());
|
||||
updateWrapper.eq(CartInfo::getId, cartInfoCache.getId());
|
||||
this.update(updateWrapper);
|
||||
this.cartCache.put(userId, cartHashKey, cartInfoCache);
|
||||
}else {
|
||||
// 存进去
|
||||
if (skuStock < cartInfo.getNum()){
|
||||
throw new ServiceException("当前库存不足");
|
||||
}
|
||||
cartInfo.setIsSelected("Y");
|
||||
cartInfo.setUserId(userId);
|
||||
cartInfo.setCreateBy(SecurityUtils.getUsername());
|
||||
cartInfo.setCreateTime(new Date());
|
||||
this.save(cartInfo);
|
||||
this.cartCache.put(userId, cartHashKey, cartInfo);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车详情
|
||||
*
|
||||
* @return 购物车详情
|
||||
*/
|
||||
@Override
|
||||
public CartDetailResp detail () {
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
List<CartInfo> cartInfoList = cartCache.getToList(userId);
|
||||
//CartInfo->CartSkuModel
|
||||
List<CartSkuModel> cartSkuModelList = cartInfoList.stream()
|
||||
.map(cartInfo -> {
|
||||
ProjectSkuInfo projectSkuInfo
|
||||
= projectSkuCache.get(cartInfo.getProjectId(), cartInfo.getProjectSku());
|
||||
ProjectInfo projectInfo =
|
||||
projectInfoCache.get(cartInfo.getProjectId());
|
||||
Long stock = projectSkuStockCache.get(SkuStockKey.builder()
|
||||
.projectId(cartInfo.getProjectId())
|
||||
.sku(cartInfo.getProjectSku())
|
||||
.build()
|
||||
);
|
||||
//RuleCache
|
||||
|
||||
RuleCacheModel ruleInfoCacheModel = ruleInfoCache.getData(projectInfo.getRuleId());
|
||||
List<RuleAttrCacheModel> ruleAttrModelList = ruleInfoCacheModel.getRuleAttrModelList();
|
||||
ArrayList<SkuRuleModel> ruleModelList = new ArrayList<>();
|
||||
String projectSku = cartInfo.getProjectSku();
|
||||
String[] skuArr = projectSku.split("-");
|
||||
for (int index = 0; index < skuArr.length; index++) {
|
||||
ruleModelList.add(
|
||||
SkuRuleModel.builder()
|
||||
.ruleName(ruleAttrModelList.get(index).getAttrName())
|
||||
.ruleValue(skuArr[index])
|
||||
.build()
|
||||
);
|
||||
}
|
||||
return CartSkuModel.builder()
|
||||
.projectId(cartInfo.getProjectId())
|
||||
.name(projectInfo.getName())
|
||||
.images(projectSkuInfo.getImage())
|
||||
.stock(stock)
|
||||
.subtotal(projectSkuInfo.getPrice().multiply(new BigDecimal(cartInfo.getNum())))
|
||||
.skuRuleList(ruleModelList)
|
||||
.price(projectSkuInfo.getPrice())
|
||||
.cartInfoId(cartInfo.getId())
|
||||
.num(cartInfo.getNum())
|
||||
.isSelected(cartInfo.getIsSelected())
|
||||
.build();
|
||||
}).toList();
|
||||
ArrayList<CartSkuModel> cartSkuList = new ArrayList<>();
|
||||
StatisticsCartModel statisticsCartModel = StatisticsCartModel.builder()
|
||||
.total(
|
||||
cartSkuModelList.stream()
|
||||
.mapToLong(CartSkuModel::getNum)
|
||||
.sum()
|
||||
)
|
||||
.selectedTotal(
|
||||
cartSkuModelList.stream()
|
||||
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
|
||||
.mapToLong(CartSkuModel::getNum)
|
||||
.sum()
|
||||
)
|
||||
.priceTotal(
|
||||
cartSkuModelList.stream()
|
||||
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
|
||||
.map(CartSkuModel::getSubtotal)
|
||||
.reduce(BigDecimal.ZERO,BigDecimal::add)
|
||||
)
|
||||
.actualTotal(
|
||||
cartSkuModelList.stream()
|
||||
.filter(cartSkuModel -> "Y".equals(cartSkuModel.getIsSelected()))
|
||||
.map(CartSkuModel::getSubtotal)
|
||||
.reduce(BigDecimal.ZERO,BigDecimal::add)
|
||||
)
|
||||
.build();
|
||||
|
||||
return CartDetailResp.builder()
|
||||
.cartSkuList(cartSkuList)
|
||||
.statisticsCartModel(statisticsCartModel)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.muyu.shop.cart.service.sourcedata;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.muyu.shop.cart.cache.key.CartHashKey;
|
||||
import com.muyu.shop.cart.cache.sourcedata.CartData;
|
||||
import com.muyu.shop.cart.domain.CartInfo;
|
||||
import com.muyu.shop.cart.service.CartInfoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* CartDataImpl
|
||||
*
|
||||
* @author DeKangLiu
|
||||
* on 2024/4/2
|
||||
*/
|
||||
@Service
|
||||
public class CartDataImpl implements CartData{
|
||||
@Autowired
|
||||
private CartInfoService cartInfoService;
|
||||
|
||||
/**
|
||||
* 通过键获取所有的hash数据
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Map<CartHashKey, CartInfo> getData(Long key) {
|
||||
LambdaQueryWrapper<CartInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CartInfo::getUserId, key);
|
||||
List<CartInfo> cartInfoList = cartInfoService.list(queryWrapper);
|
||||
return cartInfoList.stream()
|
||||
.collect(Collectors.toMap(cartInfo -> CartHashKey.builder().projectId(cartInfo.getProjectId()).projectSku(cartInfo.getProjectSku()).build(), cartInfo -> cartInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存键和hash键获取hash值
|
||||
* @param userId 缓存键
|
||||
* @param hashKey hash值
|
||||
* @return hash值
|
||||
*/
|
||||
@Override
|
||||
public CartInfo getData(Long userId, CartHashKey hashKey) {
|
||||
LambdaQueryWrapper<CartInfo> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(CartInfo::getUserId, userId);
|
||||
queryWrapper.eq(CartInfo::getProjectId, hashKey.getProjectId());
|
||||
queryWrapper.eq(CartInfo::getProjectSku, hashKey.getProjectSku());
|
||||
return cartInfoService.getOne(queryWrapper);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,8 @@ server:
|
|||
|
||||
# Spring
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
application:
|
||||
# 应用名称
|
||||
name: muyu-shop-cart
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
<module>muyu-shop-cart-common</module>
|
||||
<module>muyu-shop-cart-remote</module>
|
||||
<module>muyu-shop-cart-server</module>
|
||||
<module>muyu-shop-cart-cache</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
8
pom.xml
8
pom.xml
|
@ -243,8 +243,12 @@
|
|||
<artifactId>muyu-product-remote</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 购物车模块 缓存 依赖-->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>muyu-shop-cart</artifactId>
|
||||
<version>${muyu.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
|
Loading…
Reference in New Issue