using Furion.DependencyInjection;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.Application.Logger
{
    /// <summary>
    /// 日志管理
    /// </summary>
    public class NoticeLoggerService : INoticeLoggerService, ITransient
    {
        private readonly ISqlSugarRepository<YB_NoticeLogger> repository;
        private readonly SqlSugarClient dbClient;
        public NoticeLoggerService(ISqlSugarRepository<YB_NoticeLogger> sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
        }
        /// <summary>
        /// 操作日志列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<PageParms<AuditListS2CDto>> GetAuditListAsync(QueryParams param)
        {
            RefAsync<int> totalnum = 0;
            var temquery = dbClient.Queryable<YB_AuditLogger>();
            if (param.queryParam != null && param.queryParam.Count > 0)
            {
                List<IConditionalModel> conModels = new List<IConditionalModel>();
                param.queryParam.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.Value))
                    {
                        conModels.Add(new ConditionalModel()
                        {
                            FieldName = x.Name,
                            ConditionalType = (ConditionalType)x.Type,
                            FieldValue = x.Value.ToStr()
                        });
                    }
                });
                if (conModels.Count > 0)
                {
                    temquery = temquery.Where(conModels);
                }
            }
            var query = await temquery.OrderBy(x => x.CreatTime, OrderByType.Desc)
                .Select(x => new AuditListS2CDto
                {
                    CreatTime = x.CreatTime,
                    ColumnName = x.ColumnName,
                    NewValue = x.NewValue,
                    OldValue = x.OldValue,
                    TableName = x.TableName,
                    Title = x.Title,
                    UserName = x.UserName
                })
                 .ToPageListAsync(param.offset, param.limit, totalnum);
            return new PageParms<AuditListS2CDto>
            {
                page = param.offset,
                Items = query,
                totalnum = totalnum,
                limit = param.limit
            };
        }

        /// <summary>
        /// 通知日志
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<PageParms<YB_NoticeLogger>> GetListAsync(QueryParams param)
        {
            RefAsync<int> totalnum = 0;
            var temquery = dbClient.Queryable<YB_NoticeLogger>();
            if (param.queryParam != null && param.queryParam.Count > 0)
            {
                List<IConditionalModel> conModels = new List<IConditionalModel>();
                param.queryParam.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.Value))
                    {
                        conModels.Add(new ConditionalModel()
                        {
                            FieldName = x.Name,
                            ConditionalType = (ConditionalType)x.Type,
                            FieldValue = x.Value.ToStr()
                        });
                    }
                });
                if (conModels.Count > 0)
                {
                    temquery = temquery.Where(conModels);
                }
            }
            string sorts = string.Format("{0} {1}", param.sort, param.order);
            var query = await temquery.OrderBy(sorts)
                .Select(x => new YB_NoticeLogger
                {
                    Id = x.Id,
                    CreateTime = x.CreateTime,
                    FromInfo = x.FromInfo,
                    Info = x.Info,
                    UA = x.UA,
                    UserInfo = x.UserInfo
                })
                 .ToPageListAsync(param.offset, param.limit, totalnum);
            return new PageParms<YB_NoticeLogger>
            {
                page = param.offset,
                Items = query,
                totalnum = totalnum,
                limit = param.limit
            };
        }
    }
}
