using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.TrackLog;
using GxPress.Request.TrackLog;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement.TrackLog
{
    public class TrackLogRepository : ITrackLogRepository
    {
        private readonly Repository<Entity.tede2.TrackLog.TrackLog> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public TrackLogRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType =
                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.tede2.TrackLog.TrackLog>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        public async Task<PagedList<Entity.tede2.TrackLog.TrackLog>> GetAllAsync(TrackLogSearchRequest request)
        {
            var result = new PagedList<Entity.tede2.TrackLog.TrackLog>();
            var query = Q.NewQuery();
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Entity.tede2.TrackLog.TrackLog.Remark), $"%{request.KeyWord}%");
            if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
            {
                if (DateTime.TryParse(request.BeginTime, out var beginTime) && DateTime.TryParse(request.EndTime, out var endTime))
                {
                    query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), ">=", beginTime);
                    query.WhereDate(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate), "<=", endTime);
                }
            }
            result.Total = await _repository.CountAsync(query);
            result.Items = await _repository.GetAllAsync(query.OrderByDesc(nameof(Entity.tede2.TrackLog.TrackLog.CreatedDate)).ForPage(request.Page, request.PerPage));
            return result;
        }

        public async Task<bool> InsertAsync(Entity.tede2.TrackLog.TrackLog model)
        {
            return await _repository.InsertAsync(model) > 0;
        }

        public async Task<bool> InsertAsync(string creator, int adminId, string ip, string remark)
        {
            var model = new Entity.tede2.TrackLog.TrackLog()
            {
                Creator = creator,
                AdminId = adminId,
                Ip = ip,
                Remark = remark
            };
            return await _repository.InsertAsync(model) > 0;
        }
    }
}