支付优化
This commit is contained in:
@ -9,5 +9,5 @@ import java.math.BigDecimal;
|
||||
*/
|
||||
public interface CardApi {
|
||||
|
||||
Boolean recharge(BigDecimal money,Long userId);
|
||||
void recharge(BigDecimal money,Long userId);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class CardApiImpl implements CardApi{
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean recharge(BigDecimal money,Long userId){
|
||||
public void recharge(BigDecimal money,Long userId){
|
||||
money = money.divide(new BigDecimal("100"));
|
||||
RechargeAmountDO recharge = rechargeAmountService.getRecharge(money);
|
||||
BigDecimal giftMoney = BigDecimal.ZERO;
|
||||
@ -32,6 +32,6 @@ public class CardApiImpl implements CardApi{
|
||||
giftMoney = giftMoney.add(recharge.getDonateMoney());
|
||||
}
|
||||
|
||||
return cardService.recharge(money, CardDO.ADD,giftMoney,userId);
|
||||
cardService.recharge(money, CardDO.ADD,giftMoney,userId);
|
||||
}
|
||||
}
|
||||
|
@ -44,46 +44,6 @@ import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.E
|
||||
public class CardController {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// // 写一个示例,将一条数据插入redis
|
||||
// @Resource
|
||||
// private RedisTemplate<String, Object> redisTemplate;
|
||||
//
|
||||
// @PostMapping("/weighingAddInfo")
|
||||
// @Operation(summary = "称重信息入口")
|
||||
// public void weighingAddInfo(@RequestBody RobotReqVO reqVO) {
|
||||
//// 生成一个redis对象并且插入数据
|
||||
// redisTemplate.opsForValue().set(reqVO.getIp(), reqVO);
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/weighingGetInfo")
|
||||
// @Operation(summary = "获取称重相关信息")
|
||||
// public CommonResult<Object> weighingGetInfo(@RequestParam String ip) {
|
||||
//// 生成一个redis对象并且插入数据
|
||||
//// redisTemplate.opsForValue().get(ip);
|
||||
// return success(redisTemplate.opsForValue().get(ip));
|
||||
// }
|
||||
//
|
||||
// @GetMapping("/plateGetInfo")
|
||||
// @Operation(summary = "获取盘子信息")
|
||||
// public CommonResult<Object> plateGetInfo(@RequestParam String ip) {
|
||||
//// 生成一个redis对象并且插入数据
|
||||
// return success(redisTemplate.opsForValue().get(ip));
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/plateAddInfo")
|
||||
// @Operation(summary = "盘子信息入口")
|
||||
// public void plateAddInfo(@RequestBody RobotReqVO reqVO) {
|
||||
//// 生成一个redis对象并且插入数据
|
||||
// redisTemplate.opsForValue().set(reqVO.getIp(), reqVO);
|
||||
// }
|
||||
|
||||
|
||||
// 创建一个websocket链接
|
||||
|
||||
@Resource
|
||||
private CardService cardService;
|
||||
|
||||
@ -158,7 +118,8 @@ public class CardController {
|
||||
@Operation(summary = "管理后台充值")
|
||||
//@PreAuthorize("@ss.hasPermission('member:card:update')")
|
||||
public CommonResult<Boolean> rechargeByAdmin(@RequestBody RechargeVO vo) {
|
||||
return success(cardService.rechargeByAdmin(vo));
|
||||
cardService.rechargeByAdmin(vo);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/getCashMoney")
|
||||
|
@ -9,5 +9,5 @@ public interface AmountService {
|
||||
|
||||
BigDecimal getAmount(Long userId);
|
||||
|
||||
|
||||
BigDecimal getCashAmount(Long userId);
|
||||
}
|
||||
|
@ -27,6 +27,9 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_NOT_EXISTS;
|
||||
|
||||
|
||||
/**
|
||||
* 金额操作 Service 实现类
|
||||
@ -66,7 +69,18 @@ public class AmountServiceImpl implements AmountService {
|
||||
@Override
|
||||
public BigDecimal getAmount(Long userId) {
|
||||
MemberUserDO memberUserDO = userMapper.selectById(userId);
|
||||
if (memberUserDO == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
return memberUserDO.getMoney();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal getCashAmount(Long userId) {
|
||||
MemberUserDO memberUserDO = userMapper.selectById(userId);
|
||||
if (memberUserDO == null) {
|
||||
throw exception(USER_NOT_EXISTS);
|
||||
}
|
||||
return memberUserDO.getCashAmount();
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,9 @@ public interface DeductionService {
|
||||
* 微信提现
|
||||
*/
|
||||
void wxDraw(Integer amount, Long userId);
|
||||
|
||||
/**
|
||||
* 退款
|
||||
*/
|
||||
void refund(Long userId,BigDecimal money,DishOrderDO dishOrderDO);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.member.controller.admin.business.vo.StatisticsVo;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.order.DishOrderDO;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
|
||||
import cn.iocoder.yudao.module.member.dal.mysql.order.DishOrderMapper;
|
||||
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
|
||||
import cn.iocoder.yudao.module.member.enums.CostTypeEnum;
|
||||
import cn.iocoder.yudao.module.member.enums.TimePeriodEnum;
|
||||
@ -247,5 +248,79 @@ public class DeductionServiceImpl implements DeductionService {
|
||||
cardDO.setCashAmount(cashNewAmount);
|
||||
cardService.insertOne(cardDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refund(Long userId, BigDecimal money, DishOrderDO dishOrderDO) {
|
||||
//获取最新余额
|
||||
BigDecimal newMoney2;
|
||||
BigDecimal wxNewMoney;
|
||||
BigDecimal giftNewMoney;
|
||||
BigDecimal cashNewAmount;
|
||||
|
||||
synchronized (userId){
|
||||
MemberUserDO user = userService.getUser(userId);
|
||||
BigDecimal oldMoney = user.getMoney();
|
||||
|
||||
user.setMoney(oldMoney.add(money));
|
||||
|
||||
BigDecimal wxAmount = user.getWxAmount();
|
||||
BigDecimal cashAmount = user.getCashAmount();
|
||||
BigDecimal giftAmount = user.getGiftAmount();
|
||||
|
||||
BigDecimal dishWxAmount = dishOrderDO.getWxAmount();
|
||||
BigDecimal dishCashAmount = dishOrderDO.getCashAmount();
|
||||
BigDecimal dishGiftAmount = dishOrderDO.getGiftAmount();
|
||||
|
||||
BigDecimal newMoney = money;
|
||||
if(oldMoney.compareTo(BigDecimal.ZERO)<0){
|
||||
newMoney = user.getMoney();
|
||||
}
|
||||
|
||||
|
||||
if (user.getMoney().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
user.setWxAmount(BigDecimal.ZERO);
|
||||
user.setGiftAmount(BigDecimal.ZERO);
|
||||
user.setCashAmount(BigDecimal.ZERO);
|
||||
} else {
|
||||
if (newMoney.compareTo(dishGiftAmount) <= 0) {
|
||||
user.setGiftAmount(giftAmount.add(newMoney));
|
||||
} else {
|
||||
user.setGiftAmount(giftAmount.add(dishGiftAmount));
|
||||
BigDecimal money1 = newMoney.subtract(dishGiftAmount);
|
||||
|
||||
if (money1.compareTo(dishCashAmount) <= 0) {
|
||||
user.setCashAmount(cashAmount.add(money1));
|
||||
} else {
|
||||
user.setCashAmount(cashAmount.add(dishCashAmount));
|
||||
BigDecimal money2 = money1.subtract(dishCashAmount);
|
||||
if (money2.compareTo(dishWxAmount) <= 0) {
|
||||
user.setWxAmount(wxAmount.add(money2));
|
||||
} else {
|
||||
user.setWxAmount(wxAmount.add(dishWxAmount));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
userService.updateById(user);
|
||||
newMoney2 = user.getMoney();
|
||||
wxNewMoney = user.getWxAmount();
|
||||
giftNewMoney = user.getGiftAmount();
|
||||
cashNewAmount = user.getCashAmount();
|
||||
|
||||
}
|
||||
|
||||
CardDO cardDO = new CardDO();
|
||||
cardDO.setUserId(userId);
|
||||
cardDO.setFlag(CardDO.ADD);
|
||||
cardDO.setChangeMoney(money);
|
||||
cardDO.setType(CostTypeEnum.REFUND.getCode());
|
||||
cardDO.setWxAmount(wxNewMoney);
|
||||
cardDO.setGiftAmount(giftNewMoney);
|
||||
cardDO.setMoney(newMoney2);
|
||||
cardDO.setCashAmount(cashNewAmount);
|
||||
|
||||
cardService.insertOne(cardDO);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ public interface CardService {
|
||||
/**
|
||||
* 余额变动
|
||||
*/
|
||||
Boolean recharge(BigDecimal money, String flag,BigDecimal giftMoney,Long userId);
|
||||
void recharge(BigDecimal money, String flag,BigDecimal giftMoney,Long userId);
|
||||
|
||||
/**
|
||||
* 获取余额
|
||||
@ -82,20 +82,15 @@ public interface CardService {
|
||||
BigDecimal getMoneyByUserId(Long userId);
|
||||
|
||||
CardDO getCardDoByUserId(Long userId);
|
||||
/**
|
||||
* 批量扣款
|
||||
*/
|
||||
void insertBatch(List<CardDO> list);
|
||||
|
||||
/**
|
||||
* 批量扣款
|
||||
*/
|
||||
void insertOne(CardDO cardDO);
|
||||
|
||||
|
||||
AppCardMonthVO getMonthMoney(Long userId,String flag, String time);
|
||||
|
||||
Boolean rechargeByAdmin(RechargeVO vo);
|
||||
void rechargeByAdmin(RechargeVO vo);
|
||||
|
||||
FaceVo getFaceData(String faceId);
|
||||
|
||||
|
@ -24,6 +24,9 @@ import cn.iocoder.yudao.module.member.dal.mysql.rechargelog.RechargeLogMapper;
|
||||
import cn.iocoder.yudao.module.member.dal.mysql.refund.IntegralRefundMapper;
|
||||
import cn.iocoder.yudao.module.member.dal.mysql.refundlog.RefundLogMapper;
|
||||
import cn.iocoder.yudao.module.member.enums.CostTypeEnum;
|
||||
import cn.iocoder.yudao.module.member.service.amount.AmountService;
|
||||
import cn.iocoder.yudao.module.member.service.amount.CashRechargeService;
|
||||
import cn.iocoder.yudao.module.member.service.amount.WxRechargeService;
|
||||
import cn.iocoder.yudao.module.member.util.MemberConstants;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
@ -75,6 +78,15 @@ public class CardServiceImpl implements CardService {
|
||||
@Resource
|
||||
private RechargeLogMapper rechargeLogMapper;
|
||||
|
||||
@Resource
|
||||
private AmountService amountService;
|
||||
|
||||
@Resource
|
||||
private WxRechargeService wxRechargeService;
|
||||
|
||||
@Resource
|
||||
private CashRechargeService cashRechargeService;
|
||||
|
||||
@Override
|
||||
public Long createCard(CardSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
@ -124,77 +136,18 @@ public class CardServiceImpl implements CardService {
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean recharge(BigDecimal money, String flag, BigDecimal giftMoney,Long userId) {
|
||||
//获取最新余额
|
||||
CardDO lastCardDO = getLastCardDO(userId);
|
||||
CardDO cardDO = new CardDO();
|
||||
cardDO.setUserId(userId);
|
||||
cardDO.setFlag(flag);
|
||||
cardDO.setChangeMoney(money);
|
||||
cardDO.setType(CostTypeEnum.WX_PAY.getCode());
|
||||
BigDecimal oldMoney = BigDecimal.ZERO;
|
||||
BigDecimal wxOldMoney = BigDecimal.ZERO;
|
||||
BigDecimal giftOldMoney = BigDecimal.ZERO;
|
||||
BigDecimal cashAmount = BigDecimal.ZERO;
|
||||
if (ObjectUtil.isNotEmpty(lastCardDO) && lastCardDO.getMoney() != null) {
|
||||
oldMoney = lastCardDO.getMoney();
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(lastCardDO) && lastCardDO.getWxAmount() != null) {
|
||||
wxOldMoney = lastCardDO.getWxAmount();
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(lastCardDO) && lastCardDO.getGiftAmount() != null) {
|
||||
giftOldMoney = lastCardDO.getGiftAmount();
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(lastCardDO) && lastCardDO.getGiftAmount() != null) {
|
||||
cashAmount = lastCardDO.getCashAmount();
|
||||
}
|
||||
|
||||
cardDO.setMoney(oldMoney.add(money).setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
cardDO.setCashAmount(cashAmount);
|
||||
cardDO.setGiftAmount(giftOldMoney);
|
||||
cardDO.setWxAmount(wxOldMoney);
|
||||
if (cardDO.getMoney().compareTo(BigDecimal.ZERO) > 0) {
|
||||
BigDecimal wxAmount = money;
|
||||
if (giftMoney != null) {
|
||||
cardDO.setGiftAmount(giftOldMoney.add(giftMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
wxAmount = wxAmount.subtract(giftMoney);
|
||||
}
|
||||
if (oldMoney.compareTo(BigDecimal.ZERO) < 0) {
|
||||
if (wxAmount.add(oldMoney).compareTo(BigDecimal.ZERO) >= 0) {
|
||||
cardDO.setWxAmount(wxAmount.add(oldMoney));
|
||||
} else {
|
||||
BigDecimal left = wxAmount.add(oldMoney);
|
||||
cardDO.setWxAmount(BigDecimal.ZERO);
|
||||
cardDO.setGiftAmount(giftMoney.add(left));
|
||||
}
|
||||
} else {
|
||||
cardDO.setWxAmount(wxOldMoney.add(wxAmount).setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
}
|
||||
}
|
||||
//处理未完全支付订单
|
||||
handleOrderWx(cardDO.getUserId(),money,giftMoney);
|
||||
|
||||
int i = cardMapper.insert(cardDO);
|
||||
return i > 0;
|
||||
public void recharge(BigDecimal money, String flag, BigDecimal giftMoney,Long userId) {
|
||||
wxRechargeService.wxRecharge(money, money.subtract(giftMoney), giftMoney,userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal getMoney() {
|
||||
CardDO lastCardDO = getLastCardDO();
|
||||
if (ObjectUtil.isEmpty(lastCardDO) || lastCardDO.getMoney() == null) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
return lastCardDO.getMoney();
|
||||
return amountService.getAmount( SecurityFrameworkUtils.getLoginUserId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigDecimal getMoneyByUserId(Long userId) {
|
||||
CardDO lastCardDO = cardMapper.selectOne(Wrappers.<CardDO>lambdaQuery().eq(CardDO::getUserId, userId)
|
||||
.orderByDesc(CardDO::getCreateTime).orderByDesc(CardDO::getId).last(MemberConstants.LIMIT_ONE));
|
||||
if (lastCardDO == null || lastCardDO.getMoney() == null) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
return lastCardDO.getMoney();
|
||||
return amountService.getAmount(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -205,33 +158,6 @@ public class CardServiceImpl implements CardService {
|
||||
return lastCardDO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户最新余额明细
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public CardDO getLastCardDO() {
|
||||
CardDO lastCardDO = cardMapper.selectOne(Wrappers.<CardDO>lambdaQuery().eq(CardDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
|
||||
.orderByDesc(CardDO::getCreateTime)
|
||||
.orderByDesc(CardDO::getId)
|
||||
.last(MemberConstants.LIMIT_ONE));
|
||||
return lastCardDO;
|
||||
}
|
||||
|
||||
public CardDO getLastCardDO(Long userId) {
|
||||
CardDO lastCardDO = cardMapper.selectOne(Wrappers.<CardDO>lambdaQuery().eq(CardDO::getUserId,userId)
|
||||
.orderByDesc(CardDO::getCreateTime)
|
||||
.orderByDesc(CardDO::getId)
|
||||
.last(MemberConstants.LIMIT_ONE));
|
||||
return lastCardDO;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void insertBatch(List<CardDO> list) {
|
||||
cardMapper.insertBatch(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertOne(CardDO cardDO) {
|
||||
cardMapper.insert(cardDO);
|
||||
@ -263,62 +189,8 @@ public class CardServiceImpl implements CardService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Boolean rechargeByAdmin(RechargeVO vo) {
|
||||
List<Long> memberList = new ArrayList<>();
|
||||
if (vo.getGroupId() != null) {
|
||||
memberList.addAll(memberGroupMapper.getMemberList(vo.getGroupId()));
|
||||
} else {
|
||||
if (CollectionUtil.isEmpty(vo.getUserIds())) {
|
||||
throw new ServiceException(NEED_ADD_USER);
|
||||
}
|
||||
memberList.addAll(vo.getUserIds());
|
||||
}
|
||||
|
||||
List<CardDO> cardDOS = cardMapper.selectMoneyList(memberList);
|
||||
ArrayList<CardDO> addList = new ArrayList<>();
|
||||
List<Long> cardUserIds = cardDOS.stream().map(card -> card.getUserId()).collect(Collectors.toList());
|
||||
|
||||
List<Long> newIds = memberList.stream()
|
||||
.filter(id -> !cardUserIds.contains(id))
|
||||
.collect(Collectors.toList());
|
||||
//重新计算已有的金额
|
||||
for (CardDO cardDO : cardDOS) {
|
||||
CardDO add = new CardDO();
|
||||
add.setUserId(cardDO.getUserId());
|
||||
add.setFlag(CardDO.ADD);
|
||||
add.setChangeMoney(vo.getMoney());
|
||||
add.setType(CostTypeEnum.ADMIN_PAY.getCode());
|
||||
BigDecimal oldMoney = cardDO.getMoney();
|
||||
add.setMoney(oldMoney.add(vo.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
|
||||
if (add.getMoney().compareTo(BigDecimal.ZERO) > 0) {
|
||||
BigDecimal cashOldMoney = cardDO.getCashAmount();
|
||||
add.setCashAmount(cashOldMoney.add(add.getChangeMoney()).setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
add.setWxAmount(cardDO.getWxAmount());
|
||||
add.setGiftAmount(cardDO.getGiftAmount());
|
||||
}
|
||||
addList.add(add);
|
||||
|
||||
}
|
||||
|
||||
//添加新的用户金额
|
||||
if (CollectionUtil.isNotEmpty(newIds)) {
|
||||
for (Long id : newIds) {
|
||||
CardDO add = new CardDO();
|
||||
add.setUserId(id);
|
||||
add.setFlag(CardDO.ADD);
|
||||
add.setChangeMoney(vo.getMoney());
|
||||
add.setType(CostTypeEnum.ADMIN_PAY.getCode());
|
||||
add.setMoney(vo.getMoney());
|
||||
add.setCashAmount(vo.getMoney());
|
||||
addList.add(add);
|
||||
}
|
||||
}
|
||||
//处理订单
|
||||
handleOrderCash(memberList,vo.getMoney());
|
||||
|
||||
//批量添加
|
||||
return cardMapper.insertBatch(addList);
|
||||
public void rechargeByAdmin(RechargeVO vo) {
|
||||
cashRechargeService.rechargeByAdmin(vo.getUserIds(),vo.getGroupId(),vo.getMoney());
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user