using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.Settings;
using Hotline.Share.Tools;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Dependency;

namespace Hotline.Repository.SqlSugar.System;
public class SystemLogRepository : BaseRepository<SystemLog>, ISystemLogRepository, IScopeDependency
{
    public SystemLogRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
    {
    }

    public void Add(string name, string executeParam = "", string remark = "", [CallerMemberName] string executeUrl = "", int status = 0, string ipUrl = "", string executeResult = "")
    {
        try
        {
            var entity = new SystemLog
            {
                Name = name,
                ExecuteParam = executeParam,
                ExecuteResult = executeResult,
                ExecuteUrl = executeUrl,
                Remark = remark.Length > 255 ? remark.Substring(0, 255) : remark,
                Status = status,
                IpUrl = ipUrl
            };
            if (executeUrl.IsNullOrEmpty())
            {
                entity.ExecuteUrl = new StackTrace().GetFrame(1).GetMethod().Name;
            }
            AddAsync(entity).GetAwaiter().GetResult();
        }
        catch
        {
            // ignore
        }
    }

    public void Add(string name, object executeParam = null, string remark = "", [CallerMemberName] string executeUrl = "", int status = 0, string ipUrl = "", string executeResult = "")
    {
        try
        {
            var entity = new SystemLog
            {
                Name = name,
                ExecuteParam = executeParam,
                ExecuteResult = executeResult,
                ExecuteUrl = executeUrl,
                Remark = remark,
                Status = status,
                IpUrl = ipUrl
            };
            if (executeUrl.IsNullOrEmpty())
            {
                entity.ExecuteUrl = new StackTrace().GetFrame(1).GetMethod().Name;
            }
            AddAsync(entity).GetAwaiter().GetResult();
        }
        catch
        {
            // ignore
        }
    }

    public async Task AddAsyncNoException(SystemLog entity)
    {
        try
        {
            await AddAsync(entity);
        }
        catch
        {
            // ignore
        }
    }

    public async Task UpdateResultAsync(string id, ObjectResult result)
    {
        try
        {
            await Updateable()
                .SetColumns(m => m.ExecuteResult, result)
                .Where(m => m.Id == id)
                .ExecuteCommandAsync();
        }
        catch (Exception e)
        {
            var msg = e.Message;
        }
    }
}
