|
|
@@ -87,6 +87,7 @@ import com.fs.system.mapper.SysDictDataMapper;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import com.github.binarywang.wxpay.bean.transfer.TransferBillsResult;
|
|
|
+import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
import com.volcengine.service.vod.IVodService;
|
|
|
import com.volcengine.service.vod.model.business.VodUrlUploadURLSet;
|
|
|
import com.volcengine.service.vod.model.request.*;
|
|
|
@@ -1643,15 +1644,22 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
switch (config.getRewardType()) {
|
|
|
// 红包奖励
|
|
|
case 1:
|
|
|
- return sendRedPacketReward(param, user, watchLog, video, config);
|
|
|
+ if (param.getSource() == 3){
|
|
|
+ param.setWatchLogId(watchLog.getLogId());
|
|
|
+ return withdrawal(param);
|
|
|
+ } else {
|
|
|
+ return sendRedPacketReward(param, user, watchLog, video, config);
|
|
|
+ }
|
|
|
// 积分奖励
|
|
|
case 2:
|
|
|
return sendIntegralReward(param, user, watchLog, config);
|
|
|
// 红包+积分
|
|
|
case 3:
|
|
|
- R sendRed = sendRedPacketReward(param, user, watchLog, video, config);
|
|
|
- if (!Objects.equals(sendRed.get("code"), 200)) {
|
|
|
- return sendRed;
|
|
|
+ if (isUserRedPacketReceiveEnabled(user)) {
|
|
|
+ R sendRed = sendRedPacketReward(param, user, watchLog, video, config);
|
|
|
+ if (!Objects.equals(sendRed.get("code"), 200)) {
|
|
|
+ return sendRed;
|
|
|
+ }
|
|
|
}
|
|
|
return sendIntegralReward(param, user, watchLog, config);
|
|
|
default:
|
|
|
@@ -1757,7 +1765,7 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
if (log.getRewardType() == 1) {
|
|
|
FsCourseRedPacketLog fsCourseRedPacketLog = redPacketLogMapper.selectUserFsCourseRedPacketLog(param.getVideoId(), param.getUserId(), param.getPeriodId());
|
|
|
if (fsCourseRedPacketLog != null && fsCourseRedPacketLog.getStatus() == 1) {
|
|
|
- return R.error("已领取该课程奖励,不可重复领取!");
|
|
|
+ return R.error("已领取该课程奖励,不可重复领取!").put("data",fsCourseRedPacketLog.getResult());
|
|
|
}
|
|
|
if (fsCourseRedPacketLog != null && fsCourseRedPacketLog.getStatus() == 0) {
|
|
|
if (StringUtils.isNotEmpty(fsCourseRedPacketLog.getResult())) {
|
|
|
@@ -1779,25 +1787,39 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
String json = configService.selectConfigByKey("course.config");
|
|
|
CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
|
|
|
|
|
|
- // 判断来源是否是app,如是app,则发放积分奖励
|
|
|
- int sourceApp = 3;
|
|
|
- if (sourceApp == param.getSource() && !CloudHostUtils.hasCloudHostName("中康")) {
|
|
|
- return sendIntegralReward(param, user, log, config);
|
|
|
- }
|
|
|
+// // 判断来源是否是app,如是app,则发放积分奖励
|
|
|
+// int sourceApp = 3;
|
|
|
+// if (sourceApp == param.getSource() && !CloudHostUtils.hasCloudHostName("中康")) {
|
|
|
+// return sendIntegralReward(param, user, log, config);
|
|
|
+// }
|
|
|
|
|
|
// 根据奖励类型发放不同奖励
|
|
|
switch (config.getRewardType()) {
|
|
|
// 红包奖励
|
|
|
case 1:
|
|
|
- return sendRedPacketRewardFsUser(param, user, log, video, config);
|
|
|
+ if (param.getSource()==3){
|
|
|
+ WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
+ String openId = getOpenId(param, user);
|
|
|
+ if (StringUtils.isBlank(openId)) {
|
|
|
+ return R.error("请重新使用微信登录");
|
|
|
+ }
|
|
|
+ packetParam.setOpenId(openId);
|
|
|
+ BeanUtils.copyProperties(param, packetParam);
|
|
|
+ packetParam.setUser(user);
|
|
|
+ return sendAppRedPacket(packetParam, log,video, config);
|
|
|
+ } else {
|
|
|
+ return sendRedPacketRewardFsUser(param, user, log, video, config);
|
|
|
+ }
|
|
|
// 积分奖励
|
|
|
case 2:
|
|
|
return sendIntegralReward(param, user, log, config);
|
|
|
// 红包+积分
|
|
|
case 3:
|
|
|
- R sendRed = sendRedPacketRewardFsUser(param, user, log, video, config);
|
|
|
- if (!Objects.equals(sendRed.get("code"), 200)) {
|
|
|
- return sendRed;
|
|
|
+ if (isUserRedPacketReceiveEnabled(user)) {
|
|
|
+ R sendRed = sendRedPacketRewardFsUser(param, user, log, video, config);
|
|
|
+ if (!Objects.equals(sendRed.get("code"), 200)) {
|
|
|
+ return sendRed;
|
|
|
+ }
|
|
|
}
|
|
|
return sendIntegralReward(param, user, log, config);
|
|
|
default:
|
|
|
@@ -1817,6 +1839,9 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
*/
|
|
|
private R sendRedPacketReward(FsCourseSendRewardUParam param, FsUser user, FsCourseWatchLog log, FsUserCourseVideo video, CourseConfig config) {
|
|
|
logger.info("进入发放红包");
|
|
|
+ if (!isUserRedPacketReceiveEnabled(user)) {
|
|
|
+ return R.ok("恭喜您,答题成功啦 !");
|
|
|
+ }
|
|
|
// 确定红包金额
|
|
|
BigDecimal amount = BigDecimal.ZERO;
|
|
|
FsUserCourseVideoRedPackage redPackage = fsUserCourseVideoRedPackageMapper.selectRedPacketByCompanyId(param.getVideoId(), param.getCompanyId(), param.getPeriodId());
|
|
|
@@ -1911,6 +1936,47 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /** 发红包接口返回非成功时,优先透传支付层返回的 msg */
|
|
|
+ private static String resolveSendRedPacketFailMsg(R sendRedPacket) {
|
|
|
+ if (sendRedPacket == null) {
|
|
|
+ return "奖励发送失败,请联系客服";
|
|
|
+ }
|
|
|
+ Object msgObj = sendRedPacket.get("msg");
|
|
|
+ if (msgObj != null) {
|
|
|
+ String m = String.valueOf(msgObj).trim();
|
|
|
+ if (StringUtils.isNotEmpty(m) && !"success".equalsIgnoreCase(m)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "奖励发送失败,请联系客服";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isWxAppIdMchIdNotMatch(Throwable t) {
|
|
|
+ while (t != null) {
|
|
|
+ if (t instanceof WxPayException) {
|
|
|
+ WxPayException wx = (WxPayException) t;
|
|
|
+ if ("APPID_MCHID_NOT_MATCH".equals(wx.getErrCode())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String des = wx.getErrCodeDes();
|
|
|
+ if (des != null && des.contains("商户号和appid没有绑定关系")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 兜底:有些场景下异常类型/字段未必能完全取到,只要 message 中包含关键字也认为是该错误
|
|
|
+ String msg = t.getMessage();
|
|
|
+ if (msg != null && (msg.contains("APPID_MCHID_NOT_MATCH") || msg.contains("商户号和appid没有绑定关系"))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String str = t.toString();
|
|
|
+ if (str != null && (str.contains("APPID_MCHID_NOT_MATCH") || str.contains("商户号和appid没有绑定关系"))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ t = t.getCause();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
private R sendRedPacketRewardToUser(FsCourseSendRewardUParam param, FsCourseWatchLog log, CourseConfig config, WxSendRedPacketParam packetParam, BigDecimal amount) {
|
|
|
|
|
|
|
|
|
@@ -1998,6 +2064,9 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
// 异常时回滚余额
|
|
|
|
|
|
rollbackBalance(balanceRollbackError);
|
|
|
+ if (isWxAppIdMchIdNotMatch(e)) {
|
|
|
+ return R.error("未绑定该小程序,请联系群主");
|
|
|
+ }
|
|
|
return R.error("奖励发送失败,请联系客服");
|
|
|
}
|
|
|
|
|
|
@@ -2038,7 +2107,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
} else {
|
|
|
// 登记回滚流水表
|
|
|
rollbackBalance(balanceRollbackError);
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
+ String failMsg = resolveSendRedPacketFailMsg(sendRedPacket);
|
|
|
+ if (failMsg != null && (failMsg.contains("APPID_MCHID_NOT_MATCH") || failMsg.contains("商户号和appid没有绑定"))) {
|
|
|
+ return R.error("商户号异常,请联系群主");
|
|
|
+ }
|
|
|
+ return R.error(failMsg);
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -2082,7 +2155,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
|
|
|
return sendRedPacket;
|
|
|
} else {
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
+ String failMsg = resolveSendRedPacketFailMsg(sendRedPacket);
|
|
|
+ if (failMsg != null && (failMsg.contains("APPID_MCHID_NOT_MATCH") || failMsg.contains("商户号和appid没有绑定"))) {
|
|
|
+ return R.error("商户号异常,请联系群主");
|
|
|
+ }
|
|
|
+ return R.error(failMsg);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -2132,6 +2209,9 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
return R.error(403,"已超过领取红包时间");
|
|
|
}
|
|
|
|
|
|
+ if (!isUserRedPacketReceiveEnabled(user)) {
|
|
|
+ return R.ok("恭喜您,答题成功啦 !");
|
|
|
+ }
|
|
|
|
|
|
// 确定红包金额
|
|
|
BigDecimal amount = BigDecimal.ZERO;
|
|
|
@@ -2262,6 +2342,9 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
// 异常时回滚余额
|
|
|
|
|
|
rollbackBalance(balanceRollbackError);
|
|
|
+ if (isWxAppIdMchIdNotMatch(e)) {
|
|
|
+ return R.error("未绑定该小程序,请联系群主");
|
|
|
+ }
|
|
|
return R.error("奖励发送失败,请联系客服");
|
|
|
}
|
|
|
|
|
|
@@ -2308,7 +2391,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
} else {
|
|
|
// 发送失败,回滚余额
|
|
|
rollbackBalance(balanceRollbackError);
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
+ String failMsg = resolveSendRedPacketFailMsg(sendRedPacket);
|
|
|
+ if (failMsg != null && (failMsg.contains("APPID_MCHID_NOT_MATCH") || failMsg.contains("商户号和appid没有绑定关系"))) {
|
|
|
+ return R.error("小程序未绑定,请联系群主");
|
|
|
+ }
|
|
|
+ return R.error(failMsg);
|
|
|
}
|
|
|
|
|
|
// ===================== 本次修改目的为了实时扣减公司余额=====================
|
|
|
@@ -2356,7 +2443,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
|
|
|
return sendRedPacket;
|
|
|
} else {
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
+ String failMsg = resolveSendRedPacketFailMsg(sendRedPacket);
|
|
|
+ if (failMsg != null && (failMsg.contains("APPID_MCHID_NOT_MATCH") || failMsg.contains("商户号和appid没有绑定"))) {
|
|
|
+ return R.error("商户号异常,请联系群主");
|
|
|
+ }
|
|
|
+ return R.error(failMsg);
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
return R.error(e.getMessage());
|
|
|
@@ -2454,7 +2545,11 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
R sendRedPacket = paymentService.sendRedPacket(packetParam);
|
|
|
|
|
|
if (!sendRedPacket.get("code").equals(200)) {
|
|
|
- return R.error("奖励发送失败,请联系客服");
|
|
|
+ String failMsg = resolveSendRedPacketFailMsg(sendRedPacket);
|
|
|
+ if (failMsg != null && (failMsg.contains("APPID_MCHID_NOT_MATCH") || failMsg.contains("商户号和appid没有绑定"))) {
|
|
|
+ return R.error("商户号异常,请联系群主");
|
|
|
+ }
|
|
|
+ return R.error(failMsg);
|
|
|
}
|
|
|
|
|
|
createRedPacketLog(sendRedPacket, param, amount, log);
|
|
|
@@ -2507,25 +2602,48 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
* @param source 来源 1公众号 2小程序
|
|
|
* @return openId
|
|
|
*/
|
|
|
- private String getOpenId(Long userId, Long companyId, Integer source) {
|
|
|
- Company company = companyMapper.selectCompanyById(companyId);
|
|
|
- String appId = source == 1 ? company.getCourseMaAppId() : company.getCourseMiniAppId();
|
|
|
+ private String getOpenId(FsCourseSendRewardUParam param, FsUser user) {
|
|
|
+ Integer source = param.getSource();
|
|
|
+ Long userId = param.getUserId();
|
|
|
+ switch (source) {
|
|
|
+ case 1:
|
|
|
+ Company company = companyMapper.selectCompanyById(param.getCompanyId());
|
|
|
+ String appId = company.getCourseMaAppId();
|
|
|
|
|
|
- // 公司配置为空时获取默认配置
|
|
|
- if (StringUtils.isBlank(appId)) {
|
|
|
- String json = configService.selectConfigByKey("course.config");
|
|
|
- CourseConfig config = JSON.parseObject(json, CourseConfig.class);
|
|
|
- appId = source == 1 ? config.getMpAppId() : config.getMiniprogramAppid();
|
|
|
- }
|
|
|
+ // 公司配置为空时获取默认配置
|
|
|
+ if (StringUtils.isBlank(appId)) {
|
|
|
+ String json = configService.selectConfigByKey("course.config");
|
|
|
+ CourseConfig config = JSON.parseObject(json, CourseConfig.class);
|
|
|
+ appId = config.getMpAppId();
|
|
|
+ }
|
|
|
|
|
|
- // 查询openId
|
|
|
- Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery().eq(FsUserWx::getFsUserId, userId).eq(FsUserWx::getAppId, appId);
|
|
|
- FsUserWx fsUserWx = fsUserWxService.getOne(queryWrapper);
|
|
|
- if (Objects.isNull(fsUserWx)) {
|
|
|
- throw new CustomException("获取openId失败");
|
|
|
- }
|
|
|
+ // 查询openId
|
|
|
+ Wrapper<FsUserWx> queryWrapper = Wrappers.<FsUserWx>lambdaQuery().eq(FsUserWx::getFsUserId, userId).eq(FsUserWx::getAppId, appId);
|
|
|
+ FsUserWx fsUserWx = fsUserWxService.getOne(queryWrapper);
|
|
|
+ if (Objects.isNull(fsUserWx)) {
|
|
|
+ throw new CustomException("获取openId失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ return fsUserWx.getOpenId();
|
|
|
+ case 2:
|
|
|
+ FsUserWx userWx = fsUserWxService.selectByAppIdAndUserId(param.getAppId(),userId,1);
|
|
|
+ if (Objects.nonNull(userWx) && StringUtils.isNotBlank(userWx.getOpenId())) {
|
|
|
+ return userWx.getOpenId();
|
|
|
+ }
|
|
|
|
|
|
- return fsUserWx.getOpenId();
|
|
|
+ if (StringUtils.isNotBlank(user.getCourseMaOpenId())) {
|
|
|
+ try {
|
|
|
+ handleFsUserWx(user,param.getAppId());
|
|
|
+ } catch (Exception e){
|
|
|
+ log.error("【更新或插入用户与小程序的绑定关系失败】:{}", userId, e);
|
|
|
+ }
|
|
|
+ return user.getCourseMaOpenId();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ return user.getAppOpenId();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -3073,7 +3191,7 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
// 查询视频是否设置了红包,没有就不提示
|
|
|
Integer fsUserCourseVideoRedPackage = fsUserCourseVideoRedPackageMapper.selectRedPacketByCompanyCount(param.getVideoId(), null, param.getPeriodId());
|
|
|
if (fsUserCourseVideoRedPackage > 0) {
|
|
|
- tipsTime = courseVideoDetails.getDuration() / 3;
|
|
|
+ // tipsTime = courseVideoDetails.getDuration() / 3;
|
|
|
tipsTime2 = (courseVideoDetails.getDuration() * 2) / 3;
|
|
|
}
|
|
|
}
|
|
|
@@ -3862,14 +3980,14 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public AjaxResult updateVideoByVideoUrl(String videoUrl,String oldVideoUrl, String thumbnail,String fileName) {
|
|
|
+ public AjaxResult updateVideoByVideoUrl(String videoUrl,String oldVideoUrl, String thumbnail,String fileName, Integer duration) {
|
|
|
List<FsUserCourseVideo> videoList = fsUserCourseVideoMapper.selectByVideoUrl(oldVideoUrl);
|
|
|
if (CollectionUtils.isEmpty(videoList)){
|
|
|
log.warn("根据videoUrl:{} 未查询到fs_user_course_video表数据",oldVideoUrl);
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
List<Long> idList = videoList.stream().map(FsUserCourseVideo::getVideoId).collect(Collectors.toList());
|
|
|
- fsUserCourseVideoMapper.updateVideoByVideoUrl(videoUrl,thumbnail, idList,fileName);
|
|
|
+ fsUserCourseVideoMapper.updateVideoByVideoUrl(videoUrl,thumbnail, idList,fileName,duration);
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
@Override
|
|
|
@@ -4887,5 +5005,382 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
public List<OptionsVO> selectVideoOptionsByCourseId(Long courseId) {
|
|
|
return fsUserCourseVideoMapper.selectVideoOptionsByCourseId(courseId);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户提现
|
|
|
+ * @param param
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public R withdrawal(FsCourseSendRewardUParam param) {
|
|
|
+ Long userId = param.getUserId();
|
|
|
+ // 生成锁的key,基于用户ID和视频ID确保同一用户同一视频的请求被锁定
|
|
|
+ String lockKey = "reward_red_lock:user:" + userId;
|
|
|
+ RLock lock = redissonClient.getLock(lockKey);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 尝试获取锁,等待时间5秒,锁过期时间30秒
|
|
|
+ boolean isLocked = lock.tryLock(5, 300, TimeUnit.SECONDS);
|
|
|
+ if (!isLocked) {
|
|
|
+ logger.warn("获取锁失败,用户ID:{}", userId);
|
|
|
+ return R.error("操作频繁,请稍后再试!");
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info("成功获取锁,开始处理奖励发放,用户ID:{}", userId);
|
|
|
+ return executeWithdrawal(param);
|
|
|
+
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ logger.error("获取锁被中断,用户ID:{}", userId, e);
|
|
|
+ return R.error("系统繁忙,请重试!");
|
|
|
+ } finally {
|
|
|
+ // 释放锁
|
|
|
+ if (lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ logger.info("释放锁成功,用户ID:{}", userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private R executeWithdrawal(FsCourseSendRewardUParam param){
|
|
|
+ log.info("进入用户判断");
|
|
|
+ FsUser user = fsUserMapper.selectFsUserByUserId(param.getUserId());
|
|
|
+ if (user == null) {
|
|
|
+ return R.error("未识别到用户信息");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsCourseWatchLog watchLog = courseWatchLogMapper.getWatchCourseVideo(param.getUserId(), param.getVideoId(), param.getQwUserId(), param.getQwExternalId());
|
|
|
+ if (watchLog == null) {
|
|
|
+ watchLog = courseWatchLogMapper.getWatchCourseVideoByFsUser(param.getUserId(), param.getVideoId(), param.getCompanyUserId());
|
|
|
+ }
|
|
|
+ if (watchLog == null) {
|
|
|
+ return R.error("无记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (watchLog.getLogType() != 2) {
|
|
|
+ return R.error("未完课");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsCourseAnswerLogs rightLog = courseAnswerLogsMapper.selectRightLogByCourseVideo(param.getVideoId(), param.getUserId(), param.getQwUserId());
|
|
|
+ if (rightLog == null) {
|
|
|
+ logger.error("未答题:{}", param.getUserId());
|
|
|
+ return R.error("未答题");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsCourseRedPacketLog fsCourseRedPacketLog = redPacketLogMapper.selectUserFsCourseRedPacketLog(param.getVideoId(), param.getUserId(), param.getPeriodId());
|
|
|
+
|
|
|
+ if (watchLog.getRewardType() != null) {
|
|
|
+ if (watchLog.getRewardType() == 1) {
|
|
|
+ if (fsCourseRedPacketLog != null && fsCourseRedPacketLog.getStatus() == 1) {
|
|
|
+ return R.error("已领取该课程奖励,不可重复领取!");
|
|
|
+ }
|
|
|
+ if (fsCourseRedPacketLog != null && fsCourseRedPacketLog.getStatus() == 0) {
|
|
|
+ if (StringUtils.isNotEmpty(fsCourseRedPacketLog.getResult())) {
|
|
|
+ R r = JSON.parseObject(fsCourseRedPacketLog.getResult(), R.class);
|
|
|
+ return r;
|
|
|
+ } else {
|
|
|
+ return R.error("操作频繁,请稍后再试!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (watchLog.getRewardType() == 2) {
|
|
|
+ return R.error("已领取该课程奖励,不可重复领取!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取视频信息
|
|
|
+ FsUserCourseVideo video = fsUserCourseVideoMapper.selectFsUserCourseVideoByVideoId(param.getVideoId());
|
|
|
+
|
|
|
+ // 获取配置信息
|
|
|
+ String json = configService.selectConfigByKey("course.config");
|
|
|
+ CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
|
|
|
+
|
|
|
+ // 判断来源是否是app,如是app,则发放积分奖励
|
|
|
+// int sourceApp = 3;
|
|
|
+// if (sourceApp == param.getSource() /*&& !CloudHostUtils.hasCloudHostName("中康")*/) {
|
|
|
+// return sendIntegralReward(param, user, log, config);
|
|
|
+// }
|
|
|
+ if (ObjectUtils.isEmpty(param.getRewardType())){
|
|
|
+ param.setRewardType(config.getRewardType());
|
|
|
+ }
|
|
|
+ // 根据奖励类型发放不同奖励
|
|
|
+ switch (param.getRewardType()) {
|
|
|
+ // 红包奖励
|
|
|
+ case 1:
|
|
|
+ //来源是小程序切换openId
|
|
|
+ WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
+ String openId = getOpenId(param, user);
|
|
|
+ if (StringUtils.isBlank(openId)) {
|
|
|
+ return R.error("请重新使用微信登录");
|
|
|
+ }
|
|
|
+ packetParam.setOpenId(openId);
|
|
|
+ BeanUtils.copyProperties(param, packetParam);
|
|
|
+ packetParam.setUser(user);
|
|
|
+
|
|
|
+ return sendAppRedPacket(packetParam, watchLog,video, config);
|
|
|
+ // 积分奖励
|
|
|
+ case 2:
|
|
|
+ return sendIntegralReward(param, user, watchLog, config);
|
|
|
+// // 转盘
|
|
|
+// case 3:
|
|
|
+// return drawTurntable(param, user, log);
|
|
|
+// // 保底转盘
|
|
|
+// case 4:
|
|
|
+// return drawTurntableGuarantee(param, user, log);
|
|
|
+ default:
|
|
|
+ return R.error("参数错误!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private R sendAppRedPacket(WxSendRedPacketParam packetParam,FsCourseWatchLog log,FsUserCourseVideo video,CourseConfig config) {
|
|
|
+ // 仅手动发课(有营期)校验红包领取截止时间;自动发课不做该限制
|
|
|
+ if (log.getPeriodId() != null) {
|
|
|
+ FsUserCoursePeriodDays periodDays = new FsUserCoursePeriodDays();
|
|
|
+ periodDays.setVideoId(log.getVideoId());
|
|
|
+ periodDays.setPeriodId(log.getPeriodId());
|
|
|
+ //正常情况是只能查询到一条,之前可能存在重复的脏数据,暂使用查询list的方式
|
|
|
+ List<FsUserCoursePeriodDays> fsUserCoursePeriodDays = fsUserCoursePeriodDaysMapper.selectFsUserCoursePeriodDaysList(periodDays);
|
|
|
+ if (fsUserCoursePeriodDays != null && !fsUserCoursePeriodDays.isEmpty()) {
|
|
|
+ periodDays = fsUserCoursePeriodDays.get(0);
|
|
|
+ }
|
|
|
+ if (periodDays.getLastJoinTime() != null && LocalDateTime.now().isAfter(periodDays.getLastJoinTime())) {
|
|
|
+ return R.error(403, "已超过领取红包时间");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (packetParam.getUser() != null && !isUserRedPacketReceiveEnabled(packetParam.getUser())) {
|
|
|
+ return R.ok("恭喜您,答题成功啦 !");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确定红包金额
|
|
|
+ BigDecimal amount = BigDecimal.ZERO;
|
|
|
+ FsUserCourseVideoRedPackage redPackage = fsUserCourseVideoRedPackageMapper.selectRedPacketByCompanyId(log.getVideoId(), log.getCompanyId(), log.getPeriodId());
|
|
|
+
|
|
|
+ if (redPackage != null && redPackage.getRedPacketMoney() != null) {
|
|
|
+ amount = redPackage.getRedPacketMoney();
|
|
|
+ } else if (video != null && video.getRedPacketMoney() != null) {
|
|
|
+ amount = video.getRedPacketMoney();
|
|
|
+ }
|
|
|
+ packetParam.setAmount(amount);
|
|
|
+
|
|
|
+ if (amount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+
|
|
|
+ // 打开红包扣减功能
|
|
|
+ if ("1".equals(config.getIsRedPackageBalanceDeduction())) {
|
|
|
+ // 先注释 20251024 redis 余额 充值没有考虑 其余扣减没有考虑
|
|
|
+ // ===================== 20251022 xgb 修改 本次修改目的为了实时扣减公司余额=====================
|
|
|
+ // 1 使用redis缓存加锁 预扣减余额 红包发送失败 恢复redis缓存余额,如果回滚失败登记异常记录表 定时任务重新回滚余额
|
|
|
+ // 2 另起定时任务 同步缓存余额到redis中
|
|
|
+ // 3 注意!!!!! 启动系统时查询公司账户余额(这个时候要保证余额正确)启动会自动保存到redis缓存中
|
|
|
+ // 注意!!!!! 打开这个开关前记得检测redis缓存余额是否正确 若不正确 修改数据库字段red_package_money,删除redis缓存,重启系统,
|
|
|
+
|
|
|
+
|
|
|
+ // 预设值异常对象
|
|
|
+
|
|
|
+ BalanceRollbackError balanceRollbackError = new BalanceRollbackError();
|
|
|
+ balanceRollbackError.setCompanyId(packetParam.getCompanyId());
|
|
|
+ balanceRollbackError.setUserId(log.getUserId());
|
|
|
+ balanceRollbackError.setLogId(log.getLogId());
|
|
|
+ balanceRollbackError.setVideoId(log.getVideoId());
|
|
|
+ balanceRollbackError.setStatus(0);
|
|
|
+ balanceRollbackError.setMoney(amount);
|
|
|
+
|
|
|
+ if (packetParam.getCompanyId() == null) {
|
|
|
+ logger.error("发送红包参数错误,公司不能为空,异常请求参数{}", packetParam);
|
|
|
+ return R.error("发送红包失败,请联系管理员");
|
|
|
+ }
|
|
|
+ String companyMoneyKey = FsConstants.COMPANY_MONEY_KEY + packetParam.getCompanyId();
|
|
|
+
|
|
|
+ // 第一次加锁:预扣减余额
|
|
|
+ RLock lock1 = redissonClient.getLock(FsConstants.COMPANY_MONEY_LOCK + packetParam.getCompanyId());
|
|
|
+ boolean lockAcquired = false;
|
|
|
+ BigDecimal newMoney;
|
|
|
+ try {
|
|
|
+ if (lock1.tryLock(3, 10, TimeUnit.SECONDS)) {
|
|
|
+ lockAcquired = true;
|
|
|
+ BigDecimal originalMoney;
|
|
|
+ // 获取当前余额
|
|
|
+ String moneyStr = redisCache.getCacheObject(companyMoneyKey);
|
|
|
+ if (StringUtils.isNotEmpty(moneyStr)) {
|
|
|
+ originalMoney = new BigDecimal(moneyStr);
|
|
|
+ } else {
|
|
|
+ // 缓存没有值,重启系统恢复redis数据 保证数据正确性
|
|
|
+ logger.error("发送红包获取redis余额缓存异常,异常请求参数{}", packetParam);
|
|
|
+ return R.error("系统异常,请稍后重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (originalMoney.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
+ logger.error("服务商余额不足,异常请求参数{}", packetParam);
|
|
|
+ return R.error("服务商余额不足,请联系群主服务器充值!");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 预扣减金额
|
|
|
+ newMoney = originalMoney.subtract(amount);
|
|
|
+ redisCache.setCacheObject(companyMoneyKey, newMoney.toString());
|
|
|
+ } else {
|
|
|
+ logger.error("获取redis锁失败,异常请求参数{}", packetParam);
|
|
|
+ return R.error("系统繁忙,请稍后重试");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("预扣减余额失败: 异常请求参数{},异常信息{}", packetParam, e.getMessage(), e);
|
|
|
+ return R.error("系统异常,请稍后重试");
|
|
|
+ } finally {
|
|
|
+ // 只有在成功获取锁的情况下才释放锁
|
|
|
+ if (lockAcquired && lock1.isHeldByCurrentThread()) {
|
|
|
+ try {
|
|
|
+ lock1.unlock();
|
|
|
+ } catch (IllegalMonitorStateException e) {
|
|
|
+ logger.warn("尝试释放非当前线程持有的锁: companyId={}", packetParam.getCompanyId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 调用第三方接口(锁外操作)
|
|
|
+ R sendRedPacket;
|
|
|
+ try {
|
|
|
+ sendRedPacket = paymentService.sendAppRedPacket(packetParam);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("红包发送异常: 异常请求参数{}", packetParam, e);
|
|
|
+ // 异常时回滚余额
|
|
|
+
|
|
|
+ rollbackBalance(balanceRollbackError);
|
|
|
+ return R.error("奖励发送失败,请联系客服");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 红包发送成功处理
|
|
|
+ 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());
|
|
|
+ redPacketLog.setBatchId(transferBillsResult.getTransferBillNo());
|
|
|
+ } else {
|
|
|
+ redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
|
|
|
+ redPacketLog.setBatchId(sendRedPacket.get("batchId").toString());
|
|
|
+ }
|
|
|
+ // 添加红包记录
|
|
|
+ redPacketLog.setCourseId(log.getCourseId());
|
|
|
+ redPacketLog.setCompanyId(log.getCompanyId());
|
|
|
+ redPacketLog.setUserId(log.getUserId());
|
|
|
+ redPacketLog.setVideoId(log.getVideoId());
|
|
|
+ redPacketLog.setStatus(0);
|
|
|
+ redPacketLog.setQwUserId(log.getQwUserId() != null ? log.getQwUserId().toString() : null);
|
|
|
+ redPacketLog.setCompanyUserId(log.getCompanyUserId());
|
|
|
+ redPacketLog.setCreateTime(new Date());
|
|
|
+ redPacketLog.setAmount(amount);
|
|
|
+ redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
+ redPacketLog.setPeriodId(log.getPeriodId());
|
|
|
+ redPacketLog.setAppId(packetParam.getAppId());
|
|
|
+
|
|
|
+ redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
+
|
|
|
+ // 更新观看记录的奖励类型
|
|
|
+ log.setRewardType(config.getRewardType());
|
|
|
+ courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
+
|
|
|
+ // 异步登记余额扣减日志
|
|
|
+ BigDecimal money = amount.multiply(BigDecimal.valueOf(-1));
|
|
|
+ companyService.asyncRecordBalanceLog(log.getCompanyId(), money, 15, newMoney, "发放红包", redPacketLog.getLogId());
|
|
|
+
|
|
|
+ return sendRedPacket;
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 发送失败,回滚余额
|
|
|
+ rollbackBalance(balanceRollbackError);
|
|
|
+ return R.error("奖励发送失败,请联系客服");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ===================== 本次修改目的为了实时扣减公司余额=====================
|
|
|
+ } else {
|
|
|
+ Company company = companyMapper.selectCompanyById(log.getCompanyId());
|
|
|
+ BigDecimal money = company.getMoney();
|
|
|
+ if (money.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ return R.error("服务商余额不足,请联系群主服务器充值!");
|
|
|
+ }
|
|
|
+
|
|
|
+ try{
|
|
|
+ // 发送红包
|
|
|
+ R sendRedPacket = paymentService.sendAppRedPacket(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());
|
|
|
+ redPacketLog.setBatchId(transferBillsResult.getTransferBillNo());
|
|
|
+ } else {
|
|
|
+ redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
|
|
|
+ redPacketLog.setBatchId(sendRedPacket.get("batchId").toString());
|
|
|
+ }
|
|
|
+ // 添加红包记录
|
|
|
+ redPacketLog.setCourseId(log.getCourseId());
|
|
|
+ redPacketLog.setCompanyId(log.getCompanyId());
|
|
|
+ redPacketLog.setUserId(log.getUserId());
|
|
|
+ redPacketLog.setVideoId(log.getVideoId());
|
|
|
+ redPacketLog.setStatus(0);
|
|
|
+ redPacketLog.setQwUserId(log.getQwUserId() != null ? log.getQwUserId().toString() : null);
|
|
|
+ redPacketLog.setCompanyUserId(log.getCompanyUserId());
|
|
|
+ redPacketLog.setCreateTime(new Date());
|
|
|
+ redPacketLog.setAmount(amount);
|
|
|
+ redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
+ redPacketLog.setPeriodId(log.getPeriodId());
|
|
|
+ redPacketLog.setAppId( packetParam.getAppId());
|
|
|
+
|
|
|
+ redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
+
|
|
|
+ // 更新观看记录的奖励类型
|
|
|
+ log.setRewardType(config.getRewardType());
|
|
|
+ courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
+
|
|
|
+ return sendRedPacket;
|
|
|
+ } else {
|
|
|
+ return R.error("奖励发送失败,请联系客服");
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ return R.error(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
|
|
|
+ // 添加红包记录
|
|
|
+ redPacketLog.setCourseId(log.getCourseId());
|
|
|
+// redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
|
|
|
+ redPacketLog.setCompanyId(log.getCompanyId());
|
|
|
+ redPacketLog.setUserId(log.getUserId());
|
|
|
+ redPacketLog.setVideoId(log.getVideoId());
|
|
|
+ redPacketLog.setStatus(1);
|
|
|
+ redPacketLog.setQwUserId(log.getQwUserId() != null ? log.getQwUserId().toString() : null);
|
|
|
+ redPacketLog.setCompanyUserId(log.getCompanyUserId());
|
|
|
+ redPacketLog.setCreateTime(new Date());
|
|
|
+ redPacketLog.setAmount(BigDecimal.ZERO);
|
|
|
+ redPacketLog.setWatchLogId(log.getLogId() != null ? log.getLogId() : null);
|
|
|
+ redPacketLog.setPeriodId(log.getPeriodId());
|
|
|
+ redPacketLog.setAppId( packetParam.getAppId());
|
|
|
+ redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
|
|
|
+
|
|
|
+ // 更新观看记录的奖励类
|
|
|
+ log.setRewardType(config.getRewardType());
|
|
|
+ courseWatchLogMapper.updateFsCourseWatchLog(log);
|
|
|
+ return R.ok("答题成功!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 1 可领取红包;0 关闭;null 视为可领取(兼容历史数据) */
|
|
|
+ private boolean isUserRedPacketReceiveEnabled(FsUser user) {
|
|
|
+ if (user == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (user.getRedStatus() == null) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return Objects.equals(user.getRedStatus(), 1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|