Message.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Common.Wechat;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using ZcPeng.PublicLibrary;
  7. /*todo:该文件仅用于调试,正式部署请删除。*/
  8. namespace ZcPeng.weixin.PublicAccount
  9. {
  10. /// <summary>
  11. /// 消息类型
  12. /// </summary>
  13. public enum MessageType
  14. {
  15. /// <summary>
  16. /// 请求
  17. /// </summary>
  18. Request,
  19. /// <summary>
  20. /// 响应
  21. /// </summary>
  22. Response,
  23. /// <summary>
  24. /// 异常
  25. /// </summary>
  26. Exception
  27. }
  28. /// <summary>
  29. /// Message:消息
  30. /// </summary>
  31. public class Message
  32. {
  33. //private Guid guid;
  34. //private MessageType request;
  35. //private string v1;
  36. //private string v2;
  37. /// <summary>
  38. /// ID
  39. /// </summary>
  40. public Guid Id { get; set; }
  41. /// <summary>
  42. /// 类型
  43. /// </summary>
  44. public MessageType Type { get; set; }
  45. /// <summary>
  46. /// 内容
  47. /// </summary>
  48. public string Content { get; set; }
  49. /// <summary>
  50. /// 访问api方法
  51. /// </summary>
  52. public string Method { get; set; }
  53. /// <summary>
  54. /// 访问Result
  55. /// </summary>
  56. public string Result { get; set; }
  57. /// <summary>
  58. /// 时间
  59. /// </summary>
  60. public DateTime Time { get; set; }
  61. /// <summary>
  62. /// 构造函数
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <param name="type"></param>
  66. /// <param name="content"></param>
  67. /// <param name="time"></param>
  68. public Message(Guid id, MessageType type, string content, DateTime time,string method, string result)
  69. {
  70. Id = id;
  71. Type = type;
  72. Content = content;
  73. Time = time;
  74. Method = method;
  75. Result = result;
  76. }
  77. /// <summary>
  78. /// 构造函数
  79. /// </summary>
  80. /// <param name="type"></param>
  81. /// <param name="content"></param>
  82. Message(MessageType type, string content)
  83. : this(Guid.NewGuid(), type, content, DateTime.Now,"","")
  84. {
  85. }
  86. /// <summary>
  87. /// 返回参数列表
  88. /// </summary>
  89. /// <returns></returns>
  90. private List<SqlParameter> ToParameters()
  91. {
  92. List<SqlParameter> parameters = new List<SqlParameter>(4);
  93. SqlParameter p = new SqlParameter("@Id", SqlDbType.UniqueIdentifier, 16);
  94. p.Value = Id;
  95. parameters.Add(p);
  96. p = new SqlParameter("@Type", SqlDbType.Int, 4);
  97. p.Value = (int)Type;
  98. parameters.Add(p);
  99. p = new SqlParameter("@Content", SqlDbType.NVarChar);
  100. p.Value = Content;
  101. parameters.Add(p);
  102. p = new SqlParameter("@Time", SqlDbType.DateTime, 8);
  103. p.Value = Time;
  104. parameters.Add(p);
  105. p = new SqlParameter("@Method", SqlDbType.NVarChar);
  106. p.Value = Method;
  107. parameters.Add(p);
  108. p = new SqlParameter("@Result", SqlDbType.NVarChar);
  109. p.Value = Result;
  110. parameters.Add(p);
  111. return parameters;
  112. }
  113. /// <summary>
  114. /// 插入新消息
  115. /// </summary>
  116. /// <param name="message">消息</param>
  117. /// <returns>返回插入是否成功</returns>
  118. public static bool Insert(Message message)
  119. {
  120. string commandText = "INSERT INTO "+Config.TablePrefix+"Message (Id,Type,Content,Time,Method,Result)" +
  121. " VALUES (@Id,@Type,@Content,@Time,@Method,@Result)";
  122. string result;
  123. List<SqlParameter> parameters = message.ToParameters();
  124. return DataAccess.ExecuteCommand(commandText, parameters, out result) > 0;
  125. }
  126. }
  127. }