using System.Reflection;
using System.Threading.Tasks;
using OpenAuth.App.Base;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

namespace OpenAuth.App
{
    public class SysLogApp : SqlSugarBaseApp<SysLog, SugarDbContext>
    {
        public SysLogApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysLog> repository) : base(unitWork, repository, null)
        {
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        public async Task<TableData> Load(QuerySysLogListReq request)
        {
            int totalCount = 0;
            var result = new TableData();
            var objs = base.Repository.AsQueryable()
                .WhereIF(!string.IsNullOrEmpty(request.key), u => u.Content.Contains(request.key));

            result.data = await objs.OrderByDescending(u => u.CreateTime).ToPageListAsync(request.page, request.limit, totalCount);
            result.count = totalCount;

            return result;
        }

        public void Add(SysLog obj)
        {
            //程序类型取入口应用的名称，可以根据自己需要调整
            obj.Application = Assembly.GetEntryAssembly().FullName.Split(',')[0];
            obj.Id =Yitter.IdGenerator.YitIdHelper.NextId();
            Repository.Insert(obj);
        }

        public void Update(SysLog obj)
        {
            base.Repository.Update(u => new SysLog
            {
                //todo:要修改的字段赋值
            }, u => u.Id == obj.Id);
        }

        public SysLog Get(string id)
        {
            return Repository.GetById(id);
        }

        public void Delete(string[] ids)
        {
            Repository.DeleteByIds(ids);
        }
    }
}