package org.springblade.client; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.client.entity.CorpsDesc; import org.springblade.client.entity.GoodsDesc; import org.springblade.client.entity.StorageDesc; import org.springblade.client.entity.StorageType; import org.springblade.client.fees.service.IFeesDescService; import org.springblade.client.feign.ICorpsDescClient; import org.springblade.client.feign.IGoodsDescClient; import org.springblade.client.feign.IRedisClient; import org.springblade.client.feign.IStorageClient; import org.springblade.client.goods.enums.RedisKey; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tenant.annotation.NonDS; import org.springblade.core.tool.api.R; import org.springblade.stock.feign.IStockGoodsClient; import org.springblade.system.user.entity.User; import org.springblade.system.user.feign.IUserClient; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.annotations.ApiIgnore; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @Slf4j @NonDS @ApiIgnore() @RestController @AllArgsConstructor public class RedisClient implements IRedisClient { private final IUserClient userClient;//获取用户信息 private final IGoodsDescClient goodsDescClient;//获取商品信息 private final ICorpsDescClient corpsDescClient;//获取客户信息 private final IStorageClient iStorageClient;//获取仓库,库区信息 private final IStockGoodsClient stockGoodsClient;//获取库存帐信息 private final RedisTemplate redisTemplate; private final IFeesDescService feesDescService; @Override public boolean basicData(String type) { if (ObjectUtils.isNull(type)) { type = RedisKey.ALL; } //redis缓存基础资料数据 //判断key是否存在 //全部 if (RedisKey.ALL.equals(type)) { //用户 if (redisTemplate.hasKey(RedisKey.REDIS_USER)) { redisTemplate.opsForValue().set(RedisKey.REDIS_USER, userClient.selectUserAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.REDIS_USER, userClient.selectUserAll()); } //商品 if (redisTemplate.hasKey(RedisKey.REDIS_GOODS)) { redisTemplate.opsForValue().set(RedisKey.REDIS_GOODS, goodsDescClient.selectGoodsDescAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.REDIS_GOODS, goodsDescClient.selectGoodsDescAll()); } //商品属性、配件 if (redisTemplate.hasKey(RedisKey.SPECIFICATION)) { redisTemplate.opsForValue().set(RedisKey.SPECIFICATION, goodsDescClient.selectGoodsSpecificationAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.SPECIFICATION, goodsDescClient.selectGoodsSpecificationAll()); } //客户 CorpsDesc corpsDesc = new CorpsDesc(); corpsDesc.setTenantId(AuthUtil.getTenantId()); List list = corpsDescClient.getCorp(corpsDesc); if (ObjectUtils.isNotNull(list)) { if (redisTemplate.hasKey(RedisKey.REDIS_CORPS)) { redisTemplate.opsForValue().set(RedisKey.REDIS_CORPS, list, 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.REDIS_CORPS, list); } } //仓库 if (redisTemplate.hasKey(RedisKey.REDIS_STORAGE_TYPE)) { redisTemplate.opsForValue().set(RedisKey.REDIS_STORAGE_TYPE, iStorageClient.selectStorageTypeAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.REDIS_STORAGE_TYPE, iStorageClient.selectStorageTypeAll()); } //库区 if (redisTemplate.hasKey(RedisKey.REDIS_STORAGE_DESC)) { redisTemplate.opsForValue().set(RedisKey.REDIS_STORAGE_DESC, iStorageClient.selectStorageDescAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.REDIS_STORAGE_DESC, iStorageClient.selectStorageDescAll()); } //库存 if (redisTemplate.hasKey(RedisKey.STOCK_GOODS)) { redisTemplate.opsForValue().set(RedisKey.STOCK_GOODS, stockGoodsClient.selectStockGoodsAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.STOCK_GOODS, stockGoodsClient.selectStockGoodsAll()); } //费用名称 if (redisTemplate.hasKey(RedisKey.FEE_NAME)) { redisTemplate.opsForValue().set(RedisKey.FEE_NAME, feesDescService.list(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(RedisKey.FEE_NAME, feesDescService.list()); } } else { //用户 if (RedisKey.REDIS_USER.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, userClient.selectUserAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, userClient.selectUserAll()); } } //商品 if (RedisKey.REDIS_GOODS.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, goodsDescClient.selectGoodsDescAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, goodsDescClient.selectGoodsDescAll()); } } //商品属性、配件 if (RedisKey.SPECIFICATION.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, goodsDescClient.selectGoodsSpecificationAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, goodsDescClient.selectGoodsSpecificationAll()); } } //客户 if (RedisKey.REDIS_CORPS.equals(type)) { CorpsDesc corpsDesc = new CorpsDesc(); corpsDesc.setTenantId(AuthUtil.getTenantId()); List list = corpsDescClient.getCorp(corpsDesc); if (ObjectUtils.isNotNull(list)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, list, 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, list); } } } //仓库 if (RedisKey.REDIS_STORAGE_TYPE.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, iStorageClient.selectStorageTypeAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, iStorageClient.selectStorageTypeAll()); } } //库区 if (RedisKey.REDIS_STORAGE_DESC.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, iStorageClient.selectStorageDescAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, iStorageClient.selectStorageDescAll()); } } //库存 if (RedisKey.STOCK_GOODS.equals(type)) { if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, stockGoodsClient.selectStockGoodsAll(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, stockGoodsClient.selectStockGoodsAll()); } } //费用名称 if (RedisKey.FEE_NAME.equals(type)) { //费用名称 if (redisTemplate.hasKey(type)) { redisTemplate.opsForValue().set(type, feesDescService.list(), 30, TimeUnit.DAYS); redisTemplate.persist(type); } else { redisTemplate.opsForValue().getAndSet(type, feesDescService.list()); } } } System.out.println("商品:" + redisTemplate.opsForValue().get("goods")); System.out.println("客户:" + redisTemplate.opsForValue().get("corps")); System.out.println("仓库:" + redisTemplate.opsForValue().get("storageType")); System.out.println("库区:" + redisTemplate.opsForValue().get("storageDesc")); System.out.println("库存账:" + redisTemplate.opsForValue().get("stockGoods")); return true; } @Override public R selectRedis(String type) { //redis缓存基础资料数据 //用户 if (RedisKey.REDIS_USER.equals(type)) { //用户 List userList = castToList(redisTemplate.opsForValue().get(type), User.class); return R.data(userList); } //商品 if (RedisKey.REDIS_GOODS.equals(type)) { //库区 List goodsDescList = castToList(redisTemplate.opsForValue().get(type), GoodsDesc.class); return R.data(goodsDescList); } //客户 if (RedisKey.REDIS_CORPS.equals(type)) { //客户 List corpsDescList = castToList(redisTemplate.opsForValue().get(type), CorpsDesc.class); return R.data(corpsDescList); } //仓库 if (RedisKey.REDIS_STORAGE_TYPE.equals(type)) { //仓库 List storageTypeList = castToList(redisTemplate.opsForValue().get(type), StorageType.class); return R.data(storageTypeList); } //库区 if (RedisKey.REDIS_STORAGE_DESC.equals(type)) { //库区 List storageDescList = castToList(redisTemplate.opsForValue().get(type), StorageDesc.class); return R.data(storageDescList); } return R.fail("参数错误"); } /** * 将Object转换成List类型 * * @param obj Object对象 * @param clazz 数据类型 * @param 泛型类型 * @return List类型 */ public static List castToList(Object obj, Class clazz) { List resList = new ArrayList<>(); // 如果不是List对象,是没有办法转换的 if (obj instanceof List) { for (Object o : (List) obj) { // 将对应的元素进行类型转换 resList.add(clazz.cast(o)); } return resList; } return resList; } }