ClientMessageService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using ConsoleHttp.Model;
  9. using MqttMsgServer.Model;
  10. using MqttMsgServer.Service.ClientMessage.Dto;
  11. using MqttMsgServer.Tools;
  12. using StackExchange.Redis;
  13. namespace MqttMsgServer.Service.ClientMessage
  14. {
  15. public class ClientMessageService
  16. {
  17. public ResponseResult QueryMsgInfo(MsgQueryParamDto input)
  18. {
  19. ResponseResult result = new ResponseResult();
  20. string strWhere = "";
  21. if (!string.IsNullOrEmpty(input.ClientId) )
  22. {
  23. var cids = input.ClientId.Split(',');
  24. strWhere += "(";
  25. foreach (var cid in cids)
  26. {
  27. strWhere += $" ClientId like '%{cid}%' or";
  28. }
  29. strWhere = strWhere.Substring(0, strWhere.LastIndexOf("or", StringComparison.Ordinal)==-1? strWhere.Length: strWhere.LastIndexOf("or", StringComparison.Ordinal));
  30. strWhere += ")";
  31. }
  32. if (!string.IsNullOrEmpty(input.Topic))
  33. {
  34. var ts = input.Topic.Split(',');
  35. strWhere += string.IsNullOrEmpty(strWhere)?"(":" and (";
  36. foreach (var t in ts)
  37. {
  38. strWhere += $" Topic like '%{t}%' or ";
  39. }
  40. strWhere = strWhere.Substring(0, strWhere.LastIndexOf("or", StringComparison.Ordinal) == -1 ? strWhere.Length : strWhere.LastIndexOf("or", StringComparison.Ordinal));
  41. strWhere += ")";
  42. }
  43. if (input?.StartDate != null )
  44. {
  45. strWhere += string.IsNullOrEmpty(strWhere) ? "" : " and ";
  46. strWhere += $" CreatorDate >= '{input.StartDate?.ToString("yyyy-MM-dd HH:mm:ss")}' ";
  47. }
  48. if (input?.EndDate != null)
  49. {
  50. if (input?.StartDate != null && input?.EndDate <= input?.StartDate)
  51. {
  52. result.ErrorMessage = "结束时间需要在起始时间之后!";
  53. return result;
  54. }
  55. strWhere += string.IsNullOrEmpty(strWhere) ? "" : " and ";
  56. strWhere += $" CreatorDate <= '{input.EndDate?.ToString("yyyy-MM-dd HH:mm:ss")}' ";
  57. }
  58. string lcSql = $"select * from ReceiveMessageRecords where {strWhere}";
  59. DataTable dt = SqlDbHelper.ExecuteDataTable(lcSql);
  60. if (dt != null && dt.Rows.Count > 0)
  61. {
  62. result.IsSuccess = true;
  63. result.Data = StringHelper.ToList<ReceiveMessageRecord>(dt);
  64. }
  65. return result;
  66. }
  67. public ResponseResult CountMsgInfo(MsgQueryCount input)
  68. {
  69. ResponseResult result = QueryMsgInfo(input);
  70. if (!result.IsSuccess)
  71. {
  72. return result;
  73. }
  74. if (string.IsNullOrEmpty(input.QueryCountType))
  75. {
  76. result.IsSuccess = false;
  77. result.ErrorMessage = "分组类型为空!";
  78. return result;
  79. }
  80. List<ReceiveMessageRecord> dataList = (List<ReceiveMessageRecord>)result.Data;
  81. if (dataList==null||!dataList.Any())
  82. {
  83. result.IsSuccess = false;
  84. result.ErrorMessage = "未查询到消息数据";
  85. return result;
  86. }
  87. string dateFormatter = "yyyy-MM";
  88. switch (input.QueryCountType)
  89. {
  90. case "0":
  91. dateFormatter = "yyyy-MM-dd HH:mm";
  92. break;
  93. case "1":
  94. dateFormatter = "yyyy-MM-dd HH";
  95. break;
  96. case "2":
  97. dateFormatter = "yyyy-MM-dd";
  98. break;
  99. case "3":
  100. dateFormatter = "yyyy-MM";
  101. break;
  102. case "4":
  103. dateFormatter = "yyyy";
  104. break;
  105. }
  106. var countEntity = dataList.OrderBy(i => i.CreatorDate).GroupBy(i => i.CreatorDate.ToString(dateFormatter))
  107. .Select(g => new { CreatorDate = g.Key, CountInfo = g.Count()}).ToList();
  108. result.IsSuccess = true;
  109. result.Data = countEntity;
  110. return result;
  111. }
  112. }
  113. }