|
|
@@ -2,10 +2,7 @@ package com.fs.live.service.impl;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
@@ -14,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.fs.common.constant.FsConstants;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
+import com.fs.common.exception.base.BaseException;
|
|
|
import com.fs.common.utils.CloudHostUtils;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
@@ -35,15 +33,12 @@ import com.fs.his.mapper.FsUserIntegralLogsMapper;
|
|
|
import com.fs.his.mapper.FsUserMapper;
|
|
|
import com.fs.his.param.WxSendRedPacketParam;
|
|
|
import com.fs.his.service.IFsStorePaymentService;
|
|
|
-import com.fs.live.domain.Live;
|
|
|
-import com.fs.live.domain.LiveWatchConfig;
|
|
|
-import com.fs.live.domain.LiveWatchUser;
|
|
|
+import com.fs.live.domain.*;
|
|
|
import com.fs.live.mapper.LiveWatchUserMapper;
|
|
|
+import com.fs.live.param.CouponPO;
|
|
|
import com.fs.live.param.LiveRedPacketParam;
|
|
|
import com.fs.live.mapper.LiveMapper;
|
|
|
-import com.fs.live.service.ILiveMsgService;
|
|
|
-import com.fs.live.service.ILiveService;
|
|
|
-import com.fs.live.service.ILiveWatchUserService;
|
|
|
+import com.fs.live.service.*;
|
|
|
import com.fs.live.vo.RecordTimeRangeVO;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import com.github.binarywang.wxpay.bean.transfer.TransferBillsResult;
|
|
|
@@ -57,8 +52,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.live.mapper.LiveRedPacketLogMapper;
|
|
|
-import com.fs.live.domain.LiveRedPacketLog;
|
|
|
-import com.fs.live.service.ILiveRedPacketLogService;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.transaction.support.TransactionSynchronization;
|
|
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
@@ -75,6 +68,9 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LiveRedPacketLogServiceImpl.class);
|
|
|
|
|
|
+ /** 获取方式:4-完课奖励 */
|
|
|
+ private static final String COMPLETION_COUPON_TYPE = "4";
|
|
|
+
|
|
|
@Autowired
|
|
|
private RedissonClient redissonClient;
|
|
|
|
|
|
@@ -116,6 +112,16 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
@Lazy
|
|
|
private ILiveWatchUserService liveWatchUserService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ILiveCouponService liveCouponService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ILiveCouponIssueService liveCouponIssueService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ILiveCouponUserService liveCouponUserService;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 查询直播红包 记录
|
|
|
*
|
|
|
@@ -311,40 +317,21 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
&& replayUser.getOnlineSeconds() * 100 >= duration * completionRate) {
|
|
|
|
|
|
// 如果是 回放完课 发积分 //新增 时间范围 范围内可以领取红包,范围外领取积分或者不领
|
|
|
- if (config.getRecordRedPacketEnabled() && !StringUtil.strIsNullOrEmpty(config.getRecordTimeRangeStr())){
|
|
|
+ if (config.getRecordRedPacketEnabled()!=null
|
|
|
+ && config.getRecordRedPacketEnabled()
|
|
|
+ && !StringUtil.strIsNullOrEmpty(config.getRecordTimeRangeStr())){
|
|
|
|
|
|
List<RecordTimeRangeVO> ranges = parseRecordRewards(config.getRecordTimeRangeStr());
|
|
|
RecordTimeRangeVO matched = matchCurrentTime(ranges);
|
|
|
|
|
|
if (matched != null) {
|
|
|
- // 根据 rewardType 发放对应奖励 1=红包 2=积分
|
|
|
- if ("1".equals(matched.getRewardType())) {
|
|
|
-
|
|
|
- WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
-
|
|
|
- if (StringUtil.strIsNullOrEmpty(user.getAppOpenId())){
|
|
|
- return R.error("请重新登录app");
|
|
|
- }
|
|
|
-
|
|
|
- // 用app的 appOpenId
|
|
|
- String openId = user.getAppOpenId();
|
|
|
- packetParam.setOpenId(openId);
|
|
|
- BeanUtils.copyProperties(param, packetParam);
|
|
|
-
|
|
|
- //重置红包数
|
|
|
- config.setRedPacketAmount(matched.getRedPacketAmount());
|
|
|
- return sendAppLiveRedPacketAuto(packetParam, watchUser, config, param);
|
|
|
- }else if ("2".equals(matched.getRewardType())) {
|
|
|
- //重置发放的积分
|
|
|
- config.setScoreAmount(matched.getScoreAmount());
|
|
|
- return sendLiveIntegralReward(param, user, watchUser, config);
|
|
|
- }
|
|
|
+ return dispatchRecordRewards(matched, user, param, watchUser, config);
|
|
|
}else {
|
|
|
return R.error("当前时间段,不满足领取奖励条件");
|
|
|
}
|
|
|
}else {
|
|
|
- // 更新直播观看记录的奖励类型
|
|
|
- watchUser.setRewardType(3);
|
|
|
+ // 更新直播观看记录的奖励类型,99=未设置奖励
|
|
|
+ watchUser.setRewardType(99);
|
|
|
watchUser.setSendType(1);
|
|
|
|
|
|
watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
@@ -382,40 +369,21 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
|
|
|
//新增 时间范围 范围内可以领取红包,范围外领取积分或者不领
|
|
|
// 如果是 回放完课 发积分 //新增 时间范围 范围内可以领取红包,范围外领取积分或者不领
|
|
|
- if (config.getRecordRedPacketEnabled() && !StringUtil.strIsNullOrEmpty(config.getRecordTimeRangeStr())){
|
|
|
+ if (config.getRecordRedPacketEnabled()!=null
|
|
|
+ && config.getRecordRedPacketEnabled()
|
|
|
+ && !StringUtil.strIsNullOrEmpty(config.getRecordTimeRangeStr())){
|
|
|
|
|
|
List<RecordTimeRangeVO> ranges = parseRecordRewards(config.getRecordTimeRangeStr());
|
|
|
RecordTimeRangeVO matched = matchCurrentTime(ranges);
|
|
|
|
|
|
if (matched != null) {
|
|
|
- // 根据 rewardType 发放对应奖励 1=红包 2=积分
|
|
|
- if ("1".equals(matched.getRewardType())) {
|
|
|
-
|
|
|
- WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
-
|
|
|
- if (StringUtil.strIsNullOrEmpty(user.getAppOpenId())){
|
|
|
- return R.error("请重新登录app");
|
|
|
- }
|
|
|
-
|
|
|
- // 用app的 appOpenId
|
|
|
- String openId = user.getAppOpenId();
|
|
|
- packetParam.setOpenId(openId);
|
|
|
- BeanUtils.copyProperties(param, packetParam);
|
|
|
-
|
|
|
- //重置红包数
|
|
|
- config.setRedPacketAmount(matched.getRedPacketAmount());
|
|
|
- return sendAppLiveRedPacketAuto(packetParam, watchUser, config, param);
|
|
|
- }else if ("2".equals(matched.getRewardType())) {
|
|
|
- //重置发放的积分
|
|
|
- config.setScoreAmount(matched.getScoreAmount());
|
|
|
- return sendLiveIntegralReward(param, user, watchUser, config);
|
|
|
- }
|
|
|
+ return dispatchRecordRewards(matched, user, param, watchUser, config);
|
|
|
}else {
|
|
|
return R.error("当前时间段,不满足领取奖励条件");
|
|
|
}
|
|
|
}else {
|
|
|
- // 更新直播观看记录的奖励类型
|
|
|
- watchUser.setRewardType(3);
|
|
|
+ // 更新直播观看记录的奖励类型,99=未设置奖励
|
|
|
+ watchUser.setRewardType(99);
|
|
|
watchUser.setSendType(1);
|
|
|
|
|
|
watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
@@ -424,12 +392,12 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
}
|
|
|
|
|
|
// 更新直播观看记录的奖励类型
|
|
|
- watchUser.setRewardType(3);
|
|
|
- watchUser.setSendType(1);
|
|
|
-
|
|
|
- watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
- log.error("回放未满足-奖励设置外:{},{}",live.getLiveId(),config);
|
|
|
- return R.ok("未设置回放奖励");
|
|
|
+// watchUser.setRewardType(3);
|
|
|
+// watchUser.setSendType(1);
|
|
|
+//
|
|
|
+// watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+// log.error("回放未满足-奖励设置外:{},{}",live.getLiveId(),config);
|
|
|
+// return R.ok("未设置回放奖励");
|
|
|
// // 如果是 回放完课 发积分
|
|
|
// return sendLiveIntegralReward(param, user, watchUser, config);
|
|
|
}else {
|
|
|
@@ -456,26 +424,87 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
}
|
|
|
String[] records = recordTimeRangeStr.split(",");
|
|
|
for (String record : records) {
|
|
|
- String[] parts = record.split("-", 5);
|
|
|
+ if (record.isEmpty()) continue;
|
|
|
+ String[] parts = record.split("-", 6);
|
|
|
if (parts.length < 3) continue;
|
|
|
|
|
|
RecordTimeRangeVO range = new RecordTimeRangeVO();
|
|
|
range.setStartTime(parts[0]);
|
|
|
range.setEndTime(parts[1]);
|
|
|
- range.setRewardType(parts[2]);
|
|
|
|
|
|
- if ("1".equals(parts[2]) && parts.length > 3 && !parts[3].isEmpty()) {
|
|
|
+ // rewardTypes: 用|分隔的多个类型,如 "1|2|3"
|
|
|
+ if (parts[2] != null && !parts[2].isEmpty()) {
|
|
|
+ range.setRewardTypes(Arrays.asList(parts[2].split("\\|")));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 红包金额
|
|
|
+ if (parts.length > 3 && !parts[3].isEmpty()) {
|
|
|
range.setRedPacketAmount(BigDecimal.valueOf(Double.parseDouble(parts[3])));
|
|
|
}
|
|
|
- if ("2".equals(parts[2]) && parts.length > 4 && !parts[4].isEmpty()) {
|
|
|
+ // 积分
|
|
|
+ if (parts.length > 4 && !parts[4].isEmpty()) {
|
|
|
range.setScoreAmount(Long.valueOf(parts[4]));
|
|
|
}
|
|
|
+ // 核销卷ID
|
|
|
+ if (parts.length > 5 && !parts[5].isEmpty()) {
|
|
|
+ range.setCouponId(Long.valueOf(parts[5]));
|
|
|
+ }
|
|
|
|
|
|
result.add(range);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据匹配到的时间段,组合发放奖励(红包/积分/核销卷)
|
|
|
+ */
|
|
|
+ private R dispatchRecordRewards(RecordTimeRangeVO matched, FsUser user, LiveRedPacketParam param,
|
|
|
+ LiveWatchUser watchUser, LiveWatchConfig config) {
|
|
|
+ boolean hasReward = false;
|
|
|
+ R lastResult = R.ok();
|
|
|
+ // 组合 rewardType: 1=红包, 2=积分, 4=核销卷, 组合值相加
|
|
|
+ int combinedRewardType = 0;
|
|
|
+
|
|
|
+ // 红包
|
|
|
+ if (matched.getRewardTypes() != null && matched.getRewardTypes().contains("1")) {
|
|
|
+ WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
|
|
|
+ if (StringUtil.strIsNullOrEmpty(user.getAppOpenId())) {
|
|
|
+ return R.error("请重新登录app");
|
|
|
+ }
|
|
|
+ String openId = user.getAppOpenId();
|
|
|
+ packetParam.setOpenId(openId);
|
|
|
+ BeanUtils.copyProperties(param, packetParam);
|
|
|
+ config.setRedPacketAmount(matched.getRedPacketAmount());
|
|
|
+ lastResult = sendAppLiveRedPacketAuto(packetParam, watchUser, config, param);
|
|
|
+// lastResult = R.ok();
|
|
|
+ hasReward = true;
|
|
|
+ combinedRewardType |= 1;
|
|
|
+ }
|
|
|
+ // 积分
|
|
|
+ if (matched.getRewardTypes() != null && matched.getRewardTypes().contains("2")) {
|
|
|
+ config.setScoreAmount(matched.getScoreAmount());
|
|
|
+ lastResult = sendLiveIntegralReward(param, user, watchUser, config);
|
|
|
+ hasReward = true;
|
|
|
+ combinedRewardType |= 2;
|
|
|
+ }
|
|
|
+ // 核销卷
|
|
|
+ if (matched.getRewardTypes() != null && matched.getRewardTypes().contains("3")) {
|
|
|
+ lastResult=sendIssueCoupon(param.getLiveId(),user.getUserId(),matched.getCouponId(),watchUser);
|
|
|
+ hasReward = true;
|
|
|
+ combinedRewardType |= 4;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!hasReward) {
|
|
|
+ return R.error("当前时间段,不满足领取奖励条件");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 覆盖为组合后的 rewardType,标记实际发放了哪些奖励
|
|
|
+ watchUser.setRewardType(combinedRewardType);
|
|
|
+ watchUser.setSendType(1);
|
|
|
+ watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+ return lastResult;
|
|
|
+ }
|
|
|
+
|
|
|
private static int toMinutes(String timeStr) {
|
|
|
String[] parts = timeStr.split(":");
|
|
|
return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
|
|
|
@@ -498,6 +527,81 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 发放优惠卷
|
|
|
+ */
|
|
|
+ public R sendIssueCoupon(Long liveId, Long userId, Long couponId,LiveWatchUser watchUser) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ LiveCoupon coupon = liveCouponService.selectLiveCouponById(couponId);
|
|
|
+ if (coupon == null) {
|
|
|
+ watchUser.setRemark("核销卷不存在-发放失败");
|
|
|
+ watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ //限制领取次数
|
|
|
+ Integer limitReceiveCount = coupon.getLimitReceiveCount();
|
|
|
+
|
|
|
+ //查寻领取次数
|
|
|
+ LiveCouponUser couponUser = new LiveCouponUser();
|
|
|
+ couponUser.setCouponId(couponId);
|
|
|
+ couponUser.setUserId(userId.intValue());
|
|
|
+
|
|
|
+
|
|
|
+ List<LiveCouponUser> liveCouponUsers = liveCouponUserService.selectLiveCouponUserListVO(couponUser);
|
|
|
+ if (liveCouponUsers != null && liveCouponUsers.size() >= limitReceiveCount) {
|
|
|
+ watchUser.setRemark("该核销卷领取达到上限,发放失败");
|
|
|
+ watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ LiveCouponIssue couponIssue = liveCouponIssueService.selectLiveCouponIssueByCouponIdByStatus(couponId);
|
|
|
+ if (couponIssue == null || couponIssue.getStatus() == null || couponIssue.getStatus() != 1) {
|
|
|
+ watchUser.setRemark("核销卷未发布或已删除-发放失败");
|
|
|
+ watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (couponIssue.getRemainCount() <= 0) {
|
|
|
+ watchUser.setRemark("核销卷剩余数量不足-发放失败");
|
|
|
+ watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Date now = new Date();
|
|
|
+
|
|
|
+ couponUser.setCouponTitle(coupon.getTitle());
|
|
|
+ couponUser.setCouponPrice(coupon.getCouponPrice());
|
|
|
+ couponUser.setUseMinPrice(coupon.getUseMinPrice());
|
|
|
+ if (couponIssue.getLimitTime() != null) {
|
|
|
+ couponUser.setLimitTime(couponIssue.getLimitTime());
|
|
|
+ } else if (coupon.getCouponTime() != null) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(now);
|
|
|
+ cal.add(Calendar.DATE, coupon.getCouponTime().intValue());
|
|
|
+ couponUser.setLimitTime(cal.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ couponUser.setType(COMPLETION_COUPON_TYPE+"-live-"+liveId);
|
|
|
+ couponUser.setStatus(0);
|
|
|
+ couponUser.setIsFail(0);
|
|
|
+ couponUser.setIsDel(0);
|
|
|
+ couponUser.setCreateTime(now);
|
|
|
+
|
|
|
+ //库存 remain_count
|
|
|
+ couponIssue.setRemainCount(couponIssue.getRemainCount()-1);
|
|
|
+ liveCouponIssueService.updateLiveCouponIssue(couponIssue);
|
|
|
+
|
|
|
+ //录入直播核销卷发放记录
|
|
|
+ liveCouponUserService.insertLiveCouponUser(couponUser);
|
|
|
+
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public R syncLiveRedPacket(String outBatchNo, String batchId) {
|
|
|
LiveRedPacketLog log = redPacketLogMapper.selectLiveRedPacketLogByBatchNo(outBatchNo);
|
|
|
@@ -611,9 +715,9 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
if (sendRedPacket.get("code").equals(200)) {
|
|
|
|
|
|
// 更新观看记录的奖励类型
|
|
|
- watchUser.setRewardType(1);
|
|
|
- watchUser.setSendType(1);
|
|
|
- watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+// watchUser.setRewardType(1);
|
|
|
+// watchUser.setSendType(1);
|
|
|
+// watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
|
|
|
LiveRedPacketLog liveRedPacketLog = new LiveRedPacketLog();
|
|
|
TransferBillsResult transferBillsResult;
|
|
|
@@ -667,9 +771,9 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
if (sendRedPacket.get("code").equals(200)) {
|
|
|
|
|
|
// 更新观看记录的奖励类型
|
|
|
- watchUser.setRewardType(1);
|
|
|
- watchUser.setSendType(1);
|
|
|
- watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+// watchUser.setRewardType(1);
|
|
|
+// watchUser.setSendType(1);
|
|
|
+// watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
|
|
|
LiveRedPacketLog liveRedPacketLog = new LiveRedPacketLog();
|
|
|
TransferBillsResult transferBillsResult;
|
|
|
@@ -706,10 +810,10 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
- // 更新直播观看记录的奖励类型
|
|
|
- watchUser.setRewardType(1);
|
|
|
- watchUser.setSendType(1);
|
|
|
- watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+// // 更新直播观看记录的奖励类型
|
|
|
+// watchUser.setRewardType(1);
|
|
|
+// watchUser.setSendType(1);
|
|
|
+// watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
|
|
|
LiveRedPacketLog liveRedPacketLog = new LiveRedPacketLog();
|
|
|
// 添加红包记录
|
|
|
@@ -806,10 +910,10 @@ public class LiveRedPacketLogServiceImpl extends ServiceImpl<LiveRedPacketLogMap
|
|
|
integralLogs.setCreateTime(new Date());
|
|
|
fsUserIntegralLogsMapper.insertFsUserIntegralLogs(integralLogs);
|
|
|
|
|
|
- //更新看课记录的奖励类型
|
|
|
- watchUser.setRewardType(2);
|
|
|
- watchUser.setSendType(1);
|
|
|
- watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
+// //更新看课记录的奖励类型
|
|
|
+// watchUser.setRewardType(2);
|
|
|
+// watchUser.setSendType(1);
|
|
|
+// watchUserMapper.updateLiveWatchUser(watchUser);
|
|
|
logger.info("发放奖励====================》直播看课记录,{}", watchUser);
|
|
|
|
|
|
//积分转换红包
|