EmailContentService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using LeaRun.Application.Entity.PublicInfoManage;
  2. using LeaRun.Application.IService.PublicInfoManage;
  3. using LeaRun.Data;
  4. using LeaRun.Data.Repository;
  5. using LeaRun.Util.Extension;
  6. using LeaRun.Util.WebControl;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data.Common;
  10. using System.Linq;
  11. using System.Text;
  12. namespace LeaRun.Application.Service.PublicInfoManage
  13. {
  14. /// <summary>
  15. /// 版 本 6.1
  16. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  17. /// 创建人:佘赐雄
  18. /// 日 期:2015.12.8 11:31
  19. /// 描 述:邮件内容
  20. /// </summary>
  21. public class EmailContentService : RepositoryFactory<EmailContentEntity>, IEmailContentService
  22. {
  23. #region 获取数据
  24. /// <summary>
  25. /// 未读邮件
  26. /// </summary>
  27. /// <param name="pagination">分页参数</param>
  28. /// <param name="userId">用户Id</param>
  29. /// <param name="keyword">关键字</param>
  30. /// <returns></returns>
  31. public IEnumerable<EmailContentEntity> GetUnreadMail(Pagination pagination, string userId, string keyword)
  32. {
  33. var strSql = new StringBuilder();
  34. strSql.Append(@"SELECT a.AddresseeId AS ContentId ,
  35. c.Theme ,
  36. c.ThemeColor ,
  37. c.SenderId ,
  38. c.SenderName ,
  39. c.SenderTime ,
  40. c.SendPriority,
  41. a.IsHighlight,
  42. a.CreateDate
  43. FROM Email_Addressee a
  44. LEFT JOIN Email_Content c ON c.ContentId = a.ContentId
  45. WHERE a.RecipientId = @userId AND a.IsRead <> 1");
  46. strSql.Append(" AND a.DeleteMark = 0");
  47. var parameter = new List<DbParameter>();
  48. parameter.Add(DbParameters.CreateDbParameter("@userId", userId));
  49. if (!keyword.IsEmpty())
  50. {
  51. strSql.Append(" AND c.Theme like @Theme");
  52. parameter.Add(DbParameters.CreateDbParameter("@Theme", '%' + keyword + '%'));
  53. }
  54. return this.BaseRepository().FindList(strSql.ToString(), parameter.ToArray(), pagination);
  55. }
  56. /// <summary>
  57. /// 未读邮件数量
  58. /// </summary>
  59. /// <param name="userId">用户Id</param>
  60. /// <returns></returns>
  61. public int GetUnreadMailCount(string userId)
  62. {
  63. var expression = LinqExtensions.True<EmailAddresseeEntity>();
  64. expression = expression.And(t => t.RecipientId == userId);
  65. expression = expression.And(t => t.IsRead != 1);
  66. expression = expression.And(t => t.DeleteMark == 0);
  67. return new RepositoryFactory().BaseRepository().IQueryable<EmailAddresseeEntity>(expression).Count();
  68. }
  69. /// <summary>
  70. /// 星标邮件
  71. /// </summary>
  72. /// <param name="pagination">分页参数</param>
  73. /// <param name="userId">用户Id</param>
  74. /// <param name="keyword">关键字</param>
  75. /// <returns></returns>
  76. public IEnumerable<EmailContentEntity> GetAsteriskMail(Pagination pagination, string userId, string keyword)
  77. {
  78. var strSql = new StringBuilder();
  79. strSql.Append(@"SELECT a.AddresseeId AS ContentId ,
  80. c.Theme ,
  81. c.ThemeColor ,
  82. c.SenderId ,
  83. c.SenderName ,
  84. c.SenderTime ,
  85. c.SendPriority,
  86. a.IsHighlight,
  87. a.CreateDate
  88. FROM Email_Addressee a
  89. LEFT JOIN Email_Content c ON c.ContentId = a.ContentId
  90. WHERE a.RecipientId = @userId AND a.IsHighlight = 1");
  91. strSql.Append(" AND a.DeleteMark = 0");
  92. var parameter = new List<DbParameter>();
  93. parameter.Add(DbParameters.CreateDbParameter("@userId", userId));
  94. if (!keyword.IsEmpty())
  95. {
  96. strSql.Append(" AND c.Theme like @Theme");
  97. parameter.Add(DbParameters.CreateDbParameter("@Theme", '%' + keyword + '%'));
  98. }
  99. return this.BaseRepository().FindList(strSql.ToString(), parameter.ToArray(), pagination);
  100. }
  101. /// <summary>
  102. /// 星标邮件数量
  103. /// </summary>
  104. /// <param name="userId">用户Id</param>
  105. /// <returns></returns>
  106. public int GetAsteriskMailCount(string userId)
  107. {
  108. var expression = LinqExtensions.True<EmailAddresseeEntity>();
  109. expression = expression.And(t => t.RecipientId == userId);
  110. expression = expression.And(t => t.IsHighlight == 1);
  111. expression = expression.And(t => t.DeleteMark == 0);
  112. return new RepositoryFactory().BaseRepository().IQueryable<EmailAddresseeEntity>(expression).Count();
  113. }
  114. /// <summary>
  115. /// 草稿箱
  116. /// </summary>
  117. /// <param name="pagination">分页参数</param>
  118. /// <param name="userId">用户Id</param>
  119. /// <param name="keyword">关键字</param>
  120. /// <returns></returns>
  121. public IEnumerable<EmailContentEntity> GetDraftMail(Pagination pagination, string userId, string keyword)
  122. {
  123. var expression = LinqExtensions.True<EmailContentEntity>();
  124. expression = expression.And(t => t.SendState == 0);
  125. expression = expression.And(t => t.CreateUserId == userId);
  126. if (!keyword.IsEmpty())
  127. {
  128. expression = expression.And(t => t.Theme.Contains(keyword));
  129. }
  130. return this.BaseRepository().FindList(expression, pagination);
  131. }
  132. /// <summary>
  133. /// 草稿箱数量
  134. /// </summary>
  135. /// <param name="userId">用户Id</param>
  136. /// <returns></returns>
  137. public int GetDraftMailCount(string userId)
  138. {
  139. var expression = LinqExtensions.True<EmailContentEntity>();
  140. expression = expression.And(t => t.SendState == 0);
  141. expression = expression.And(t => t.CreateUserId == userId);
  142. return this.BaseRepository().IQueryable(expression).Count();
  143. }
  144. /// <summary>
  145. /// 回收箱
  146. /// </summary>
  147. /// <param name="pagination">分页参数</param>
  148. /// <param name="userId">用户Id</param>
  149. /// <param name="keyword">关键字</param>
  150. /// <returns></returns>
  151. public IEnumerable<EmailContentEntity> GetRecycleMail(Pagination pagination, string userId, string keyword)
  152. {
  153. var strSql = new StringBuilder();
  154. strSql.Append(@"SELECT *
  155. FROM ( SELECT a.AddresseeId AS ContentId ,
  156. c.Theme ,
  157. c.ThemeColor ,
  158. c.SenderId ,
  159. c.SenderName ,
  160. c.SenderTime ,
  161. c.SendPriority ,
  162. a.IsHighlight ,
  163. a.CreateDate
  164. FROM Email_Addressee a
  165. LEFT JOIN Email_Content c ON c.ContentId = a.ContentId
  166. WHERE a.RecipientId = @userId
  167. AND a.DeleteMark = 1
  168. UNION
  169. SELECT c.ContentId ,
  170. c.Theme ,
  171. c.ThemeColor ,
  172. c.SenderId ,
  173. c.SenderName ,
  174. c.SenderTime ,
  175. c.SendPriority ,
  176. c.IsHighlight ,
  177. c.CreateDate
  178. FROM Email_Content c
  179. WHERE c.CreateUserId = @userId
  180. AND c.DeleteMark = 1
  181. ) t WHERE 1=1");
  182. var parameter = new List<DbParameter>();
  183. parameter.Add(DbParameters.CreateDbParameter("@userId", userId));
  184. if (!keyword.IsEmpty())
  185. {
  186. strSql.Append(" AND Theme like @Theme");
  187. parameter.Add(DbParameters.CreateDbParameter("@Theme", '%' + keyword + '%'));
  188. }
  189. return this.BaseRepository().FindList(strSql.ToString(), parameter.ToArray(), pagination);
  190. }
  191. /// <summary>
  192. /// 回收箱数量
  193. /// </summary>
  194. /// <param name="userId">用户Id</param>
  195. /// <returns></returns>
  196. public int GetRecycleMailCount(string userId)
  197. {
  198. var strSql = new StringBuilder();
  199. strSql.Append(@"SELECT COUNT(1)
  200. FROM ( SELECT a.AddresseeId AS ContentId ,
  201. c.Theme ,
  202. c.ThemeColor ,
  203. c.SenderId ,
  204. c.SenderName ,
  205. c.SenderTime ,
  206. c.SendPriority ,
  207. a.IsHighlight ,
  208. a.CreateDate
  209. FROM Email_Addressee a
  210. LEFT JOIN Email_Content c ON c.ContentId = a.ContentId
  211. WHERE a.RecipientId = @userId
  212. AND a.DeleteMark = 1
  213. UNION
  214. SELECT c.ContentId ,
  215. c.Theme ,
  216. c.ThemeColor ,
  217. c.SenderId ,
  218. c.SenderName ,
  219. c.SenderTime ,
  220. c.SendPriority ,
  221. c.IsHighlight ,
  222. c.CreateDate
  223. FROM Email_Content c
  224. WHERE c.CreateUserId = @userId
  225. AND c.DeleteMark = 1
  226. ) t");
  227. var parameter = new List<DbParameter>();
  228. parameter.Add(DbParameters.CreateDbParameter("@userId", userId));
  229. return this.BaseRepository().FindObject(strSql.ToString(), parameter.ToArray()).ToInt();
  230. }
  231. /// <summary>
  232. /// 收件箱
  233. /// </summary>
  234. /// <param name="pagination">分页参数</param>
  235. /// <param name="userId">用户Id</param>
  236. /// <param name="keyword">关键字</param>
  237. /// <returns></returns>
  238. public IEnumerable<EmailContentEntity> GetAddresseeMail(Pagination pagination, string userId, string keyword)
  239. {
  240. var strSql = new StringBuilder();
  241. strSql.Append(@"SELECT a.AddresseeId AS ContentId ,
  242. c.Theme ,
  243. c.ThemeColor ,
  244. c.SenderId ,
  245. c.SenderName ,
  246. c.SenderTime ,
  247. c.SendPriority,
  248. c.CreateDate,
  249. a.IsHighlight
  250. FROM Email_Addressee a
  251. LEFT JOIN Email_Content c ON c.ContentId = a.ContentId
  252. WHERE a.RecipientId = @userId");
  253. strSql.Append(" AND a.DeleteMark = 0 ");
  254. var parameter = new List<DbParameter>();
  255. parameter.Add(DbParameters.CreateDbParameter("@userId", userId));
  256. if (!keyword.IsEmpty())
  257. {
  258. strSql.Append(" AND c.Theme like @Theme");
  259. parameter.Add(DbParameters.CreateDbParameter("@Theme", '%' + keyword + '%'));
  260. }
  261. return this.BaseRepository().FindList(strSql.ToString(), parameter.ToArray(), pagination);
  262. }
  263. /// <summary>
  264. /// 收件箱数量
  265. /// </summary>
  266. /// <param name="userId">用户Id</param>
  267. /// <returns></returns>
  268. public int GetAddresseeMailCount(string userId)
  269. {
  270. var expression = LinqExtensions.True<EmailAddresseeEntity>();
  271. expression = expression.And(t => t.RecipientId == userId);
  272. expression = expression.And(t => t.DeleteMark == 0);
  273. return new RepositoryFactory().BaseRepository().IQueryable<EmailAddresseeEntity>(expression).Count();
  274. }
  275. /// <summary>
  276. /// 已发送
  277. /// </summary>
  278. /// <param name="pagination">分页参数</param>
  279. /// <param name="userId">用户Id</param>
  280. /// <param name="keyword">关键字</param>
  281. /// <returns></returns>
  282. public IEnumerable<EmailContentEntity> GetSentMail(Pagination pagination, string userId, string keyword)
  283. {
  284. var expression = LinqExtensions.True<EmailContentEntity>();
  285. expression = expression.And(t => t.SendState == 1);
  286. expression = expression.And(t => t.CreateUserId == userId);
  287. expression = expression.And(t => t.DeleteMark == 0);
  288. if (!keyword.IsEmpty())
  289. {
  290. expression = expression.And(t => t.Theme.Contains(keyword));
  291. }
  292. return this.BaseRepository().FindList(expression, pagination);
  293. }
  294. /// <summary>
  295. /// 已发送数量
  296. /// </summary>
  297. /// <param name="userId">用户Id</param>
  298. /// <returns></returns>
  299. public int GetSentMailCount(string userId)
  300. {
  301. var expression = LinqExtensions.True<EmailContentEntity>();
  302. expression = expression.And(t => t.SendState == 1);
  303. expression = expression.And(t => t.CreateUserId == userId);
  304. expression = expression.And(t => t.DeleteMark == 0);
  305. return this.BaseRepository().IQueryable(expression).Count();
  306. }
  307. /// <summary>
  308. /// 邮件实体
  309. /// </summary>
  310. /// <param name="keyValue">主键值</param>
  311. /// <returns></returns>
  312. public EmailContentEntity GetEntity(string keyValue)
  313. {
  314. return this.BaseRepository().FindEntity(keyValue);
  315. }
  316. /// <summary>
  317. /// 收件邮件实体
  318. /// </summary>
  319. /// <param name="keyValue">主键值</param>
  320. /// <returns></returns>
  321. public EmailAddresseeEntity GetAddresseeEntity(string keyValue)
  322. {
  323. return new RepositoryFactory().BaseRepository().FindEntity<EmailAddresseeEntity>(keyValue);
  324. }
  325. #endregion
  326. #region 提交数据
  327. /// <summary>
  328. /// 删除草稿
  329. /// </summary>
  330. /// <param name="keyValue">主键</param>
  331. public void RemoveDraftForm(string keyValue)
  332. {
  333. this.BaseRepository().Delete(keyValue);
  334. }
  335. /// <summary>
  336. /// 删除未读、星标、收件
  337. /// </summary>
  338. /// <param name="keyValue">主键</param>
  339. public void RemoveAddresseeForm(string keyValue)
  340. {
  341. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  342. emailAddresseeEntity.AddresseeId = keyValue;
  343. emailAddresseeEntity.DeleteMark = 1;
  344. new RepositoryFactory().BaseRepository().Update(emailAddresseeEntity);
  345. }
  346. /// <summary>
  347. /// 彻底删除未读、星标、收件
  348. /// </summary>
  349. /// <param name="keyValue">主键</param>
  350. public void ThoroughRemoveAddresseeForm(string keyValue)
  351. {
  352. new RepositoryFactory().BaseRepository().Delete<EmailAddresseeEntity>(keyValue);
  353. }
  354. /// <summary>
  355. /// 删除回收
  356. /// </summary>
  357. /// <param name="keyValue">主键</param>
  358. /// <param name="Type">类型</param>
  359. public void RemoveRecycleForm(string keyValue, int Type)
  360. {
  361. this.BaseRepository().Delete(keyValue);
  362. }
  363. /// <summary>
  364. /// 删除已发
  365. /// </summary>
  366. /// <param name="keyValue">主键</param>
  367. public void RemoveSentForm(string keyValue)
  368. {
  369. EmailContentEntity emailContentEntity = new EmailContentEntity();
  370. emailContentEntity.ContentId = keyValue;
  371. emailContentEntity.DeleteMark = 1;
  372. this.BaseRepository().Update(emailContentEntity);
  373. }
  374. /// <summary>
  375. /// 彻底删除已发
  376. /// </summary>
  377. /// <param name="keyValue">主键</param>
  378. public void ThoroughRemoveSentForm(string keyValue)
  379. {
  380. EmailContentEntity emailContentEntity = new EmailContentEntity();
  381. emailContentEntity.ContentId = keyValue;
  382. emailContentEntity.DeleteMark = 2;
  383. this.BaseRepository().Update(emailContentEntity);
  384. }
  385. /// <summary>
  386. /// 保存邮件表单(发送、存入草稿、草稿编辑)
  387. /// </summary>
  388. /// <param name="keyValue">主键值</param>
  389. /// <param name="emailContentEntity">邮件实体</param>
  390. /// <param name="addresssIds">收件人</param>
  391. /// <param name="copysendIds">抄送人</param>
  392. /// <param name="bccsendIds">密送人</param>
  393. /// <returns></returns>
  394. public void SaveForm(string keyValue, EmailContentEntity emailContentEntity, string[] addresssIds, string[] copysendIds, string[] bccsendIds)
  395. {
  396. if (emailContentEntity.SendState == 0)
  397. {
  398. if (!string.IsNullOrEmpty(keyValue))
  399. {
  400. emailContentEntity.Modify(keyValue);
  401. this.BaseRepository().Update(emailContentEntity);
  402. }
  403. else
  404. {
  405. emailContentEntity.Create();
  406. this.BaseRepository().Insert(emailContentEntity);
  407. }
  408. }
  409. else
  410. {
  411. IRepository db = new RepositoryFactory().BaseRepository().BeginTrans();
  412. try
  413. {
  414. if (!string.IsNullOrEmpty(keyValue))
  415. {
  416. emailContentEntity.Modify(keyValue);
  417. db.Update(emailContentEntity);
  418. }
  419. else
  420. {
  421. emailContentEntity.Create();
  422. db.Insert(emailContentEntity);
  423. }
  424. #region 收件人
  425. foreach (var item in addresssIds)
  426. {
  427. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  428. emailAddresseeEntity.Create();
  429. emailAddresseeEntity.ContentId = emailContentEntity.ContentId;
  430. emailAddresseeEntity.RecipientId = item;
  431. emailAddresseeEntity.RecipientState = 0;
  432. db.Insert(emailAddresseeEntity);
  433. }
  434. #endregion
  435. #region 抄送人
  436. foreach (var item in copysendIds)
  437. {
  438. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  439. emailAddresseeEntity.Create();
  440. emailAddresseeEntity.ContentId = emailContentEntity.ContentId;
  441. emailAddresseeEntity.RecipientId = item;
  442. emailAddresseeEntity.RecipientState = 1;
  443. db.Insert(emailAddresseeEntity);
  444. }
  445. #endregion
  446. #region 密送人
  447. foreach (var item in bccsendIds)
  448. {
  449. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  450. emailAddresseeEntity.Create();
  451. emailAddresseeEntity.ContentId = emailContentEntity.ContentId;
  452. emailAddresseeEntity.RecipientId = item;
  453. emailAddresseeEntity.RecipientState = 2;
  454. db.Insert(emailAddresseeEntity);
  455. }
  456. #endregion
  457. db.Commit();
  458. }
  459. catch (System.Exception)
  460. {
  461. db.Rollback();
  462. throw;
  463. }
  464. }
  465. }
  466. /// <summary>
  467. /// 设置邮件已读/未读
  468. /// </summary>
  469. /// <param name="keyValue">主键</param>
  470. /// <param name="IsRead">是否已读:0-未读1-已读</param>
  471. public void ReadEmail(string keyValue, int IsRead = 1)
  472. {
  473. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  474. emailAddresseeEntity.AddresseeId = keyValue;
  475. emailAddresseeEntity.IsRead = IsRead;
  476. emailAddresseeEntity.ReadCount = emailAddresseeEntity.ReadCount + 1;
  477. emailAddresseeEntity.ReadDate = DateTime.Now;
  478. new RepositoryFactory().BaseRepository().Update(emailAddresseeEntity);
  479. }
  480. /// <summary>
  481. /// 设置邮件星标/取消星标
  482. /// </summary>
  483. /// <param name="keyValue">主键</param>
  484. /// <param name="asterisk">星标:0-取消星标1-星标</param>
  485. public void SteriskEmail(string keyValue, int sterisk = 1)
  486. {
  487. EmailAddresseeEntity emailAddresseeEntity = new EmailAddresseeEntity();
  488. emailAddresseeEntity.AddresseeId = keyValue;
  489. emailAddresseeEntity.IsHighlight = sterisk;
  490. new RepositoryFactory().BaseRepository().Update(emailAddresseeEntity);
  491. }
  492. #endregion
  493. }
  494. }