wanshenmean
3 天以前 ce1292c9cf37195b6abd2699dfc5d6cb3e143c9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using WIDESEA_Core;
using WIDESEA_DTO.MES;
using WIDESEA_IMesService;
using WIDESEA_Model.Models.Mes;
 
namespace WIDESEA_MesService
{
    /// <summary>
    /// MES接口日志服务实现
    /// </summary>
    public class MesLogService : IMesLogService
    {
        private readonly ISqlSugarClient _db;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db">数据库客户端</param>
        public MesLogService(ISqlSugarClient db)
        {
            _db = db;
        }
 
        /// <summary>
        /// 记录MES接口调用日志
        /// </summary>
        /// <param name="log">日志DTO</param>
        /// <returns>是否记录成功</returns>
        public async Task<bool> LogAsync(MesApiLogDto log)
        {
            try
            {
                var entity = new Dt_MesApiLog
                {
                    ApiType = log.ApiType,
                    RequestJson = log.RequestJson,
                    ResponseJson = log.ResponseJson,
                    IsSuccess = log.IsSuccess,
                    ErrorMessage = log.ErrorMessage,
                    ElapsedMs = log.ElapsedMs,
                    CreateDate = DateTime.Now,
                    Creator = log.Creator
                };
 
                var result = await _db.Insertable(entity).ExecuteCommandAsync();
                return result > 0;
            }
            catch (Exception ex)
            {
                // 记录日志失败不影响主流程
                Console.WriteLine($"记录MES日志失败: {ex.Message}");
                return false;
            }
        }
 
        /// <summary>
        /// 获取最近的MES接口调用记录
        /// </summary>
        /// <param name="apiType">接口类型</param>
        /// <param name="count">记录数量</param>
        /// <returns>日志列表</returns>
        public async Task<List<MesApiLogDto>> GetRecentLogsAsync(string apiType, int count = 50)
        {
            try
            {
                var entities = await _db.Queryable<Dt_MesApiLog>()
                    .Where(x => x.ApiType == apiType)
                    .OrderByDescending(x => x.CreateDate)
                    .Take(count)
                    .ToListAsync();
 
                return entities.Select(e => new MesApiLogDto
                {
                    ApiType = e.ApiType,
                    RequestJson = e.RequestJson,
                    ResponseJson = e.ResponseJson,
                    IsSuccess = e.IsSuccess,
                    ErrorMessage = e.ErrorMessage,
                    ElapsedMs = e.ElapsedMs,
                    Creator = e.Creator
                }).ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取MES日志失败: {ex.Message}");
                return new List<MesApiLogDto>();
            }
        }
    }
}