feat:充值记录、更新余额

This commit is contained in:
dzc committed 2024-12-30 20:07:25 +08:00
1 parent 0bf9e847c7
commit 1dce8f6147
16 files changed
+280 -60

No files matched your search

+19 -8
View File
@@ -89,6 +89,12 @@ CREATE TABLE wallet_freeze
user_id bigint NOT NULL COMMENT '用户id',
free_status TINYINT NOT NULL COMMENT '冻结状态',
remark varchar(1024) COMMENT '备注',
create_by varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
create_time datetime DEFAULT NULL COMMENT '创建时间',
update_by varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人',
update_time datetime DEFAULT NULL COMMENT '修改时间',
del_flag char(1) COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '删除标记',
tenant_id bigint DEFAULT NULL COMMENT '租户ID',
PRIMARY KEY (id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='饭卡冻结记录表';
@@ -101,14 +107,19 @@ CREATE INDEX idx_dept_id ON wallet_freeze (user_id);
DROP TABLE IF EXISTS wallet_transaction;
CREATE TABLE wallet_transaction
(
id bigint NOT NULL COMMENT '编号',
wallet_id bigint NOT NULL COMMENT '会员钱包 id',
biz_type tinyint NOT NULL COMMENT '关联类型',
biz_id varchar(64) NOT NULL COMMENT '关联业务编号',
no varchar(64) NOT NULL COMMENT '流水号',
title varchar(128) NOT NULL COMMENT '流水标题',
price int NOT NULL COMMENT '交易金额, 单位分',
balance int NOT NULL COMMENT '余额, 单位分',
id bigint NOT NULL COMMENT '编号',
wallet_id bigint NOT NULL COMMENT '会员钱包 id',
biz_type tinyint NOT NULL COMMENT '关联类型',
biz_id varchar(64) NOT NULL COMMENT '关联业务编号',
title varchar(128) NOT NULL COMMENT '流水标题',
price int NOT NULL COMMENT '交易金额, 单位分',
balance int NOT NULL COMMENT '余额, 单位分',
create_by varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '创建人',
create_time datetime DEFAULT NULL COMMENT '创建时间',
update_by varchar(64) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '修改人',
update_time datetime DEFAULT NULL COMMENT '修改时间',
del_flag char(1) COLLATE utf8mb4_general_ci DEFAULT '0' COMMENT '删除标记',
tenant_id bigint DEFAULT NULL COMMENT '租户ID',
PRIMARY KEY (id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='会员钱包流水表';
@@ -13,16 +13,16 @@ import lombok.Getter;
@AllArgsConstructor
public enum YesNoEnum {
YES("1", "是"), NO("0", "否");
YES("1", "是"), NO("0", "否");
/**
* 编码
*/
private String code;
/**
* 编码
*/
private String code;
/**
* 描述
*/
private String desc;
/**
* 描述
*/
private String desc;
}
@@ -1,6 +1,7 @@
package com.bsd.pjcloud.admin.api.utils;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bsd.pjcloud.admin.api.entity.SysDept;
import com.bsd.pjcloud.admin.api.entity.SysMenu;
import com.bsd.pjcloud.admin.api.entity.SysUser;
@@ -96,6 +97,7 @@ public class DataUtil {
}
public <T extends BaseUserInterface> T convertUser(T v, SysUser user) {
if (ObjectUtil.isEmpty(user)) return v;
v.setUsername(user.getUsername());
v.setUserMobile(user.getPhone());
v.setUserAvatar(user.getAvatar());
@@ -107,6 +109,7 @@ public class DataUtil {
}
public <T extends BaseDeptInterface> T convertDept(T v, SysDept dept) {
if (ObjectUtil.isEmpty(dept)) return v;
v.setDeptId(dept.getDeptId());
v.setDeptName(dept.getName());
return v;
@@ -64,6 +64,7 @@ import org.springframework.validation.BindingResult;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
@@ -140,7 +141,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}).forEach(sysUserRoleMapper::insert);
// 创建用户默认钱包
remoteWalletService.createWallet(userDto.getUserId());
CompletableFuture.runAsync(() -> remoteWalletService.createWallet(userDto.getUserId()));
return Boolean.TRUE;
}
@@ -224,6 +225,10 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
sysUserRoleMapper.delete(Wrappers.<SysUserRole>lambdaQuery().in(SysUserRole::getUserId, CollUtil.toList(ids)));
this.removeBatchByIds(CollUtil.toList(ids));
// 删除钱包
CompletableFuture.runAsync(() -> remoteWalletService.delWallet(List.of(ids)));
return Boolean.TRUE;
}
@@ -282,7 +287,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
// 创建用户默认钱包
remoteWalletService.createWallet(userDto.getUserId());
CompletableFuture.runAsync(() -> remoteWalletService.createWallet(userDto.getUserId()));
return Boolean.TRUE;
}
@@ -1,10 +1,7 @@
package com.bsd.pjcloud.wallet.api.dto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class WalletManageDto extends WalletManageEntity {
@@ -0,0 +1,14 @@
package com.bsd.pjcloud.wallet.api.dto;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import lombok.Data;
@Data
public class WalletTransactionDto extends WalletTransactionEntity {
/**
* 用户id
*/
private Long userId;
}
@@ -6,6 +6,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 会员钱包流水表
*
@@ -44,12 +46,6 @@ public class WalletTransactionEntity extends Model<WalletTransactionEntity> {
@Schema(description="关联业务编号")
private String bizId;
/**
* 流水号
*/
@Schema(description="流水号")
private String no;
/**
* 流水标题
*/
@@ -67,4 +63,46 @@ public class WalletTransactionEntity extends Model<WalletTransactionEntity> {
*/
@Schema(description="余额, 单位分")
private Integer balance;
/**
* 创建人
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description="创建人")
private String createBy;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
@Schema(description="创建时间")
private LocalDateTime createTime;
/**
* 修改人
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
@Schema(description="修改人")
private String updateBy;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
@Schema(description="修改时间")
private LocalDateTime updateTime;
/**
* 删除标记
*/
@TableLogic
@TableField(fill = FieldFill.INSERT)
@Schema(description="删除标记")
private String delFlag;
/**
* 租户ID
*/
@Schema(description="租户ID")
private Long tenantId;
}
@@ -7,8 +7,11 @@ import com.bsd.pjcloud.wallet.api.constant.WalletTypeCodeEnum;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(contextId = "remoteWalletService", value = ServiceNameConstants.WALLET_SERVICE)
public interface RemoteWalletService {
@@ -22,6 +25,10 @@ public interface RemoteWalletService {
@NoToken
@GetMapping("/wallet-manage/create-wallet")
R<Object> createWallet(@RequestParam Long userId);
R<WalletManageEntity> createWallet(@RequestParam Long userId);
@NoToken
@GetMapping("/wallet-manage/del-wallet")
R<Boolean> delWallet(@RequestParam("userIds") List<Long> userIds);
}
@@ -0,0 +1,8 @@
package com.bsd.pjcloud.wallet.api.vo;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import lombok.Data;
@Data
public class WalletTransactionVo extends WalletTransactionEntity {
}
@@ -73,6 +73,12 @@ public class WalletManageController {
return R.ok(wallet);
}
@Inner
@GetMapping("/del-wallet")
public R<Object> delWallet(@RequestParam("userIds") List<Long> userIds){
return R.ok(walletManageService.delWalletByUserIds(userIds));
}
/* ************************************* start 远程调用 start ************************************* */
/**
@@ -108,7 +114,7 @@ public class WalletManageController {
@GetMapping("/{id}")
@PreAuthorize("@pms.hasPermission('wallet_walletManage_view')")
public R getById(@PathVariable("id") Long id) {
return R.ok(walletManageService.getById(id));
return R.ok(walletManageService.getWallet(id));
}
/**
@@ -2,12 +2,16 @@ package com.bsd.pjcloud.wallet.controller;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
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.util.R;
import com.bsd.pjcloud.common.log.annotation.SysLog;
import com.bsd.pjcloud.wallet.api.dto.WalletTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.service.WalletManageService;
import com.bsd.pjcloud.wallet.service.WalletTransactionService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.bsd.pjcloud.common.excel.annotation.ResponseExcel;
@@ -29,75 +33,93 @@ import java.util.List;
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/walletTransaction" )
@Tag(description = "walletTransaction" , name = "会员钱包流水表管理" )
@RequestMapping("/walletTransaction")
@Tag(description = "walletTransaction", name = "会员钱包流水表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WalletTransactionController {
private final WalletTransactionService walletTransactionService;
private final WalletTransactionService walletTransactionService;
@PutMapping("/update-balance")
@Operation(summary = "更新会员用户余额")
public R<Boolean> updateWalletBalance(@RequestBody WalletTransactionDto dto) {
return R.ok(walletTransactionService.updateWalletBalance(dto));
}
/**
* 分页查询
*
* @param page 分页对象
* @param walletTransaction 会员钱包流水表
* @param dto 会员钱包流水表
* @return
*/
@Operation(summary = "分页查询" , description = "分页查询" )
@GetMapping("/page" )
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_view')" )
public R getWalletTransactionPage(@ParameterObject Page page, @ParameterObject WalletTransactionEntity walletTransaction) {
LambdaQueryWrapper<WalletTransactionEntity> wrapper = Wrappers.lambdaQuery();
@Operation(summary = "分页查询", description = "分页查询")
@GetMapping("/page")
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_view')")
public R getWalletTransactionPage(@ParameterObject Page page, @ParameterObject WalletTransactionEntity dto) {
LambdaQueryWrapper<WalletTransactionEntity> wrapper = new LambdaQueryWrapper<WalletTransactionEntity>();
return R.ok(walletTransactionService.page(page, wrapper));
}
@Operation(summary = "分页查询复杂查询", description = "分页查询复杂查询")
@GetMapping("/page-join")
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_view')")
public R getWalletTransactionPageJoin(@ParameterObject Page page, @ParameterObject WalletTransactionDto dto) {
return R.ok(walletTransactionService.pageJoin(page, dto));
}
/**
* 通过id查询会员钱包流水表
*
* @param id id
* @return R
*/
@Operation(summary = "通过id查询" , description = "通过id查询" )
@GetMapping("/{id}" )
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_view')" )
public R getById(@PathVariable("id" ) Long id) {
@Operation(summary = "通过id查询", description = "通过id查询")
@GetMapping("/{id}")
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_view')")
public R getById(@PathVariable("id") Long id) {
return R.ok(walletTransactionService.getById(id));
}
/**
* 新增会员钱包流水表
*
* @param walletTransaction 会员钱包流水表
* @return R
*/
@Operation(summary = "新增会员钱包流水表" , description = "新增会员钱包流水表" )
@SysLog("新增会员钱包流水表" )
@Operation(summary = "新增会员钱包流水表", description = "新增会员钱包流水表")
@SysLog("新增会员钱包流水表")
@PostMapping
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_add')" )
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_add')")
public R save(@RequestBody WalletTransactionEntity walletTransaction) {
return R.ok(walletTransactionService.save(walletTransaction));
}
/**
* 修改会员钱包流水表
*
* @param walletTransaction 会员钱包流水表
* @return R
*/
@Operation(summary = "修改会员钱包流水表" , description = "修改会员钱包流水表" )
@SysLog("修改会员钱包流水表" )
@Operation(summary = "修改会员钱包流水表", description = "修改会员钱包流水表")
@SysLog("修改会员钱包流水表")
@PutMapping
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_edit')" )
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_edit')")
public R updateById(@RequestBody WalletTransactionEntity walletTransaction) {
return R.ok(walletTransactionService.updateById(walletTransaction));
}
/**
* 通过id删除会员钱包流水表
*
* @param ids id列表
* @return R
*/
@Operation(summary = "通过id删除会员钱包流水表" , description = "通过id删除会员钱包流水表" )
@SysLog("通过id删除会员钱包流水表" )
@Operation(summary = "通过id删除会员钱包流水表", description = "通过id删除会员钱包流水表")
@SysLog("通过id删除会员钱包流水表")
@DeleteMapping
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_del')" )
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_del')")
public R removeById(@RequestBody Long[] ids) {
return R.ok(walletTransactionService.removeBatchByIds(CollUtil.toList(ids)));
}
@@ -105,14 +127,15 @@ public class WalletTransactionController {
/**
* 导出excel 表格
*
* @param walletTransaction 查询条件
* @param ids 导出指定ID
* @param ids 导出指定ID
* @return excel 文件流
*/
@ResponseExcel
@GetMapping("/export")
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_export')" )
public List<WalletTransactionEntity> export(WalletTransactionEntity walletTransaction,Long[] ids) {
@PreAuthorize("@pms.hasPermission('wallet_walletTransaction_export')")
public List<WalletTransactionEntity> export(WalletTransactionEntity walletTransaction, Long[] ids) {
return walletTransactionService.list(Wrappers.lambdaQuery(walletTransaction).in(ArrayUtil.isNotEmpty(ids), WalletTransactionEntity::getId, ids));
}
}
@@ -1,12 +1,24 @@
package com.bsd.pjcloud.wallet.mapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsd.pjcloud.common.data.datascope.PjcloudBaseMapper;
import com.bsd.pjcloud.wallet.api.dto.WalletTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.WalletManageVo;
import com.bsd.pjcloud.wallet.api.vo.WalletTransactionVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface WalletTransactionMapper extends PjcloudBaseMapper<WalletTransactionEntity> {
default IPage<WalletTransactionVo> pageJoin(Page page, WalletTransactionDto dto) {
MPJLambdaWrapper<WalletTransactionEntity> wrapper = new MPJLambdaWrapper<WalletTransactionEntity>()
.eqIfExists(WalletTransactionEntity::getWalletId, dto.getWalletId());
return selectJoinPage(page, WalletTransactionVo.class, wrapper);
}
}
@@ -6,8 +6,11 @@ import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.wallet.api.constant.WalletTypeCodeEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletManageDto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
public interface WalletManageService extends IService<WalletManageEntity> {
/**
@@ -34,12 +37,29 @@ public interface WalletManageService extends IService<WalletManageEntity> {
/**
* 获取钱包(有缓存)
*
* @param userId
* @param walletTypeCode
* @return
*/
WalletManageEntity getWallet(@NotNull Long userId, @NotNull WalletTypeCodeEnum walletTypeCode);
/**
* 获取钱包(有缓存)
*
* @param walletId
* @return
*/
WalletManageEntity getWallet(@NotNull Long walletId);
/**
* 根据用户id删除用户钱包
*
* @param userIds
* @return
*/
Boolean delWalletByUserIds(List<@NotNull Long> userIds);
/**
* 分页连表查询
@@ -1,8 +1,13 @@
package com.bsd.pjcloud.wallet.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.wallet.api.constant.WalletBizEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.WalletTransactionVo;
public interface WalletTransactionService extends IService<WalletTransactionEntity> {
@@ -29,4 +34,18 @@ public interface WalletTransactionService extends IService<WalletTransactionEnti
*/
WalletTransactionEntity addWalletBalance(Long walletId, Integer price, String bizId, WalletBizEnum bizType);
/**
* 更新会员用户余额
* @param dto
* @return
*/
Boolean updateWalletBalance(WalletTransactionDto dto);
/**
* 分页查询复杂查询
* @param page
* @param dto
* @return
*/
IPage<WalletTransactionVo> pageJoin(Page page, WalletTransactionDto dto);
}
@@ -24,18 +24,19 @@ import com.bsd.pjcloud.wallet.mapper.WalletManageMapper;
import com.bsd.pjcloud.wallet.mapper.WalletTypeMapper;
import com.bsd.pjcloud.wallet.service.WalletManageService;
import com.bsd.pjcloud.wallet.service.WalletTypeService;
import com.bsd.pjcloud.wallet.utils.ConvertUtil;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.ehcache.xml.model.CacheTemplate;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import java.util.stream.Collectors;
/**
* 钱包管理
@@ -58,6 +59,8 @@ public class WalletManageServiceImpl extends ServiceImpl<WalletManageMapper, Wal
private final RemoteUserService userService;
private final RemoteDeptService deptService;
private final StringRedisTemplate redisTemplate;
@Override
@Transactional(rollbackFor = Exception.class)
public void createWallet(Long userId, Boolean checkPermission) {
@@ -108,7 +111,6 @@ public class WalletManageServiceImpl extends ServiceImpl<WalletManageMapper, Wal
}
@Override
@Cacheable(value = CacheConstants.WALLET_DETAIL, key = "#userId-#walletTypeCode.name()", unless = "#result == null ")
public WalletManageEntity getWallet(Long userId, WalletTypeCodeEnum walletTypeCode) {
WalletTypeEntity walletType = walletTypeService.getByUniqueCode(walletTypeCode);
if (ObjectUtil.isEmpty(walletType)) return null;
@@ -118,6 +120,26 @@ public class WalletManageServiceImpl extends ServiceImpl<WalletManageMapper, Wal
);
}
@Override
@Cacheable(value = CacheConstants.WALLET_DETAIL, key = "#walletId", condition = "#walletId != null", unless = "#result == null")
public WalletManageEntity getWallet(Long walletId) {
return baseMapper.selectById(walletId);
}
@Override
public Boolean delWalletByUserIds(List<@NotNull Long> userIds) {
if (ObjectUtil.isEmpty(userIds)) return false;
for (Long userId : userIds) {
List<WalletManageEntity> list = baseMapper.selectList(new LambdaQueryWrapper<WalletManageEntity>()
.eq(WalletManageEntity::getUserId, userId)
);
if (ObjectUtil.isEmpty(list)) return false;
baseMapper.deleteByIds(CollectionUtils.convertList(list, WalletManageEntity::getId));
list.forEach(v -> redisTemplate.opsForValue().getAndDelete(CacheConstants.WALLET_DETAIL + v.getId()));
}
return true;
}
@Override
public R pageJoin(Page page, WalletManageDto dto) {
Page<WalletManageVo> list = baseMapper.pageJoin(page, dto);
@@ -1,17 +1,26 @@
package com.bsd.pjcloud.wallet.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsd.pjcloud.common.core.constant.CacheConstants;
import com.bsd.pjcloud.common.core.exception.CheckedException;
import com.bsd.pjcloud.wallet.api.constant.WalletBizEnum;
import com.bsd.pjcloud.wallet.api.dto.WalletTransactionDto;
import com.bsd.pjcloud.wallet.api.entity.WalletManageEntity;
import com.bsd.pjcloud.wallet.api.entity.WalletTransactionEntity;
import com.bsd.pjcloud.wallet.api.vo.WalletTransactionVo;
import com.bsd.pjcloud.wallet.mapper.WalletManageMapper;
import com.bsd.pjcloud.wallet.mapper.WalletTransactionMapper;
import com.bsd.pjcloud.wallet.service.WalletManageService;
import com.bsd.pjcloud.wallet.service.WalletTransactionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@@ -42,6 +51,7 @@ public class WalletTransactionServiceImpl extends ServiceImpl<WalletTransactionM
@Override
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value = CacheConstants.WALLET_DETAIL, key = "#walletId")
public WalletTransactionEntity reduceWalletBalance(Long walletId, Integer price, Long bizId, WalletBizEnum bizType) {
WalletManageEntity wallet = walletManageService.getById(walletId);
if (ObjectUtil.isEmpty(wallet)) {
@@ -87,7 +97,7 @@ public class WalletTransactionServiceImpl extends ServiceImpl<WalletTransactionM
}
WalletTransactionEntity insetObj = new WalletTransactionEntity();
insetObj.setWalletId(walletId);
insetObj.setPrice(-price);
insetObj.setPrice(price);
insetObj.setBalance(wallet.getBalance() + price);
insetObj.setBizId(String.valueOf(bizId));
insetObj.setBizType(bizType.getCode());
@@ -95,4 +105,29 @@ public class WalletTransactionServiceImpl extends ServiceImpl<WalletTransactionM
save(insetObj);
return insetObj;
}
@Override
@CacheEvict(value = CacheConstants.WALLET_DETAIL, key = "#dto.getWalletId()")
public Boolean updateWalletBalance(WalletTransactionDto dto) {
WalletManageEntity wallet = walletManageService.getWallet(dto.getWalletId());
if (ObjectUtil.isEmpty(wallet)) {
walletManageService.createWallet(dto.getUserId());
}
if (wallet == null) {
log.error("|{}|,updateReqVO({}) 用户钱包不存在.", TAG, JSON.toJSONString(dto));
throw new CheckedException("用户钱包不存在");
}
// 更新钱包余额
WalletTransactionEntity result = getSelf().addWalletBalance(wallet.getId(), dto.getBalance(), String.valueOf(dto.getUserId()), WalletBizEnum.UPDATE);
return ObjectUtil.isNotEmpty(result);
}
private WalletTransactionServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
@Override
public IPage<WalletTransactionVo> pageJoin(Page page, WalletTransactionDto dto) {
return baseMapper.pageJoin(page, dto);
}
}