添加账户资金

This commit is contained in:
韩鹏辉
2023-08-16 14:50:25 +08:00
parent dc44094c68
commit 2e0ef00ce0
6 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
package cn.pluss.platform.userAccount;
import cn.pluss.platform.entity.UserAccount;
import java.math.BigDecimal;
public interface UserAccountService {
/**
* 查询用户信息
* @param userId
* @return
*/
public UserAccount getUserAccountByUserId(Integer userId);
/**
* 查询用户信息行锁
* @param userId
* @return
*/
public UserAccount getUserAccountByUserIdForUpdate(Integer userId);
/**
* 冻结资金/解冻资金
* @param userId 用户id
* @param bizCode 业务编码
* @param bizName 业务名称
* @param difference 变动资金 正数冻结 负数解冻
* @param remark 备注
*/
public void modForzenFunds(Integer userId, String bizCode, String bizName, BigDecimal difference,String remark);
/**
* 变动资金
* @param userId 用户id
* @param bizCode 业务编码
* @param bizName 业务名称
* @param difference 变动资金 正数增加 负数较少
* @param remark 备注
*/
public void modFunds(Integer userId, String bizCode, String bizName, BigDecimal difference,String remark);
}

View File

@@ -0,0 +1,132 @@
package cn.pluss.platform.userAccount.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.pluss.platform.entity.UserAccount;
import cn.pluss.platform.entity.UserAccountFlow;
import cn.pluss.platform.entity.UserExtra;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.UserAccountFlowMapper;
import cn.pluss.platform.mapper.UserAccountMapper;
import cn.pluss.platform.mapper.UserExtraMapper;
import cn.pluss.platform.user.UserExtraService;
import cn.pluss.platform.userAccount.UserAccountService;
import cn.pluss.platform.util.N;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {
@Autowired
private UserAccountMapper userAccountMapper;
@Autowired
private UserAccountFlowMapper userAccountFlowMapper;
@Override
public UserAccount getUserAccountByUserId(Integer userId) {
return baseMapper.selectById(userId);
}
@Override
public UserAccount getUserAccountByUserIdForUpdate(Integer userId) {
return userAccountMapper.selectUserByUserId(userId);
}
private UserAccount initUserAccount(Integer userId){
Assert.notNull(userId, "NOt Null");
synchronized (this){
UserAccount userAccount=getUserAccountByUserId(userId);
if(ObjectUtil.isEmpty(userAccount)){
userAccount=new UserAccount();
userAccount.setUserId(userId);
userAccount.setFreezeBalance(BigDecimal.ZERO);
userAccount.setBalance(BigDecimal.ZERO);
userAccount.setOutBalance(BigDecimal.ZERO);
userAccount.setCreateTime(new Date());
userAccount.setUpdateTime(new Date());
int result=baseMapper.insert(userAccount);
if(result<1){
MsgException.checkNull(null,"账户信息不存在");
}
}
return userAccount;
}
}
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
public void modForzenFunds(Integer userId, String bizCode, String bizName, BigDecimal difference, String remark) {
Assert.notNull(userId, "NOt Null");
Assert.notNull(bizCode, "Not Null");
Assert.notNull(bizName, "Not Null");
Assert.notNull(difference, "Not Null");
UserAccount userAccount= initUserAccount(userId);
if(N.gt(BigDecimal.ZERO,difference)){
difference=difference.abs().negate();
}
if(N.gt(difference,BigDecimal.ZERO)){
difference=difference.abs();
}
userAccountMapper.updateFreezon(userId,difference);
UserAccountFlow flow=new UserAccountFlow();
flow.setUserId(userId);
flow.setBizCode(bizCode);
flow.setBizName(bizName);
flow.setAmount(difference.abs());
flow.setBalance(userAccount.getBalance());
flow.setCreateTime(new Date());
userAccountFlowMapper.insert(flow);
}
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.MANDATORY)
public void modFunds(Integer userId, String bizCode, String bizName, BigDecimal difference, String remark) {
Assert.notNull(userId, "NOt Null");
Assert.notNull(bizCode, "Not Null");
Assert.notNull(bizName, "Not Null");
Assert.notNull(difference, "Not Null");
UserAccount userAccount= initUserAccount(userId);
if(N.gt(BigDecimal.ZERO,difference)){
difference=difference.abs().negate();
}
if(N.gt(difference,BigDecimal.ZERO)){
difference=difference.abs();
}
userAccountMapper.updateBanlance(userId,difference);
UserAccountFlow flow=new UserAccountFlow();
flow.setUserId(userId);
flow.setBizCode(bizCode);
flow.setBizName(bizName);
flow.setAmount(difference.abs());
flow.setBalance(userAccount.getBalance());
flow.setCreateTime(new Date());
userAccountFlowMapper.insert(flow);
}
}