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

namespace GxPress.Repository.Implement.SpecialLabel
{
    /// <summary>
    /// 特殊标签管理
    /// </summary>
    public class SpecialLabelRepository : ISpecialLabelRepository
    {
        private readonly Repository<Entity.SystemLabel.SpecialLabel> _repository;
        private readonly IMapper _mapper;
        public SpecialLabelRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            var databaseType =
                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.SystemLabel.SpecialLabel>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(Entity.SystemLabel.SpecialLabel model)
        {
            return await _repository.InsertAsync(model);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Entity.SystemLabel.SpecialLabel model)
        {
            var specialLabel = await GetAsync(model.Id);
            if (!string.IsNullOrEmpty(model.ImageUrls))
                specialLabel.ImageUrls = model.ImageUrls;
            if (!string.IsNullOrEmpty(model.Remark))
                specialLabel.Remark = model.Remark;
            if (model.Sort > 0)
                specialLabel.Sort = model.Sort;
            if (!string.IsNullOrEmpty(model.Title))
                specialLabel.Title = model.Title;
            if (model.TypeValue > 0)
                specialLabel.TypeValue = model.TypeValue;
            if (model.MiddleLable > 0)
                specialLabel.MiddleLable = model.MiddleLable;
            return await _repository.UpdateAsync(specialLabel);
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Entity.SystemLabel.SpecialLabel> 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="typeValue"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.SystemLabel.SpecialLabel>> GetAllAsync(int typeValue,int pageSize=10)
        {
            var query = Q.NewQuery();
            query.Where(nameof(Entity.SystemLabel.SpecialLabel.TypeValue), typeValue);
            query.OrderByDesc(nameof(Entity.SystemLabel.SpecialLabel.Sort)).Take(pageSize);
            return await _repository.GetAllAsync(query);
        }
        /// <summary>
        ///特使标签数据
        /// </summary>
        /// <param name="typeValue"></param>
        /// <returns></returns>
        public async Task<PagedList<Entity.SystemLabel.SpecialLabel>> GetAllAsync(SpecialLabelSearchRequest request)
        {
            var result = new PagedList<Entity.SystemLabel.SpecialLabel>();
            var query = Q.NewQuery();
            if (request.TypeValue > 0)
                query.Where(nameof(Entity.SystemLabel.SpecialLabel.TypeValue), request.TypeValue);
            if (!string.IsNullOrEmpty(request.KeyWord))
                query.WhereLike(nameof(Entity.SystemLabel.SpecialLabel.Title), $"%{request.KeyWord}%");
            result.Total = await _repository.CountAsync(query);
            query.OrderByDesc(nameof(Entity.SystemLabel.SpecialLabel.Sort));
            query.ForPage(request.Page, request.PerPage);
            result.Items = await _repository.GetAllAsync(query);
            return result;
        }
    }
}