src/main/java/cc/mrbird/febs/mall/mapper/MallMemberWalletMapper.java
@@ -12,5 +12,7 @@ int updateCommissionWithVersion(@Param("record") MallMemberWallet wallet); int updateAmountWithVersion(@Param("record") MallMemberWallet wallet); void updateBalanceWithId(@Param("record")MallMemberWallet mallMemberWallet); } src/main/java/cc/mrbird/febs/mall/service/IApiMallMemberWalletService.java
@@ -14,4 +14,8 @@ void addCommission(BigDecimal amount, Long member); void reduceCommission(BigDecimal amount, Long member); void add(BigDecimal amount, Long memberId, String field); void reduce(BigDecimal amount, Long memberId, String field); } src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberWalletServiceImpl.java
@@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.lang.reflect.Field; import java.math.BigDecimal; /** @@ -88,6 +89,15 @@ } } public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { Field commission = MallMemberWallet.class.getDeclaredField("commission"); MallMemberWallet wallet = new MallMemberWallet(); commission.setAccessible(true); commission.set(wallet, BigDecimal.valueOf(12.34d)); System.out.println(1); } @Override public void reduceCommission(BigDecimal amount, Long memberId) { int i = 0; @@ -112,4 +122,77 @@ } } } @Override public void add(BigDecimal amount, Long memberId, String field) { int i = 0; boolean flag = true; while (flag) { i++; MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId); wallet.setCommission(wallet.getCommission().add(amount)); MallMemberWallet update = new MallMemberWallet(); update.setId(wallet.getId()); update.setRevision(wallet.getRevision()); Field declaredField = null; try { declaredField = MallMemberWallet.class.getDeclaredField(field); declaredField.setAccessible(true); declaredField.set(update, amount); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); throw new FebsException("金额新增失败"); } int result = this.baseMapper.updateAmountWithVersion(wallet); if (result > 0) { flag = false; } else { if (i > 2) { throw new FebsException("金额新增失败"); } } } } @Override public void reduce(BigDecimal amount, Long memberId, String field) { int i = 0; boolean flag = true; while (flag) { i++; MallMemberWallet wallet = this.baseMapper.selectWalletByMemberId(memberId); MallMemberWallet update = new MallMemberWallet(); update.setId(wallet.getId()); update.setRevision(wallet.getRevision()); try { Field declaredField = MallMemberWallet.class.getDeclaredField(field); declaredField.setAccessible(true); BigDecimal balance = (BigDecimal) declaredField.get(wallet); if (amount.compareTo(balance) > 0) { throw new FebsException("余额不足"); } declaredField.set(update, wallet.getCommission().subtract(amount)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } int result = this.baseMapper.updateAmountWithVersion(wallet); if (result > 0) { flag = false; } else { if (i > 2) { throw new FebsException("余额修改失败"); } } } } } src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -209,6 +209,8 @@ MallGoodsSku sku = mallGoodsSkuMapper.selectById(orderItem.getSkuId()); BigDecimal score = sku.getOriginalPrice().multiply(mallGoods.getStaticMultiple()); memberWalletService.add(score, member.getId(), "score"); memberService.addMoneyFlow(member.getId(), score, MoneyFlowTypeEnum.STATIC_BONUS.getValue(), orderInfo.getOrderNo(), null, null, null, null, FlowTypeEnum.SCORE.getValue()); } } @@ -246,7 +248,7 @@ throw new FebsException("支付密码错误"); } memberWalletService.reduceBalance(orderInfo.getAmount(), mallMember.getId()); memberWalletService.reduce(orderInfo.getAmount(), mallMember.getId(), "balance"); return orderInfo.getOrderNo(); } src/main/resources/mapper/modules/MallMemberWalletMapper.xml
@@ -26,4 +26,22 @@ balance = #{record.balance} where id=#{record.id} </update> <update id="updateAmountWithVersion"> update mall_member_wallet set revision = revision + 1 <if test="record.balance != null"> , balance = #{record.balance} </if> <if test="record.score != null"> , score = #{record.score} </if> <if test="record.prizeScore != null"> , prize_score = #{record.prizeScoree} </if> <if test="record.commission != null"> , commission = #{record.commission} </if> where id=#{record.id} and revision=#{record.revision} </update> </mapper>