| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System.Text.Json;
- using Yitter.IdGenerator;
- namespace Admin.NET.Plugin.AiDOP.Infrastructure;
- /// <summary>统一写入 aidop_action_run_log。</summary>
- public class AidopActionRunLogWriter : ITransient
- {
- private readonly ISqlSugarClient _db;
- public AidopActionRunLogWriter(ISqlSugarClient db)
- {
- _db = db;
- }
- public async Task<long> StartAsync(
- string actionCode,
- long tenantId,
- string bizType,
- long? bizId,
- string? bizNo)
- {
- var id = YitIdHelper.NextId();
- var now = DateTime.Now;
- await _db.Ado.ExecuteCommandAsync(
- """
- INSERT INTO aidop_action_run_log
- (id, tenant_id, action_code, biz_type, biz_id, biz_no, status, start_time, create_time)
- VALUES
- (@Id, @TenantId, @ActionCode, @BizType, @BizId, @BizNo, 'RUNNING', @Now, @Now)
- """,
- new SugarParameter("@Id", id),
- new SugarParameter("@TenantId", tenantId),
- new SugarParameter("@ActionCode", actionCode),
- new SugarParameter("@BizType", bizType),
- new SugarParameter("@BizId", bizId ?? (object)DBNull.Value),
- new SugarParameter("@BizNo", bizNo ?? string.Empty),
- new SugarParameter("@Now", now));
- return id;
- }
- public async Task SuccessAsync(long id, string message, object? detail = null) =>
- await FinishAsync(id, "SUCCESS", message, detail);
- public async Task FailedAsync(long id, string message, object? detail = null) =>
- await FinishAsync(id, "FAILED", message, detail);
- private async Task FinishAsync(long id, string status, string message, object? detail)
- {
- var now = DateTime.Now;
- await _db.Ado.ExecuteCommandAsync(
- """
- UPDATE aidop_action_run_log
- SET status = @Status, message = @Message, detail_json = @Detail, end_time = @Now
- WHERE id = @Id
- """,
- new SugarParameter("@Status", status),
- new SugarParameter("@Message", Truncate(message, 1000)),
- new SugarParameter("@Detail", detail is null ? (object)DBNull.Value : JsonSerializer.Serialize(detail)),
- new SugarParameter("@Now", now),
- new SugarParameter("@Id", id));
- }
- private static string Truncate(string? s, int max) =>
- string.IsNullOrEmpty(s) || s.Length <= max ? s ?? string.Empty : s[..max];
- }
|