EventDBWriter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.DBCore;
  6. using System.Data;
  7. using Aitex.Core.RT.Log;
  8. namespace Aitex.Core.RT.Event
  9. {
  10. class EventDBWriter
  11. {
  12. EventDB _db = new EventDB();
  13. public void Initialize()
  14. {
  15. Dictionary<string, Type> columns = new Dictionary<string,Type>();
  16. columns["event_id"] = typeof(int);
  17. columns["role_id"] = typeof(int);
  18. columns["user_name"] = typeof(string);
  19. columns["event_enum"] = typeof(string);
  20. columns["type"] = typeof(string);
  21. columns["source"] = typeof(string);
  22. columns["description"] = typeof(string);
  23. columns["level"] = typeof(string);
  24. columns["occur_time"] = typeof(DateTime);
  25. DB.CreateTableIfNotExisted("event_data", columns, true, "gid");
  26. }
  27. public void WriteEvent(EventItem ev)
  28. {
  29. string executeInsert = string.Format(
  30. @"Insert into ""event_data""(""event_id"",""role_id"",""user_name"",""event_enum"",""type"",""source"",""description"",""level"",""occur_time"") values({0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
  31. ev.Id, ev.RoleId, ev.UserName, ev.EventEnum, ev.Type.ToString(), ev.Source, ev.Description.Replace("'", "''"), ev.Level.ToString(), ev.OccuringTime.ToString("yyyy/MM/dd HH:mm:ss.fff"));
  32. DB.Insert(executeInsert);
  33. }
  34. public List<EventItem> QueryDBEvent(string sql)
  35. {
  36. List<EventItem> result = new List<EventItem>();
  37. DataSet ds = DB.ExecuteDataset(sql);
  38. if (ds == null)
  39. return result;
  40. try
  41. {
  42. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  43. {
  44. EventItem ev = new EventItem();
  45. int id = 0;
  46. if (int.TryParse(ds.Tables[0].Rows[i]["event_id"].ToString(), out id))
  47. ev.Id = id;
  48. int roleId = 0;
  49. if(ds.Tables[0].Columns.Contains("role_id"))
  50. {
  51. if (int.TryParse(ds.Tables[0].Rows[i]["role_id"].ToString(), out roleId))
  52. ev.RoleId = roleId;
  53. }
  54. if(ds.Tables[0].Columns.Contains("user_name"))
  55. ev.UserName = ds.Tables[0].Rows[i]["user_name"].ToString();
  56. EventType type = EventType.EventUI_Notify;
  57. if (Enum.TryParse<EventType>(ds.Tables[0].Rows[i]["type"].ToString(), out type))
  58. ev.Type = type;
  59. ev.Source = ds.Tables[0].Rows[i]["source"].ToString();
  60. ev.EventEnum = ds.Tables[0].Rows[i]["event_enum"].ToString();
  61. ev.Description = ds.Tables[0].Rows[i]["description"].ToString();
  62. EventLevel level = EventLevel.Information;
  63. if (Enum.TryParse<EventLevel>(ds.Tables[0].Rows[i]["level"].ToString(), out level))
  64. ev.Level = level;
  65. if (!ds.Tables[0].Rows[i]["occur_time"].Equals(DBNull.Value))
  66. ev.OccuringTime = ((DateTime)ds.Tables[0].Rows[i]["occur_time"]) ;
  67. //DateTime dt = (DateTime)ds.Tables[0].Rows[i]["occur_time"];
  68. //if (DateTime.TryParse(ds.Tables[0].Rows[i]["occur_time"].ToString(), out dt ))
  69. //ev. = dt;
  70. result.Add(ev);
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. LOG.Write(ex);
  76. }
  77. return result;
  78. }
  79. }
  80. }