feat:增加缓存

This commit is contained in:
dzc committed 2026-05-12 15:07:38 +08:00
1 parent 17dd0fc6e9
commit 8e5f91a68b
17 files changed
+153 -12

No files matched your search

@@ -135,6 +135,12 @@ public interface CacheConstants {
String WALLET_DETAIL = "wallet:detail:manage";
// 根据钱包id查询钱包详情
String WALLET_DETAIL_ID = "wallet:detail:id";
// 根据用户id钱包类型查询钱包详情
String WALLET_DETAIL_USER_ID_WALLET_TYPE = "wallet:detail:user_id_wallet_type";
String WALLET_TYPE_DETAIL = "wallet:detail:type";
String WALLET_SETTING_DETAIL = "wallet:detail:setting";
@@ -144,6 +150,9 @@ public interface CacheConstants {
// 用户消费机的额外价格
String WALLET_USER_DISCOUNTS_LIST = "wallet:detail:user_discounts_list";
// 用户的食堂商户(从视图里查询)
String WALLET_VIEWS_CANTEEN_MCH = "wallet:detail:view_canteen_mch";
/* ************************************* end 钱包相关 end ************************************* */
/* ************************************* start 第三方对接相关 start ************************************* */
@@ -53,7 +53,7 @@ public class HardApiServiceImpl implements HardApiService {
result.setWalletBalance(wallet.getBalance());
result.setWalletIsFreeze(wallet.getIsFreeze());
// 查询用户食堂信息
MchManageVo mchDetail = remoteMchService.getFirstMchByDeptId(user.getSysUser().getDeptId(), List.of(MerchantTypeEnum.CANTEEN)).getData();
MchManageVo mchDetail = remoteMchService.getViewsCanteenMchByDeptId(user.getSysUser().getDeptId()).getData();
if (ObjectUtil.isNotEmpty(mchDetail)) {
result.setCanteenId(mchDetail.getId());
}
@@ -86,7 +86,7 @@ public class HardApiServiceImpl implements HardApiService {
result.setWalletBalance(wallet.getBalance());
result.setWalletIsFreeze(wallet.getIsFreeze());
// 查询用户食堂信息
MchManageVo mchDetail = remoteMchService.getFirstMchByDeptId(user.getSysUser().getDeptId(), List.of(MerchantTypeEnum.CANTEEN)).getData();
MchManageVo mchDetail = remoteMchService.getViewsCanteenMchByDeptId(user.getSysUser().getDeptId()).getData();
if (ObjectUtil.isNotEmpty(mchDetail)) {
result.setCanteenId(mchDetail.getId());
}
@@ -1110,7 +1110,7 @@ public class HardOrderServiceImpl extends ServiceImpl<HardOrderMapper, HardOrder
List<MchRuleJoinDtoVo> list = mchRuleJoinService.innerMchRoleList(mch.getId()).getData();
if (ObjectUtil.isEmpty(list)) return extraPrice;
// 查询用户食堂
MchManageVo userMch = remoteMchService.getFirstMchByDeptId(sysUser.getDeptId(), List.of(MerchantTypeEnum.CANTEEN)).getData();
MchManageVo userMch = remoteMchService.getViewsCanteenMchByDeptId(sysUser.getDeptId()).getData();
for (MchRuleJoinDtoVo v : list) {
Long basePrice = v.getBasePrice();
Long multiple = v.getMultiple();
@@ -241,4 +241,5 @@ public interface SysUserService extends IService<SysUser> {
List<SimpleUserListVO> listUserIdByDeptId(Long deptId, String name);
void deleteDiscountsCacheByUserId(Long userId);
}
@@ -38,6 +38,7 @@ import com.bsd.pjcloud.admin.api.vo.DeptExcelVO;
import com.bsd.pjcloud.admin.api.vo.OrgTreeVO;
import com.bsd.pjcloud.admin.mapper.*;
import com.bsd.pjcloud.admin.service.SysDeptService;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.excel.vo.ErrorMessage;
@@ -46,6 +47,7 @@ import com.bsd.pjcloud.common.security.util.SecurityUtils;
import com.bsd.pjcloud.wallet.api.feign.RemoteWalletService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
@@ -112,9 +114,15 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* 修改部门
*/
@Override
public Boolean edit(SysDept sysDept) {
boolean isEdit = updateById(sysDept);
remoteWalletService.deptChange(sysDept.getDeptId(), Boolean.FALSE);
@CacheEvict(
value = CacheConstants.WALLET_VIEWS_CANTEEN_MCH,
key = "#dto.getDeptId()",
condition = "#dto.getDeptId() != null",
beforeInvocation = true
)
public Boolean edit(SysDept dto) {
boolean isEdit = updateById(dto);
remoteWalletService.deptChange(dto.getDeptId(), Boolean.FALSE);
return isEdit;
}
@@ -123,7 +123,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
private final SysDeptService sysDeptService;
private StringRedisTemplate stringRedisTemplate;
private final StringRedisTemplate stringRedisTemplate;
@Lazy
private final SysUserFaceService sysUserFaceService;
@@ -52,4 +52,8 @@ public interface RemoteMchService {
@NoToken
@GetMapping("/mch-manage/get-first-mch-by-dept-id")
R<MchManageVo> getFirstMchByDeptId(@RequestParam Long deptId, @RequestParam List<MerchantTypeEnum> mchType);
@NoToken
@GetMapping("/mch-manage/get-views-canteen-mch-by-dept-id")
R<MchManageVo> getViewsCanteenMchByDeptId(@RequestParam Long deptId);
}
@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.data.datascope.DataScopeFuncEnum;
@@ -23,6 +24,7 @@ import com.bsd.pjcloud.wallet.mapper.WalletManageMapper;
import com.bsd.pjcloud.wallet.service.WalletCodeUseService;
import com.bsd.pjcloud.wallet.service.WalletManageService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
@@ -91,6 +93,12 @@ public class WalletManageController {
@Inner
@GetMapping("/get-wallet")
// @Cacheable(
// value = CacheConstants.WALLET_DETAIL_USER_ID_WALLET_TYPE + "#3600",
// key = "#userId + ':' + #walletTypeCode.getCode()",
// condition = "#userId != null && #walletTypeCode.getCode() != null",
// unless = "#result == null || #result.data == null"
// )
public R getWallet(@RequestParam Long userId, @RequestParam WalletTypeEnum walletTypeCode) {
WalletManageEntity wallet = walletManageService.getWallet(userId, walletTypeCode);
if (ObjectUtil.isEmpty(wallet)) return R.failed("用户钱包不存在");
@@ -99,6 +107,12 @@ public class WalletManageController {
@Inner
@GetMapping("/get-wallet-by-id")
// @Cacheable(
// value = CacheConstants.WALLET_DETAIL_ID + "#3600",
// key = "#walletId",
// condition = "#walletId != null",
// unless = "#result == null || #result.data != null"
// )
public R<WalletManageVo> getWalletById(@RequestParam Long walletId) {
WalletManageVo wallet = walletManageMapper.join(walletId);
if (ObjectUtil.isEmpty(wallet)) return R.failed("用户钱包不存在");
@@ -61,7 +61,7 @@ public class WalletSettingController {
@GetMapping("/ad/get-discounts-list")
@Cacheable(
value = CacheConstants.WALLET_USER_DISCOUNTS_LIST,
value = CacheConstants.WALLET_USER_DISCOUNTS_LIST + "#3600",
key = "#userId + ':' + #hardId",
condition = "#userId != null && #hardId != null",
unless = "#result == null || #result.data == null"
@@ -10,6 +10,7 @@ import com.bsd.pjcloud.admin.api.dto.UserInfo;
import com.bsd.pjcloud.admin.api.entity.SysDept;
import com.bsd.pjcloud.admin.api.feign.RemoteDeptService;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.constant.enums.MerchantTypeEnum;
import com.bsd.pjcloud.common.core.constant.enums.YesNoEnum;
import com.bsd.pjcloud.common.core.util.R;
@@ -23,6 +24,7 @@ import com.bsd.pjcloud.wallet.api.dto.MchManageDto;
import com.bsd.pjcloud.wallet.api.entity.mch.MchManageEntity;
import com.bsd.pjcloud.wallet.api.vo.MchManageVo;
import com.bsd.pjcloud.wallet.service.MchManageService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import com.bsd.pjcloud.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
@@ -126,6 +128,15 @@ public class MchManageController {
if (ObjectUtil.isEmpty(mchManage)) return R.failed("商户不存在");
return R.ok(mchManage);
}
@Inner
@GetMapping("/get-views-canteen-mch-by-dept-id")
public R<MchManageVo> getViewsCanteenMchByDeptId(@RequestParam Long deptId) {
MchManageVo mchManage = mchManageService.getViewsCanteenMchByDeptId(deptId);
if (ObjectUtil.isEmpty(mchManage)) return R.failed("商户不存在");
return R.ok(mchManage);
}
/* ************************************* end 远程调用 end ************************************* */
/* ************************************* start app start ************************************* */
@@ -14,6 +14,6 @@ public interface MchDeptViewMapper {
List<WalletMchDeptViewVo> selectList(@Param("dto") WalletMchDeptViewDto dto);
WalletMchDeptViewVo selectByUserId(@Param("userId") Long userId);
WalletMchDeptViewVo selectByDeptId(@Param("deptId") Long deptId);
}
@@ -2,12 +2,15 @@ package com.bsd.pjcloud.wallet.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.constant.enums.MerchantTypeEnum;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.security.service.PjcloudUser;
import com.bsd.pjcloud.wallet.api.dto.MchManageDto;
import com.bsd.pjcloud.wallet.api.entity.mch.MchManageEntity;
import com.bsd.pjcloud.wallet.api.vo.MchManageVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import java.util.ArrayList;
import java.util.List;
@@ -41,6 +44,11 @@ public interface MchManageService extends IService<MchManageEntity> {
MchManageVo getFirstMchByDeptId(Long deptId, List<MerchantTypeEnum> mchTypeList);
/**
* 从视图里获取食堂商户信息
*/
MchManageVo getViewsCanteenMchByDeptId(Long deptId);
/**
* 刷新商户二维码
*/
@@ -121,4 +121,9 @@ public interface WalletManageService extends IService<WalletManageEntity> {
* 是够能够使用智能柜
*/
String isUseDoor(Long userId);
/**
* 删除钱包缓存
*/
void delWalletCache(WalletManageVo wallet);
}
@@ -29,6 +29,7 @@ import com.bsd.pjcloud.common.core.util.CollectionUtils;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.data.resolver.ParamResolver;
import com.bsd.pjcloud.common.data.tenant.TenantContextHolder;
import com.bsd.pjcloud.wallet.api.constant.WalletTypeEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletManageBalanceDto;
import com.bsd.pjcloud.wallet.api.dto.WalletManageDto;
@@ -79,6 +80,7 @@ public class WalletManageServiceImpl extends ServiceImpl<WalletManageMapper, Wal
@Lazy
private final WalletThirdZyService walletThirdZyService;
private final MchManageService mchManageService;
private final StringRedisTemplate stringRedisTemplate;
private final RemoteUserService userService;
private final RemoteDeptService deptService;
@@ -548,6 +550,23 @@ public class WalletManageServiceImpl extends ServiceImpl<WalletManageMapper, Wal
return "消费钱包余额不足100元";
}
/**
* 删除钱包缓存
*/
@Override
public void delWalletCache(WalletManageVo wallet) {
if (ObjectUtil.isEmpty(wallet)) return;
Long tenantId = TenantContextHolder.getTenantId();
if (ObjectUtil.isNotEmpty(wallet.getId())) {
String redisKey = tenantId + ":" + CacheConstants.WALLET_DETAIL_ID + "::" + wallet.getId();
stringRedisTemplate.delete(redisKey);
}
if (ObjectUtil.isNotEmpty(wallet.getUserId()) && ObjectUtil.isNotEmpty(wallet.getWalletTypeUniqueCode())) {
String redisKey = tenantId + ":" + CacheConstants.WALLET_DETAIL_USER_ID_WALLET_TYPE + "::" + wallet.getUserId() + ":" + wallet.getWalletTypeUniqueCode();
stringRedisTemplate.delete(redisKey);
}
}
private void disposeList(List<WalletManageVo> list) {
if (ObjectUtil.isNotEmpty(list)) {
// 用户
@@ -6,9 +6,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsd.pjcloud.admin.api.dto.UserInfo;
import com.bsd.pjcloud.admin.api.entity.SysUser;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.common.core.constant.enums.MerchantTypeEnum;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.constant.enums.YesNoEnum;
import com.bsd.pjcloud.common.core.util.BeanUtils;
import com.bsd.pjcloud.common.data.tenant.TenantContextHolder;
import com.bsd.pjcloud.wallet.api.constant.WalletXfjDiscountsEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletSettingDto;
import com.bsd.pjcloud.wallet.api.entity.WalletSettingEntity;
@@ -23,12 +24,19 @@ import com.bsd.pjcloud.wallet.service.WalletSettingService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 支付设置
@@ -40,6 +48,7 @@ import java.util.List;
public class WalletSettingServiceImpl extends ServiceImpl<WalletSettingMapper, WalletSettingEntity> implements WalletSettingService {
private final MchManageService mchManageService;
private final StringRedisTemplate stringRedisTemplate;
private final RemoteUserService remoteUserService;
@@ -60,7 +69,7 @@ public class WalletSettingServiceImpl extends ServiceImpl<WalletSettingMapper, W
SysUser sysUser = userInfo.getSysUser();
// 在自营食堂
MchManageVo userMch = mchManageService.getFirstMchByDeptId(sysUser.getDeptId(), List.of(MerchantTypeEnum.CANTEEN));
MchManageVo userMch = mchManageService.getViewsCanteenMchByDeptId(sysUser.getDeptId());
boolean isSelfCatering = ObjectUtil.isNotEmpty(userMch);
List<WalletSettingEntity.WalletXfjDiscounts> list = new ArrayList<>();
@@ -125,6 +134,24 @@ public class WalletSettingServiceImpl extends ServiceImpl<WalletSettingMapper, W
}
WalletSettingEntity updateObj = BeanUtils.toBean(dto, WalletSettingEntity.class);
baseMapper.updateById(updateObj);
deleteAllDiscountsCacheByUserId();
}
public void deleteAllDiscountsCacheByUserId() {
String pattern = TenantContextHolder.getTenantId() + ":" + CacheConstants.WALLET_USER_DISCOUNTS_LIST + "::" + "*";
Set<String> keys = new HashSet<>();
stringRedisTemplate.execute((RedisCallback<Void>) connection -> {
Cursor<byte[]> cursor = connection.scan(
ScanOptions.scanOptions().match(pattern).count(1000).build()
);
while (cursor.hasNext()) {
keys.add(new String(cursor.next(), StandardCharsets.UTF_8));
}
return null;
});
if (!keys.isEmpty()) {
stringRedisTemplate.delete(keys);
}
}
@Data
@@ -36,12 +36,16 @@ import com.bsd.pjcloud.wallet.api.entity.WalletTypeEntity;
import com.bsd.pjcloud.wallet.api.utils.WalletDataUtil;
import com.bsd.pjcloud.wallet.api.vo.MchManageVo;
import com.bsd.pjcloud.wallet.api.vo.code.MchCollectionCodeVo;
import com.bsd.pjcloud.wallet.api.vo.mch.WalletMchDeptViewVo;
import com.bsd.pjcloud.wallet.mapper.MchManageMapper;
import com.bsd.pjcloud.wallet.mapper.WalletTypeMapper;
import com.bsd.pjcloud.wallet.mapper.mch.view.MchDeptViewMapper;
import com.bsd.pjcloud.wallet.service.MchManageService;
import com.bsd.pjcloud.wallet.service.WalletThirdZyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
@@ -82,6 +86,7 @@ public class MchManageServiceImpl extends ServiceImpl<MchManageMapper, MchManage
@Lazy
private final WalletThirdZyService walletThirdZyService;
private final WalletTypeMapper walletTypeMapper;
private final MchDeptViewMapper mchDeptViewMapper;
private final StringRedisTemplate redisTemplate;
@@ -121,6 +126,12 @@ public class MchManageServiceImpl extends ServiceImpl<MchManageMapper, MchManage
@Override
// @CacheEvict(value = CacheConstants.WALLET_MCH, key = "#dto.getId()", condition = "#dto.getId() != null")
@CacheEvict(
value = CacheConstants.WALLET_VIEWS_CANTEEN_MCH,
key = "#dto.getDeptId()",
condition = "#dto.getDeptId() != null",
beforeInvocation = true
)
public void edit(MchManageEntity dto) {
if (existName(dto.getName(), dto.getTypes(), dto.getId())) {
throw new RuntimeException("商户名不能重复");
@@ -311,6 +322,24 @@ public class MchManageServiceImpl extends ServiceImpl<MchManageMapper, MchManage
return getFirstMch(deptIds, mchTypeList);
}
/**
* 从视图里获取食堂商户信息
*/
@Override
@Cacheable(
value = CacheConstants.WALLET_VIEWS_CANTEEN_MCH + "#3600",
key = "#deptId",
condition = "#deptId != null",
unless = "#result == null"
)
public MchManageVo getViewsCanteenMchByDeptId(Long deptId) {
WalletMchDeptViewVo walletMchDeptView = mchDeptViewMapper.selectByDeptId(deptId);
if (ObjectUtil.isEmpty(walletMchDeptView) || ObjectUtil.isEmpty(walletMchDeptView.getDeptId())) {
return null;
}
return baseMapper.join(walletMchDeptView.getMchId());
}
/**
* 刷新商户二维码
*/
@@ -52,6 +52,12 @@
order by a.mchDeptId, a.level
</select>
<select id="selectByUserId" resultType="com.bsd.pjcloud.wallet.api.vo.mch.WalletMchDeptViewVo">
<select id="selectByDeptId" resultType="com.bsd.pjcloud.wallet.api.vo.mch.WalletMchDeptViewVo">
<include refid="selectA"/>
from v_mch_dept_tree a
<where>
and a.deptId = #{deptId}
</where>
</select>
</mapper>