|
|
@@ -4,6 +4,7 @@ import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.storlead.framework.common.constant.CommonConstant;
|
|
|
import com.storlead.framework.common.constant.DSConstants;
|
|
|
import com.storlead.framework.common.result.Result;
|
|
|
import com.storlead.mail.pojo.entity.MktMailMessageEntity;
|
|
|
@@ -17,12 +18,14 @@ import com.storlead.trade.service.*;
|
|
|
import com.storlead.trade.service.impl.support.TradeDataSourceServiceImpl;
|
|
|
import com.storlead.trade.vo.CustomerVO;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -43,6 +46,9 @@ public class MarketingCampaignSopDetailServiceImpl
|
|
|
private MktMailMessageService mktMailMessageService;
|
|
|
@Resource
|
|
|
private SysDictService sysDictService;
|
|
|
+ @Resource
|
|
|
+ @Lazy
|
|
|
+ private MarketingCampaignService marketingCampaignService;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
@@ -62,6 +68,21 @@ public class MarketingCampaignSopDetailServiceImpl
|
|
|
return Result.error("存在已发送的邮件,不能进行修改");
|
|
|
}
|
|
|
|
|
|
+ // 要更新邮件内容时,先确认该步骤下确实有邮件可写。
|
|
|
+ // 放在 updateById 之前:否则步骤已改、内容却没写进去,前后端数据就对不上了。
|
|
|
+ List<MarketEmailsEntity> pendingEmailList = Collections.emptyList();
|
|
|
+ if (!ObjectUtils.isEmpty(dto.getEmailContent())) {
|
|
|
+ LambdaQueryWrapper<MarketEmailsEntity> pendingWrapper = new LambdaQueryWrapper<>();
|
|
|
+ pendingWrapper.eq(MarketEmailsEntity::getMarketingCampaignSopDetailId, dto.getId());
|
|
|
+ pendingWrapper.eq(MarketEmailsEntity::getMarketingCampaignId, dto.getMarketingCampaignId());
|
|
|
+ pendingWrapper.eq(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ pendingEmailList = marketEmailsService.list(pendingWrapper);
|
|
|
+ if (CollectionUtils.isEmpty(pendingEmailList)) {
|
|
|
+ MarketingCampaignEntity marketingCampaignEntity = marketingCampaignService.getById(dto.getMarketingCampaignId());
|
|
|
+ marketEmailsService.addSendTime(marketingCampaignEntity);
|
|
|
+ return Result.error("邮件还未生成或生成失败,请稍后重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
MarketingCampaignSopDetailEntity entity = new MarketingCampaignSopDetailEntity();
|
|
|
BeanUtils.copyProperties(dto, entity);
|
|
|
@@ -69,22 +90,28 @@ public class MarketingCampaignSopDetailServiceImpl
|
|
|
if (!isOk) {
|
|
|
return Result.error("修改失败");
|
|
|
}
|
|
|
- if (!ObjectUtils.isEmpty(dto.getEmailContent())){
|
|
|
+ if (!ObjectUtils.isEmpty(dto.getEmailContent())) {
|
|
|
//写入每个客户的邮件内容
|
|
|
- this.updateEmailContent(dto.getId(),dto.getEmailSubject(), dto.getEmailContent(), dto.getMarketingCampaignId());
|
|
|
+ Result<?> writeResult = this.updateEmailContent(pendingEmailList, dto.getEmailSubject(), dto.getEmailContent());
|
|
|
+ if (writeResult != null) {
|
|
|
+ return writeResult;
|
|
|
+ }
|
|
|
}
|
|
|
return Result.result(entity);
|
|
|
}
|
|
|
|
|
|
- private void updateEmailContent(Long id, String emailSubject, String emailContent, Long marketingCampaignId) {
|
|
|
- //查询营销活动关联的所有客户
|
|
|
-// MarketingCampaignSopDetailEntity entity = this.getById(id);
|
|
|
- LambdaQueryWrapper<MarketEmailsEntity> marketEmailsEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
- marketEmailsEntityLambdaQueryWrapper.eq(MarketEmailsEntity::getMarketingCampaignSopDetailId, id);
|
|
|
- marketEmailsEntityLambdaQueryWrapper.eq(MarketEmailsEntity::getMarketingCampaignId, marketingCampaignId);
|
|
|
- marketEmailsEntityLambdaQueryWrapper.eq(MarketEmailsEntity::getIsDelete, 0);
|
|
|
- List<MarketEmailsEntity> marketEmailsEntityList = marketEmailsService.list(marketEmailsEntityLambdaQueryWrapper);
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 把邮件模板内容写入该步骤下的每封邮件(含 ${变量}$ 占位符替换)。
|
|
|
+ * <p>
|
|
|
+ * 注意:这里<strong>不抛异常</strong>。抛 RuntimeException 会走全局异常处理器,
|
|
|
+ * 返回的是 code=900000 的兜底结构、HTTP 状态码也可能不是 200,前端拦截器往往识别不了,
|
|
|
+ * 表现为「接口一直卡着没有任何返回」。业务性失败统一返回 {@link Result},由上层直接透传。
|
|
|
+ *
|
|
|
+ * @param marketEmailsEntityList 该步骤下待写入的邮件(调用方已保证非空)
|
|
|
+ * @return null 表示成功;非 null 为失败原因,由上层直接透传给前端
|
|
|
+ */
|
|
|
+ private Result<?> updateEmailContent(List<MarketEmailsEntity> marketEmailsEntityList,
|
|
|
+ String emailSubject, String emailContent) {
|
|
|
List<Long> customerIdList = marketEmailsEntityList.stream().map(MarketEmailsEntity::getCustomerId).collect(Collectors.toList());
|
|
|
CustomerDTO dto = new CustomerDTO();
|
|
|
dto.setIdList(customerIdList);
|
|
|
@@ -92,7 +119,7 @@ public class MarketingCampaignSopDetailServiceImpl
|
|
|
dto.setPageSize(customerIdList.size());
|
|
|
IPage<CustomerVO> customerPageList = customerService.getList(dto);
|
|
|
if (customerPageList == null || customerPageList.getRecords() == null || customerPageList.getRecords().size() == 0) {
|
|
|
- return;
|
|
|
+ return Result.error("未查询到关联的客户,邮件内容写入失败");
|
|
|
}
|
|
|
List<CustomerVO> customerVOList = customerPageList.getRecords();
|
|
|
|
|
|
@@ -168,13 +195,20 @@ public class MarketingCampaignSopDetailServiceImpl
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
- boolean isOk = marketEmailsService.updateBatchById(marketEmailsEntityList);
|
|
|
- if (!isOk) {
|
|
|
- return;
|
|
|
+ for (MarketEmailsEntity marketEmailsEntity : marketEmailsEntityList){
|
|
|
+ //异步修改邮件内容并写入
|
|
|
+
|
|
|
+ marketEmailsService.updateEmailContent(marketEmailsEntity);
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
+// if (!isOk) {
|
|
|
+// return Result.error("邮件内容写入失败");
|
|
|
+// }
|
|
|
|
|
|
//异步写入邮件发送时间及内容
|
|
|
// this.insertMailMessage(marketEmailsEntityList,customerEntityMap);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Async
|