|
@@ -25,6 +25,7 @@ import com.storlead.trade.service.CustomerTradeService;
|
|
|
import com.storlead.trade.service.CustomerAnalysisResultEntityService;
|
|
import com.storlead.trade.service.CustomerAnalysisResultEntityService;
|
|
|
import com.storlead.trade.service.LiaisonTradeService;
|
|
import com.storlead.trade.service.LiaisonTradeService;
|
|
|
import com.storlead.trade.service.SysDictService;
|
|
import com.storlead.trade.service.SysDictService;
|
|
|
|
|
+import com.storlead.trade.vo.CustomerMailThreadVO;
|
|
|
import com.storlead.trade.vo.CustomerSourceDistributionVO;
|
|
import com.storlead.trade.vo.CustomerSourceDistributionVO;
|
|
|
import com.storlead.trade.vo.CustomerVO;
|
|
import com.storlead.trade.vo.CustomerVO;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
@@ -38,6 +39,7 @@ import java.time.LocalDate;
|
|
|
import java.time.ZoneId;
|
|
import java.time.ZoneId;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
@@ -299,7 +301,10 @@ public class CustomerTradeServiceImpl
|
|
|
List<Long> allEmailIdList = extractAllEmailIds(sentEmailList);
|
|
List<Long> allEmailIdList = extractAllEmailIds(sentEmailList);
|
|
|
vo.setEmailIdList(allEmailIdList);
|
|
vo.setEmailIdList(allEmailIdList);
|
|
|
// 邮件详情:按邮件id去 B 服务拉取;失败降级为空数组,不影响客户详情主流程
|
|
// 邮件详情:按邮件id去 B 服务拉取;失败降级为空数组,不影响客户详情主流程
|
|
|
- vo.setEmailDetailList(findMailDetailList(allEmailIdList));
|
|
|
|
|
|
|
+ List<MailMessageDTO> emailDetailList = findMailDetailList(allEmailIdList);
|
|
|
|
|
+ vo.setEmailDetailList(emailDetailList);
|
|
|
|
|
+ // 二级结构:一级=本方发出的邮件,二级=该邮件的客户回复列表
|
|
|
|
|
+ vo.setMailThreadList(buildMailThreadList(sentEmailList, emailDetailList));
|
|
|
// 最后联系时间 / 天数:取互动记录首条,省掉一次查询
|
|
// 最后联系时间 / 天数:取互动记录首条,省掉一次查询
|
|
|
Date lastContactTime = sentEmailList.isEmpty() ? null : sentEmailList.get(0).getExpectSendTime();
|
|
Date lastContactTime = sentEmailList.isEmpty() ? null : sentEmailList.get(0).getExpectSendTime();
|
|
|
vo.setLastContactTime(lastContactTime);
|
|
vo.setLastContactTime(lastContactTime);
|
|
@@ -347,18 +352,114 @@ public class CustomerTradeServiceImpl
|
|
|
* 单个邮件id的安全转换(Long 或数字字符串均可);null / 空白 / 非法值直接忽略。
|
|
* 单个邮件id的安全转换(Long 或数字字符串均可);null / 空白 / 非法值直接忽略。
|
|
|
*/
|
|
*/
|
|
|
private void addEmailId(Set<Long> idSet, Object value) {
|
|
private void addEmailId(Set<Long> idSet, Object value) {
|
|
|
|
|
+ Long id = parseEmailId(value);
|
|
|
|
|
+ if (id != null) {
|
|
|
|
|
+ idSet.add(id);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 单个邮件id的安全转换(Long 或数字字符串均可);null / 空白 / 非法值直接忽略。
|
|
|
|
|
+ */
|
|
|
|
|
+ private Long parseEmailId(Object value) {
|
|
|
if (value == null) {
|
|
if (value == null) {
|
|
|
- return;
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
String text = String.valueOf(value).trim();
|
|
String text = String.valueOf(value).trim();
|
|
|
if (text.isEmpty()) {
|
|
if (text.isEmpty()) {
|
|
|
- return;
|
|
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
- idSet.add(Long.valueOf(text));
|
|
|
|
|
|
|
+ return Long.valueOf(text);
|
|
|
} catch (NumberFormatException ex) {
|
|
} catch (NumberFormatException ex) {
|
|
|
- log.warn("[extractAllEmailIds] 非法邮件id已忽略: {}", text);
|
|
|
|
|
|
|
+ log.warn("[parseEmailId] 非法邮件id已忽略: {}", text);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 把「已发送邮件列表 + 邮件详情列表」拼成二级结构:
|
|
|
|
|
+ * <b>一级就是本方发出的那封邮件</b>(邮件详情字段平铺 + 少量营销侧字段),
|
|
|
|
|
+ * 二级是它收到的客户回复(reply_email_ids 展开后,从邮件详情里取)。
|
|
|
|
|
+ * <p>
|
|
|
|
|
+ * 形如:发送邮件 1 收到回复 2、3 → {@code {1: [2,3]}}。
|
|
|
|
|
+ * 顺序与 sentEmailList 一致(按发送时间倒序);邮件服务取不到详情时用营销侧记录兜底,不影响主流程。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param sentEmailList 该客户全部已发送成功邮件
|
|
|
|
|
+ * @param emailDetailList 邮件服务返回的邮件详情(一级邮件和回复邮件都在这份结果里,靠 id 区分)
|
|
|
|
|
+ * @return 二级结构列表;无数据为空数组(不返回 null)
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<CustomerMailThreadVO> buildMailThreadList(List<MarketEmailsEntity> sentEmailList,
|
|
|
|
|
+ List<MailMessageDTO> emailDetailList) {
|
|
|
|
|
+ List<CustomerMailThreadVO> threadList = new ArrayList<>();
|
|
|
|
|
+ if (CollectionUtils.isEmpty(sentEmailList)) {
|
|
|
|
|
+ return threadList;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 邮件详情按 id 建索引:一级邮件和回复邮件都在这份详情里,靠 id 区分
|
|
|
|
|
+ Map<Long, MailMessageDTO> detailMap = new HashMap<>();
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(emailDetailList)) {
|
|
|
|
|
+ for (MailMessageDTO detail : emailDetailList) {
|
|
|
|
|
+ if (detail != null && detail.getId() != null) {
|
|
|
|
|
+ detailMap.put(detail.getId(), detail);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (MarketEmailsEntity sent : sentEmailList) {
|
|
|
|
|
+ if (sent == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ CustomerMailThreadVO thread = new CustomerMailThreadVO();
|
|
|
|
|
+ MailMessageDTO detail = sent.getEmailId() == null ? null : detailMap.get(sent.getEmailId());
|
|
|
|
|
+ if (detail != null) {
|
|
|
|
|
+ // 一级 = 这封发送邮件的详情本身,字段全部平铺到节点上
|
|
|
|
|
+ BeanUtils.copyProperties(detail, thread);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 邮件服务没返回详情(未拉取到/已删除)时,用营销侧记录兜底填充关键字段,保证一级不丢
|
|
|
|
|
+ thread.setId(sent.getEmailId());
|
|
|
|
|
+ thread.setSubject(sent.getSubject());
|
|
|
|
|
+ thread.setContent(sent.getContent());
|
|
|
|
|
+ thread.setRecipient(sent.getRecipient());
|
|
|
|
|
+ thread.setEmailSendTime(sent.getExpectSendTime());
|
|
|
|
|
+ }
|
|
|
|
|
+ thread.setMarketEmailsId(sent.getId());
|
|
|
|
|
+ thread.setExpectSendTime(sent.getExpectSendTime());
|
|
|
|
|
+ thread.setEmailStatus(sent.getStatus());
|
|
|
|
|
+
|
|
|
|
|
+ // 二级:reply_email_ids 按原顺序展开并去重;邮件服务没返回该 id 的详情则跳过
|
|
|
|
|
+ List<MailMessageDTO> replyList = new ArrayList<>();
|
|
|
|
|
+ Set<Long> seen = new LinkedHashSet<>();
|
|
|
|
|
+ if (StringUtils.hasText(sent.getReplyEmailIds())) {
|
|
|
|
|
+ for (String part : sent.getReplyEmailIds().split(",")) {
|
|
|
|
|
+ Long replyId = parseEmailId(part);
|
|
|
|
|
+ if (replyId == null || !seen.add(replyId)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ MailMessageDTO reply = detailMap.get(replyId);
|
|
|
|
|
+ if (reply != null) {
|
|
|
|
|
+ replyList.add(reply);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 回复按时间正序(先回的在前),时间取不到的排最后,保证列表稳定
|
|
|
|
|
+ replyList.sort(Comparator.comparing(this::pickMailSendTime,
|
|
|
|
|
+ Comparator.nullsLast(Comparator.naturalOrder())));
|
|
|
|
|
+
|
|
|
|
|
+ thread.setReplyList(replyList);
|
|
|
|
|
+ thread.setReplyCount(replyList.size());
|
|
|
|
|
+ threadList.add(thread);
|
|
|
|
|
+ }
|
|
|
|
|
+ return threadList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取邮件详情的发送时间:优先 emailSendTime,取不到再退回 sendDate(有的记录只填了其中一个)。
|
|
|
|
|
+ */
|
|
|
|
|
+ private Date pickMailSendTime(MailMessageDTO mail) {
|
|
|
|
|
+ if (mail == null) {
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
+ return mail.getEmailSendTime() != null ? mail.getEmailSendTime() : mail.getSendDate();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|