OperationRecordService.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Model;
  2. using StandardLibrary;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data.Common;
  6. using System.Data.SQLite;
  7. using System.Security.Claims;
  8. public class OperationRecordService
  9. {
  10. private static readonly object _lock = new object();
  11. public static void Log(OperationRecord operationRecord)
  12. {
  13. using (var connection = new SQLiteConnection(DatabaseHelper.ConnectionString))
  14. {
  15. connection.Open();
  16. using (var command = new SQLiteCommand("INSERT INTO OperationLogs (UserId, Username, ActionType, ControlName,OldValue, NewValue, Details ,Timestamp) VALUES (@userId, @username, @actionType, @controlName, @oldValue, @newValue, @details, @timestamp)", connection))
  17. {
  18. command.Parameters.AddWithValue("@userId", operationRecord.User.Id);
  19. command.Parameters.AddWithValue("@username", operationRecord.User.Username);
  20. command.Parameters.AddWithValue("@actionType",(int)operationRecord.OperationType);
  21. command.Parameters.AddWithValue("@controlName", operationRecord.ControlName);
  22. command.Parameters.AddWithValue("@oldValue", operationRecord.OldValue?.ToString() ?? (object)DBNull.Value);
  23. command.Parameters.AddWithValue("@newValue", operationRecord.NewValue?.ToString() ?? (object)DBNull.Value);
  24. command.Parameters.AddWithValue("@details", operationRecord.Detail?? (object)DBNull.Value);
  25. command.Parameters.AddWithValue("@timestamp", operationRecord.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  26. command.ExecuteNonQuery();
  27. }
  28. }
  29. }
  30. public static List<OperationRecordDisplay> GetOperationRecordByTimeRange(DateTime start, DateTime end)
  31. {
  32. using (var connection = new SQLiteConnection(DatabaseHelper.ConnectionString))
  33. {
  34. connection.Open();
  35. var records = new List<OperationRecordDisplay>();
  36. string sql = @"
  37. SELECT Id, Timestamp, Username, ActionType, ControlName,OldValue, NewValue, Details
  38. FROM OperationLogs
  39. WHERE Timestamp BETWEEN @Start AND @End
  40. ORDER BY Timestamp DESC";
  41. using (var cmd = new SQLiteCommand(sql, connection))
  42. {
  43. // 参数化查询,防止注入
  44. cmd.Parameters.AddWithValue("@Start", start.ToString("yyyy-MM-dd HH:mm:ss"));
  45. cmd.Parameters.AddWithValue("@End", end.ToString("yyyy-MM-dd HH:mm:ss"));
  46. using (var reader = cmd.ExecuteReader())
  47. {
  48. while (reader.Read())
  49. {
  50. records.Add(new OperationRecordDisplay
  51. {
  52. Id = reader.GetInt32(0),
  53. Timestamp = DateTime.Parse(reader.GetString(1)),
  54. Username = reader.GetString(2),
  55. OperationType= EnumExt.TryParseEnum<OperationType>(reader.GetInt32(3), out OperationType opType)? opType.GetDescription() : "未知操作",
  56. ControlName = reader.IsDBNull(4) ? null : reader.GetString(4),
  57. OldValue = reader.IsDBNull(5) ? null : reader.GetString(5),
  58. NewValue = reader.IsDBNull(6) ? null : reader.GetString(6),
  59. Detail = reader.IsDBNull(7) ? null : reader.GetString(7),
  60. });
  61. }
  62. }
  63. }
  64. return records;
  65. }
  66. }
  67. }