ソースを参照

修改邮箱状态

YPZ 1 ヶ月 前
親
コミット
d2c149bb38

+ 1 - 1
java/storlead-mail/storlead-mail-api/src/main/java/com/storlead/mail/webclient/client/MktMailRemoteClient.java

@@ -52,7 +52,7 @@ public class MktMailRemoteClient {
     }
 
     public Result<?> send(MktSendMailRemoteDTO dto) {
-        return http.postJson(MailRemoteApiConstants.MKT_SEND, dto);
+        return http.postJson(MailRemoteApiConstants.MKT_DISPATCH_SENT, dto);
     }
 
     /**

+ 3 - 0
java/storlead-mail/storlead-mail-api/src/main/java/com/storlead/mail/webclient/constant/MailRemoteApiConstants.java

@@ -34,6 +34,9 @@ public final class MailRemoteApiConstants {
     /** 发送营销邮件(默认同步;async=true 异步),JSON:MktSendMailRemoteDTO */
     public static final String MKT_SEND = "/mkt/send";
 
+    /** 发送营销邮件不需要token(默认同步;async=true 异步),JSON:MktSendMailRemoteDTO */
+    public static final String MKT_DISPATCH_SENT = "/mkt/dispatch_sent";
+
     /** 编辑营销邮件正文(仅更新内容,不发送、不改收件人/附件),JSON:MktEditMailContentRemoteDTO */
     public static final String MKT_EDIT_CONTENT = "/mkt/edit_content";
 

+ 6 - 2
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/entity/MarketEmailsEntity.java

