|
|
@@ -1,5 +1,6 @@
|
|
|
package com.fs.app.service;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
@@ -9,13 +10,13 @@ import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.common.utils.date.DateUtil;
|
|
|
import com.fs.common.utils.http.HttpUtils;
|
|
|
import com.fs.common.utils.spring.SpringUtils;
|
|
|
+import com.fs.company.domain.Company;
|
|
|
import com.fs.company.domain.CompanyMiniapp;
|
|
|
+import com.fs.company.mapper.CompanyMapper;
|
|
|
import com.fs.company.service.ICompanyMiniappService;
|
|
|
import com.fs.config.ai.AiHostProper;
|
|
|
-import com.fs.course.domain.FsCoursePlaySourceConfig;
|
|
|
-import com.fs.course.domain.FsCourseWatchLog;
|
|
|
-import com.fs.course.domain.FsUserCourseVideo;
|
|
|
-import com.fs.course.domain.LuckyBagCollectRecord;
|
|
|
+import com.fs.course.config.CourseConfig;
|
|
|
+import com.fs.course.domain.*;
|
|
|
import com.fs.course.mapper.FsCoursePlaySourceConfigMapper;
|
|
|
import com.fs.course.mapper.FsUserCourseVideoMapper;
|
|
|
import com.fs.course.service.IFsCoursePlaySourceConfigService;
|
|
|
@@ -31,6 +32,7 @@ import com.fs.qw.domain.QwExternalContact;
|
|
|
import com.fs.qw.domain.QwUser;
|
|
|
import com.fs.qw.domain.QwUserVideo;
|
|
|
import com.fs.qw.mapper.LuckyBagCollectRecordMapper;
|
|
|
+import com.fs.qw.mapper.LuckyBagMapper;
|
|
|
import com.fs.qw.mapper.QwExternalContactMapper;
|
|
|
import com.fs.qw.mapper.QwUserMapper;
|
|
|
import com.fs.qw.service.IQwUserService;
|
|
|
@@ -40,6 +42,7 @@ import com.fs.qw.vo.QwSopCourseFinishTempSetting;
|
|
|
import com.fs.sop.domain.QwSopLogs;
|
|
|
import com.fs.sop.service.IQwSopLogsService;
|
|
|
import com.fs.sop.service.impl.QwSopLogsServiceImpl;
|
|
|
+import com.fs.system.service.ISysConfigService;
|
|
|
import com.fs.wxwork.dto.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -49,12 +52,15 @@ import org.json.JSONObject;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.*;
|
|
|
import java.util.Optional;
|
|
|
import java.util.*;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -73,6 +79,9 @@ public class IpadSendServer {
|
|
|
private final ICompanyMiniappService companyMiniappService;
|
|
|
private final AiHookService aiHookService;
|
|
|
private final AiHostProper aiHostProper;
|
|
|
+ private final ISysConfigService configService;
|
|
|
+ private final LuckyBagMapper luckyBagMapper;
|
|
|
+ private final CompanyMapper companyMapper;
|
|
|
private final ExecutorService executor = new ThreadPoolExecutor(
|
|
|
8, 32, 60L, TimeUnit.SECONDS,
|
|
|
new LinkedBlockingQueue<>(1000),
|
|
|
@@ -759,7 +768,7 @@ public class IpadSendServer {
|
|
|
break;
|
|
|
case "14":
|
|
|
// 记录福袋发送记录
|
|
|
- addLuckyBagCollectRecord(vo, content,qwSopLogs);
|
|
|
+ addLuckyBagCollectRecord(qwUser, content,qwSopLogs);
|
|
|
// 福袋
|
|
|
sendMiniProgram(vo, content, miniMap);
|
|
|
break;
|
|
|
@@ -777,14 +786,17 @@ public class IpadSendServer {
|
|
|
content.setSendRemarks("发送失败:" + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- private void addLuckyBagCollectRecord(BaseVo vo,QwSopCourseFinishTempSetting.Setting content,QwSopLogs qwSopLogs) {
|
|
|
- try {
|
|
|
- // 写入福袋发放记录
|
|
|
- LuckyBagCollectRecord luckyBagCollectRecord = luckyBagCollectRecordMapper.selectLuckyBagCollectRecordById(Long.valueOf(content.getBusinessId()));
|
|
|
- luckyBagCollectRecord.setSendTime(new Date());
|
|
|
- luckyBagCollectRecord.setCollectType("0");
|
|
|
- luckyBagCollectRecordMapper.updateLuckyBagCollectRecord(luckyBagCollectRecord);
|
|
|
+ private void addLuckyBagCollectRecord(QwUser qwUser, QwSopCourseFinishTempSetting.Setting content, QwSopLogs qwSopLogs) {
|
|
|
|
|
|
+ try {
|
|
|
+ String json = configService.selectConfigByKey("course.config");
|
|
|
+ CourseConfig config = JSON.parseObject(json, CourseConfig.class);
|
|
|
+ Date updateTime = createUpdateTime(content, new Date(), config);
|
|
|
+ LuckyBag luckyBag = luckyBagMapper.selectLuckyBagById(content.getLuckyBagId());
|
|
|
+ String companyUserId = String.valueOf(qwUser.getCompanyUserId()).trim();
|
|
|
+ String companyId = String.valueOf(qwUser.getCompanyId()).trim();
|
|
|
+ Long businessId = addLuckyBagCollectRecord(qwUser,luckyBag,content,qwSopLogs,updateTime,companyUserId,companyId,content.getChatId());
|
|
|
+ content.setMiniprogramPage(quickProcess(content.getMiniprogramPage(),businessId));
|
|
|
}catch (Exception e){
|
|
|
log.error("ID:" + qwSopLogs.getId() + "-添加福袋记录失败", e);
|
|
|
}
|
|
|
@@ -793,4 +805,191 @@ public class IpadSendServer {
|
|
|
public void loginOut(QwUser user) {
|
|
|
ipadSendUtils.loginOut(user.getUid(), user.getServerId());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 过期时间
|
|
|
+ *
|
|
|
+ * @param setting
|
|
|
+ * @param sendTime
|
|
|
+ * @param config
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Date createUpdateTime(QwSopCourseFinishTempSetting.Setting setting, Date sendTime, CourseConfig config) {
|
|
|
+
|
|
|
+ Integer expireDays = (setting.getExpiresDays() == null || setting.getExpiresDays() == 0)
|
|
|
+ ? config.getVideoLinkExpireDate()
|
|
|
+ : setting.getExpiresDays();
|
|
|
+
|
|
|
+// 使用 Java 8 时间 API 计算过期时间
|
|
|
+ LocalDateTime sendDateTime = sendTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
|
|
+ LocalDateTime expireDateTime = sendDateTime.plusDays(expireDays - 1);
|
|
|
+ expireDateTime = expireDateTime.toLocalDate().atTime(23, 59, 59);
|
|
|
+ Date updateTime = Date.from(expireDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
+
|
|
|
+ return updateTime;
|
|
|
+ }
|
|
|
+ private static final Pattern LINK_PATTERN =
|
|
|
+ Pattern.compile("link=([^&]*)");
|
|
|
+ /**
|
|
|
+ * 快速处理方法(使用fastjson)
|
|
|
+ */
|
|
|
+ public static String quickProcess(String miniprogramPage, Long businessId) {
|
|
|
+ if (miniprogramPage == null || businessId == null) {
|
|
|
+ return miniprogramPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Matcher matcher = LINK_PATTERN.matcher(miniprogramPage);
|
|
|
+ if (matcher.find()) {
|
|
|
+ String encodedLink = matcher.group(1);
|
|
|
+ String decodedLink = java.net.URLDecoder.decode(encodedLink, "UTF-8");
|
|
|
+
|
|
|
+ // 解析JSON
|
|
|
+ com.alibaba.fastjson.JSONObject linkJson = JSON.parseObject(decodedLink);
|
|
|
+
|
|
|
+ // 检查并添加businessId
|
|
|
+ if (!linkJson.containsKey("businessId") ||
|
|
|
+ linkJson.getString("businessId").isEmpty()) {
|
|
|
+ linkJson.put("businessId", businessId);
|
|
|
+
|
|
|
+ // 重新编码
|
|
|
+ String updatedJson = linkJson.toJSONString();
|
|
|
+ String updatedEncoded = java.net.URLEncoder.encode(updatedJson, "UTF-8");
|
|
|
+
|
|
|
+ // 替换原link参数
|
|
|
+ return miniprogramPage.replaceFirst(
|
|
|
+ "link=" + Pattern.quote(encodedLink),
|
|
|
+ "link=" + updatedEncoded
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return miniprogramPage;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增加福袋发放记录、领取记录
|
|
|
+ *
|
|
|
+ * @param companyId
|
|
|
+ * @param qwUser
|
|
|
+ * @param luckyBag
|
|
|
+ * @param content
|
|
|
+ * @param qwSopLogs
|
|
|
+ * @param sendTime
|
|
|
+ * @param companyUserId
|
|
|
+ * @param chatId
|
|
|
+ */
|
|
|
+ private Long addLuckyBagCollectRecord(QwUser qwUser, LuckyBag luckyBag, QwSopCourseFinishTempSetting.Setting content,
|
|
|
+ QwSopLogs qwSopLogs,
|
|
|
+ Date sendTime,
|
|
|
+ String companyUserId,
|
|
|
+ String companyId,
|
|
|
+ String chatId) {
|
|
|
+ try {
|
|
|
+ // 参数校验
|
|
|
+ if (content == null || qwSopLogs == null || sendTime == null) {
|
|
|
+ log.warn("添加福袋记录失败:必要参数为空 [content:{}, qwSopLogs:{}, sendTime:{}]",
|
|
|
+ content, qwSopLogs, sendTime);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证福袋ID
|
|
|
+ if (content.getLuckyBagId() == null) {
|
|
|
+ log.warn("福袋ID为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询福袋信息
|
|
|
+ if (luckyBag == null) {
|
|
|
+ log.warn("未找到对应的福袋信息 [luckyBagId:{}]", content.getLuckyBagId());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查福袋状态
|
|
|
+ if (luckyBag.getDataStatus() != null && luckyBag.getDataStatus().equals(0)) {
|
|
|
+ log.warn("福袋被禁用 [luckyBagId:{}]", content.getLuckyBagId());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询公司信息
|
|
|
+ Company company = companyMapper.selectCompanyById(Long.valueOf(companyId));
|
|
|
+
|
|
|
+ // 构建福袋记录
|
|
|
+ LuckyBagCollectRecord luckyBagCollectRecord = buildLuckyBagRecord(qwUser,content, qwSopLogs, sendTime,
|
|
|
+ companyUserId, companyId, chatId, company, luckyBag);
|
|
|
+ luckyBagCollectRecord.setSendTime(new Date());
|
|
|
+ luckyBagCollectRecord.setCollectType("0");
|
|
|
+ // 插入记录并返回ID
|
|
|
+ int result = luckyBagCollectRecordMapper.insertLuckyBagCollectRecord(luckyBagCollectRecord);
|
|
|
+ if (result <= 0) {
|
|
|
+ log.warn("福袋记录插入失败 [luckyBagId:{}]", content.getLuckyBagId());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回新增记录的ID
|
|
|
+ Long recordId = luckyBagCollectRecord.getId();
|
|
|
+ if (recordId == null) {
|
|
|
+ log.warn("福袋记录插入成功但未返回ID [luckyBagId:{}]", content.getLuckyBagId());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("福袋记录添加成功 [recordId:{}, luckyBagId:{}]", recordId, content.getLuckyBagId());
|
|
|
+ return recordId;
|
|
|
+
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.error("ID转换失败 [companyId:{}, companyUserId:{}]", companyId, companyUserId, e);
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("ID:" + (content != null ? content.getLuckyBagId() : "unknown") + "-添加福袋记录失败", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建福袋记录对象
|
|
|
+ */
|
|
|
+ private LuckyBagCollectRecord buildLuckyBagRecord(QwUser qwUser,QwSopCourseFinishTempSetting.Setting content,
|
|
|
+ QwSopLogs qwSopLogs,
|
|
|
+ Date sendTime,
|
|
|
+ String companyUserId,
|
|
|
+ String companyId,
|
|
|
+ String chatId,
|
|
|
+ Company company,
|
|
|
+ LuckyBag luckyBag) {
|
|
|
+ LuckyBagCollectRecord record = new LuckyBagCollectRecord();
|
|
|
+ record.setQwUserId(qwUser.getQwUserId());
|
|
|
+ record.setQwUserName(qwUser.getQwUserName());
|
|
|
+ record.setLuckyBagId(content.getLuckyBagId());
|
|
|
+ record.setExpiryTime(sendTime);
|
|
|
+ record.setCollectType("3");
|
|
|
+ record.setCompanyId(Long.valueOf(companyId));
|
|
|
+ record.setUserId(qwSopLogs.getFsUserId());
|
|
|
+ if (ObjectUtil.isNotEmpty(qwSopLogs.getFsUserId())){
|
|
|
+ FsUser fsUser = fsUserMapper.selectFsUserByUserId(qwSopLogs.getFsUserId());
|
|
|
+ record.setUserName(ObjectUtil.isNotEmpty(fsUser)?fsUser.getNickName():null);
|
|
|
+ }
|
|
|
+ record.setCompanyName(company.getCompanyName());
|
|
|
+ record.setCompanyUserId(Long.valueOf(companyUserId));
|
|
|
+ record.setSendLink(content.getMiniprogramPage());
|
|
|
+
|
|
|
+ // 设置奖励类型和聊天信息
|
|
|
+ if (StringUtils.isNotEmpty(chatId)) {
|
|
|
+ record.setRewardType(1L);
|
|
|
+ record.setChatId(chatId);
|
|
|
+ record.setExternalUserName(qwSopLogs.getExternalUserName());
|
|
|
+ } else {
|
|
|
+ record.setRewardType(2L);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置币种金额
|
|
|
+ if (luckyBag.getRewardType() != null && luckyBag.getRewardType().equals("1")) {
|
|
|
+ record.setCoinAmount(luckyBag.getAmount());
|
|
|
+ }
|
|
|
+
|
|
|
+ return record;
|
|
|
+ }
|
|
|
}
|