|
|
@@ -1,16 +1,30 @@
|
|
|
package com.fs.his.task;
|
|
|
|
|
|
|
|
|
+
|
|
|
import com.fs.hisStore.mapper.FsStoreOrderScrmMapper;
|
|
|
import com.fs.hisStore.vo.FsStoreOrderScrmPhoneAndOrderNumVO;
|
|
|
-import com.fs.qw.mapper.FsCompanyCustomerMapper;
|
|
|
+import com.fs.qw.domain.QuickTagTask;
|
|
|
+import com.fs.qw.domain.QuickTagTaskLog;
|
|
|
+import com.fs.qw.domain.QwExternalContact;
|
|
|
+import com.fs.qw.mapper.*;
|
|
|
+import com.fs.qwApi.domain.QwResult;
|
|
|
+import com.fs.qwApi.param.QwEditUserTagParam;
|
|
|
+import com.fs.qwApi.service.QwApiService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.time.DateUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商城小程序相关定时任务
|
|
|
+ * */
|
|
|
@Slf4j
|
|
|
@Component("StoreTask")
|
|
|
public class StoreTask {
|
|
|
@@ -20,6 +34,18 @@ public class StoreTask {
|
|
|
|
|
|
@Autowired
|
|
|
private FsCompanyCustomerMapper fsCompanyCustomerMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QuickTagTaskMapper quickTagTaskMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QuickTagTaskLogMapper quickTagTaskLogMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QwExternalContactMapper qwExternalContactMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QwApiService qwApiService;
|
|
|
|
|
|
/**
|
|
|
* 同步客户信息表客户的有效下单数
|
|
|
@@ -34,4 +60,127 @@ public class StoreTask {
|
|
|
//根据手机号批量更新客户信息表客户有效下单数
|
|
|
int updateResult=fsCompanyCustomerMapper.updateCompanyUserBuyCountByUserPhone(fsStoreOrderScrmPhoneAndOrderNumVOS);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 简洁版自动打标签定时任务
|
|
|
+ */
|
|
|
+ public void quickTagTask() {
|
|
|
+ log.info("========== 简洁版自动打标签任务开始 ==========");
|
|
|
+ // 查询所有任务(若需状态过滤,可添加条件)
|
|
|
+ QuickTagTask query = new QuickTagTask();
|
|
|
+ List<QuickTagTask> taskList = quickTagTaskMapper.selectQuickTagTaskList(query);
|
|
|
+ if (CollectionUtils.isEmpty(taskList)) {
|
|
|
+ log.info("无自动打标签任务,结束");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历每个任务,单个任务异常不影响其他任务
|
|
|
+ for (QuickTagTask task : taskList) {
|
|
|
+ try {
|
|
|
+ executeSingleTask(task);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("执行任务失败,任务ID: {}, 任务详情: {}", task.getId(), task, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("========== 简洁版自动打标签任务结束 ==========");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行单个打标签任务
|
|
|
+ */
|
|
|
+ private void executeSingleTask(QuickTagTask task) {
|
|
|
+ log.info("开始执行任务 ID: {}, corpId: {}", task.getId(), task.getCorpId());
|
|
|
+
|
|
|
+ // 1. 校验必要参数
|
|
|
+ Integer executionDays = task.getExecutionDays();
|
|
|
+ if (executionDays == null || executionDays < 0) {
|
|
|
+ log.warn("任务 {} 执行天数为空或无效,跳过", task.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String tagIdStr = task.getTagId();
|
|
|
+ if (StringUtils.isEmpty(tagIdStr)) {
|
|
|
+ log.warn("任务 {} 未配置标签,跳过", task.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> tagIds = Arrays.stream(tagIdStr.split(","))
|
|
|
+ .filter(StringUtils::isNotBlank)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (tagIds.isEmpty()) {
|
|
|
+ log.warn("任务 {} 标签列表为空,跳过", task.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 计算触发日期范围(查询过去某一天的客户)
|
|
|
+ Date zeroToday = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
|
|
|
+ // 目标日期:今天减去 executionDays 天
|
|
|
+ Date targetDate = DateUtils.addDays(zeroToday, -executionDays);
|
|
|
+ // 查询范围:targetDate 00:00:00 ~ targetDate 23:59:59
|
|
|
+ Date startTime = targetDate;
|
|
|
+ Date endTime = DateUtils.addDays(targetDate, 1); // 加1天即为第二天的零点
|
|
|
+
|
|
|
+ // 3. 查询该时间段内新增的客户
|
|
|
+ List<QwExternalContact> contactList = qwExternalContactMapper.selectQwExternalContactListByCreateTime(startTime, endTime);
|
|
|
+ if (CollectionUtils.isEmpty(contactList)) {
|
|
|
+ log.info("任务 {} 在 {} 至 {} 期间无新增客户", task.getId(), startTime, endTime);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("任务 {} 命中 {} 个客户,开始逐个打标签", task.getId(), contactList.size());
|
|
|
+ int successCount = 0;
|
|
|
+ int failCount = 0;
|
|
|
+
|
|
|
+ // 4. 遍历客户逐个打标签(单个失败不影响其他)
|
|
|
+ for (QwExternalContact contact : contactList) {
|
|
|
+ boolean apiSuccess = false;
|
|
|
+ String errorMsg = null;
|
|
|
+ try {
|
|
|
+ // 4.1 调用企微API打标签
|
|
|
+ QwEditUserTagParam param = new QwEditUserTagParam();
|
|
|
+ param.setUserid(contact.getUserId());
|
|
|
+ param.setExternal_userid(contact.getExternalUserId());
|
|
|
+ param.setAdd_tag(tagIds);
|
|
|
+
|
|
|
+ QwResult result = qwApiService.editUserTag(param, task.getCorpId());
|
|
|
+ if (result != null && result.getErrcode() == 0) {
|
|
|
+ apiSuccess = true;
|
|
|
+ log.debug("客户 {} 打标签成功", contact.getExternalUserId());
|
|
|
+ } else {
|
|
|
+ errorMsg = result != null ? result.getErrmsg() : "API返回空";
|
|
|
+ log.warn("客户 {} 打标签失败: errcode={}, errmsg={}",
|
|
|
+ contact.getExternalUserId(),
|
|
|
+ result != null ? result.getErrcode() : "null",
|
|
|
+ errorMsg);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ errorMsg = e.getMessage();
|
|
|
+ log.error("客户 {} 打标签发生异常", contact.getExternalUserId(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4.2 记录日志(无论成败均记录,可扩展表字段存储状态和错误信息)
|
|
|
+ try {
|
|
|
+ QuickTagTaskLog logEntry = new QuickTagTaskLog();
|
|
|
+ logEntry.setAutoTagId(task.getId());
|
|
|
+ logEntry.setType(1); // 可定义为常量 TagLogType.ADD_FRIEND
|
|
|
+ logEntry.setQwUserid(contact.getQwUserId());
|
|
|
+ logEntry.setExternalUserId(contact.getExternalUserId());
|
|
|
+ logEntry.setAddTime(contact.getCreateTime()); // 记录客户原始添加时间
|
|
|
+ logEntry.setCompanyId(contact.getCompanyId());
|
|
|
+ logEntry.setCorpId(task.getCorpId());
|
|
|
+ logEntry.setStatus(apiSuccess ? 1 : 0);
|
|
|
+ logEntry.setErrorMsg(apiSuccess ? null : errorMsg);
|
|
|
+ quickTagTaskLogMapper.insertQuickTagTaskLog(logEntry);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 日志插入失败不应影响打标签业务,仅记录错误
|
|
|
+ log.error("插入打标签日志失败,客户: {}, 任务: {}", contact.getExternalUserId(), task.getId(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (apiSuccess) {
|
|
|
+ successCount++;
|
|
|
+ } else {
|
|
|
+ failCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("任务 {} 执行完成,成功: {}, 失败: {}", task.getId(), successCount, failCount);
|
|
|
+ }
|
|
|
}
|