From 9eb7837b67c0ed8b9d4e85efac33372a0a79ae27 Mon Sep 17 00:00:00 2001 From: owen Date: Tue, 5 Sep 2023 19:43:07 +0800 Subject: [PATCH] =?UTF-8?q?member:=20=E4=BD=A3=E9=87=91=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/TradeOrderUpdateServiceImpl.java | 9 ++++-- .../api/brokerage/BrokerageApiImpl.java | 2 +- .../record/MemberBrokerageRecordMapper.java | 5 +++ .../dal/mysql/user/MemberUserMapper.java | 32 ++++++++++++++++++- .../record/MemberBrokerageRecordService.java | 10 ++++-- .../MemberBrokerageRecordServiceImpl.java | 26 ++++++++++++++- .../service/user/MemberUserService.java | 8 +++++ .../service/user/MemberUserServiceImpl.java | 22 ++++++++++--- 8 files changed, 102 insertions(+), 12 deletions(-) diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index afb125e08..e2a0f6baf 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java @@ -635,12 +635,12 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { .setRefundStatus(TradeOrderRefundStatusEnum.PART.getStatus()).setRefundPrice(orderRefundPrice)); } - // TODO 芋艿:未来如果有分佣,需要更新相关分佣订单为已失效 - // 扣减用户积分 getSelf().reduceUserPointAsync(order.getUserId(), orderRefundPrice, afterSaleId); // 扣减用户经验 getSelf().reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, afterSaleId); + // 更新分佣记录为已失效 + getSelf().cancelBrokerageAsync(order.getUserId(), id); } @Override @@ -757,6 +757,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { brokerageApi.addBrokerage(userId, list); } + @Async + protected void cancelBrokerageAsync(Long userId, Long orderItemId) { + brokerageApi.cancelBrokerage(userId, String.valueOf(orderItemId)); + } + /** * 获得自身的代理对象,解决 AOP 生效问题 * diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/brokerage/BrokerageApiImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/brokerage/BrokerageApiImpl.java index 2891bdcc1..662c5165a 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/brokerage/BrokerageApiImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/brokerage/BrokerageApiImpl.java @@ -27,6 +27,6 @@ public class BrokerageApiImpl implements BrokerageApi { @Override public void cancelBrokerage(Long userId, String bizId) { - + memberBrokerageRecordService.cancelBrokerage(userId, bizId); } } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/brokerage/record/MemberBrokerageRecordMapper.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/brokerage/record/MemberBrokerageRecordMapper.java index fd3ce05fc..52154aea5 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/brokerage/record/MemberBrokerageRecordMapper.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/dal/mysql/brokerage/record/MemberBrokerageRecordMapper.java @@ -39,4 +39,9 @@ public interface MemberBrokerageRecordMapper extends BaseMapperX { update(null, lambdaUpdateWrapper); } + /** + * 更新用户可用佣金(减少) + * 注意:理论上佣金可能已经提现,这时会扣出负数,确保平台不会造成损失 + * + * @param id 用户编号 + * @param incrCount 增加佣金(负数) + */ + default void updateBrokeragePriceDecr(Long id, int incrCount) { + Assert.isTrue(incrCount < 0); + LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper() + .setSql(" brokerage_price = brokerage_price + " + incrCount) // 负数,所以使用 + 号 + .eq(MemberUserDO::getId, id); + update(null, lambdaUpdateWrapper); + } + /** * 更新用户冻结佣金(增加) * @@ -94,12 +109,27 @@ public interface MemberUserMapper extends BaseMapperX { /** * 更新用户冻结佣金(减少) + * 注意:理论上冻结佣金可能已经解冻,这时会扣出负数,确保平台不会造成损失 + * + * @param id 用户编号 + * @param incrCount 减少冻结佣金(负数) + */ + default void updateFrozenBrokeragePriceDecr(Long id, int incrCount) { + Assert.isTrue(incrCount < 0); + LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper() + .setSql(" frozen_brokerage_price = frozen_brokerage_price + " + incrCount) // 负数,所以使用 + 号 + .eq(MemberUserDO::getId, id); + update(null, lambdaUpdateWrapper); + } + + /** + * 更新用户冻结佣金(减少), 更新用户佣金(增加) * * @param id 用户编号 * @param incrCount 减少冻结佣金(负数) * @return 更新条数 */ - default int updateFrozenBrokeragePriceDecr(Long id, int incrCount) { + default int updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int incrCount) { Assert.isTrue(incrCount < 0); LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper() .setSql(" frozen_brokerage_price = frozen_brokerage_price + " + incrCount + // 负数,所以使用 + 号 diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordService.java index 1d0f4e816..cd966e670 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordService.java @@ -33,16 +33,22 @@ public interface MemberBrokerageRecordService { /** * 增加佣金 * - * @param userId 会员ID + * @param userId 会员编号 * @param list 请求参数列表 */ void addBrokerage(Long userId, List list); + /** + * 取消佣金:将佣金记录,状态修改为已失效 + * @param userId 会员编号 + * @param bizId 业务编号 + */ + void cancelBrokerage(Long userId, String bizId); + /** * 解冻佣金:将待结算的佣金记录,状态修改为已结算 * * @return 解冻佣金的数量 */ int unfreezeRecord(); - } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordServiceImpl.java index c3d6bcebc..32656910c 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/brokerage/record/MemberBrokerageRecordServiceImpl.java @@ -13,6 +13,7 @@ import cn.iocoder.yudao.module.member.dal.dataobject.brokerage.record.MemberBrok import cn.iocoder.yudao.module.member.dal.dataobject.point.MemberPointConfigDO; import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO; import cn.iocoder.yudao.module.member.dal.mysql.brokerage.record.MemberBrokerageRecordMapper; +import cn.iocoder.yudao.module.member.enums.brokerage.BrokerageRecordBizTypeEnum; import cn.iocoder.yudao.module.member.enums.brokerage.BrokerageRecordStatusEnum; import cn.iocoder.yudao.module.member.service.point.MemberPointConfigService; import cn.iocoder.yudao.module.member.service.user.MemberUserService; @@ -85,6 +86,29 @@ public class MemberBrokerageRecordServiceImpl implements MemberBrokerageRecordSe addBrokerage(secondUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageSecondPercent(), BrokerageAddReqDTO::getSkuSecondBrokeragePrice); } + @Override + @Transactional(rollbackFor = Exception.class) + public void cancelBrokerage(Long userId, String bizId) { + MemberBrokerageRecordDO record = memberBrokerageRecordMapper.selectByUserIdAndBizTypeAndBizId(BrokerageRecordBizTypeEnum.ORDER.getType(), bizId); + if (record == null || ObjectUtil.notEqual(record.getUserId(), userId)) { + log.error("[cancelBrokerage][userId({})][bizId({}) 更新为已失效失败:记录不存在]", userId, bizId); + return; + } + + MemberBrokerageRecordDO updateObj = new MemberBrokerageRecordDO().setStatus(BrokerageRecordStatusEnum.CANCEL.getStatus()); + int updateRows = memberBrokerageRecordMapper.updateByIdAndStatus(record.getId(), record.getStatus(), updateObj); + if (updateRows == 0) { + log.error("[cancelBrokerage][record({}) 更新为已失效失败]", record.getId()); + return; + } + + if (BrokerageRecordStatusEnum.WAIT_SETTLEMENT.getStatus().equals(record.getStatus())) { + memberUserService.updateUserFrozenBrokeragePrice(userId, -record.getPrice()); + } else if (BrokerageRecordStatusEnum.SETTLEMENT.getStatus().equals(record.getStatus())) { + memberUserService.updateUserBrokeragePrice(userId, -record.getPrice()); + } + } + /** * 计算佣金 * @@ -190,7 +214,7 @@ public class MemberBrokerageRecordServiceImpl implements MemberBrokerageRecordSe } // 更新用户冻结佣金 - memberUserService.updateUserFrozenBrokeragePrice(record.getUserId(), -record.getPrice()); + memberUserService.updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(record.getUserId(), -record.getPrice()); log.info("[unfreezeRecord][record({}) 更新为已结算成功]", record.getId()); return true; diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index 380ad2c6c..7f599a049 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java @@ -190,4 +190,12 @@ public interface MemberUserService { * @param frozenBrokeragePrice 用户冻结佣金 */ void updateUserFrozenBrokeragePrice(Long id, int frozenBrokeragePrice); + + /** + * 更新用户冻结佣金(减少), 更新用户佣金(增加) + * + * @param id 用户编号 + * @param frozenBrokeragePrice 减少冻结佣金(负数) + */ + void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int frozenBrokeragePrice); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index 0bd17facf..53b452d7c 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.member.service.user; +import cn.hutool.core.lang.Assert; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; @@ -272,7 +273,11 @@ public class MemberUserServiceImpl implements MemberUserService { @Override @Transactional(rollbackFor = Exception.class) public void updateUserBrokeragePrice(Long id, int brokeragePrice) { - memberUserMapper.updateBrokeragePriceIncr(id, brokeragePrice); + if (brokeragePrice > 0) { + memberUserMapper.updateBrokeragePriceIncr(id, brokeragePrice); + } else if (brokeragePrice < 0) { + memberUserMapper.updateBrokeragePriceDecr(id, brokeragePrice); + } } @Override @@ -281,10 +286,17 @@ public class MemberUserServiceImpl implements MemberUserService { if (frozenBrokeragePrice > 0) { memberUserMapper.updateFrozenBrokeragePriceIncr(id, frozenBrokeragePrice); } else if (frozenBrokeragePrice < 0) { - int updateRows = memberUserMapper.updateFrozenBrokeragePriceDecr(id, frozenBrokeragePrice); - if (updateRows == 0) { - throw exception(MEMBER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH); - } + memberUserMapper.updateFrozenBrokeragePriceDecr(id, frozenBrokeragePrice); + } + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int frozenBrokeragePrice) { + Assert.isTrue(frozenBrokeragePrice < 0); + int updateRows = memberUserMapper.updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(id, frozenBrokeragePrice); + if (updateRows == 0) { + throw exception(MEMBER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH); } }