购物车缓存

master
Jiang Peng 2024-04-03 08:43:08 +08:00
parent 49b3695835
commit 3288308225
26 changed files with 1099 additions and 77 deletions

View File

@ -0,0 +1,37 @@
package com.muyu.common.cache;
import com.muyu.common.cache.decoration.DecorationKey;
/**
* @author DongZl
* @description:
* @Date 2024-4-1 08:07
*/
public interface AtomicSequenceCache<K> extends DecorationKey<K> {
/**
*
* @param key
* @return
*/
public Long get(K key);
/**
*
*/
public Long increment(K key);
/**
*
*/
public Long decrement(K key);
/**
*
*/
public Long increment(K key, Long number);
/**
*
*/
public Long decrement(K key, Long number);
}

View File

@ -1,27 +1,13 @@
package com.muyu.common.cache;
public interface Cache <K, V> {
import com.muyu.common.cache.decoration.DecorationKey;
/**
* key
* @return key
*/
public String keyPre();
/**
*
* @param key ID
* @return
*/
public String encode(K key);
/**
*
* @param redisKey
* @return ID
*/
public K decode(String redisKey);
/**
* @author DongZl
* @description:
* @Date 2024-3-26 03:25
*/
public interface Cache <K, V> extends DecorationKey<K> {
/**
* Keyvalue

View File

@ -0,0 +1,97 @@
package com.muyu.common.cache;
import com.muyu.common.cache.decoration.DecorationKey;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* @author DongZl
* @description: Hash
* @Date 2024-3-29 03:16
*/
public interface HashCache <K, HK, HV> extends DecorationKey<K> {
/**
*
* @param hashKey ID
* @return
*/
public String encodeHashKey(HK hashKey);
/**
*
* @param redisHashKey
* @return ID
*/
public HK decodeHashKey(String redisHashKey);
/**
* Keymap
* @param key
* @return Map
*/
public Map<HK, HV> get(K key);
/**
* hashKeyhashValue
* @param key
* @param hashKey hash
* @return hash
*/
public HV get(K key, HK hashKey);
/**
* hashKeyhashValue
* @param key
* @param hashKeyList hash
* @return hash
*/
public List<HV> get(K key, HK... hashKeyList);
/**
* hash
* @param key
* @return hash
*/
public List<HV> getToList(K key);
/**
*
* @param key redis
* @param map hashMap
*/
public void put(K key, Map<HK, HV> map);
/**
*
* @param key redis
* @param dataList
* @param hashKey hash
*/
public void put(K key, List<HV> dataList, Function<HV, HK> hashKey);
/**
*
* @param key redis
* @param hashKey hash
* @param hashValue hash
*/
public void put(K key, HK hashKey, HV hashValue);
/**
* redis
* @param key hash
*/
public void remove(K key);
/**
* redishash
* @param key redis
* @param hashKey hash
*/
public void remove(K key, HK hashKey);
}

View File

@ -0,0 +1,93 @@
package com.muyu.common.cache.abs;
import com.muyu.common.cache.AtomicSequenceCache;
import com.muyu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author DongZl
* @description:
* @Date 2024-4-1 08:33
*/
public abstract class AtomicSequenceCacheAbs<K> implements AtomicSequenceCache<K> {
@Autowired
private RedisService redisService;
/**
*
* @param key
* @return
*/
@Override
public Long get (K key) {
return this.redisService.getCacheObject(encode(key));
}
/**
*
* @param key
*/
@Override
public Long increment (K key) {
return this.increment(key, 1L);
}
/**
*
*
* @param key
*/
@Override
public Long decrement (K key) {
return this.decrement(key, 1L);
}
/**
*
*
* @param key
* @param number
*/
@Override
public Long increment (K key, Long number) {
Long numberValue = redisService.getCacheObject(encode(key));
if (numberValue == null){
Long data = getData(key);
data = data == null ? 0L : data;
redisService.setCacheObject(encode(key), data);
}
return redisService.increment(encode(key), number);
}
/**
*
*
* @param key
* @param number
*/
@Override
public Long decrement (K key, Long number) {
Long numberValue = redisService.getCacheObject(encode(key));
if (numberValue == null){
Long data = getData(key);
data = data == null ? 0L : data;
redisService.setCacheObject(encode(key), data);
}
return redisService.decrement(encode(key), number);
}
/**
*
*
* @param key ID
*
* @return
*/
@Override
public String encode (K key) {
return keyPre() + key;
}
public abstract Long getData(K key);
}

View File

@ -6,6 +6,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.TimeUnit;
/**
* @author DongZl
* @description:
* @Date 2024-3-27 03:10
*/
public abstract class CacheAbs<K, V> implements Cache<K, V> {
@Autowired

View File

@ -0,0 +1,212 @@
package com.muyu.common.cache.abs;
import com.muyu.common.cache.HashCache;
import com.muyu.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* @author DongZl
* @description: hash
* @Date 2024-3-29 07:40
*/
public abstract class HashCacheAbs<K, HK, HV> implements HashCache<K, HK, HV> {
@Autowired
private RedisService redisService;
/**
*
*
* @param key ID
*
* @return
*/
@Override
public String encode (K key) {
return keyPre() + key;
}
/**
*
* @param hashKey ID
* @return
*/
@Override
public String encodeHashKey (HK hashKey) {
return hashKey.toString();
}
/**
* Keymap
* @param key
* @return Map
*/
@Override
public Map<HK, HV> get (K key) {
// 获取为null的情况
Map<String, HV> cacheMap = redisService.getCacheMap(encode(key));
if (cacheMap == null || cacheMap.isEmpty()){
Map<HK, HV> dataMap = getData(key);
if (dataMap != null && !dataMap.isEmpty()){
cacheMap = encodeMap(dataMap);
}else {
cacheMap = encodeMap(defaultValue());
}
redisService.setCacheMap(encode(key), cacheMap);
}
return decodeMap(cacheMap);
}
/**
* hashKeyhashValue
*
* @param key
* @param hashKey hash
*
* @return hash
*/
@Override
public HV get (K key, HK hashKey) {
HV hashValue = redisService.getCacheMapValue(encode(key), encodeHashKey(hashKey));
if (hashValue == null){
HV dataValue = getData(key, hashKey);
hashValue = dataValue != null ? dataValue : defaultHashValue();
put(key, hashKey, hashValue);
}
return hashValue;
}
/**
* hashKeyhashValue
*
* @param key
* @param hashKeyList hash
*
* @return hash
*/
@Override
public List<HV> get (K key, HK... hashKeyList) {
List<String> encodeHashKeyList = Arrays.stream(hashKeyList).map(this::encodeHashKey).toList();
return redisService.getMultiCacheMapValue(encode(key), encodeHashKeyList);
}
/**
* hash
*
* @param key
*
* @return hash
*/
@Override
public List<HV> getToList (K key) {
Map<HK, HV> hkhvMap = get(key);
return hkhvMap.values().stream().toList();
}
/**
*
*
* @param key redis
* @param map hashMap
*/
@Override
public void put (K key, Map<HK, HV> map) {
redisService.setCacheMap(encode(key), encodeMap(map));
}
/**
*
*
* @param key redis
* @param dataList
* @param hashKey hash
*/
@Override
public void put (K key, List<HV> dataList, Function<HV, HK> hashKey) {
Map<HK, HV> dataMap = new HashMap<>();
dataList.forEach((data) -> dataMap.put(hashKey.apply(data), data));
redisService.setCacheMap(encode(key), encodeMap(dataMap));
}
/**
*
*
* @param key redis
* @param hashKey hash
* @param hashValue hash
*/
@Override
public void put (K key, HK hashKey, HV hashValue) {
redisService.setCacheMapValue(encode(key), encodeHashKey(hashKey), hashValue);
}
/**
* redis
*
* @param key hash
*/
@Override
public void remove (K key) {
redisService.deleteObject(encode(key));
}
/**
* redishash
*
* @param key redis
* @param hashKey hash
*/
@Override
public void remove (K key, HK hashKey) {
redisService.deleteCacheMapValue(encode(key), encodeHashKey(hashKey));
}
/**
*
* @param dataMap
* @return
*/
private Map<String, HV> encodeMap(Map<HK, HV> dataMap){
Map<String, HV> encodeDataMap = new HashMap<>();
dataMap.forEach((hashKey, HashValue) -> encodeDataMap.put(encodeHashKey(hashKey), HashValue));
return encodeDataMap;
}
/**
*
* @param encodeDataMap
* @return
*/
private Map<HK, HV> decodeMap(Map<String, HV> encodeDataMap){
Map<HK, HV> dataMap = new HashMap<>();
encodeDataMap.forEach((hashKey, hashValue) -> dataMap.put(decodeHashKey(hashKey), hashValue));
return dataMap;
}
/**
* hash
* @param key
* @return
*/
public abstract Map<HK, HV> getData(K key);
/**
* hashhash
* @param key
* @param hashKey hash
* @return hash
*/
public abstract HV getData(K key, HK hashKey);
/**
*
*/
public abstract Map<HK, HV> defaultValue();
public abstract HV defaultHashValue();
}

View File

@ -0,0 +1,30 @@
package com.muyu.common.cache.decoration;
/**
* @author DongZl
* @description: Key
* @Date 2024-3-29 03:19
*/
public interface DecorationKey <K>{
/**
* key
* @return key
*/
public String keyPre();
/**
*
* @param key ID
* @return
*/
public String encode(K key);
/**
*
* @param redisKey
* @return ID
*/
public K decode(String redisKey);
}

View File

@ -229,7 +229,7 @@ public class RedisService {
*
* @return Hash
*/
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<Object> hKeys) {
public <T> List<T> getMultiCacheMapValue (final String key, final Collection<?> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
@ -255,4 +255,23 @@ public class RedisService {
public Collection<String> keys (final String pattern) {
return redisTemplate.keys(pattern);
}
/**
*
* @param key key
* @param number
* @return
*/
public Long decrement (final String key, Long number) {
return redisTemplate.opsForValue().decrement(key,number);
}
/**
*
* @param key key
* @param number
* @return
*/
public Long increment (final String key, Long number) {
return redisTemplate.opsForValue().increment(key,number);
}
}

View File

@ -1,36 +1,40 @@
package com.muyu.product.cache;
import com.muyu.common.cache.HashCache;
import com.muyu.common.cache.abs.CacheAbs;
import com.muyu.common.core.text.Convert;
import com.muyu.product.cache.datasource.ProjectInfoData;
import com.muyu.product.domain.ProjectInfo;
import com.muyu.product.domain.ProjectSkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @author DongZl
* @description:
* @Date 2024-3-27 03:30
*/
@Component
public class ProjectInfoCache extends CacheAbs<Long, ProjectInfo> {
public static void main(String[] args) {
Long projectId = 10L;
HashCache<Long, Long, ProjectSkuInfo> hashCache = null;
ArrayList<ProjectSkuInfo> projectSkuInfoList = new ArrayList<>(){{
add(new ProjectSkuInfo());
add(new ProjectSkuInfo());
}};
hashCache.put(projectId, projectSkuInfoList, ProjectSkuInfo::getProjectId);
Long[] longArr = {10L, 11L, 12L, 13L, 14L, 15L};
hashCache.get(projectId, longArr);
}
@Autowired
private ProjectInfoData projectInfoData;
/**
* key
* @return key
*/
@Override
public String keyPre () {
return "project:info:";
}
/**
*
* @param redisKey
* @return ID
*/
@Override
public Long decode(String redisKey){
return Convert.toLong(redisKey.replace(keyPre(),""),0L);
}
/**
*
* @param key ID
@ -48,4 +52,24 @@ public class ProjectInfoCache extends CacheAbs<Long, ProjectInfo> {
public ProjectInfo defaultValue() {
return new ProjectInfo();
}
/**
* keye
* @return key
*/
@Override
public String keyPre() {
return "project:info:";
}
/**
*
* @param redisKey
* @return ID
*/
@Override
public Long decode(String redisKey) {
return Convert.toLong(redisKey.replace(keyPre(),""),0L);
}
}

View File

@ -0,0 +1,88 @@
package com.muyu.product.cache;
import com.muyu.common.cache.abs.HashCacheAbs;
import com.muyu.common.core.text.Convert;
import com.muyu.product.cache.datasource.ProjectSkuData;
import com.muyu.product.domain.ProjectSkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* @author DongZl
* @description: sku
* @Date 2024-3-29 03:06
*/
@Component
public class ProjectSkuCache extends HashCacheAbs<Long, String, ProjectSkuInfo> {
@Autowired
private ProjectSkuData projectSkuData;
/**
* key
* @return key
*/
@Override
public String keyPre () {
return "project:sku:";
}
/**
*
* @param redisKey
* @return ID
*/
@Override
public Long decode (String redisKey) {
return Convert.toLong(redisKey.replace(keyPre(), ""));
}
/**
*
* @param redisHashKey
* @return ID
*/
@Override
public String decodeHashKey (String redisHashKey) {
return redisHashKey;
}
/**
* hash
* @param key
* @return
*/
@Override
public Map<String, ProjectSkuInfo> getData (Long key) {
return projectSkuData.getData(key);
}
/**
* hashhash
* @param key
* @param hashKey hash
*
* @return hash
*/
@Override
public ProjectSkuInfo getData (Long key, String hashKey) {
return projectSkuData.getData(key, hashKey);
}
/**
*
*/
@Override
public Map<String, ProjectSkuInfo> defaultValue () {
return new HashMap<>();
}
@Override
public ProjectSkuInfo defaultHashValue () {
return new ProjectSkuInfo();
}
}

View File

@ -0,0 +1,61 @@
package com.muyu.product.cache;
import com.muyu.common.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;
/**
* @author DongZl
* @description: SKU
* @Date 2024-3-29 03:06
*/
@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();
}
}

View File

@ -1,7 +1,12 @@
package com.muyu.product.cache;
package com.muyu.product.cache.datasource;
import com.muyu.product.domain.ProjectInfo;
/**
* @author DongZl
* @description:
* @Date 2024-3-27 03:34
*/
public interface ProjectInfoData {
/**
@ -10,5 +15,4 @@ public interface ProjectInfoData {
* @return
*/
public ProjectInfo getData (Long key);
}

View File

@ -0,0 +1,29 @@
package com.muyu.product.cache.datasource;
import com.muyu.product.domain.ProjectSkuInfo;
import java.util.Map;
/**
* @author DongZl
* @description: SKU
* @Date 2024-4-1 11:35
*/
public interface ProjectSkuData {
/**
* hash
* @param projectId ID
* @return
*/
public Map<String, ProjectSkuInfo> getData (Long projectId) ;
/**
* hashhash
* @param projectId ID
* @param projectSku SKU
*
* @return hash
*/
public ProjectSkuInfo getData (Long projectId, String projectSku);
}

View File

@ -0,0 +1,13 @@
package com.muyu.product.cache.datasource;
import com.muyu.product.cache.key.SkuStockKey;
/**
* @author DongZl
* @description: SKU
* @Date 2024-4-2 10:52
*/
public interface ProjectSkuStockData {
public Long getData (SkuStockKey key) ;
}

View File

@ -0,0 +1,28 @@
package com.muyu.product.cache.key;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author DongZl
* @description: SKUKey
* @Date 2024-4-2 10:41
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SkuStockKey {
/**
* ID
*/
private Long projectId;
/**
* sku
*/
private String sku;
}

View File

@ -1,11 +1,16 @@
package com.muyu.product.cache.impl;
import com.muyu.product.cache.ProjectInfoData;
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.stereotype.Service;
/**
* @author DongZl
* @description:
* @Date 2024-3-27 03:37
*/
@Service
public class ProjectInfoDataImpl implements ProjectInfoData {
@ -18,8 +23,7 @@ public class ProjectInfoDataImpl implements ProjectInfoData {
* @return
*/
@Override
public ProjectInfo getData(Long key) {
public ProjectInfo getData (Long key) {
return projectInfoService.getById(key);
}
}

View File

@ -0,0 +1,52 @@
package com.muyu.product.cache.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author DongZl
* @description: SKU
* @Date 2024-4-1 11:38
*/
@Service
public class ProjectSkuDataImpl implements ProjectSkuData {
@Autowired
private ProjectSkuInfoService projectSkuInfoService;
/**
* 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));
}
/**
* hashhash
* @param projectId ID
* @param projectSku SKU
* @return hash
*/
@Override
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);
}
}

View File

@ -0,0 +1,30 @@
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.stereotype.Service;
/**
* @author DongZl
* @description: sku
* @Date 2024-4-2 10:53
*/
@Service
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);
return projectSkuInfo.getStock();
}
}

