package com.ks.goldcorn.service;
|
|
import com.ks.goldcorn.exception.GoldTradeException;
|
import com.ks.goldcorn.exception.GoldUserException;
|
import com.ks.goldcorn.mapper.GoldCornBalanceMapper;
|
import com.ks.goldcorn.pojo.DO.GoldCornBalance;
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
|
@Component
|
public class GoldCornBalanceManager {
|
|
@Resource
|
private GoldCornBalanceMapper goldCornBalanceMapper;
|
|
@Transactional(rollbackFor = Exception.class)
|
public void addMoney(Long appId, String uid, int money) throws GoldUserException {
|
GoldCornBalance balance = goldCornBalanceMapper.selectByAppIdAndUidForUpdate(appId, uid);
|
if (balance == null) {
|
throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
|
}
|
goldCornBalanceMapper.addMoney(balance.getId(), money);
|
}
|
|
|
@Transactional(rollbackFor = Exception.class)
|
public void subMoney(Long appId, String uid, int money) throws GoldUserException, GoldTradeException {
|
GoldCornBalance balance = goldCornBalanceMapper.selectByAppIdAndUidForUpdate(appId, uid);
|
if (balance == null) {
|
throw new GoldUserException(GoldUserException.CODE_NOT_EXIST, "uid is not exist");
|
}
|
|
if (balance.getBalance() < money) {
|
throw new GoldUserException(GoldTradeException.CODE_BALANCE_NOT_ENOUGH, "余额不足");
|
}
|
|
goldCornBalanceMapper.subMoney(balance.getId(), money);
|
}
|
}
|