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.SensitiveWord;
using GxPress.Request.SensitiveWord;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement.SensitiveWord
{
    public class SensitiveWordRepository : ISensitiveWordRepository
    {
        private readonly Repository<Entity.tede2.SensitiveWord.SensitiveWord> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public SensitiveWordRepository(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.SensitiveWord.SensitiveWord>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 查询详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.tede2.SensitiveWord.SensitiveWord> GetAsync(int id)
        {
            return await _repository.GetAsync(id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(Entity.tede2.SensitiveWord.SensitiveWord note)
        {
            return await _repository.InsertAsync(note);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Entity.tede2.SensitiveWord.SensitiveWord note)
        {
            return await _repository.UpdateAsync(note);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedList<Entity.tede2.SensitiveWord.SensitiveWord>> GetAllAsync(SensitiveWordRequest request)
        {
            var result = new PagedList<Entity.tede2.SensitiveWord.SensitiveWord>();
            var query = Q.NewQuery();
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.Where(Q.WhereLike(nameof(Entity.tede2.SensitiveWord.SensitiveWord.Name), $"%{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.SensitiveWord.SensitiveWord.CreatedDate), ">", beginTime);
                    query.WhereDate(nameof(Entity.tede2.SensitiveWord.SensitiveWord.CreatedDate), ">", endTime);
                }
            }
            result.Total = await _repository.CountAsync(query);
            result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
            return result;
        }
    }
}