View File

@ -0,0 +1,107 @@
package com.muyu.shop.cart.cache;
import com.muyu.common.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.cache.key.CartHashKey;
import com.muyu.shop.cart.cache.sourcedata.CartData;
import com.muyu.shop.cart.domain.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author DongZl
* @description:
* @Date 2024-4-2 11:23
*/
@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);
}
/**
* hashhash
*
* @param key
* @param hashKey hash
*
* @return hash
*/
@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);
}
}

View File

@ -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;
}

View File

@ -0,0 +1,31 @@
package com.muyu.shop.cart.cache.sourcedata;
import com.muyu.shop.cart.cache.key.CartHashKey;
import com.muyu.shop.cart.domain.CartInfo;
import java.util.Map;
/**
* @author DongZl
* @description:
* @Date 2024-4-2 11:49
*/
public interface CartData {
/**
* hash
* @param key
* @return
*/
public Map<CartHashKey, CartInfo> getData (Long key);
/**
* hashhash
*
* @param key
* @param hashKey hash
*
* @return hash
*/
public CartInfo getData (Long key, CartHashKey hashKey);
}

View File

@ -86,7 +86,7 @@ public class CartInfo extends BaseEntity {
/**
*
*/
public static CartInfo editBuid(Long id, CartInfoEditReq cartInfoEditReq){
public static CartInfo editBuild(Long id, CartInfoEditReq cartInfoEditReq){
return CartInfo.builder()
.id(id)
.projectId(cartInfoEditReq.getProjectId())

View File

@ -84,7 +84,12 @@
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-common</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 购物车模块 缓存 依赖 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-cache</artifactId>
</dependency>
</dependencies>

View File

@ -1,31 +1,41 @@
package com.muyu.shop.cart.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.shop.cart.domain.CartInfo;
import com.muyu.shop.cart.domain.req.CartInfoEditReq;
import com.muyu.shop.cart.domain.req.CartInfoQueryReq;
import com.muyu.shop.cart.domain.req.CartInfoSaveReq;
import com.muyu.shop.cart.domain.req.CartInfoEditReq;
import com.muyu.shop.cart.service.CartInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.muyu.common.core.web.page.TableDataInfo;
/**
* Controller
*
* @author DongZeLiang
* @date 2024-03-29
*/
@Api(tags = "购物车")
@RestController
@RequestMapping("/Info")
public class CartInfoController extends BaseController {
@Autowired
private CartInfoService cartInfoService;
@ -35,7 +45,7 @@ public class CartInfoController extends BaseController {
@ApiOperation("获取购物车列表")
@RequiresPermissions("shopCart:Info:list")
@GetMapping("/list")
public Result<TableDataInfo<CartInfo>> list(CartInfoQueryReq cartInfoQueryReq){
public Result<TableDataInfo<CartInfo>> list(CartInfoQueryReq cartInfoQueryReq) {
startPage();
List<CartInfo> list = cartInfoService.list(CartInfo.queryBuild(cartInfoQueryReq));
return getDataTable(list);
@ -46,9 +56,9 @@ public class CartInfoController extends BaseController {
*/
@ApiOperation("导出购物车列表")
@RequiresPermissions("shopCart:Info:export")
@Log(title = "购物车",businessType = BusinessType.EXPORT)
@Log(title = "购物车", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response,CartInfo cartInfo){
public void export(HttpServletResponse response, CartInfo cartInfo) {
List<CartInfo> list = cartInfoService.list(cartInfo);
ExcelUtil<CartInfo> util = new ExcelUtil<CartInfo>(CartInfo.class);
util.exportExcel(response, list, "购物车数据");
@ -61,7 +71,7 @@ public class CartInfoController extends BaseController {
@RequiresPermissions("shopCart:Info:query")
@GetMapping(value = "/{id}")
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
public Result<CartInfo> getInfo(@PathVariable("id") Long id){
public Result<CartInfo> getInfo(@PathVariable("id") Long id) {
return Result.success(cartInfoService.getById(id));
}
@ -69,10 +79,10 @@ public class CartInfoController extends BaseController {
*
*/
@RequiresPermissions("shopCart:Info:add")
@Log(title = "购物车",businessType = BusinessType.INSERT)
@Log(title = "购物车", businessType = BusinessType.INSERT)
@PostMapping
@ApiOperation("新增购物车")
public Result<String> add(@RequestBody CartInfoSaveReq cartInfoSaveReq){
public Result<String> add(@RequestBody CartInfoSaveReq cartInfoSaveReq) {
return toAjax(cartInfoService.save(CartInfo.saveBuild(cartInfoSaveReq)));
}
@ -80,22 +90,22 @@ public class CartInfoController extends BaseController {
*
*/
@RequiresPermissions("shopCart:Info:edit")
@Log(title = "购物车",businessType = BusinessType.UPDATE)
@Log(title = "购物车", businessType = BusinessType.UPDATE)
@PutMapping("/{id}")
@ApiOperation("修改购物车")
public Result<String> edit(@PathVariable Long id, @RequestBody CartInfoEditReq cartInfoEditReq){
return toAjax(cartInfoService.updateById(CartInfo.editBuid(id,cartInfoEditReq)));
public Result<String> edit(@PathVariable Long id, @RequestBody CartInfoEditReq cartInfoEditReq) {
return toAjax(cartInfoService.updateById(CartInfo.editBuild(id,cartInfoEditReq)));
}
/**
*
*/
@RequiresPermissions("shopCart:Info:remove")
@Log(title = "购物车",businessType = BusinessType.DELETE)
@Log(title = "购物车", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
@ApiOperation("删除购物车")
@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
public Result<String> remove(@PathVariable List<Long> ids){
public Result<String> remove(@PathVariable List<Long> ids) {
return toAjax(cartInfoService.removeBatchByIds(ids));
}
}

View File

@ -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>

42
pom.xml
View File

@ -143,13 +143,6 @@
<version>${transmittable-thread-local.version}</version>
</dependency>
<!-- 缓存基准模块 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-common-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 核心模块 -->
<dependency>
<groupId>com.muyu</groupId>
@ -157,6 +150,13 @@
<version>${muyu.version}</version>
</dependency>
<!-- 缓存基准模块 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-common-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 接口模块 -->
<dependency>
<groupId>com.muyu</groupId>
@ -227,6 +227,34 @@
<version>${muyu.version}</version>
</dependency>
<!-- 商品模块 缓存 依赖 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-product-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 购物车模块 公共依赖 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-common</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 购物车模块 远程客户端 依赖 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-remote</artifactId>
<version>${muyu.version}</version>
</dependency>
<!-- 购物车模块 缓存 依赖 -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>muyu-shop-cart-cache</artifactId>
<version>${muyu.version}</version>
</dependency>
</dependencies>
</dependencyManagement>