添加账户资金
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package cn.pluss.platform.mapper;
|
||||
|
||||
import cn.pluss.platform.entity.UserAccountFlow;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface UserAccountFlowMapper extends BaseMapper<UserAccountFlow> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.pluss.platform.mapper;
|
||||
|
||||
import cn.pluss.platform.entity.UserAccount;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Mapper
|
||||
public interface UserAccountMapper extends BaseMapper<UserAccount> {
|
||||
|
||||
|
||||
|
||||
@Select(value = "select * from tb_pluss_user_account where user_id=#{userId} for update")
|
||||
UserAccount selectUserByUserId(Integer userId);
|
||||
|
||||
@Update(value = "update tb_pluss_user_account set freeze_balance=freeze_balance+#{amount} where user_id=#{userId}")
|
||||
Integer updateFreezon(@Param("userId") Integer userId, @Param("amount") BigDecimal amount);
|
||||
|
||||
@Update(value = "update tb_pluss_user_account set balance=balance+#{amount} where user_id=#{userId}")
|
||||
Integer updateBanlance(@Param("userId") Integer userId, @Param("amount") BigDecimal amount);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.pluss.platform.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("tb_pluss_user_account")
|
||||
public class UserAccount implements Serializable {
|
||||
|
||||
|
||||
|
||||
@TableId
|
||||
private Integer userId;
|
||||
|
||||
private BigDecimal freezeBalance;
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
private BigDecimal outBalance;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Date updateTime;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package cn.pluss.platform.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("tb_pluss_user_account_flow")
|
||||
public class UserAccountFlow implements Serializable {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String bizCode;
|
||||
|
||||
private String bizName;
|
||||
|
||||
private BigDecimal amount;
|
||||
|
||||
private BigDecimal balance;
|
||||
|
||||
private String remark;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Date updateTime;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user