@@ -35,7 +35,7 @@ public class MarketEmailsEntity extends SysBaseField {
     @TableField("email_id")
     private Long emailId;
 
-    @ApiModelProperty(value = "发送状态:-3取消 -2草稿 -1待定时 0发送中 1成功 2失败;收信记录一般为1")
+    @ApiModelProperty(value = "发送状态:-3取消 -2草稿 -1定时 0发送中 1成功 2失败;收信记录一般为1")
     @TableField("status")
     private Integer status;
 
@@ -43,6 +43,10 @@ public class MarketEmailsEntity extends SysBaseField {
     @TableField("has_reader")
     private Integer hasReader;
 
+    @ApiModelProperty("主题")
+    @TableField("subject")
+    private String subject;
+
     @ApiModelProperty(value = "是否已回复:0否,1是")
     @TableField("has_reply")
     private Integer hasReply;
@@ -66,7 +70,7 @@ public class MarketEmailsEntity extends SysBaseField {
     @TableField("has_suspend")
     private Integer hasSuspend;
 
-    @ApiModelProperty(value = "发件人邮箱")
+    @ApiModelProperty(value = "收件人邮箱")
     @TableField("recipient")
     private String recipient;
 

+ 8 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/service/MarketEmailsService.java

@@ -77,4 +77,12 @@ public interface MarketEmailsService extends MyBaseService<MarketEmailsEntity> {
     @Async
     void addSendTime(MarketingCampaignEntity marketingCampaignEntity);
 
+    /***
+      * @Description: 定时发送邮件
+      * @Param:
+      * @return:
+      * @Author: YPZ
+      * @Date: 2026/9/11 16:46
+      */
+    void send();
 }

+ 82 - 10
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/service/impl/MarketEmailsServiceImpl.java

@@ -18,6 +18,8 @@ import com.storlead.knowledge.service.ChatService;
 import com.storlead.knowledge.service.WorkflowsService;
 import com.storlead.mail.pojo.entity.MktMailStatusEntity;
 import com.storlead.mail.service.MktMailStatusService;
+import com.storlead.mail.webclient.client.MktMailRemoteClient;
+import com.storlead.mail.webclient.dto.MktSendMailRemoteDTO;
 import com.storlead.trade.dto.MarketEmailsDTO;
 import com.storlead.trade.entity.*;
 import com.storlead.trade.mapper.MarketEmailsMapper;
@@ -32,6 +34,8 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.util.ObjectUtils;
 
 import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -72,6 +76,9 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
     @Resource
     private MktMailStatusService mktMailStatusService;
 
+    @Resource
+    private MktMailRemoteClient mktMailRemoteClient;
+
 
     @Override
     public Result<Object> getList(MarketEmailsDTO dto) {
@@ -720,12 +727,12 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
         markerEmailUpdateWrapper.set(MarketEmailsEntity::getStatus, marketEmailsCustomerVO.getStatus());
         this.update(null, markerEmailUpdateWrapper);
 
-        if (!CollectionUtils.isEmpty(emailIdList) && !ObjectUtils.isEmpty(marketEmailsCustomerVO.getStatus())){
-            LambdaUpdateWrapper<MktMailStatusEntity> updateWrapper = new LambdaUpdateWrapper<>();
-            updateWrapper.in(MktMailStatusEntity::getId, emailIdList);
-            updateWrapper.set(MktMailStatusEntity::getSendStatus, marketEmailsCustomerVO.getStatus());
-            mktMailStatusService.update(null, updateWrapper);
-        }
+//        if (!CollectionUtils.isEmpty(emailIdList) && !ObjectUtils.isEmpty(marketEmailsCustomerVO.getStatus())){
+//            LambdaUpdateWrapper<MktMailStatusEntity> updateWrapper = new LambdaUpdateWrapper<>();
+//            updateWrapper.in(MktMailStatusEntity::getId, emailIdList);
+//            updateWrapper.set(MktMailStatusEntity::getSendStatus, marketEmailsCustomerVO.getStatus());
+//            mktMailStatusService.update(null, updateWrapper);
+//        }
 
 
     }
@@ -847,6 +854,7 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
                     MarketEmailsEntity marketEmail = new MarketEmailsEntity();
                     marketEmail.setMarketingCampaignId(marketingCampaignEntity.getId());
                     marketEmail.setCustomerId(customerVO.getCustomerId());
+                    marketEmail.setRecipient(customerVO.getEmail());
                     marketEmail.setMarketingCampaignSopDetailId(marketingCampaignSopDetail.getId());
                     marketEmail.setExpectSendTime(scheduleStage.getBeijingDatetime());
                     marketEmailList.add(marketEmail);
@@ -859,16 +867,80 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
         }
         if (!marketEmailList.isEmpty()){
             //删除原有营销关联的邮件信息
-            LambdaUpdateWrapper<MarketEmailsEntity> updateWrapper = new LambdaUpdateWrapper<>();
-            updateWrapper.eq(MarketEmailsEntity::getMarketingCampaignId, marketingCampaignEntity.getId());
-            updateWrapper.set(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_1);
-            this.update(updateWrapper);
+            LambdaQueryWrapper<MarketEmailsEntity> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(MarketEmailsEntity::getMarketingCampaignId, marketingCampaignEntity.getId());
+            queryWrapper.eq(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
+            List<MarketEmailsEntity> marketEmailsOldList = this.list(queryWrapper);
+            marketEmailsOldList.forEach(marketEmail -> marketEmail.setIsDelete(CommonConstant.DEL_FLAG_1));
+            this.updateBatchById(marketEmailsOldList);
+            //删除原有关联邮件记录
+//            List<Long> emailIdList = marketEmailsOldList.stream().map(MarketEmailsEntity::getEmailId).collect(Collectors.toList());
+//            LambdaUpdateWrapper<MktMailStatusEntity> updateWrapper = new LambdaUpdateWrapper<>();
+//            updateWrapper.in(MktMailStatusEntity::getId, emailIdList);
+//            updateWrapper.set(MktMailStatusEntity::getIsDelete, CommonConstant.DEL_FLAG_1);
+//            mktMailStatusService.update(updateWrapper);
+
+            marketEmailList.forEach(
+                    marketEmail -> {
+                        marketEmail.setIsDelete(CommonConstant.DEL_FLAG_0);
+                    }
+            );
             this.saveBatch(marketEmailList);
+
         }
 
 
     }
 
+    @Override
+    public void send() {
+        LambdaQueryWrapper<MarketEmailsEntity> queryWrapper = new LambdaQueryWrapper<>();
+
+//        queryWrapper.eq(MarketEmailsEntity::getExpectSendTime, new Date());
+        // get start and end of current minute
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime start = now.withSecond(0).withNano(0);
+        LocalDateTime end = now.withSecond(59).withNano(999_999_999);
+        queryWrapper.between(MarketEmailsEntity::getExpectSendTime, start, end);
+
+        queryWrapper.eq(MarketEmailsEntity::getStatus, -1);
+        queryWrapper.eq(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
+        List<MarketEmailsEntity> marketEmailList = this.list(queryWrapper);
+
+        if (CollectionUtils.isEmpty(marketEmailList)){
+            return;
+        }
+        log.info("queryWrapper: {}", queryWrapper);
+        log.info("marketEmailList: {}", marketEmailList);
+        for (MarketEmailsEntity marketEmail : marketEmailList){
+            MktSendMailRemoteDTO dto = new MktSendMailRemoteDTO();
+            List<String> recipientls = new ArrayList<>();
+            recipientls.add(marketEmail.getRecipient());
+            dto.setRecipientls(recipientls);
+            dto.setSubject(marketEmail.getSubject());
+            dto.setContent(marketEmail.getContent());
+            dto.setDelaySendTime(marketEmail.getExpectSendTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
+            Result result = mktMailRemoteClient.send(dto);
+            if (result.isSuccess()) {
+                // 远程返回 result 是 LinkedHashMap(Jackson 默认反序列化),按 Map 取字段
+                Map<String, Object> sentMail = (Map<String, Object>) result.getResult();
+                //将邮件id写入MarketEmailsEntity
+                marketEmail.setEmailId(((Number) sentMail.get("id")).longValue());
+                marketEmail.setStatus(1);
+                log.info("id为:{}", sentMail.get("id"));
+                log.info("status为:{}", sentMail.get("status"));
+                log.info("邮件发送成功,邮件ID:{}", marketEmail.getId());
+            } else {
+                log.error("邮件发送失败,邮件ID:{},错误信息:{}", marketEmail.getId(), result.getMessage());
+            }
+
+//            123
+        }
+        if (!CollectionUtils.isEmpty(marketEmailList)){
+            this.updateBatchById(marketEmailList);
+        }
+    }
+
     /**
      * 调用 AI workflow 并解析调度阶段,带重试。
      * 入参 chatDTO 经 workflowsService.AiWorkflows 返回 Result<Object>,

+ 28 - 24
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/service/impl/MarketingCampaignSopDetailServiceImpl.java

@@ -61,12 +61,12 @@ public class MarketingCampaignSopDetailServiceImpl
         }
         if (!ObjectUtils.isEmpty(dto.getEmailContent())){
             //写入每个客户的邮件内容
-            this.updateEmailContent(dto.getId(), dto.getEmailContent());
+            this.updateEmailContent(dto.getId(),dto.getEmailSubject(), dto.getEmailContent());
         }
         return Result.result(entity);
     }
 
-    private void updateEmailContent(Long id, String emailContent) {
+    private void updateEmailContent(Long id, String emailSubject, String emailContent) {
         //查询营销活动关联的所有客户
 //        MarketingCampaignSopDetailEntity entity = this.getById(id);
         LambdaQueryWrapper<MarketEmailsEntity> marketEmailsEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
@@ -123,8 +123,11 @@ public class MarketingCampaignSopDetailServiceImpl
 
         Map<Long,CustomerVO> customerEntityMap = customerVOList.stream().collect(Collectors.toMap(CustomerVO::getId, customerVO -> customerVO));
         for (MarketEmailsEntity marketEmailsEntity : marketEmailsEntityList) {
+            marketEmailsEntity.setSubject(emailSubject);
+            marketEmailsEntity.setStatus(-1);
             CustomerVO customerEntity = customerEntityMap.get(marketEmailsEntity.getCustomerId());
             if (customerEntity != null) {
+
 //                ${areaName}$、${customerIndustry}$
                 //修改emailContent中全部的${customerName}$为customerEntity.getCustomerName()
                 emailContent = emailContent.replace("${customerName}$", customerEntity.getCustomerName())
@@ -149,33 +152,34 @@ public class MarketingCampaignSopDetailServiceImpl
         }
 
         //异步写入邮件发送时间及内容
-        this.insertMailMessage(marketEmailsEntityList,customerEntityMap);
+//        this.insertMailMessage(marketEmailsEntityList,customerEntityMap);
     }
 
     @Async
     public void insertMailMessage(List<MarketEmailsEntity> marketEmailsEntityList, Map<Long, CustomerVO> customerEntityMap) {
         for (MarketEmailsEntity marketEmailsEntity : marketEmailsEntityList) {
-            if (ObjectUtils.isEmpty(marketEmailsEntity.getCustomerId()) || ObjectUtils.isEmpty(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany()) || ObjectUtils.isEmpty(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany().getEmail())){
-                continue;
-            }
-            if (!ObjectUtils.isEmpty(marketEmailsEntity.getEmailId())){
-                LambdaUpdateWrapper<MktMailMessageEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
-                lambdaUpdateWrapper.eq(MktMailMessageEntity::getId, marketEmailsEntity.getEmailId());
-//                lambdaUpdateWrapper.set(MktMailMessageEntity::getStatus, -1);
-                lambdaUpdateWrapper.set(MktMailMessageEntity::getContent, marketEmailsEntity.getContent());
-                mktMailMessageService.update(lambdaUpdateWrapper);
-            }else {
-                MktMailMessageEntity mktMailMessageEntity = new MktMailMessageEntity();
-                mktMailMessageEntity.setStatus(-1);
-                mktMailMessageEntity.setDelaySendTime(marketEmailsEntity.getExpectSendTime());
-                mktMailMessageEntity.setContent(marketEmailsEntity.getContent());
-                mktMailMessageEntity.setRecipient(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany().getEmail());
-                mktMailMessageEntity.setIsTrack(1);
-                mktMailMessageService.save(mktMailMessageEntity);
-                if (mktMailMessageEntity.getId() != null) {
-                    marketEmailsEntity.setEmailId(mktMailMessageEntity.getId());
-                }
-            }
+
+//            if (ObjectUtils.isEmpty(marketEmailsEntity.getCustomerId()) || ObjectUtils.isEmpty(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany()) || ObjectUtils.isEmpty(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany().getEmail())){
+//                continue;
+//            }
+//            if (!ObjectUtils.isEmpty(marketEmailsEntity.getEmailId())){
+//                LambdaUpdateWrapper<MktMailMessageEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
+//                lambdaUpdateWrapper.eq(MktMailMessageEntity::getId, marketEmailsEntity.getEmailId());
+////                lambdaUpdateWrapper.set(MktMailMessageEntity::getStatus, -1);
+//                lambdaUpdateWrapper.set(MktMailMessageEntity::getContent, marketEmailsEntity.getContent());
+//                mktMailMessageService.update(lambdaUpdateWrapper);
+//            }else {
+//                MktMailMessageEntity mktMailMessageEntity = new MktMailMessageEntity();
+//                mktMailMessageEntity.setStatus(-1);
+//                mktMailMessageEntity.setDelaySendTime(marketEmailsEntity.getExpectSendTime());
+//                mktMailMessageEntity.setContent(marketEmailsEntity.getContent());
+//                mktMailMessageEntity.setRecipient(customerEntityMap.get(marketEmailsEntity.getCustomerId()).getCustomerCompany().getEmail());
+//                mktMailMessageEntity.setIsTrack(1);
+//                mktMailMessageService.save(mktMailMessageEntity);
+//                if (mktMailMessageEntity.getId() != null) {
+//                    marketEmailsEntity.setEmailId(mktMailMessageEntity.getId());
+//                }
+//            }
         }
         boolean isOk = marketEmailsService.updateBatchById(marketEmailsEntityList);
     }

+ 30 - 0
java/storlead-sasa/storlead-trade/src/main/java/com/storlead/trade/task/MarketEmailsSendSyncTask.java

@@ -0,0 +1,30 @@
+package com.storlead.trade.task;
+
+import com.storlead.trade.service.MarketEmailsService;
+import com.storlead.trade.service.MarketEmailsStatusSyncService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * 定时发送邮件
+ */
+@Slf4j
+@Component
+public class MarketEmailsSendSyncTask {
+
+    @Resource
+    private MarketEmailsService marketEmailsService;
+
+    @Scheduled(cron = "0 */1 * * * ?")
+    public void sync() {
+        long start = System.currentTimeMillis();
+        try {
+            marketEmailsService.send();
+        } catch (Exception e) {
+            log.error("mkt_mail_status → market_emails 同步失败", e);
+        }
+    }
+}