Commit f79e9257 by fanjiaxin

联调问题处理

parent c93fb097
Pipeline #71623 passed with stages
in 1 minute 6 seconds
......@@ -8,7 +8,9 @@ package com.netease.yanxuan.wx.store.sharer.biz.config;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
......@@ -28,8 +30,6 @@ import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopSe
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopSetSharerProductCommissionRequest;
import com.netease.yanxuan.wx.store.sharer.integration.handler.impl.WeChatShopSharerListRequest;
import com.netease.yanxuan.wx.store.sharer.integration.meta.enums.CommissionTypeEnum;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatSharerInfoVO;
import com.netease.yanxuan.wx.store.sharer.integration.meta.model.vo.WeChatSharerListVO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -63,7 +63,7 @@ public class DrmSharerConfig {
private final WeChatShopSetSharerCommissionRequest weChatShopSetSharerCommissionRequest;
private final WeChatShopSetSharerProductCommissionRequest weChatShopSetSharerProductCommissionRequest;
private final Map<String, String> sharerAppidMap = new ConcurrentHashMap<>(0);
private final Map<String, SharerInfo> sharerInfoMap = new ConcurrentHashMap<>(0);
/**
* 默认分佣比例
......@@ -121,23 +121,18 @@ public class DrmSharerConfig {
if (StringUtils.isBlank(change.getNewValue()) || change.getNewValue().equals(change.getOldValue())) {
return;
}
// 获取推客绑定的所有用户
String nextKey = null;
int pageNum = 1;
int pageSize = 10;
Page<SharerInfo> sharerInfoPage;
while (true) {
WeChatSharerListVO sharerListVO = weChatShopSharerListRequest.handle(null, nextKey, pageSize);
if (sharerListVO == null) {
break; // 如果返回值为空,直接终止循环
sharerInfoPage = sharerInfoMapper.selectPage(new Page<>(pageNum++, pageSize), null);
if (null == sharerInfoPage || CollectionUtils.isEmpty(sharerInfoPage.getRecords())) {
// 如果返回值为空,直接终止循环
break;
}
nextKey = sharerListVO.getNext_key();
if (!CollectionUtils.isEmpty(sharerListVO.getSharer_info_list())) {
for (WeChatSharerInfoVO sharerInfoVO : sharerListVO.getSharer_info_list()) {
// 处理每个 sharerInfoVO
setSharerDefaultCommissionInfo(change, sharerInfoVO);
}
}
if (CollectionUtils.isEmpty(sharerListVO.getSharer_info_list()) || sharerListVO.getSharer_info_list().size() < pageSize) {
break; // 如果列表为空或小于 pageSize,终止循环
for (SharerInfo sharerInfo : sharerInfoPage.getRecords()) {
// 处理每个 sharerInfoVO
setSharerDefaultCommissionInfo(change, sharerInfo);
}
}
}
......@@ -145,7 +140,7 @@ public class DrmSharerConfig {
/**
* 设置推客的的分佣类型和比例信息
*/
private void setSharerDefaultCommissionInfo(ConfigChange change, WeChatSharerInfoVO sharerInfoVO) {
private void setSharerDefaultCommissionInfo(ConfigChange change, SharerInfo sharerInfo) {
// 分佣比例为小数, 微信接口参数应当分佣比例*倍数 为整数
BigDecimal commissionRatio = new BigDecimal(change.getNewValue())
.multiply(new BigDecimal(WeChatApi.WECHAT_COMMISSION_RATIO_MULTIPLIER))
......@@ -154,13 +149,16 @@ public class DrmSharerConfig {
boolean isSuccess = false;
try {
// 设置推客分佣比例
weChatShopSetSharerCommissionRequest.handle(sharerInfoVO.getSharer_appid(), commissionRatio.longValue());
if (StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
weChatShopSetSharerCommissionRequest.handle(sharerInfo.getSharerAppid(), commissionRatio.longValue());
}
updateSharerInfo(sharerInfo, change.getNewValue());
isSuccess = true;
} catch (Exception e) {
log.error("设置推客的的分佣类型和比例信息失败", e);
} finally {
// 保存记录
saveSharerProductCommissionRecord(sharerInfoVO.getSharer_appid(), null,
saveSharerProductCommissionRecord(sharerInfo.getOpenId(), sharerInfo.getSharerAppid(), null,
new BigDecimal(change.getOldValue()), new BigDecimal(change.getNewValue()),
CommissionChangeOptTypeEnum.DEFAULT_CHANGE, isSuccess);
}
......@@ -207,9 +205,8 @@ public class DrmSharerConfig {
*/
private void setSharerCommissionInfo(CommissionSharerBO newCommissionSharer,
CommissionSharerBO oldCommissionSharer) {
String sharerAppid = getSharerAppidByOpenId(newCommissionSharer.getOpenId());
if (StringUtils.isBlank(sharerAppid)) {
log.error("微信用户暂未绑定微信小店,openId={}", newCommissionSharer.getOpenId());
SharerInfo sharerInfo = getSharerInfoByOpenId(newCommissionSharer.getOpenId());
if (null == sharerInfo) {
return;
}
// 分佣比例为小数, 微信接口参数应当分佣比例*1000 为整数
......@@ -219,14 +216,19 @@ public class DrmSharerConfig {
// 设置推客分佣比例
boolean isSuccess = false;
try {
// 设置推客分佣比例
weChatShopSetSharerCommissionRequest.handle(sharerAppid, commissionRatio.longValue());
if (StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
// 设置推客分佣比例
weChatShopSetSharerCommissionRequest.handle(sharerInfo.getSharerAppid(), commissionRatio.longValue());
}
// 更新推客分佣比例
updateSharerInfo(sharerInfo, newCommissionSharer.getCommissionRatio()
.setScale(2, RoundingMode.HALF_UP).toPlainString());
isSuccess = true;
} catch (Exception e) {
log.error("设置推客的的分佣类型和比例信息失败", e);
} finally {
// 保存记录
saveSharerProductCommissionRecord(sharerAppid, null,
saveSharerProductCommissionRecord(sharerInfo.getOpenId(), sharerInfo.getSharerAppid(), null,
oldCommissionSharer.getCommissionRatio(), newCommissionSharer.getCommissionRatio(),
CommissionChangeOptTypeEnum.SET, isSuccess);
}
......@@ -236,27 +238,30 @@ public class DrmSharerConfig {
* 删除推客的的分佣类型和比例信息
*/
private void deleteSharerCommissionInfo(CommissionSharerBO oldCommissionSharer) {
String sharerAppid = getSharerAppidByOpenId(oldCommissionSharer.getOpenId());
if (StringUtils.isBlank(sharerAppid)) {
log.error("微信用户暂未绑定微信小店,openId={}", oldCommissionSharer.getOpenId());
SharerInfo sharerInfo = getSharerInfoByOpenId(oldCommissionSharer.getOpenId());
if (null == sharerInfo) {
return;
}
// 分佣比例为小数, 微信接口参数应当分佣比例*倍数 为整数
BigDecimal sharerCommissionRatio = getSharerCommissionRatio(oldCommissionSharer.getOpenId(), null);
BigDecimal sharerCommissionRatio = getSharerCommissionRatio(oldCommissionSharer.getOpenId());
BigDecimal commissionRatio = sharerCommissionRatio
.multiply(new BigDecimal(WeChatApi.WECHAT_COMMISSION_RATIO_MULTIPLIER))
.setScale(0, RoundingMode.DOWN);
// 设置推客分佣比例
boolean isSuccess = false;
try {
// 设置推客分佣比例
weChatShopSetSharerCommissionRequest.handle(sharerAppid, commissionRatio.longValue());
if (StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
// 设置推客分佣比例
weChatShopSetSharerCommissionRequest.handle(sharerInfo.getSharerAppid(), commissionRatio.longValue());
}
// 更新推客分佣比例
updateSharerInfo(sharerInfo, sharerCommissionRatio.setScale(2, RoundingMode.HALF_UP).toPlainString());
isSuccess = true;
} catch (Exception e) {
log.error("设置推客的的分佣类型和比例信息失败", e);
} finally {
// 保存记录
saveSharerProductCommissionRecord(sharerAppid, null,
saveSharerProductCommissionRecord(sharerInfo.getOpenId(), sharerInfo.getSharerAppid(), null,
oldCommissionSharer.getCommissionRatio(), sharerCommissionRatio,
CommissionChangeOptTypeEnum.UNSET, isSuccess);
}
......@@ -284,18 +289,10 @@ public class DrmSharerConfig {
// 修改
if (newInfo.getCommissionRatio().compareTo(oldInfo.getCommissionRatio()) != 0) {
setSharerProductCommissionInfo(newInfo, oldInfo);
// 保存推客商品记录
saveSharerProductCommissionInfo(newInfo.getOpenId(), newInfo.getProductId(),
newInfo.getCommissionRatio(), SharerProductCommission.UnsetEnum.SET);
}
} else {
// 删除
deleteSharerProductCommissionInfo(oldInfo);
// 获取推客的分佣比例
BigDecimal sharerCommissionRatio = getSharerCommissionRatio(oldInfo.getOpenId(), null);
// 保存推客商品记录
saveSharerProductCommissionInfo(oldInfo.getOpenId(), oldInfo.getProductId(),
sharerCommissionRatio, SharerProductCommission.UnsetEnum.UNSET);
}
}
for (CommissionSharerProductBO newInfo : newCommissionSharerList) {
......@@ -303,32 +300,10 @@ public class DrmSharerConfig {
if (oldCommissionSharerList.stream().noneMatch(item -> item.getOpenId().equals(newInfo.getOpenId())
&& item.getProductId().equals(newInfo.getProductId()))) {
setSharerProductCommissionInfo(newInfo, null);
// 保存推客商品记录
saveSharerProductCommissionInfo(newInfo.getOpenId(), newInfo.getProductId(),
newInfo.getCommissionRatio(), SharerProductCommission.UnsetEnum.SET);
}
}
}
/**
* 根据 openId 获取推客的 SharerAppid
*/
private String getSharerAppidByOpenId(String openId) {
String sharerAppid = sharerAppidMap.get(openId);
if (StringUtils.isBlank(sharerAppid)) {
LambdaQueryWrapper<SharerInfo> sharerInfoLqw = Wrappers.lambdaQuery();
sharerInfoLqw.eq(SharerInfo::getOpenId, openId);
sharerInfoLqw.last("LIMIT 1");
SharerInfo sharerInfo = sharerInfoMapper.selectOne(sharerInfoLqw);
if (null != sharerInfo && StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
sharerAppid = sharerInfo.getSharerAppid();
sharerAppidMap.put(openId, sharerAppid);
}
}
return sharerAppid;
}
private String getSharerProductCommissionMapKey(CommissionSharerProductBO bo) {
return bo.getOpenId() + "&" + bo.getProductId();
}
......@@ -347,6 +322,13 @@ public class DrmSharerConfig {
// 返回推客商品分佣
return sharerProductCommissionOpt.get().getCommissionRatio();
}
return getSharerCommissionRatio(openId);
}
/**
* 获取推客分佣比例
*/
private BigDecimal getSharerCommissionRatio(String openId) {
Optional<CommissionSharerBO> sharerCommissionOpt = Optional.ofNullable(commissionSharerList)
.orElseGet(ArrayList::new)
.stream()
......@@ -365,9 +347,8 @@ public class DrmSharerConfig {
*/
private void setSharerProductCommissionInfo(CommissionSharerProductBO newBo,
CommissionSharerProductBO oldBo) {
String sharerAppid = getSharerAppidByOpenId(newBo.getOpenId());
if (StringUtils.isBlank(sharerAppid)) {
log.error("微信用户暂未绑定微信小店,openId={}", newBo.getOpenId());
SharerInfo sharerInfo = getSharerInfoByOpenId(newBo.getOpenId());
if (null == sharerInfo) {
return;
}
// 分佣比例为小数, 微信接口参数应当分佣比例*倍数 为整数
......@@ -377,16 +358,22 @@ public class DrmSharerConfig {
// 设置推客分佣比例
boolean isSuccess = false;
try {
// 设置推客分佣比例
weChatShopSetSharerProductCommissionRequest.handle(sharerAppid,
Long.valueOf(newBo.getProductId()),
commissionRatio.longValue());
if (StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
// 设置推客分佣比例
weChatShopSetSharerProductCommissionRequest.handle(sharerInfo.getSharerAppid(),
Long.valueOf(newBo.getProductId()),
commissionRatio.longValue());
}
// 保存推客商品记录
saveSharerProductCommissionInfo(sharerInfo.getOpenId(), newBo.getProductId(),
commissionRatio, SharerProductCommission.UnsetEnum.SET);
isSuccess = true;
} catch (Exception e) {
log.error("设置推客的的分佣类型和比例信息失败", e);
} finally {
// 保存记录
saveSharerProductCommissionRecord(sharerAppid, oldBo.getProductId(),
saveSharerProductCommissionRecord(sharerInfo.getOpenId(),
sharerInfo.getSharerAppid(), oldBo.getProductId(),
oldBo.getCommissionRatio(), newBo.getCommissionRatio(),
CommissionChangeOptTypeEnum.SET, isSuccess);
}
......@@ -396,23 +383,27 @@ public class DrmSharerConfig {
* 删除推客的的分佣类型和比例信息
*/
private void deleteSharerProductCommissionInfo(CommissionSharerProductBO oldBo) {
String sharerAppid = getSharerAppidByOpenId(oldBo.getOpenId());
if (StringUtils.isBlank(sharerAppid)) {
log.error("微信用户暂未绑定微信小店,openId={}", oldBo.getOpenId());
return;
}
SharerInfo sharerInfo = getSharerInfoByOpenId(oldBo.getOpenId());
// 设置推客分佣比例
boolean isSuccess = false;
try {
// 设置推客分佣比例
weChatShopSetSharerProductCommissionRequest.handle(sharerAppid,
Long.valueOf(oldBo.getProductId()), null);
if (StringUtils.isNotBlank(sharerInfo.getSharerAppid())) {
// 设置推客分佣比例
weChatShopSetSharerProductCommissionRequest.handle(sharerInfo.getSharerAppid(),
Long.valueOf(oldBo.getProductId()), null);
}
// 获取推客的分佣比例
BigDecimal sharerCommissionRatio = getSharerCommissionRatio(sharerInfo.getOpenId(), null);
// 保存推客商品记录
saveSharerProductCommissionInfo(sharerInfo.getOpenId(), oldBo.getProductId(),
sharerCommissionRatio, SharerProductCommission.UnsetEnum.UNSET);
isSuccess = true;
} catch (Exception e) {
log.error("设置推客的的分佣类型和比例信息失败", e);
} finally {
// 保存记录
saveSharerProductCommissionRecord(sharerAppid, oldBo.getProductId(),
saveSharerProductCommissionRecord(sharerInfo.getOpenId(),
sharerInfo.getSharerAppid(), oldBo.getProductId(),
oldBo.getCommissionRatio(), null,
CommissionChangeOptTypeEnum.UNSET, isSuccess);
}
......@@ -422,20 +413,9 @@ public class DrmSharerConfig {
/**
* 保存推客商品分佣记录
*/
private void saveSharerProductCommissionRecord(String sharerAppid, String productId,
BigDecimal oldCommissionRatio,
BigDecimal newCommissionRatio,
CommissionChangeOptTypeEnum optTypeEnum,
boolean isSuccess) {
String openId = null;
LambdaQueryWrapper<SharerInfo> sharerInfoLqw = Wrappers.lambdaQuery();
sharerInfoLqw.select(SharerInfo::getOpenId);
sharerInfoLqw.eq(SharerInfo::getSharerAppid, sharerAppid);
sharerInfoLqw.last("LIMIT 1");
SharerInfo sharerInfo = sharerInfoMapper.selectOne(sharerInfoLqw);
if (null != sharerInfo && StringUtils.isNotBlank(sharerInfo.getOpenId())) {
openId = sharerInfo.getOpenId();
}
private void saveSharerProductCommissionRecord(String openId, String sharerAppid, String productId,
BigDecimal oldCommissionRatio, BigDecimal newCommissionRatio,
CommissionChangeOptTypeEnum optTypeEnum, boolean isSuccess) {
Date now = new Date();
SharerProductCommissionRecord record = new SharerProductCommissionRecord();
record.setOpenId(openId);
......@@ -487,4 +467,63 @@ public class DrmSharerConfig {
sharerProductCommissionMapper.updateById(sharerProductCommission);
}
}
/**
* 根据 openId 获取推客信息
*/
private SharerInfo getSharerInfoByOpenId(String openId) {
SharerInfo sharerInfo = sharerInfoMap.get(openId);
if (null == sharerInfo) {
LambdaQueryWrapper<SharerInfo> sharerInfoLqw = Wrappers.lambdaQuery();
sharerInfoLqw.eq(SharerInfo::getOpenId, openId);
sharerInfoLqw.last("LIMIT 1");
sharerInfo = sharerInfoMapper.selectOne(sharerInfoLqw);
if (null != sharerInfo) {
sharerInfoMap.put(openId, sharerInfo);
}
}
return sharerInfo;
}
/**
* 根据 openId 获取推客的 SharerAppid
*/
private String getSharerAppidByOpenId(String openId) {
SharerInfo sharerInfo = sharerInfoMap.get(openId);
if (null == sharerInfo) {
LambdaQueryWrapper<SharerInfo> sharerInfoLqw = Wrappers.lambdaQuery();
sharerInfoLqw.eq(SharerInfo::getOpenId, openId);
sharerInfoLqw.last("LIMIT 1");
sharerInfo = sharerInfoMapper.selectOne(sharerInfoLqw);
if (null != sharerInfo) {
sharerInfoMap.put(openId, sharerInfo);
}
}
return null != sharerInfo ? sharerInfo.getSharerAppid() : null;
}
private String getOpenIdBySharerAppid(String sharerAppid) {
LambdaQueryWrapper<SharerInfo> sharerInfoLqw = Wrappers.lambdaQuery();
sharerInfoLqw.select(SharerInfo::getOpenId);
sharerInfoLqw.eq(SharerInfo::getSharerAppid, sharerAppid);
sharerInfoLqw.last("LIMIT 1");
SharerInfo sharerInfo = sharerInfoMapper.selectOne(sharerInfoLqw);
if (null != sharerInfo && StringUtils.isNotBlank(sharerInfo.getOpenId())) {
return sharerInfo.getOpenId();
}
return null;
}
private void updateSharerInfo(SharerInfo sharerInfo, String commissionRatio) {
String openId = sharerInfo.getOpenId();
if (StringUtils.isNotBlank(openId)) {
BigDecimal sharerCommissionRatio = getSharerCommissionRatio(openId);
commissionRatio = sharerCommissionRatio.setScale(2, RoundingMode.HALF_UP).toPlainString();
}
LambdaUpdateWrapper<SharerInfo> sharerInfoLuw = Wrappers.lambdaUpdate();
sharerInfoLuw.set(SharerInfo::getCommissionRatio, commissionRatio);
sharerInfoLuw.set(SharerInfo::getUpdateTime, new Date());
sharerInfoLuw.eq(SharerInfo::getId, sharerInfo.getId());
sharerInfoMapper.update(null, sharerInfoLuw);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment