schangxiang@126.com
2024-09-10 429923d78ca4a016ab86adf30d189eb0e7774925
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Xml.Linq;
 
namespace iWare.Wms.Application
{
    /// <summary>
    /// 外部访问接口日志
    /// </summary>
    [ApiDescriptionSettings("外部访问接口日志", Name = "AccessInterfaceLog", Order = 100)]
    [Route("api/[Controller]")]
    public class AccessInterfaceLogService : IAccessInterfaceLogService, IDynamicApiController, ITransient
    {
        private readonly IRepository<AccessInterfaceLog, MasterDbContextLocator> _accessInterfaceLogRep;
 
        /// <summary>
        /// 外部访问接口日志构造
        /// </summary>
        /// <param name="accessInterfaceLogRep"></param>
        public AccessInterfaceLogService(IRepository<AccessInterfaceLog, MasterDbContextLocator> accessInterfaceLogRep)
        {
            _accessInterfaceLogRep = accessInterfaceLogRep;
        }
 
 
 
        [HttpGet("page")]
        public async Task<PageResult<AddAccessInterfaceLogOutnput>> Page([FromQuery] AccessInterfaceLogInputSearch input)
        {
            var workPieceOutbounds = await _accessInterfaceLogRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.WorkPieceID), u => u.WorkPieceID.Contains(input.WorkPieceID))
                                     .Where(!string.IsNullOrEmpty(input.WorkingProcedureCurrent), u => u.WorkingProcedureCurrent.Contains(input.WorkingProcedureCurrent))
                                     .Where(!string.IsNullOrEmpty(input.createdUserName), u => u.CreatedUserName.Contains(input.createdUserName))
                                     .Where(input.QualityState != null, u => u.QualityState == input.QualityState)
                                     .Where(!string.IsNullOrEmpty(input.StartTimeBeginTime.ToString()), u => u.CreatedTime >= input.StartTimeBeginTime)
                                     .Where(!string.IsNullOrEmpty(input.StartTimeEndTime.ToString()), u => u.CreatedTime <= input.StartTimeEndTime)
                                     //.OrderBy(PageInputOrder.OrderBuilder<AccessInterfaceLogInputSearch>(input))
                                     .OrderByDescending(x => x.Id)
                                     .ProjectToType<AddAccessInterfaceLogOutnput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return workPieceOutbounds;
        }
 
 
 
 
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpPost("Add")]
        public async Task AddInterfaceLogAsync(AddAccessInterfaceLogInput input)
        {
            AccessInterfaceLog log = new();
 
            log.WorkPieceID = input.WorkPieceID;
            log.WorkingProcedureCurrent = input.WorkingProcedureCurrent;
            log.QualityState = input.QualityState;
 
            log.Id = input.Id;
            log.ParaJSON = input.JsonString;
            log.Action = input.Action;
            log.IPAddress = input.IpAddress;
            log.OperateAddress = input.OperateAddress;
            log.OperateUserId = input.OperateUserId;
            log.OperateUserName = input.OperateUserName;
 
            try
            {
                // 处理请求参数
                JObject jsonObject = JObject.Parse(input.JsonString);
                int count = 0;
                foreach (JProperty property in jsonObject.Properties())
                {
                    switch (count)
                    {
                        case 0:
                            log.Param1 = property.Value.ToString();
                            count++;
                            break;
                        case 1:
                            log.Param2 = property.Value.ToString();
                            count++;
                            break;
                        case 2:
                            log.Param3 = property.Value.ToString();
                            count++;
                            break;
                        default:
                            break;
                    }
                }
            }
            catch
            {
                log.Param1 = "解析失败";
            }
 
            // 处理返回结果
            var keys = new List<string>();
            try
            {
                //if (input.Result!=null&&input.Result.GetType().IsGenericType && input.Result.GetType().GetGenericTypeDefinition() == typeof(List<>) )
                //{
                //    PropertyInfo property = input.Result.GetType().GetProperty("Item");
                //    if (property != null)
                //    {
                //        IEnumerable collection = input.Result as IEnumerable;
                //        foreach (var item in collection)
                //        {
                //            PropertyInfo idProperty = item.GetType().GetProperty($"{input.Key}");
                //            if (idProperty == null) continue;
                //            var idValue = idProperty.GetValue(item);
                //            keys.Add(idValue.ToString());
                //        }
                //    }
                //}
                //else if(input.Result != null)
                //{
                //    PropertyInfo idProperty = input.Result.GetType().GetProperty($"{input.Key}");
                //    if (idProperty == null)
                //    {
                //        var idValue = idProperty.GetValue(input.Result);
                //        keys.Add(idValue.ToString());
                //    }
                //}
                //log.Keys = string.Join(", ", keys);
                if (input.Result.GetType().Name.Equals("String"))
                {
                    log.ResultJson = input.Result.ToString();
 
                }
                else
                {
 
                    log.ResultJson = JsonSerializer.Serialize(input.Result);
                }
 
            }
            catch
            {
                log.ResultJson = "日志异常";
            }
            //return log;
            await _accessInterfaceLogRep.InsertNowAsync(log);
        }
    }
}