|
@@ -116,32 +116,6 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
private Boolean isNewWxMerchant;
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FsUserCourseVideoServiceImpl.class);
|
|
|
|
|
|
- /**
|
|
|
- * 红包账户锁
|
|
|
- */
|
|
|
- private static final String REDPACKET_POOL_LOCK = "redpacket_pool_lock";
|
|
|
-
|
|
|
- /**
|
|
|
- * 公司红包金额
|
|
|
- */
|
|
|
- private static final String REDPACKET_COMPANY_MONEY = "redpacket_money";
|
|
|
-
|
|
|
- /**
|
|
|
- * 用户领取红包限制
|
|
|
- */
|
|
|
- private static final String REDPACKET_USER_LIMIT = "redpacket_user_limit:%s:%d";
|
|
|
-
|
|
|
- /**
|
|
|
- * 红包改变记录
|
|
|
- */
|
|
|
- private static final String REDPACKET_COMPANY_MONEY_CHANGE = "redpacket_money_change";
|
|
|
-
|
|
|
- /**
|
|
|
- * 是否开启红包账户扣减
|
|
|
- */
|
|
|
- @Value("${enableRedPackAccount:0}")
|
|
|
- private String ENABLE_RED_PACK_ACCOUNT;
|
|
|
-
|
|
|
private static final String miniappRealLink = "/pages_course/video.html?course=";
|
|
|
private static final String REAL_LINK_PREFIX = "/courseH5/pages/course/learning?course=";
|
|
|
private static final String SHORT_LINK_PREFIX = "/courseH5/pages/course/learning?s=";
|
|
@@ -258,17 +232,7 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
@Autowired
|
|
|
ConfigUtil configUtil;
|
|
|
|
|
|
- @Autowired
|
|
|
- private RedisTemplate<String,BigDecimal> redisTemplate;
|
|
|
|
|
|
- @Autowired
|
|
|
- private RedisTemplate<String,Integer> redisTemplateInteger;
|
|
|
-
|
|
|
- /**
|
|
|
- * 红包领取数量限制 默认一个用户当天最多只能领取10个
|
|
|
- */
|
|
|
- @Value("${RED_PACKET_LIMIT_COUNT:10}")
|
|
|
- private Integer RED_PACKET_LIMIT_COUNT;
|
|
|
|
|
|
/**
|
|
|
* 查询课堂视频
|
|
@@ -1176,23 +1140,46 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
|
|
|
// 准备发送红包参数
|
|
|
WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
- packetParam.setOpenId(user.getMpOpenId());
|
|
|
- // 来源是小程序切换openId
|
|
|
- if (param.getSource() == 2) {
|
|
|
- //处理多小程序问题
|
|
|
+
|
|
|
+ if (user.getMpOpenId()!=null&&!isNewWxMerchant){
|
|
|
+ packetParam.setOpenId(user.getMpOpenId());
|
|
|
+ }else {
|
|
|
+ //修复数据
|
|
|
FsUserWx fsUserWx = fsUserWxService.selectByAppIdAndUserId(param.getAppId(),user.getUserId(),1);
|
|
|
if (fsUserWx ==null){
|
|
|
+ if (user.getCourseMaOpenId()==null){
|
|
|
+ logger.error(" 【转账openId参数错误】:{}", user.getUserId());
|
|
|
+ return R.error("openId参数错误,请清理缓存后重新授权!");
|
|
|
+ }
|
|
|
+ packetParam.setOpenId(user.getCourseMaOpenId());
|
|
|
try {
|
|
|
handleFsUserWx(user,param.getAppId());
|
|
|
}catch (Exception e){
|
|
|
- logger.error("【更新或插入用户与小程序的绑定关系失败】:{}", user.getUserId());
|
|
|
+ logger.error(" 【更新或插入用户与小程序的绑定关系失败】:{}", user.getUserId(),e);
|
|
|
}
|
|
|
+
|
|
|
}else {
|
|
|
packetParam.setOpenId(fsUserWx.getOpenId());
|
|
|
}
|
|
|
- //查出公司绑定openid并赋值
|
|
|
}
|
|
|
|
|
|
+// packetParam.setOpenId(user.getMpOpenId());
|
|
|
+// // 来源是小程序切换openId
|
|
|
+// if (param.getSource() == 2) {
|
|
|
+// //处理多小程序问题
|
|
|
+// FsUserWx fsUserWx = fsUserWxService.selectByAppIdAndUserId(param.getAppId(),user.getUserId(),1);
|
|
|
+// if (fsUserWx ==null){
|
|
|
+// try {
|
|
|
+// handleFsUserWx(user,param.getAppId());
|
|
|
+// }catch (Exception e){
|
|
|
+// logger.error("【更新或插入用户与小程序的绑定关系失败】:{}", user.getUserId());
|
|
|
+// }
|
|
|
+// }else {
|
|
|
+// packetParam.setOpenId(fsUserWx.getOpenId());
|
|
|
+// }
|
|
|
+// //查出公司绑定openid并赋值
|
|
|
+// }
|
|
|
+
|
|
|
//判断服务号配置是否存在
|
|
|
if (StringUtils.isNotEmpty(config.getMpAppId())){
|
|
|
packetParam.setMpAppId(config.getMpAppId());
|
|
@@ -1207,28 +1194,8 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
logger.info("红包金额 {},红包商户号 {}",amount,packetParam);
|
|
|
//2025.6.19 红包金额为0的时候
|
|
|
if (amount.compareTo(BigDecimal.ZERO)>0){
|
|
|
-
|
|
|
- //---------------发红包前先判断润天账户余额是否足够---------
|
|
|
- RLock lock = redissonClient.getLock(REDPACKET_POOL_LOCK);
|
|
|
- try{
|
|
|
- boolean locked = lock.tryLock(3, 10, TimeUnit.SECONDS);
|
|
|
-
|
|
|
- if (!locked) {
|
|
|
- logger.error("获取锁失败");
|
|
|
- return R.error("[红包领取] 系统繁忙,请重试!");
|
|
|
- }
|
|
|
-
|
|
|
- // 发送红包
|
|
|
- return sendRedPacketRewardToUser(param, log, config, packetParam, amount);
|
|
|
-
|
|
|
- }catch (Exception e){
|
|
|
- logger.error("领取红包失败原因:{}", ExceptionUtils.getFullStackTrace(e),e);
|
|
|
- throw new RuntimeException(e);
|
|
|
- }finally {
|
|
|
- if (lock.isHeldByCurrentThread()) {
|
|
|
- lock.unlock();
|
|
|
- }
|
|
|
- }
|
|
|
+ // 发送红包
|
|
|
+ return sendRedPacketRewardToUser(param, log, config, packetParam, amount);
|
|
|
} else {
|
|
|
FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
|
|
|
// 添加红包记录
|
|
@@ -1256,44 +1223,6 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
|
|
|
private R sendRedPacketRewardToUser(FsCourseSendRewardUParam param, FsCourseWatchLog log, CourseConfig config, WxSendRedPacketParam packetParam, BigDecimal amount) {
|
|
|
|
|
|
- // 判断当前用户是否限流
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
- String today = sdf.format(new Date());
|
|
|
- String userLimitKey = String.format(REDPACKET_USER_LIMIT, today, param.getUserId());
|
|
|
- Integer userCount = redisTemplateInteger.opsForValue().get(userLimitKey);
|
|
|
-
|
|
|
- // 首次领取
|
|
|
- if(userCount == null) {
|
|
|
- userCount = 0;
|
|
|
- long expireSeconds = getExpireSeconds();
|
|
|
- redisTemplateInteger.opsForValue().set(userLimitKey, userCount, expireSeconds, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
-
|
|
|
- if(userCount >= RED_PACKET_LIMIT_COUNT){
|
|
|
- logger.info("[红包领取] 用户{} 领取红包已经达到最大限制!",param.getUserId());
|
|
|
- return R.error("[红包领取] 当前用户当前已经领取红包已经达到限制!");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- BigDecimal companyMoney = null;
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- companyMoney = redisTemplate.opsForValue().get(REDPACKET_COMPANY_MONEY);
|
|
|
-
|
|
|
- if(ObjectUtils.isNull(companyMoney)){
|
|
|
- SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("company.money");
|
|
|
- if(ObjectUtils.isNull(sysConfig)){
|
|
|
- throw new IllegalArgumentException("润天公司账户余额不能为空!请检查配置!");
|
|
|
- }
|
|
|
- String configValue = sysConfig.getConfigValue();
|
|
|
- companyMoney = new BigDecimal(configValue);
|
|
|
- logger.info("缓存公司余额为空,从数据库读取 companyMoney: {}",companyMoney);
|
|
|
- }
|
|
|
-
|
|
|
- if (companyMoney.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
- logger.info("润天账户余额: {} 不足!", companyMoney);
|
|
|
- return R.error("[红包领取] 账户余额不足,请联系管理员!");
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
// 发送红包
|
|
|
R sendRedPacket = paymentService.sendRedPacket(packetParam);
|
|
@@ -1319,44 +1248,19 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
redPacketLog.setAmount(amount);
|
|
|
redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
redPacketLog.setPeriodId(param.getPeriodId());
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- redPacketLog.setAccBalanceBefore(companyMoney);
|
|
|
- redPacketLog.setAccBalanceAfter(companyMoney.subtract(amount));
|
|
|
- }
|
|
|
|
|
|
redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
// 更新观看记录的奖励类型
|
|
|
log.setRewardType(config.getRewardType());
|
|
|
courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- // 更新账户余额
|
|
|
- logger.info("[更新账户余额] 当前余额{} 更新后余额{}",companyMoney.toPlainString(),companyMoney.subtract(amount).toPlainString());
|
|
|
-
|
|
|
- companyMoney = companyMoney.subtract(amount);
|
|
|
- redisTemplate.opsForValue().set(REDPACKET_COMPANY_MONEY,companyMoney);
|
|
|
- }
|
|
|
-
|
|
|
- // 用户领取红包次数+1
|
|
|
- redisTemplateInteger.opsForValue().increment(userLimitKey, 1);
|
|
|
-
|
|
|
return sendRedPacket;
|
|
|
} else {
|
|
|
return R.error("奖励发送失败,请联系客服");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private static long getExpireSeconds() {
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
- calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
- calendar.set(Calendar.MINUTE, 0);
|
|
|
- calendar.set(Calendar.SECOND, 0);
|
|
|
- calendar.set(Calendar.MILLISECOND, 0);
|
|
|
- // 计算从现在到明天凌晨的秒数
|
|
|
- long expireSeconds = (calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000;
|
|
|
- return expireSeconds;
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
|
|
|
private void handleFsUserWx(FsUser user, String appId) {
|
|
@@ -1463,112 +1367,43 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
return R.error("服务商余额不足,请联系群主服务器充值!");
|
|
|
}
|
|
|
|
|
|
- //---------------发红包前先判断润天账户余额是否足够---------
|
|
|
- RLock lock = redissonClient.getLock(REDPACKET_POOL_LOCK);
|
|
|
- try{
|
|
|
- boolean locked = lock.tryLock(3, 10, TimeUnit.SECONDS);
|
|
|
-
|
|
|
- if (!locked) {
|
|
|
- logger.error("获取锁失败");
|
|
|
- return R.error("[红包领取] 系统繁忙,请重试!");
|
|
|
- }
|
|
|
-
|
|
|
- // 判断当前用户是否限流
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
- String today = sdf.format(new Date());
|
|
|
- String userLimitKey = String.format(REDPACKET_USER_LIMIT, today, param.getUserId());
|
|
|
- Integer userCount = redisTemplateInteger.opsForValue().get(userLimitKey);
|
|
|
-
|
|
|
- // 首次领取
|
|
|
- if(userCount == null) {
|
|
|
- userCount = 0;
|
|
|
- long expireSeconds = getExpireSeconds();
|
|
|
- redisTemplateInteger.opsForValue().set(userLimitKey, userCount, expireSeconds, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
-
|
|
|
- if(userCount >= RED_PACKET_LIMIT_COUNT){
|
|
|
- logger.info("[红包领取] 用户{} 领取红包已经达到最大限制!",param.getUserId());
|
|
|
- return R.error("[红包领取] 当前用户当前已经领取红包已经达到限制!");
|
|
|
- }
|
|
|
-
|
|
|
- BigDecimal companyMoney = null;
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- companyMoney = redisTemplate.opsForValue().get(REDPACKET_COMPANY_MONEY);
|
|
|
- if(ObjectUtils.isNull(companyMoney)){
|
|
|
- SysConfig sysConfig = sysConfigService.selectConfigByConfigKey("company.money");
|
|
|
- if(ObjectUtils.isNull(sysConfig)){
|
|
|
- throw new IllegalArgumentException("润天公司账户余额不能为空!请检查配置!");
|
|
|
- }
|
|
|
- String configValue = sysConfig.getConfigValue();
|
|
|
- companyMoney = new BigDecimal(configValue);
|
|
|
- logger.info("缓存公司余额为空,从数据库读取 companyMoney: {}",companyMoney);
|
|
|
- }
|
|
|
-
|
|
|
- if (companyMoney.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
- logger.info("润天账户余额: {} 不足!", companyMoney);
|
|
|
- return R.error("[红包领取] 账户余额不足,请联系管理员!");
|
|
|
- }
|
|
|
- }
|
|
|
- // 发送红包
|
|
|
- R sendRedPacket = paymentService.sendRedPacket(packetParam);
|
|
|
- if (sendRedPacket.get("code").equals(200)) {
|
|
|
- FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
|
|
|
- TransferBillsResult transferBillsResult;
|
|
|
- if (sendRedPacket.get("isNew").equals(1)){
|
|
|
- transferBillsResult = (TransferBillsResult)sendRedPacket.get("data");
|
|
|
- redPacketLog.setResult(JSON.toJSONString(sendRedPacket));
|
|
|
- redPacketLog.setOutBatchNo(transferBillsResult.getOutBillNo());
|
|
|
- }else {
|
|
|
- redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
|
|
|
- redPacketLog.setBatchId(sendRedPacket.get("batchId").toString());
|
|
|
- }
|
|
|
- // 添加红包记录
|
|
|
- redPacketLog.setCourseId(param.getCourseId());
|
|
|
- redPacketLog.setCompanyId(param.getCompanyId());
|
|
|
- redPacketLog.setUserId(param.getUserId());
|
|
|
- redPacketLog.setVideoId(param.getVideoId());
|
|
|
- redPacketLog.setStatus(0);
|
|
|
- redPacketLog.setQwUserId(param.getQwUserId() != null ? param.getQwUserId() : null);
|
|
|
- redPacketLog.setCompanyUserId(param.getCompanyUserId());
|
|
|
- redPacketLog.setCreateTime(new Date());
|
|
|
- redPacketLog.setAmount(amount);
|
|
|
- redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
- redPacketLog.setPeriodId(param.getPeriodId());
|
|
|
- redPacketLog.setAppId(param.getAppId());
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- redPacketLog.setAccBalanceBefore(companyMoney);
|
|
|
- redPacketLog.setAccBalanceAfter(companyMoney.subtract(amount));
|
|
|
- }
|
|
|
- redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
-
|
|
|
- // 更新观看记录的奖励类型
|
|
|
- log.setRewardType(config.getRewardType());
|
|
|
- courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
-
|
|
|
-
|
|
|
- if(StringUtils.equals(ENABLE_RED_PACK_ACCOUNT,"1")) {
|
|
|
- // 更新账户余额
|
|
|
- logger.info("[更新账户余额] 当前余额{} 更新后余额{}",companyMoney.toPlainString(),companyMoney.subtract(amount).toPlainString());
|
|
|
-
|
|
|
- companyMoney = companyMoney.subtract(amount);
|
|
|
- redisTemplate.opsForValue().set(REDPACKET_COMPANY_MONEY,companyMoney);
|
|
|
- }
|
|
|
-
|
|
|
- // 用户领取红包次数+1
|
|
|
- redisTemplateInteger.opsForValue().increment(userLimitKey, 1);
|
|
|
- return sendRedPacket;
|
|
|
- } else {
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- logger.error("领取红包失败原因:{}", ExceptionUtils.getFullStackTrace(e),e);
|
|
|
- throw new RuntimeException(e);
|
|
|
- }finally {
|
|
|
- if (lock.isHeldByCurrentThread()) {
|
|
|
- lock.unlock();
|
|
|
+ // 发送红包
|
|
|
+ R sendRedPacket = paymentService.sendRedPacket(packetParam);
|
|
|
+ if (sendRedPacket.get("code").equals(200)) {
|
|
|
+ FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
|
|
|
+ TransferBillsResult transferBillsResult;
|
|
|
+ if (sendRedPacket.get("isNew").equals(1)){
|
|
|
+ transferBillsResult = (TransferBillsResult)sendRedPacket.get("data");
|
|
|
+ redPacketLog.setResult(JSON.toJSONString(sendRedPacket));
|
|
|
+ redPacketLog.setOutBatchNo(transferBillsResult.getOutBillNo());
|
|
|
+ }else {
|
|
|
+ redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
|
|
|
+ redPacketLog.setBatchId(sendRedPacket.get("batchId").toString());
|
|
|
}
|
|
|
+ // 添加红包记录
|
|
|
+ redPacketLog.setCourseId(param.getCourseId());
|
|
|
+ redPacketLog.setCompanyId(param.getCompanyId());
|
|
|
+ redPacketLog.setUserId(param.getUserId());
|
|
|
+ redPacketLog.setVideoId(param.getVideoId());
|
|
|
+ redPacketLog.setStatus(0);
|
|
|
+ redPacketLog.setQwUserId(param.getQwUserId() != null ? param.getQwUserId() : null);
|
|
|
+ redPacketLog.setCompanyUserId(param.getCompanyUserId());
|
|
|
+ redPacketLog.setCreateTime(new Date());
|
|
|
+ redPacketLog.setAmount(amount);
|
|
|
+ redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
+ redPacketLog.setPeriodId(param.getPeriodId());
|
|
|
+ redPacketLog.setAppId(param.getAppId());
|
|
|
+
|
|
|
+ redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
+
|
|
|
+ // 更新观看记录的奖励类型
|
|
|
+ log.setRewardType(config.getRewardType());
|
|
|
+ courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
+
|
|
|
+ return sendRedPacket;
|
|
|
+ } else {
|
|
|
+ return R.error("奖励发送失败,请联系客服");
|
|
|
}
|
|
|
-
|
|
|
} else {
|
|
|
FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
|
|
|
// 添加红包记录
|
|
@@ -1868,7 +1703,7 @@ public class FsUserCourseVideoServiceImpl implements IFsUserCourseVideoService
|
|
|
}
|
|
|
|
|
|
// 查询【用户-项目】关系
|
|
|
- FsUserCompanyUser userCompanyUser = userCompanyUserService.selectByUserIdAndProjectId(fsUser.getUserId(), courseProject);
|
|
|
+ FsUserCompanyUser userCompanyUser = userCompanyUserService.selectByUserIdAndProjectId(fsUser.getUserId(), courseProject,companyUser.getUserId());
|
|
|
|
|
|
// 添加逻辑:如果存在fs_user表数据,但是又不存在fs_user_company_user表,则表示是以前企微看课的,需要手动绑定
|
|
|
if(Objects.isNull(userCompanyUser)) {
|