Parcourir la source

根据询盘分配规则,进行分配询盘负责人

chenlei1231 il y a 1 mois
Parent
commit
986fe86197

+ 56 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/enquiry/service/impl/AdwebEnquiryServiceImpl.java

@@ -31,6 +31,7 @@ import org.jeecg.modules.adweb.common.service.FeishuService;
 import org.jeecg.modules.adweb.common.service.WebSocketService;
 import org.jeecg.modules.adweb.common.util.*;
 import org.jeecg.modules.adweb.enquiry.constant.EnquiryConstants;
+import org.jeecg.modules.adweb.enquiry.constant.EnquiryDistributeTypeConstant;
 import org.jeecg.modules.adweb.enquiry.constant.EnquirySendStatus;
 import org.jeecg.modules.adweb.enquiry.dto.EnquiryDTO;
 import org.jeecg.modules.adweb.enquiry.dto.param.EffectiveEnquiryParamDto;
@@ -45,6 +46,10 @@ import org.jeecg.modules.adweb.system.entity.SysException;
 import org.jeecg.modules.adweb.system.service.IMasterSubAccountRelationService;
 import org.jeecg.modules.adweb.system.service.ISysExceptionService;
 import org.jeecg.modules.adweb.system.service.impl.SysAdwebApiImpl;
+import org.jeecg.modules.adweb.userCountry.entity.AdwebCountry;
+import org.jeecg.modules.adweb.userCountry.entity.AdwebUserCountry;
+import org.jeecg.modules.adweb.userCountry.service.IAdwebCountryService;
+import org.jeecg.modules.adweb.userCountry.service.IAdwebUserCountryService;
 import org.jeecg.modules.message.websocket.WebSocket;
 import org.jeecg.modules.system.entity.SysDictItem;
 import org.jeecg.modules.system.entity.SysUser;
@@ -104,6 +109,10 @@ public class AdwebEnquiryServiceImpl extends ServiceImpl<AdwebEnquiryMapper, Adw
 
   @Resource private IAdwebSiteService adwebSiteService;
 
+  @Resource private IAdwebCountryService adwebCountryService;
+
+  @Resource private IAdwebUserCountryService adwebUserCountryService;
+
   @Resource private AdwebOpenApiService adwebOpenApiService;
 
   @Resource private IAdwebEnquiryFormService adwebEnquiryFormService;
@@ -290,7 +299,7 @@ public class AdwebEnquiryServiceImpl extends ServiceImpl<AdwebEnquiryMapper, Adw
       String forms = FastJsonUtil.toJSONString(enquiryDto.getForms());
       parseFormAndSetFields(adwebEnquiry, forms, adwebSite);
       setCountryByIp(adwebEnquiry);
-
+      setPrincipal(adwebEnquiry, adwebSite.getEnquiryDistributeType(), adwebSite.getId());
       adwebEnquiryList.add(adwebEnquiry);
 
       form.setEnquiryId(adwebEnquiry.getId());
@@ -381,6 +390,52 @@ public class AdwebEnquiryServiceImpl extends ServiceImpl<AdwebEnquiryMapper, Adw
     sendValidateEmail(target, adwebSite, principalEmailMap);
   }
 
