EmailsMapper.xml 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.storlead.sales.mail.mapper.EmailsMapper">
  4. <!-- 通用查询映射结果 -->
  5. <resultMap id="BaseResultMap" type="com.storlead.sales.mail.entity.EmailsEntity" extends="com.storlead.frame.mapper.SysBaseFieldMapper.BaseResultMap">
  6. <id column="id" property="id" />
  7. <result column="smtp_pop_id" property="smtpPopId" />
  8. <result column="msg_uid" property="msgUid" />
  9. <result column="message_id" property="messageId" />
  10. <result column="subject" property="subject" />
  11. <result column="from" property="from" />
  12. <result column="from_name" property="fromName" />
  13. <result column="recipient" property="recipient" />
  14. <result column="recipient_name" property="recipientName" />
  15. <result column="recipient_cc" property="recipientCc" />
  16. <result column="recipient_cc_name" property="recipientCcName" />
  17. <result column="recipient_bcc" property="recipientBcc" />
  18. <result column="recipient_bcc_name" property="recipientBccName" />
  19. <result column="folder" property="folder" />
  20. <result column="source_type" property="sourceType" />
  21. <result column="custom_folder_id" property="customFolderId" />
  22. <result column="content" property="content" />
  23. <result column="is_star" property="isStar" />
  24. <result column="is_trash" property="isTrash" />
  25. <result column="sent_date" property="sentDate" />
  26. <result column="recipient_date" property="recipientDate" />
  27. <result column="email_size" property="emailSize" />
  28. <result column="remark" property="remark" />
  29. <result column="is_read" property="isRead" />
  30. <result column="reply_msg_id" property="replyMsgId" />
  31. <result column="status" property="status" />
  32. <result column="is_receipt" property="isReceipt" />
  33. <result column="is_track" property="isTrack" />
  34. <result column="read_remind" property="readRemind" />
  35. <result column="delay_send_time" property="delaySendTime" />
  36. <result column="is_only_head" property="isOnlyHead" />
  37. <result column="liaison_ids" property="liaisonIds" />
  38. <result column="customer_ids" property="customerIds" />
  39. <result column="customer_id" property="customerId" />
  40. <result column="follow_up_id" property="followUpId" />
  41. <result column="follow_up_time" property="followUpTime" />
  42. <result column="customer_mail_id" property="customerMailId" />
  43. <result column="delete_tag" property="deleteTag" />
  44. </resultMap>
  45. <!-- 通用查询结果列 -->
  46. <sql id="Base_Column_List">
  47. id, smtp_pop_id, msg_uid,source_type,message_id,follow_up_id,follow_up_time,is_receipt,is_only_head,is_track,delete_tag,read_remind,delay_send_time,customer_ids,liaison_ids, is_star,is_trash,subject,`from`, from_name, recipient, recipient_name, recipient_cc, recipient_cc_name, recipient_bcc, recipient_bcc_name, folder,custom_folder_id, content,remark, sent_date, recipient_date, email_size, is_read, reply_msg_id, status, owner_by, create_time, update_time, is_delete, enabled, create_by, update_by, sort
  48. </sql>
  49. <select id="updateCleanBindCustomerMail">
  50. -- UPDATE emails e
  51. -- SET e.customer_id = NULL;
  52. </select>
  53. <select id="updateBindCustomerMail">
  54. UPDATE emails e
  55. LEFT JOIN customer_company c ON e.from = c.email
  56. SET e.customer_id = c.customer_id
  57. WHERE c.customer_id IS NOT NULL AND e.customer_id is null and c.is_delete = 0;
  58. </select>
  59. <select id="updateBindLiaisonMail">
  60. UPDATE emails e
  61. LEFT JOIN liaison l ON e.from = l.email
  62. SET e.customer_id = l.customer_id, e.liaison_id = l.id
  63. WHERE l.customer_id IS NOT NULL AND (e.customer_id is null or e.liaison_id is null) and l.is_delete = 0;
  64. </select>
  65. <select id="getMailCustomerIdByMailIdAndUserId" resultType="java.lang.Long">
  66. SELECT customer_id FROM customer_company WHERE email is not null AND email != '' AND FIND_IN_SET(LOWER(email),#{mailAddress}) and is_delete = 0
  67. </select>
  68. <select id="getLiaisonMailCustomerIdByMailIdAndUserId" resultType="com.storlead.sales.mail.pojo.vo.MailLiaisonVO">
  69. SELECT id as liaisonId,customer_id as customerId FROM liaison WHERE email is not null AND email != '' AND FIND_IN_SET(LOWER(email),#{mailAddress}) and is_delete = 0
  70. </select>
  71. <select id="bindMailCustomerIdByMailId">
  72. update emails
  73. set customer_ids= #{customerIds}
  74. , liaison_ids = #{liaisonIds}
  75. where id = #{mailId}
  76. </select>
  77. <select id="getEmailsIdByMessageId" resultType="java.lang.Long">
  78. select id from emails where message_id = #{messageId} and smtp_pop_id = #{smtpPopId} limit 1
  79. </select>
  80. <select id="getEmailsByMessageId" resultType="com.storlead.sales.mail.entity.EmailsEntity">
  81. select * from emails where message_id = #{messageId} and folder = #{folder} and smtp_pop_id = #{smtpPopId} limit 1
  82. </select>
  83. <select id="getEmailMessageIdsByMessageIds" resultType="java.lang.String">
  84. select message_id from emails
  85. <where>
  86. <if test="messageIds != null and messageIds.size > 0">
  87. AND message_id IN
  88. <foreach collection="messageIds" item="messageId" open="(" separator="," close=")">
  89. #{messageId}
  90. </foreach>
  91. </if>
  92. and folder = #{folder} and smtp_pop_id = #{smtpPopId}
  93. </where>
  94. </select>
  95. <select id="getEmailMessageIdsByMessageId" resultType="java.lang.String">
  96. select message_id from emails where message_id = #{messageId} and folder = #{folder} and smtp_pop_id = #{smtpPopId}
  97. </select>
  98. <select id="pageList" resultMap="BaseResultMap">
  99. select e.id, e.smtp_pop_id, e.msg_uid,e.message_id,e.is_receipt,e.is_only_head,e.is_track,e.read_remind,
  100. e.delay_send_time,e.customer_ids,e.liaison_ids, e.is_star,e.is_trash,e.subject,e.from, e.from_name, e.recipient,
  101. e.recipient_name, e.recipient_cc, e.recipient_cc_name,e. recipient_bcc, e.recipient_bcc_name, e.folder,e.remark,
  102. e.sent_date, e.recipient_date, e.email_size, e.is_read, e.reply_msg_id, e.status, e.owner_by, e.create_time, e.update_time,
  103. e.is_delete, e.enabled, e.create_by, e.update_by, e.sort from emails as e
  104. <where>
  105. <if test="(dto.customerName != null and dto.customerName != '') or dto.customerId != null">
  106. AND EXISTS (select 1 from customer as c
  107. <where>
  108. <if test="dto.customerName != null and dto.customerName != ''">
  109. AND c.customer_name like concat('%',#{dto.customerName},'%')
  110. </if>
  111. <if test="dto.customerId != null">
  112. and c.id = #{dto.customerId}
  113. </if>
  114. <if test="dto.customerType == null">
  115. <if test="dto.bindCustomer != null and dto.bindCustomer == 1">
  116. and c.owner_by = #{dto.ownerBy}
  117. </if>
  118. </if>
  119. and FIND_IN_SET(c.id,e.customer_ids) > 0 )
  120. </where>
  121. </if>
  122. <if test="dto.smtpPopId != null">
  123. and e.smtp_pop_id = #{dto.smtpPopId}
  124. </if>
  125. <if test="dto.emailAddress != null and dto.emailAddress != ''">
  126. and (e.from like concat('%',#{dto.emailAddress},'%') or e.recipient like concat('%',#{dto.emailAddress},'%') or e.recipient_cc like concat('%',#{dto.emailAddress},'%'))
  127. </if>
  128. <if test="dto.folder != null and dto.folder != ''">
  129. and e.folder = #{dto.folder}
  130. </if>
  131. <if test="dto.isStar != null and dto.isStar == 1">
  132. and e.is_star = #{dto.isStar}
  133. </if>
  134. <if test="dto.systemMail != null and dto.systemMail == 1">
  135. and e.from = 'system@storlead.com'
  136. </if>
  137. <if test="dto.systemMail == null">
  138. and (e.from is null or e.from <![CDATA[<>]]> 'system@storlead.com')
  139. </if>
  140. <if test="dto.isTrash != null">
  141. and is_trash = #{dto.isTrash}
  142. </if>
  143. <if test="dto.readStatu != null">
  144. and is_read = #{dto.readStatu}
  145. </if>
  146. <if test="dto.followUpState != null">
  147. <if test="dto.followUpState == 0">
  148. and e.follow_up_id is null
  149. </if>
  150. <if test="dto.followUpState == 1">
  151. and e.follow_up_id is not null
  152. </if>
  153. </if>
  154. <if test="dto.customerType == null">
  155. <if test="dto.bindCustomer != null and dto.bindCustomer == 0">
  156. and (e.customer_ids = '' or e.customer_ids is null)
  157. </if>
  158. <if test="dto.bindCustomer != null and dto.bindCustomer == 1">
  159. and e.customer_ids is not null
  160. </if>
  161. </if>
  162. <if test="dto.blurry != null and dto.blurry != ''">
  163. and (subject like concat('%',#{dto.blurry},'%') or content like concat('%',#{dto.blurry},'%') or `from` like concat('%',#{dto.blurry},'%') or recipient like concat('%',#{dto.blurry},'%'))
  164. </if>
  165. </where>
  166. and e.is_delete = 0
  167. order by e.recipient_date desc
  168. </select>
  169. <select id="pageListCus" resultMap="BaseResultMap">
  170. select e.id, e.smtp_pop_id, e.msg_uid,e.message_id,e.is_receipt,e.is_only_head,e.is_track,e.read_remind,
  171. e.delay_send_time,e.customer_ids,e.liaison_ids, e.is_star,e.is_trash,e.subject,e.from, e.from_name, e.recipient,
  172. e.recipient_name, e.recipient_cc, e.recipient_cc_name,e. recipient_bcc, e.recipient_bcc_name, e.folder,e.remark,
  173. e.sent_date, e.recipient_date, e.email_size, e.is_read, e.reply_msg_id, e.status, e.owner_by, e.create_time, e.update_time,
  174. e.is_delete, e.enabled, e.create_by, e.update_by, e.sort,c.id as customer_id from emails as e
  175. left join customer as c
  176. on FIND_IN_SET(c.id,e.customer_ids)
  177. <where>
  178. <if test="dto.ownerBy != null">
  179. and e.owner_by = #{dto.ownerBy}
  180. </if>
  181. <if test="dto.smtpPopId != null">
  182. and e.smtp_pop_id = #{dto.smtpPopId}
  183. </if>
  184. <if test="dto.customerId != null">
  185. and c.id = #{dto.customerId}
  186. </if>
  187. <if test="dto.customerId == null">
  188. and e.source_type = 0
  189. </if>
  190. <if test="dto.folder != null and dto.folder != ''">
  191. and e.folder = #{dto.folder}
  192. </if>
  193. <if test="dto.isStar != null and dto.isStar == 1">
  194. and e.is_star = #{dto.isStar}
  195. </if>
  196. <if test="dto.systemMail != null and dto.systemMail == 1">
  197. and e.from = 'system@storlead.com'
  198. </if>
  199. <if test="dto.systemMail == null">
  200. and (e.from is null or e.from <![CDATA[<>]]> 'system@storlead.com')
  201. </if>
  202. <if test="dto.isTrash != null">
  203. and is_trash = #{dto.isTrash}
  204. </if>
  205. <if test="dto.readStatu != null">
  206. and is_read = #{dto.readStatu}
  207. </if>
  208. <if test="dto.emailAddress != null and dto.emailAddress != ''">
  209. and (e.from like concat('%',#{dto.emailAddress},'%') or e.recipient like concat('%',#{dto.emailAddress},'%') or e.recipient_cc like concat('%',#{dto.emailAddress},'%'))
  210. </if>
  211. <if test="dto.customerName != null and dto.customerName != ''">
  212. and c.customer_name like concat('%',#{dto.customerName},'%')
  213. </if>
  214. <if test="dto.folder != null and dto.folder != ''">
  215. and e.folder = #{dto.folder}
  216. </if>
  217. <if test="dto.followUpState != null">
  218. <if test="dto.followUpState == 0">
  219. and e.follow_up_id is null
  220. </if>
  221. <if test="dto.followUpState == 1">
  222. and c.owner_by = #{dto.ownerBy}
  223. and e.follow_up_id is not null
  224. and c.is_delete = 0
  225. </if>
  226. <if test="dto.bindCustomer != null and dto.bindCustomer == 0">
  227. and c.id is null
  228. </if>
  229. <if test="dto.bindCustomer != null and dto.bindCustomer == 1">
  230. and c.id is not null
  231. and c.owner_by = #{dto.ownerBy}
  232. and c.is_delete = 0
  233. </if>
  234. </if>
  235. <if test="dto.customerType != null">
  236. and c.id is not null
  237. and c.owner_by = #{dto.ownerBy}
  238. and c.customer_form = #{dto.customerType}
  239. <if test="dto.customerLevelDictValue != null and dto.customerLevelDictValue != ''">
  240. and c.customer_level_dict_value = #{dto.customerLevelDictValue}
  241. </if>
  242. <if test="dto.customerSourceDictValue != null and dto.customerSourceDictValue != ''">
  243. and c.customer_source_dict_value = #{dto.customerSourceDictValue}
  244. </if>
  245. <if test="dto.customerTypeDictValue != null and dto.customerTypeDictValue != ''">
  246. and c.customer_type_dict_value = #{dto.customerTypeDictValue}
  247. </if>
  248. <if test="dto.clueStatusDictValue != null and dto.clueStatusDictValue != ''">
  249. and c.clue_status_dict_value = #{dto.clueStatusDictValue}
  250. </if>
  251. <if test="dto.continent != null and dto.continent != '' and dto.country != null and dto.country != ''">
  252. and c.continent = #{dto.continent} and c.country = #{dto.country}
  253. </if>
  254. and c.is_delete = 0
  255. </if>
  256. <if test="dto.blurry != null and dto.blurry != ''">
  257. and (subject like concat('%',#{dto.blurry},'%') or content like concat('%',#{dto.blurry},'%') or `from` like concat('%',#{dto.blurry},'%') or recipient like concat('%',#{dto.blurry},'%'))
  258. </if>
  259. and e.is_delete = 0
  260. <if test="dto.customerType != null">
  261. AND e.id IN (
  262. SELECT MAX(e1.id) FROM emails e1
  263. LEFT JOIN customer c1 ON FIND_IN_SET(c1.id, e1.customer_ids)
  264. where e1.is_delete = 0 and c1.is_delete = 0
  265. <if test="dto.ownerBy != null">
  266. and e1.owner_by = #{dto.ownerBy}
  267. </if>
  268. <if test="dto.folder != null and dto.folder != ''">
  269. and e1.folder = #{dto.folder}
  270. </if>
  271. <if test="dto.isTrash != null">
  272. and e1.is_trash = #{dto.isTrash}
  273. </if>
  274. <if test="dto.smtpPopId != null">
  275. and e1.smtp_pop_id = #{dto.smtpPopId}
  276. </if>
  277. <if test="dto.customerType != null">
  278. and c.customer_form = #{dto.customerType}
  279. </if>
  280. GROUP BY c1.id
  281. )
  282. </if>
  283. </where>
  284. order by e.recipient_date desc
  285. </select>
  286. <select id="pageListAllCus" resultMap="BaseResultMap">
  287. WITH ranked_emails AS (
  288. select e.id, e.smtp_pop_id, e.msg_uid,e.message_id,e.is_receipt,e.is_only_head,e.is_track,e.read_remind,
  289. e.delay_send_time,e.customer_ids,e.liaison_ids, e.is_star,e.is_trash,e.subject,e.from, e.from_name, e.recipient,
  290. e.recipient_name, e.recipient_cc, e.recipient_cc_name,e. recipient_bcc, e.recipient_bcc_name, e.folder,e.remark,
  291. e.sent_date, e.recipient_date, e.email_size, e.is_read, e.reply_msg_id, e.status, e.owner_by, e.create_time,
  292. e.update_time,
  293. e.is_delete, e.enabled, e.create_by, e.update_by, e.sort,c.id as customer_id
  294. ,ROW_NUMBER() OVER (PARTITION BY c.id ORDER BY e.id DESC) AS rn
  295. from emails as e
  296. left join customer as c
  297. on FIND_IN_SET(c.id,e.customer_ids)
  298. <where>
  299. <if test="dto.ownerBy != null">
  300. and e.owner_by = #{dto.ownerBy}
  301. </if>
  302. <if test="dto.smtpPopId != null">
  303. and e.smtp_pop_id = #{dto.smtpPopId}
  304. </if>
  305. <if test="dto.customerId != null">
  306. and c.id = #{dto.customerId}
  307. </if>
  308. <if test="dto.folder != null and dto.folder != ''">
  309. and e.folder = #{dto.folder}
  310. </if>
  311. <if test="dto.isStar != null and dto.isStar == 1">
  312. and e.is_star = #{dto.isStar}
  313. </if>
  314. <if test="dto.systemMail != null and dto.systemMail == 1">
  315. and e.from = 'system@storlead.com'
  316. </if>
  317. <if test="dto.systemMail == null">
  318. and (e.from is null or e.from <![CDATA[<>]]> 'system@storlead.com')
  319. </if>
  320. <if test="dto.isTrash != null">
  321. and is_trash = #{dto.isTrash}
  322. </if>
  323. <if test="dto.readStatu != null">
  324. and is_read = #{dto.readStatu}
  325. </if>
  326. <if test="dto.emailAddress != null and dto.emailAddress != ''">
  327. and (e.from like concat('%',#{dto.emailAddress},'%') or e.recipient like
  328. concat('%',#{dto.emailAddress},'%') or e.recipient_cc like concat('%',#{dto.emailAddress},'%'))
  329. </if>
  330. <if test="dto.customerName != null and dto.customerName != ''">
  331. and c.customer_name like concat('%',#{dto.customerName},'%')
  332. </if>
  333. <if test="dto.folder != null and dto.folder != ''">
  334. and e.folder = #{dto.folder}
  335. </if>
  336. and e.source_type = 0
  337. <if test="dto.followUpState != null">
  338. <if test="dto.followUpState == 0">
  339. and e.follow_up_id is null
  340. </if>
  341. <if test="dto.followUpState == 1">
  342. and c.owner_by = #{dto.ownerBy}
  343. and e.follow_up_id is not null
  344. and c.is_delete = 0
  345. </if>
  346. <if test="dto.bindCustomer != null and dto.bindCustomer == 0">
  347. and c.id is null
  348. </if>
  349. <if test="dto.bindCustomer != null and dto.bindCustomer == 1">
  350. and c.id is not null
  351. and c.owner_by = #{dto.ownerBy}
  352. and c.is_delete = 0
  353. </if>
  354. </if>
  355. <if test="dto.customerType != null">
  356. and c.id is not null
  357. and c.owner_by = #{dto.ownerBy}
  358. and c.customer_form = #{dto.customerType}
  359. <if test="dto.customerLevelDictValue != null and dto.customerLevelDictValue != ''">
  360. and c.customer_level_dict_value = #{dto.customerLevelDictValue}
  361. </if>
  362. <if test="dto.customerSourceDictValue != null and dto.customerSourceDictValue != ''">
  363. and c.customer_source_dict_value = #{dto.customerSourceDictValue}
  364. </if>
  365. <if test="dto.customerTypeDictValue != null and dto.customerTypeDictValue != ''">
  366. and c.customer_type_dict_value = #{dto.customerTypeDictValue}
  367. </if>
  368. <if test="dto.clueStatusDictValue != null and dto.clueStatusDictValue != ''">
  369. and c.clue_status_dict_value = #{dto.clueStatusDictValue}
  370. </if>
  371. <if test="dto.continent != null and dto.continent != '' and dto.country != null and dto.country != ''">
  372. and c.continent = #{dto.continent} and c.country = #{dto.country}
  373. </if>
  374. and c.is_delete = 0
  375. </if>
  376. <if test="dto.blurry != null and dto.blurry != ''">
  377. and (subject like concat('%',#{dto.blurry},'%') or content like concat('%',#{dto.blurry},'%') or `from`
  378. like concat('%',#{dto.blurry},'%') or recipient like concat('%',#{dto.blurry},'%'))
  379. </if>
  380. and e.is_delete = 0
  381. </where>
  382. order by e.recipient_date desc
  383. )
  384. SELECT * FROM ranked_emails re
  385. WHERE re.rn = 1
  386. </select>
  387. <select id="pageListNew" resultMap="BaseResultMap">
  388. select e1.*,cb.id as customer_mail_id,cb.follow_up_id,cb.follow_up_time,GROUP_CONCAT(DISTINCT cb.customer_id ORDER BY e1.id ASC) as customer_ids from
  389. (
  390. select e.id, e.smtp_pop_id, e.msg_uid,e.message_id,e.is_receipt,e.is_only_head,e.is_track,e.read_remind,
  391. e.delay_send_time, e.is_star,e.is_trash,e.subject,e.from, e.from_name, e.recipient,
  392. e.recipient_name, e.recipient_cc, e.recipient_cc_name,e. recipient_bcc, e.recipient_bcc_name, e.folder,e.remark,
  393. e.sent_date, e.recipient_date, e.email_size, e.is_read, e.reply_msg_id, e.status, e.owner_by, e.create_time, e.update_time,
  394. e.is_delete, e.enabled, e.create_by, e.update_by, e.sort from emails as e
  395. <where>
  396. <if test="dto.smtpPopId != null">
  397. and e.smtp_pop_id = #{dto.smtpPopId}
  398. </if>
  399. <if test="dto.emailAddress != null and dto.emailAddress != ''">
  400. and (e.from like concat('%',#{dto.emailAddress},'%') or e.recipient like concat('%',#{dto.emailAddress},'%') or e.recipient_cc like concat('%',#{dto.emailAddress},'%'))
  401. </if>
  402. <if test="dto.folder != null and dto.folder != ''">
  403. and e.folder = #{dto.folder}
  404. </if>
  405. <if test="dto.isStar != null and dto.isStar == 1">
  406. and e.is_star = #{dto.isStar}
  407. </if>
  408. <if test="dto.systemMail != null and dto.systemMail == 1">
  409. and e.from = 'system@storlead.com'
  410. </if>
  411. <if test="dto.systemMail == null">
  412. and (e.from is null or e.from <![CDATA[<>]]> 'system@storlead.com')
  413. </if>
  414. <if test="dto.isTrash != null">
  415. and e.is_trash = #{dto.isTrash}
  416. </if>
  417. <if test="dto.readStatu != null">
  418. and e.is_read = #{dto.readStatu}
  419. </if>
  420. <if test="dto.blurry != null and dto.blurry != ''">
  421. and (e.subject like concat('%',#{dto.blurry},'%') or e.content like concat('%',#{dto.blurry},'%'))
  422. </if>
  423. <if test="dto.folderId != null">
  424. and e.custom_folder_id = #{dto.folderId}
  425. </if>
  426. <if test="dto.bindCustomer == null and dto.customerId == null">
  427. <if test="dto.folderId == null and dto.isTrash == 0">
  428. and e.custom_folder_id is null
  429. </if>
  430. </if>
  431. <if test="dto.customerId == null">
  432. and e.source_type = 0
  433. </if>
  434. and e.is_delete = 0
  435. </where>
  436. ) as e1
  437. left join customer_mail_bing_mark as cb
  438. on e1.id = cb.mail_id
  439. left join customer as c
  440. on cb.customer_id = c.id and c.is_delete = 0
  441. <where>
  442. <if test="dto.customerId != null">
  443. and c.id = #{dto.customerId}
  444. </if>
  445. <if test="(dto.customerName != null and dto.customerName != '')">
  446. and c.customer_name like concat('%',#{dto.customerName},'%')
  447. and c.owner_by = #{dto.ownerBy}
  448. </if>
  449. <if test="dto.customerLevelDictValue != null and dto.customerLevelDictValue != ''">
  450. and c.customer_level_dict_value = #{dto.customerLevelDictValue}
  451. </if>
  452. <if test="dto.customerSourceDictValue != null and dto.customerSourceDictValue != ''">
  453. and c.customer_source_dict_value = #{dto.customerSourceDictValue}
  454. </if>
  455. <if test="dto.customerTypeDictValue != null and dto.customerTypeDictValue != ''">
  456. and c.customer_type_dict_value = #{dto.customerTypeDictValue}
  457. </if>
  458. <if test="dto.clueStatusDictValue != null and dto.clueStatusDictValue != ''">
  459. and c.clue_status_dict_value = #{dto.clueStatusDictValue}
  460. </if>
  461. <if test="dto.continent != null and dto.continent != '' and dto.country != null and dto.country != ''">
  462. and c.continent = #{dto.continent} and c.country = #{dto.country}
  463. </if>
  464. <if test="dto.customerType != null">
  465. and c.customer_form = #{dto.customerType}
  466. </if>
  467. </where>
  468. <if test="dto.customerType == null">
  469. GROUP BY e1.id
  470. </if>
  471. <if test="dto.customerType != null">
  472. GROUP BY e1.id,cb.customer_id
  473. </if>
  474. <if test="dto.followUpState != null and dto.followUpState == 0">
  475. <if test="dto.bindCustomer != null and dto.bindCustomer == 0">
  476. HAVING customer_ids is null and reply_msg_id is null
  477. </if>
  478. <if test="dto.bindCustomer != null and dto.bindCustomer == 1">
  479. HAVING customer_ids is not null and follow_up_id is null
  480. </if>
  481. </if>
  482. order by e1.recipient_date desc
  483. </select>
  484. <select id="pageListNewAllCus" resultMap="BaseResultMap">
  485. WITH ranked_emails AS (
  486. select e1.*,cc.customer_id as customer_ids,ROW_NUMBER() OVER (PARTITION BY cc.customer_id ORDER BY e1.id DESC) AS rn
  487. from
  488. (
  489. select e.id, e.smtp_pop_id, e.msg_uid,e.message_id,e.is_receipt,e.is_only_head,e.is_track,e.read_remind,
  490. e.delay_send_time, e.is_star,e.is_trash,e.subject,e.from, e.from_name, e.recipient,
  491. e.recipient_name, e.recipient_cc, e.recipient_cc_name,e. recipient_bcc, e.recipient_bcc_name, e.folder,e.remark,
  492. e.sent_date, e.recipient_date, e.email_size, e.is_read, e.reply_msg_id, e.status, e.owner_by, e.create_time,
  493. e.update_time,
  494. e.is_delete, e.enabled, e.create_by, e.update_by, e.sort from emails as e
  495. <where>
  496. <if test="dto.smtpPopId != null">
  497. and e.smtp_pop_id = #{dto.smtpPopId}
  498. </if>
  499. <if test="dto.emailAddress != null and dto.emailAddress != ''">
  500. and (e.from like concat('%',#{dto.emailAddress},'%') or e.recipient like
  501. concat('%',#{dto.emailAddress},'%') or e.recipient_cc like concat('%',#{dto.emailAddress},'%'))
  502. </if>
  503. <if test="dto.folder != null and dto.folder != ''">
  504. and e.folder = #{dto.folder}
  505. </if>
  506. <if test="dto.isStar != null and dto.isStar == 1">
  507. and e.is_star = #{dto.isStar}
  508. </if>
  509. <if test="dto.systemMail != null and dto.systemMail == 1">
  510. and e.from = 'system@storlead.com'
  511. </if>
  512. <if test="dto.systemMail == null">
  513. and (e.from is null or e.from <![CDATA[<>]]> 'system@storlead.com')
  514. </if>
  515. <if test="dto.isTrash != null">
  516. and e.is_trash = #{dto.isTrash}
  517. </if>
  518. <if test="dto.readStatu != null">
  519. and e.is_read = #{dto.readStatu}
  520. </if>
  521. <if test="dto.followRemind != null">
  522. and e.subject = '跟进提醒'
  523. </if>
  524. <if test="dto.customerId == null">
  525. and e.source_type = 0
  526. </if>
  527. <if test="dto.blurry != null and dto.blurry != ''">
  528. and (e.subject like concat('%',#{dto.blurry},'%') or e.content like concat('%',#{dto.blurry},'%'))
  529. </if>
  530. and e.is_delete = 0
  531. </where>
  532. ) as e1
  533. left join (
  534. SELECT
  535. cb.customer_id,
  536. cb.mail_id,
  537. c.customer_form
  538. FROM
  539. customer_mail_bing_mark AS cb
  540. LEFT JOIN customer AS c ON cb.customer_id = c.id
  541. WHERE c.is_delete = 0
  542. AND c.owner_by = #{dto.ownerBy}
  543. <if test="dto.customerId != null">
  544. and c.id = #{dto.customerId}
  545. </if>
  546. <if test="(dto.customerName != null and dto.customerName != '')">
  547. and c.customer_name like concat('%',#{dto.customerName},'%')
  548. and c.owner_by = #{dto.ownerBy}
  549. </if>
  550. <if test="dto.customerLevelDictValue != null and dto.customerLevelDictValue != ''">
  551. and c.customer_level_dict_value = #{dto.customerLevelDictValue}
  552. </if>
  553. <if test="dto.customerSourceDictValue != null and dto.customerSourceDictValue != ''">
  554. and c.customer_source_dict_value = #{dto.customerSourceDictValue}
  555. </if>
  556. <if test="dto.customerTypeDictValue != null and dto.customerTypeDictValue != ''">
  557. and c.customer_type_dict_value = #{dto.customerTypeDictValue}
  558. </if>
  559. <if test="dto.clueStatusDictValue != null and dto.clueStatusDictValue != ''">
  560. and c.clue_status_dict_value = #{dto.clueStatusDictValue}
  561. </if>
  562. <if test="dto.customerType != null">
  563. and c.customer_form = #{dto.customerType}
  564. </if>
  565. <if test="dto.continent != null and dto.continent != '' and dto.country != null and dto.country != ''">
  566. and c.continent = #{dto.continent} and c.country = #{dto.country}
  567. </if>
  568. ) as cc
  569. on cc.mail_id = e1.id
  570. where e1.owner_by = #{dto.ownerBy} and cc.mail_id is not null
  571. order by e1.recipient_date desc
  572. )
  573. SELECT * FROM ranked_emails re
  574. WHERE re.rn = 1
  575. </select>
  576. <select id="countMailSummaryNum" resultType="com.storlead.sales.mail.pojo.vo.NewMailCountTipVO">
  577. SELECT
  578. IFNULL(SUM(CASE WHEN folder = 'INBOX' AND is_read = 0 and is_trash = 0 and `from` <![CDATA[<>]]> 'system@storlead.com' THEN 1 ELSE 0 END),0) AS inboxCount,
  579. IFNULL(SUM(CASE WHEN folder = 'SENT' AND is_delete = 0 and is_trash = 0 THEN 1 ELSE 0 END),0) AS sentCount,
  580. IFNULL(SUM(CASE WHEN folder = 'SENTING' AND is_delete = 0 and is_trash = 0 THEN 1 ELSE 0 END),0) AS sentingCount,
  581. IFNULL(SUM(CASE WHEN folder = 'DRAFT' AND is_delete = 0 and is_trash = 0 THEN 1 ELSE 0 END),0) AS draftCount,
  582. IFNULL(SUM(CASE WHEN `subject` = '跟进提醒' AND is_delete = 0 THEN 1 ELSE 0 END),0) AS followUpCount,
  583. IFNULL(SUM(CASE WHEN `from` = 'system@storlead.com' AND is_read = 0 THEN 1 ELSE 0 END),0) AS systemCount,
  584. IFNULL(SUM(CASE WHEN is_star = 1 THEN 1 ELSE 0 END),0) AS starCount,
  585. IFNULL(SUM(CASE WHEN is_trash = 1 THEN 1 ELSE 0 END),0) AS trashCount
  586. FROM emails WHERE is_delete = 0 and owner_by = #{ownerBy} and smtp_pop_id = #{smtpPopId} and custom_folder_id is null and source_type = 0
  587. </select>
  588. <select id="countCustomerFollowUpMailNum" resultType="com.storlead.sales.mail.pojo.vo.NewMailCountTipVO">
  589. select
  590. IFNULL(SUM(CASE WHEN m.customer_ids is null and m.reply_msg_id is null THEN 1 ELSE 0 END),0) AS unknownCount,
  591. IFNULL(SUM(CASE WHEN m.customer_ids is not null and m.follow_up_id is null THEN 1 ELSE 0 END),0) AS knownCount
  592. from (
  593. select e1.id,e1.reply_msg_id,cb.follow_up_id as follow_up_id,GROUP_CONCAT(DISTINCT cb.customer_id ORDER BY e1.id ASC) as customer_ids
  594. from emails as e1
  595. left join customer_mail_bing_mark as cb
  596. on e1.id = cb.mail_id
  597. left join customer as c
  598. on e1.id = cb.mail_id
  599. and cb.customer_id = c.id and c.is_delete = 0
  600. where e1.owner_by = #{ownerBy} and e1.smtp_pop_id = #{smtpPopId} and e1.from <![CDATA[<>]]> 'system@storlead.com'
  601. AND e1.is_delete = 0
  602. AND e1.folder = 'INBOX'
  603. AND e1.is_trash = 0
  604. AND e1.is_delete = 0
  605. and e1.source_type = 0
  606. GROUP BY e1.id
  607. ) as m
  608. </select>
  609. <select id="countCustomerAndClueNumber" resultType="com.storlead.sales.mail.pojo.vo.NewMailCountTipVO">
  610. SELECT
  611. IFNULL(SUM( CASE WHEN m.customer_form = 10 THEN 1 ELSE 0 END ),0) AS customerCount,
  612. IFNULL(SUM( CASE WHEN m.customer_form = 11 THEN 1 ELSE 0 END ),0) AS clueCount
  613. FROM
  614. (
  615. SELECT
  616. cb.customer_id,
  617. c.customer_form,
  618. MAX( cb.mail_id ) AS mail_id
  619. FROM
  620. customer_mail_bing_mark AS cb
  621. LEFT JOIN customer AS c ON cb.customer_id = c.id
  622. left join emails as e on e.id = cb.mail_id and e.smtp_pop_id = #{smtpPopId}
  623. WHERE c.is_delete = 0
  624. AND c.owner_by = #{ownerBy}
  625. AND e.id is not null
  626. GROUP BY
  627. cb.customer_id,
  628. c.customer_form
  629. ) AS m
  630. </select>
  631. <select id="selectCustomerLastFollowUpTime" resultType="java.time.LocalDateTime">
  632. SELECT
  633. max( follow_up_time ) AS follow_up_time
  634. FROM
  635. (
  636. SELECT
  637. max( create_time ) AS follow_up_time
  638. FROM
  639. customer_follow_up
  640. WHERE
  641. customer_id = #{customerId}
  642. UNION ALL
  643. SELECT
  644. max( sent_date ) AS follow_up_time
  645. FROM
  646. customer_mail_bing_mark AS mbm
  647. LEFT JOIN emails AS e1 ON e1.id = mbm.mail_id
  648. WHERE
  649. mbm.customer_id = #{customerId}
  650. and e1.source_type = 0
  651. ) AS m
  652. </select>
  653. <!-- 获取删除后需要删除服务器的邮件 -->
  654. <select id="selectDelayDeleteMail" resultMap="BaseResultMap">
  655. -- SELECT id,smtp_pop_id, msg_uid, message_id,folder,create_time,sent_date,delete_tag FROM emails e
  656. -- WHERE e.is_only_head = 0 AND e.smtp_pop_id = 10 and delete_tag = 0
  657. -- AND e.create_time <![CDATA[<]]> DATE_SUB(NOW(), INTERVAL 15 DAY)
  658. -- an'd'
  659. -- AND EXISTS ( SELECT 1 FROM smtp_pop_settings sp WHERE sp.id = e.smtp_pop_id AND sp.smtp_pop_settings = 1)
  660. -- AND NOT EXISTS ( SELECT 1 FROM mail_attachment ma WHERE ma.email_id = e.id AND ma.download <![CDATA[<>]]> 1) order by sent_date desc limit 10;
  661. SELECT
  662. id,
  663. smtp_pop_id,
  664. msg_uid,
  665. message_id,
  666. folder,
  667. create_time,
  668. sent_date,
  669. delete_tag,
  670. is_delete
  671. FROM
  672. emails e
  673. WHERE
  674. e.is_only_head = 0
  675. AND delete_tag = 0
  676. AND recipient_date IS NOT NULL
  677. AND ( e.folder = 'INBOX' OR e.folder = 'SENT' )
  678. AND ( ( e.recipient_date > '2025-11-13 15:45:09' AND e.recipient_date <![CDATA[<]]> DATE_SUB( NOW( ), INTERVAL 15 DAY ) ) )
  679. AND e.delete_tag = 0
  680. AND EXISTS ( SELECT 1 FROM smtp_pop_settings sp WHERE sp.id = e.smtp_pop_id )
  681. AND NOT EXISTS ( SELECT 1 FROM mail_attachment ma WHERE ma.email_id = e.id AND ma.download <![CDATA[<>]]> 1 )
  682. </select>
  683. <!-- <select id="selectUserClickTitle" resultType="com.storlead.sales.mail.pojo.vo.UserClickTitleVO">-->
  684. <!-- SELECT a.id as dataId,a.subject as title,b.click_count AS clickCount FROM emails as a LEFT JOIN user_click_record as b ON a.id = b.data_id and b.click_type = 1-->
  685. <!-- where a.owner_by = #{ownerBy} and a.is_delete = 0-->
  686. <!-- <if test="title != null and title != ''">-->
  687. <!-- and a.`subject` like concat('%',#{title},'%')-->
  688. <!-- </if>-->
  689. <!-- order by b.click_count desc limit 10-->
  690. <!-- </select>-->
  691. <select id="selectUserClickTitle" resultType="com.storlead.sales.mail.pojo.vo.UserClickTitleVO">
  692. SELECT
  693. dataId,
  694. title,
  695. clickCount
  696. FROM
  697. (
  698. SELECT
  699. a.id AS dataId,
  700. a.SUBJECT AS title,
  701. b.click_count AS clickCount,
  702. ROW_NUMBER ( ) OVER ( PARTITION BY a.SUBJECT ORDER BY b.click_count DESC ) AS rn
  703. FROM
  704. emails AS a
  705. LEFT JOIN user_click_record AS b ON a.id = b.data_id
  706. AND b.click_type = 1
  707. WHERE
  708. a.owner_by = #{ownerBy}
  709. <if test="title != null and title != ''">
  710. AND a.`subject` LIKE CONCAT('%',#{title},'%')
  711. </if>
  712. AND a.is_delete = 0
  713. ) AS t
  714. WHERE
  715. t.rn = 1
  716. ORDER BY
  717. clickCount DESC
  718. LIMIT 10;
  719. </select>
  720. <select id="selectCustomerEmailPage" resultType="com.storlead.sales.mail.pojo.vo.CustomerEmailVo">
  721. SELECT
  722. cm.mail_id AS mailId,
  723. e.from_name AS fromName,
  724. e.recipient_date AS recipientDate,
  725. e.recipient_name AS recipientName,
  726. e.recipient AS recipient,
  727. e.subject AS subject,
  728. e.in_out_mark AS inOutMark
  729. FROM
  730. customer_mail_bing_mark AS cm
  731. LEFT JOIN emails AS e ON e.id = cm.mail_id
  732. WHERE
  733. cm.customer_id = #{query.customerId}
  734. <if test="query.blurry != null and query.blurry != ''">
  735. AND e.`subject` LIKE CONCAT('%',#{query.blurry},'%')
  736. </if>
  737. <if test="query.liaisonId != null and query.liaisonId != ''">
  738. AND FIND_IN_SET(#{query.liaisonId}, cm.`liaison_ids`)
  739. </if>
  740. <if test="query.beginTime != null and query.endTime != null">
  741. and e.recipient_date >= #{query.beginTime} and e.recipient_date <![CDATA[<=]]> #{query.endTime}
  742. </if>
  743. AND e.is_delete = 0
  744. ORDER BY
  745. e.recipient_date
  746. DESC
  747. </select>
  748. <select id="selectCusIdByMailId" resultType="java.lang.Long">
  749. select customer_id from customer_mail_bing_mark where is_delete = 0
  750. and mail_id in
  751. <foreach collection="mailIdls" item="mailId" open="(" separator="," close=")">
  752. #{mailId}
  753. </foreach>
  754. </select>
  755. <select id="selectCusInfoByCusId" resultType="com.storlead.sales.mail.pojo.vo.CustomerMailVO">
  756. select customer_name as customerName,country from customer where is_delete = 0
  757. and id in
  758. <foreach collection="customerIdls" item="customerId" open="(" separator="," close=")">
  759. #{customerId}
  760. </foreach>
  761. </select>
  762. <select id="countSmtpMailCount" resultType="java.lang.Integer">
  763. select count(1) from smtp_pop_settings where is_delete = 0 and owner_by = #{userId}
  764. </select>
  765. </mapper>