using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using iWare.Wms.Core;
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
{
///
/// 外部访问接口日志
///
//[ApiDescriptionSettings("外部访问接口日志", Name = "AccessInterfaceLogForRequest", Order = 100)]
//[Route("api/[Controller]")]
public class AccessInterfaceLogForRequestService : IAccessInterfaceLogForRequestService, IDynamicApiController, ITransient
{
private readonly IRepository _AccessInterfaceLogForRequestRep;
///
/// 外部访问接口日志构造
///
///
public AccessInterfaceLogForRequestService(IRepository AccessInterfaceLogForRequestRep)
{
_AccessInterfaceLogForRequestRep = AccessInterfaceLogForRequestRep;
}
///
/// 添加日志
///
///
///
//[HttpPost("Add")]
public async Task AddInterfaceLogForRequestAsync(AddAccessInterfaceLogForRequestInput input)
{
AccessInterfaceLogForRequest log = new();
log.WorkPieceID = input.WorkPieceID;
log.WorkingProcedureCurrent = input.WorkingProcedureCurrent;
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();
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 _AccessInterfaceLogForRequestRep.InsertNowAsync(log);
}
}
}