+  /** 设置询盘负责人 */
+  private void setPrincipal(AdwebEnquiry adwebEnquiry, String distributeType, Integer siteId) {
+
+    // 根据询盘发送规则
+    if (EnquiryDistributeTypeConstant.MASTER.equals(distributeType)) {
+      adwebEnquiry.setPrincipalUid(adwebEnquiry.getUid());
+    } else if (EnquiryDistributeTypeConstant.ALL.equals(distributeType)) {
+      adwebEnquiry.setPrincipalUid("ALL");
+    } else if (EnquiryDistributeTypeConstant.REGION.equals(distributeType)) {
+      String countryCode = adwebEnquiry.getCountryCode();
+      if (StringUtils.isNotBlank(countryCode)) {
+        LambdaQueryWrapper<AdwebCountry> countryQueryWrapper =
+            new LambdaQueryWrapper<AdwebCountry>().eq(AdwebCountry::getCountryIsoCode, countryCode);
+        AdwebCountry country = adwebCountryService.getOne(countryQueryWrapper);
+        if (country != null) {
+          String countryId = country.getGeonameId();
+          QueryWrapper<AdwebUserCountry> userCountryQueryWrapper = new QueryWrapper<>();
+          userCountryQueryWrapper.eq("status", 1);
+          userCountryQueryWrapper.eq("site_id", siteId);
+          userCountryQueryWrapper.eq("country_id", countryId);
+          AdwebUserCountry adwebUserCountry =
+              adwebUserCountryService.getOne(userCountryQueryWrapper);
+          if (adwebUserCountry != null && StringUtils.isNotBlank(adwebUserCountry.getUid())) {
+            adwebEnquiry.setPrincipalUid(adwebUserCountry.getUid());
+          } else {
+            adwebEnquiry.setPrincipalUid(adwebEnquiry.getUid());
+          }
+        }
+      }
+    }
+    adwebEnquiry.setPrincipalType(0);
+
+    // 数据字典中配置某些网站只发给主账户
+    List<DictModel> contactEnquiryWebsites =
+        dictService.queryDictItemsByCode("send_contact_enquiry_website");
+    List<String> contactEnquiryCodes =
+        contactEnquiryWebsites.stream().map(DictModel::getValue).collect(Collectors.toList());
+    if (contactEnquiryCodes.contains(adwebEnquiry.getSiteCode())) {
+      if (StringUtils.isNotBlank(adwebEnquiry.getFromPage())
+          && adwebEnquiry.getFromPage().contains("contact")) {
+        adwebEnquiry.setPrincipalType(1);
+        adwebEnquiry.setExistContactEnquiry(1);
+      }
+    }
+  }
+
   /**
    * 发送询盘邮件
    *

+ 283 - 298
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/enquiry/service/impl/EnquiryEmailMessageServiceImpl.java

@@ -1,17 +1,17 @@
 package org.jeecg.modules.adweb.enquiry.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
-
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.google.common.io.Resources;
-
 import jakarta.annotation.Resource;
-
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+import java.util.stream.Collectors;
 import lombok.Data;
-
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.jeecg.common.api.vo.Result;
@@ -27,6 +27,7 @@ import org.jeecg.modules.adweb.enquiry.entity.AdwebEnquiryForm;
 import org.jeecg.modules.adweb.enquiry.entity.EnquiryEmailMessage;
 import org.jeecg.modules.adweb.enquiry.mapper.EnquiryEmailMessageMapper;
 import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryFormService;
+import org.jeecg.modules.adweb.enquiry.service.IAdwebEnquiryService;
 import org.jeecg.modules.adweb.enquiry.service.IEnquiryEmailMessageService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
@@ -36,342 +37,326 @@ import org.jeecg.modules.adweb.system.service.ISysExceptionService;
 import org.jeecg.modules.message.handle.enums.SendMsgTypeEnum;
 import org.jeecg.modules.message.handle.impl.EmailSendMsgHandle;
 import org.jeecg.modules.system.entity.SysUser;
-import org.jeecg.modules.system.service.ISysDictService;
 import org.jeecg.modules.system.service.ISysUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.*;
-import java.util.stream.Collectors;
-
 /**
- * @Description: adweb_enquiry_email_message
- * @Author: jeecg-boot
- * @Date: 2023-02-01
- * @Version: V1.0
+ * @Description: adweb_enquiry_email_message @Author: jeecg-boot @Date: 2023-02-01 @Version: V1.0
  */
 @Service
 public class EnquiryEmailMessageServiceImpl
-        extends ServiceImpl<EnquiryEmailMessageMapper, EnquiryEmailMessage>
-        implements IEnquiryEmailMessageService {
+    extends ServiceImpl<EnquiryEmailMessageMapper, EnquiryEmailMessage>
+    implements IEnquiryEmailMessageService {
 
-    private static byte[] redisKey = EnquiryConstants.ENQUIRY_EMAIL.getBytes();
+  private static byte[] redisKey = EnquiryConstants.ENQUIRY_EMAIL.getBytes();
 
-    @Resource private EnquiryEmailMessageMapper enquiryEmailMessageMapper;
+  @Resource private EnquiryEmailMessageMapper enquiryEmailMessageMapper;
 
-    @Resource private FeishuService feishuService;
+  @Resource private FeishuService feishuService;
 
-    @Resource private AdwebRedisUtil adwebRedisUtil;
+  @Resource private AdwebRedisUtil adwebRedisUtil;
 
-    @Resource private ISysDictService sysDictService;
+  @Resource private IAdwebEnquiryFormService adwebEnquiryFormService;
 
-    @Resource private IAdwebEnquiryFormService adwebEnquiryFormService;
+  @Resource private IAdwebEnquiryService adwebEnquiryService;
 
-    @Resource private ISysExceptionService sysExceptionService;
+  @Resource private ISysExceptionService sysExceptionService;
 
-    @Resource private IAdwebSiteService adwebSiteService;
+  @Resource private IAdwebSiteService adwebSiteService;
 
-    @Resource @Lazy private ISysUserService sysUserService;
+  @Resource @Lazy private ISysUserService sysUserService;
 
-    @Autowired private IMasterSubAccountRelationService masterSubAccountRelationService;
+  @Autowired private IMasterSubAccountRelationService masterSubAccountRelationService;
 
-    @Override
-    public void sendEnquiryEmail(Long enquiryId) {
-        String keyName = "ENQUIRY_SEND_EMAIL_" + enquiryId;
-        if (!adwebRedisUtil.lock(keyName, 10 * 60 * 1000)) {
-            return;
-        }
-
-        LambdaQueryWrapper<EnquiryEmailMessage> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(EnquiryEmailMessage::getEnquiryId, enquiryId);
-        queryWrapper.eq(EnquiryEmailMessage::getSendStatus, EnquirySendStatus.WAIT_SEND);
-        List<EnquiryEmailMessage> messageList = list(queryWrapper);
-        if (CollUtil.isNotEmpty(messageList)) {
-            LambdaUpdateWrapper<EnquiryEmailMessage> updateWrapper = new LambdaUpdateWrapper<>();
-            updateWrapper.in(
-                    EnquiryEmailMessage::getId,
-                    messageList.stream()
-                            .map(EnquiryEmailMessage::getId)
-                            .collect(Collectors.toList()));
-            updateWrapper.set(EnquiryEmailMessage::getSendStatus, EnquirySendStatus.SENDING);
-            updateWrapper.set(EnquiryEmailMessage::getSendStartTime, new Date());
-            this.update(updateWrapper);
-            for (EnquiryEmailMessage message : messageList) {
-                sendEmailByMessage(message);
-            }
-        }
-
-        adwebRedisUtil.unlock(keyName);
+  @Override
+  public void sendEnquiryEmail(Long enquiryId) {
+    String keyName = "ENQUIRY_SEND_EMAIL_" + enquiryId;
+    if (!adwebRedisUtil.lock(keyName, 10 * 60 * 1000)) {
+      return;
     }
 
-    @Override
-    public void sendEmailByMessage(EnquiryEmailMessage message) {
-        try {
-            EmailSendMsgHandle sendMsgHandle =
-                    (EmailSendMsgHandle)
-                            Class.forName(SendMsgTypeEnum.EMAIL.getImplClass()).newInstance();
-            AdwebSite site =
-                    adwebSiteService.getOne(
-                            new LambdaQueryWrapper<AdwebSite>()
-                                    .eq(AdwebSite::getCode, message.getSiteCode()));
-            String fromName = Objects.toString(site.getCompanyName(), site.getName());
-            sendMsgHandle.sendMsg(
-                    message.getEmail(), fromName, getEmailContent(message.getEnquiryId()));
-            dealSendSuccess(message);
-        } catch (Exception e) {
-            log.error(e.getMessage(), e);
-            dealSendFail(message, e.getMessage());
-        }
+    LambdaQueryWrapper<EnquiryEmailMessage> queryWrapper = new LambdaQueryWrapper<>();
+    queryWrapper.eq(EnquiryEmailMessage::getEnquiryId, enquiryId);
+    queryWrapper.eq(EnquiryEmailMessage::getSendStatus, EnquirySendStatus.WAIT_SEND);
+    List<EnquiryEmailMessage> messageList = list(queryWrapper);
+    if (CollUtil.isNotEmpty(messageList)) {
+      LambdaUpdateWrapper<EnquiryEmailMessage> updateWrapper = new LambdaUpdateWrapper<>();
+      updateWrapper.in(
+          EnquiryEmailMessage::getId,
+          messageList.stream().map(EnquiryEmailMessage::getId).collect(Collectors.toList()));
+      updateWrapper.set(EnquiryEmailMessage::getSendStatus, EnquirySendStatus.SENDING);
+      updateWrapper.set(EnquiryEmailMessage::getSendStartTime, new Date());
+      this.update(updateWrapper);
+      for (EnquiryEmailMessage message : messageList) {
+        sendEmailByMessage(message);
+      }
     }
 
-    /**
-     * 处理发送时间超过10分钟的邮件消息 1、清除发送时间
-     *
-     * @return 如果返回true,需要重新发送
-     */
-    // private boolean dealTimeoutMessage(EnquiryEmailMessage message) {
-    //     Date now = new Date();
-    //     Date dealTime = message.getSendStartTime();
-    //     if (DateUtil.between(dealTime, now, DateUnit.MINUTE) >= 10) {
-    //         message.setSendStartTime(new Date());
-    //         updateById(message);
-    //         return true;
-    //     }
-    //     return false;
-    // }
-
-    /** 邮件发送失败处理 1、更新发送状态 2、清除发送时间 */
-    private void dealSendSuccess(EnquiryEmailMessage message) {
-        message.setSendStatus(EnquirySendStatus.SEND_SUCCESS);
-        message.setSendStartTime(null);
-        updateById(message);
+    adwebRedisUtil.unlock(keyName);
+  }
+
+  @Override
+  public void sendEmailByMessage(EnquiryEmailMessage message) {
+    try {
+      EmailSendMsgHandle sendMsgHandle =
+          (EmailSendMsgHandle) Class.forName(SendMsgTypeEnum.EMAIL.getImplClass()).newInstance();
+      AdwebSite site =
+          adwebSiteService.getOne(
+              new LambdaQueryWrapper<AdwebSite>().eq(AdwebSite::getCode, message.getSiteCode()));
+      String fromName = Objects.toString(site.getCompanyName(), site.getName());
+
+      AdwebEnquiry adwebEnquiry = adwebEnquiryService.getById(message.getEnquiryId());
+
+      if (adwebEnquiry != null
+          && org.apache.commons.lang.StringUtils.isNotBlank(adwebEnquiry.getName())) {
+        fromName = "Enquiry " + adwebEnquiry.getName();
+      }
+
+      sendMsgHandle.sendMsg(message.getEmail(), fromName, getEmailContent(message.getEnquiryId()));
+      dealSendSuccess(message);
+    } catch (Exception e) {
+      log.error(e.getMessage(), e);
+      dealSendFail(message, e.getMessage());
     }
-
-    /** 邮件发送失败处理 1、增加错误次数 2、更新发送状态 3、清除发送时间 4、如果错误次数到达三次,发送飞书通知 */
-    private void dealSendFail(EnquiryEmailMessage message, String errorMsg) {
-        if (StringUtils.isEmpty(errorMsg)) {
-            errorMsg = "发送询盘邮件失败";
-        }
-        message.setSendErrorNum(message.getSendErrorNum() + 1);
-        if (message.getSendErrorNum() >= 3) {
-            message.setSendStatus(EnquirySendStatus.SEND_FAIL);
-        } else {
-            message.setSendStatus(EnquirySendStatus.WAIT_SEND);
-        }
-        message.setSendStartTime(null);
-        String msg = Objects.toString(message.getSendErrorMsg(), "");
-        msg = msg + "," + errorMsg;
-        message.setSendErrorMsg(msg);
-        updateById(message);
-        if (message.getSendErrorNum() >= 3) {
-            feishuService.sendEnquiryEmailFailMsg(
-                    message.getEnquiryId(), message.getSiteCode(), message.getEmail(), errorMsg);
-        }
-
-        // 异常入库
-        SysException exception = new SysException();
-        exception.setType(0);
-        exception.setFunctionModule("发送询盘邮件错误");
-        exception.setExceptionDetail(errorMsg);
-        exception.setCreateTime(new Date());
-        exception.setStatus(1);
-        exception.setHandle(0);
-        exception.setPriority(1);
-        sysExceptionService.save(exception);
+  }
+
+  /**
+   * 处理发送时间超过10分钟的邮件消息 1、清除发送时间
+   *
+   * @return 如果返回true,需要重新发送
+   */
+  // private boolean dealTimeoutMessage(EnquiryEmailMessage message) {
+  //     Date now = new Date();
+  //     Date dealTime = message.getSendStartTime();
+  //     if (DateUtil.between(dealTime, now, DateUnit.MINUTE) >= 10) {
+  //         message.setSendStartTime(new Date());
+  //         updateById(message);
+  //         return true;
+  //     }
+  //     return false;
+  // }
+
+  /** 邮件发送失败处理 1、更新发送状态 2、清除发送时间 */
+  private void dealSendSuccess(EnquiryEmailMessage message) {
+    message.setSendStatus(EnquirySendStatus.SEND_SUCCESS);
+    message.setSendStartTime(null);
+    updateById(message);
+  }
+
+  /** 邮件发送失败处理 1、增加错误次数 2、更新发送状态 3、清除发送时间 4、如果错误次数到达三次,发送飞书通知 */
+  private void dealSendFail(EnquiryEmailMessage message, String errorMsg) {
+    if (StringUtils.isEmpty(errorMsg)) {
+      errorMsg = "发送询盘邮件失败";
     }
-
-    private String getEmailContent(long enquiryId) throws IOException {
-        String bodyTemplate =
-                Resources.toString(
-                        Objects.requireNonNull(
-                                this.getClass()
-                                        .getClassLoader()
-                                        .getResource("enquiry/email/body.tmpl")),
-                        StandardCharsets.UTF_8);
-
-        String itemTemplate =
-                Resources.toString(
-                        Objects.requireNonNull(
-                                this.getClass()
-                                        .getClassLoader()
-                                        .getResource("enquiry/email/item.tmpl")),
-                        StandardCharsets.UTF_8);
-        StringBuilder content = new StringBuilder();
-
-        AdwebEnquiryForm adwebEnquiryForm = adwebEnquiryFormService.getByEnquiryId(enquiryId);
-
-        List<EnquiryFormItem> formItems = new ArrayList<>();
-
-        formItems = dealWpformEnquiryForm(adwebEnquiryForm);
-
-        for (int i = 0; i < formItems.size(); i++) {
-            EnquiryFormItem enquiryFormItem = formItems.get(i);
-            String formItemName = enquiryFormItem.getName();
-            String formItemValue = enquiryFormItem.getValue();
-            String item1 = itemTemplate;
-            item1 = item1.replace("{{title}}", formItemName);
-            item1 =
-                    item1.replace(
-                            "{{content}}",
-                            formItemValue == null || formItemValue == "" ? " " : formItemValue);
-            if (i == 0) {
-                item1 = item1.replace("border-top:1px solid #dddddd;", "");
-            }
-            content.append(item1);
-        }
-
-        return bodyTemplate.replace("{{items_content}}", content.toString());
+    message.setSendErrorNum(message.getSendErrorNum() + 1);
+    if (message.getSendErrorNum() >= 3) {
+      message.setSendStatus(EnquirySendStatus.SEND_FAIL);
+    } else {
+      message.setSendStatus(EnquirySendStatus.WAIT_SEND);
     }
-
-    /**
-     * 获取待发送询盘 1、首次发送失败的 2、超过timeout分钟未处理的
-     *
-     * @param timeout 留给首次实时处理的时间,单位分钟,超过这个时间则认为首次邮件发送失败,应该由定时器处理
-     * @param num 获取邮件数量
-     * @return 邮件列表
-     */
-    @Override
-    public List<EnquiryEmailMessage> getWaitSendEmail(int timeout, int num) {
-        return enquiryEmailMessageMapper.getWaitSendEmail(timeout, num);
+    message.setSendStartTime(null);
+    String msg = Objects.toString(message.getSendErrorMsg(), "");
+    msg = msg + "," + errorMsg;
+    message.setSendErrorMsg(msg);
+    updateById(message);
+    if (message.getSendErrorNum() >= 3) {
+      feishuService.sendEnquiryEmailFailMsg(
+          message.getEnquiryId(), message.getSiteCode(), message.getEmail(), errorMsg);
     }
 
-    /**
-     * 添加并发送询盘邮件
-     *
-     * @param adwebEnquiry
-     */
-    @Override
-    public Result<?> addAndEnquiryEmailMsg(AdwebEnquiry adwebEnquiry, String uid) {
-        // 处理已有发送的询盘邮件
-        List<String> emailList = new ArrayList<>();
-        if ("ALL".equals(uid)) {
-            // 获取子账户邮箱
-            List<String> subAccountIdList =
-                    masterSubAccountRelationService.getSubAccountIdByMaster(
-                            String.valueOf(adwebEnquiry.getUid()));
-            if (!subAccountIdList.isEmpty()) {
-                List<SysUser> principalEmailList =
-                        sysUserService.list(
-                                new LambdaQueryWrapper<SysUser>()
-                                        .in(SysUser::getId, subAccountIdList)
-                                        .eq(SysUser::getDelFlag, 0)
-                                        .isNotNull(SysUser::getEmail)
-                                        .ne(SysUser::getEmail, "")
-                                        .select(SysUser::getId, SysUser::getEmail));
-                if (CollectionUtils.isNotEmpty(principalEmailList)) {
-                    emailList =
-                            principalEmailList.stream()
-                                    .map(SysUser::getEmail)
-                                    .collect(Collectors.toList());
-                }
-            }
-        } else {
-            SysUser sysUser = sysUserService.getById(adwebEnquiry.getPrincipalUid());
-            if (StringUtils.isNotBlank(sysUser.getEmail())) {
-                emailList.add(sysUser.getEmail());
-            }
-        }
+    // 异常入库
+    SysException exception = new SysException();
+    exception.setType(0);
+    exception.setFunctionModule("发送询盘邮件错误");
+    exception.setExceptionDetail(errorMsg);
+    exception.setCreateTime(new Date());
+    exception.setStatus(1);
+    exception.setHandle(0);
+    exception.setPriority(1);
+    sysExceptionService.save(exception);
+  }
+
+  private String getEmailContent(long enquiryId) throws IOException {
+    String bodyTemplate =
+        Resources.toString(
+            Objects.requireNonNull(
+                this.getClass().getClassLoader().getResource("enquiry/email/body.tmpl")),
+            StandardCharsets.UTF_8);
+
+    String itemTemplate =
+        Resources.toString(
+            Objects.requireNonNull(
+                this.getClass().getClassLoader().getResource("enquiry/email/item.tmpl")),
+            StandardCharsets.UTF_8);
+    StringBuilder content = new StringBuilder();
+
+    AdwebEnquiryForm adwebEnquiryForm = adwebEnquiryFormService.getByEnquiryId(enquiryId);
+
+    List<EnquiryFormItem> formItems = new ArrayList<>();
+
+    formItems = dealWpformEnquiryForm(adwebEnquiryForm);
+
+    for (int i = 0; i < formItems.size(); i++) {
+      EnquiryFormItem enquiryFormItem = formItems.get(i);
+      String formItemName = enquiryFormItem.getName();
+      String formItemValue = enquiryFormItem.getValue();
+      String item1 = itemTemplate;
+      item1 = item1.replace("{{title}}", formItemName);
+      item1 =
+          item1.replace(
+              "{{content}}", formItemValue == null || formItemValue == "" ? " " : formItemValue);
+      if (i == 0) {
+        item1 = item1.replace("border-top:1px solid #dddddd;", "");
+      }
+      content.append(item1);
+    }
 
-        if (CollectionUtils.isNotEmpty(emailList)) {
-            LambdaQueryWrapper<EnquiryEmailMessage> enquiryEmailMessageQueryWrapper =
-                    new LambdaQueryWrapper<>();
-            enquiryEmailMessageQueryWrapper.eq(
-                    EnquiryEmailMessage::getEnquiryId, adwebEnquiry.getId());
-            enquiryEmailMessageQueryWrapper.eq(
-                    EnquiryEmailMessage::getSiteCode, adwebEnquiry.getSiteCode());
-            enquiryEmailMessageQueryWrapper.in(EnquiryEmailMessage::getEmail, emailList);
-            List<EnquiryEmailMessage> enquiryEmailMessageList =
-                    list(enquiryEmailMessageQueryWrapper);
-
-            if (CollectionUtils.isNotEmpty(enquiryEmailMessageList)) {
-                List<String> oldEmailList =
-                        enquiryEmailMessageList.stream()
-                                .map(EnquiryEmailMessage::getEmail)
-                                .collect(Collectors.toList());
-                List<String> noNeedEmailList = new ArrayList<>();
-                for (String email : emailList) {
-                    if (oldEmailList.contains(email)) {
-                        noNeedEmailList.add(email);
-                    }
-                }
-                if (CollectionUtils.isNotEmpty(noNeedEmailList)) {
-                    emailList.removeAll(noNeedEmailList);
-                }
-            }
-
-            if (CollectionUtils.isNotEmpty(emailList)) {
-                List<EnquiryEmailMessage> enquiryEmailMessageList1 = new ArrayList<>();
-                for (String email : emailList) {
-                    EnquiryEmailMessage enquiryEmailMessage = new EnquiryEmailMessage();
-                    enquiryEmailMessage.setEnquiryId(adwebEnquiry.getId());
-                    enquiryEmailMessage.setSiteCode(adwebEnquiry.getSiteCode());
-                    enquiryEmailMessage.setEmail(email);
-                    enquiryEmailMessage.setFromEmail(adwebEnquiry.getFromEmail());
-                    enquiryEmailMessage.setSendStatus(EnquirySendStatus.WAIT_SEND);
-                    enquiryEmailMessage.setSendErrorNum(0);
-                    enquiryEmailMessage.setCreateTime(new Date());
-                    enquiryEmailMessageList1.add(enquiryEmailMessage);
-                }
-
-                boolean result = saveBatch(enquiryEmailMessageList1);
-                if (result) {
-                    try {
-                        JedisUtil.lpush(redisKey, ObjectUtil.object2Bytes(adwebEnquiry.getId()));
-                        JedisUtil.publishMsg(
-                                EnquiryConstants.ENQUIRY_EMAIL_CHANNEL,
-                                EnquiryConstants.ENQUIRY_EMAIL);
-                        return Result.OK();
-                    } catch (Exception e) {
-                        log.error("发送失败询盘id:" + adwebEnquiry.getId());
-                        log.error("推送发送询盘邮件的消息失败{}", e);
-                    }
-                }
-            }
-            return Result.OK();
+    return bodyTemplate.replace("{{items_content}}", content.toString());
+  }
+
+  /**
+   * 获取待发送询盘 1、首次发送失败的 2、超过timeout分钟未处理的
+   *
+   * @param timeout 留给首次实时处理的时间,单位分钟,超过这个时间则认为首次邮件发送失败,应该由定时器处理
+   * @param num 获取邮件数量
+   * @return 邮件列表
+   */
+  @Override
+  public List<EnquiryEmailMessage> getWaitSendEmail(int timeout, int num) {
+    return enquiryEmailMessageMapper.getWaitSendEmail(timeout, num);
+  }
+
+  /**
+   * 添加并发送询盘邮件
+   *
+   * @param adwebEnquiry
+   */
+  @Override
+  public Result<?> addAndEnquiryEmailMsg(AdwebEnquiry adwebEnquiry, String uid) {
+    // 处理已有发送的询盘邮件
+    List<String> emailList = new ArrayList<>();
+    if ("ALL".equals(uid)) {
+      // 获取子账户邮箱
+      List<String> subAccountIdList =
+          masterSubAccountRelationService.getSubAccountIdByMaster(
+              String.valueOf(adwebEnquiry.getUid()));
+      if (!subAccountIdList.isEmpty()) {
+        List<SysUser> principalEmailList =
+            sysUserService.list(
+                new LambdaQueryWrapper<SysUser>()
+                    .in(SysUser::getId, subAccountIdList)
+                    .eq(SysUser::getDelFlag, 0)
+                    .isNotNull(SysUser::getEmail)
+                    .ne(SysUser::getEmail, "")
+                    .select(SysUser::getId, SysUser::getEmail));
+        if (CollectionUtils.isNotEmpty(principalEmailList)) {
+          emailList =
+              principalEmailList.stream().map(SysUser::getEmail).collect(Collectors.toList());
         }
-        return Result.error("子账户邮箱没有配置,询盘邮件发送失败");
+      }
+    } else {
+      SysUser sysUser = sysUserService.getById(adwebEnquiry.getPrincipalUid());
+      if (StringUtils.isNotBlank(sysUser.getEmail())) {
+        emailList.add(sysUser.getEmail());
+      }
     }
 
-    /** 处理询盘发送信息 wpform */
-    private List<EnquiryFormItem> dealWpformEnquiryForm(AdwebEnquiryForm adwebEnquiryForm) {
-        String form = adwebEnquiryForm.getForm();
-        Map<String, JSONObject> formObj = FastJsonUtil.parseObject(form, Map.class);
-        List<EnquiryFormItem> formItems = new ArrayList<>();
-        boolean countryFlag = true;
-        for (String key : formObj.keySet()) {
-            JSONObject formItem = formObj.get(key);
-            EnquiryFormItem enquiryFormItem = new EnquiryFormItem();
-            enquiryFormItem.setSort(Integer.parseInt(key));
-            if ("Country".equals(formItem.getString("name"))
-                    || "country".equals(formItem.getString("name"))) {
-                countryFlag = false;
-            }
-            enquiryFormItem.setName(formItem.getString("name"));
-            enquiryFormItem.setValue(formItem.getString("value"));
-            formItems.add(enquiryFormItem);
+    if (CollectionUtils.isNotEmpty(emailList)) {
+      LambdaQueryWrapper<EnquiryEmailMessage> enquiryEmailMessageQueryWrapper =
+          new LambdaQueryWrapper<>();
+      enquiryEmailMessageQueryWrapper.eq(EnquiryEmailMessage::getEnquiryId, adwebEnquiry.getId());
+      enquiryEmailMessageQueryWrapper.eq(
+          EnquiryEmailMessage::getSiteCode, adwebEnquiry.getSiteCode());
+      enquiryEmailMessageQueryWrapper.in(EnquiryEmailMessage::getEmail, emailList);
+      List<EnquiryEmailMessage> enquiryEmailMessageList = list(enquiryEmailMessageQueryWrapper);
+
+      if (CollectionUtils.isNotEmpty(enquiryEmailMessageList)) {
+        List<String> oldEmailList =
+            enquiryEmailMessageList.stream()
+                .map(EnquiryEmailMessage::getEmail)
+                .collect(Collectors.toList());
+        List<String> noNeedEmailList = new ArrayList<>();
+        for (String email : emailList) {
+          if (oldEmailList.contains(email)) {
+            noNeedEmailList.add(email);
+          }
         }
-        if (countryFlag) {
-            EnquiryFormItem countryForm = new EnquiryFormItem();
-            countryForm.setSort(99);
-            countryForm.setName("Country");
-            countryForm.setValue(adwebEnquiryForm.getCountry());
-            formItems.add(countryForm);
+        if (CollectionUtils.isNotEmpty(noNeedEmailList)) {
+          emailList.removeAll(noNeedEmailList);
+        }
+      }
+
+      if (CollectionUtils.isNotEmpty(emailList)) {
+        List<EnquiryEmailMessage> enquiryEmailMessageList1 = new ArrayList<>();
+        for (String email : emailList) {
+          EnquiryEmailMessage enquiryEmailMessage = new EnquiryEmailMessage();
+          enquiryEmailMessage.setEnquiryId(adwebEnquiry.getId());
+          enquiryEmailMessage.setSiteCode(adwebEnquiry.getSiteCode());
+          enquiryEmailMessage.setEmail(email);
+          enquiryEmailMessage.setFromEmail(adwebEnquiry.getFromEmail());
+          enquiryEmailMessage.setSendStatus(EnquirySendStatus.WAIT_SEND);
+          enquiryEmailMessage.setSendErrorNum(0);
+          enquiryEmailMessage.setCreateTime(new Date());
+          enquiryEmailMessageList1.add(enquiryEmailMessage);
         }
 
-        formItems.sort(Comparator.comparingInt(EnquiryFormItem::getSort));
-        return formItems;
+        boolean result = saveBatch(enquiryEmailMessageList1);
+        if (result) {
+          try {
+            JedisUtil.lpush(redisKey, ObjectUtil.object2Bytes(adwebEnquiry.getId()));
+            JedisUtil.publishMsg(
+                EnquiryConstants.ENQUIRY_EMAIL_CHANNEL, EnquiryConstants.ENQUIRY_EMAIL);
+            return Result.OK();
+          } catch (Exception e) {
+            log.error("发送失败询盘id:" + adwebEnquiry.getId());
+            log.error("推送发送询盘邮件的消息失败{}", e);
+          }
+        }
+      }
+      return Result.OK();
+    }
+    return Result.error("子账户邮箱没有配置,询盘邮件发送失败");
+  }
+
+  /** 处理询盘发送信息 wpform */
+  private List<EnquiryFormItem> dealWpformEnquiryForm(AdwebEnquiryForm adwebEnquiryForm) {
+    String form = adwebEnquiryForm.getForm();
+    Map<String, JSONObject> formObj = FastJsonUtil.parseObject(form, Map.class);
+    List<EnquiryFormItem> formItems = new ArrayList<>();
+    boolean countryFlag = true;
+    for (String key : formObj.keySet()) {
+      JSONObject formItem = formObj.get(key);
+      EnquiryFormItem enquiryFormItem = new EnquiryFormItem();
+      enquiryFormItem.setSort(Integer.parseInt(key));
+      if ("Country".equals(formItem.getString("name"))
+          || "country".equals(formItem.getString("name"))) {
+        countryFlag = false;
+      }
+      enquiryFormItem.setName(formItem.getString("name"));
+      enquiryFormItem.setValue(formItem.getString("value"));
+      formItems.add(enquiryFormItem);
     }
+    if (countryFlag) {
+      EnquiryFormItem countryForm = new EnquiryFormItem();
+      countryForm.setSort(99);
+      countryForm.setName("Country");
+      countryForm.setValue(adwebEnquiryForm.getCountry());
+      formItems.add(countryForm);
+    }
+
+    formItems.sort(Comparator.comparingInt(EnquiryFormItem::getSort));
+    return formItems;
+  }
 }
 
 @Data
 class EnquiryFormItem {
 
-    private int sort;
+  private int sort;
 
-    private String name;
+  private String name;
 
-    private String value;
+  private String value;
 }

+ 0 - 5
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/enterprise/controller/EnterpriseUserManageController.java

@@ -147,21 +147,16 @@ public class EnterpriseUserManageController {
     this.addSeqNumber(pageList2.getRecords(), pageNo, pageSize);
     // 统计管理的询盘和产品数量
     Map<String, Integer> map1 = adwebEnquiryService.getEnquiryCount(idList);
-    //    Map<String, Integer> map2 = adwebMaterialProductService.getProductCount(idList);
     for (User user1 : pageList2.getRecords()) {
       if (map1.get(user1.getId()) != null) {
         user1.setEnquiryNum(map1.get(user1.getId()));
       }
-      //      if (map2.get(user1.getId()) != null) {
-      //        user1.setProductNum(map2.get(user1.getId()));
-      //      }
     }
     // 主账户产品和询盘数量统计规则不同
     if (pageNo == 1) {
       User user1 = pageList2.getRecords().get(0);
       user1.setPrimaryAccount(true);
       user1.setEnquiryNum(adwebEnquiryService.getEnquiryCount(user1.getId()));
-      //      user1.setProductNum(adwebMaterialProductService.getProductCount(user1.getId()));
     }
     return Result.OK(pageList2);
   }