using JiShe.CollectBus.Common.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace JiShe.CollectBus.ShardingStrategy
{
    /// <summary>
    /// 按天分表策略
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class DayShardingStrategy<TEntity> : IShardingStrategy<TEntity>
    {
        /// <summary>
        /// 获取指定时间对应的集合名
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public string GetCollectionName(DateTime dateTime)
        {
            var baseName = typeof(TEntity).Name;
            return $"{baseName}_{dateTime.GetDataTableShardingStrategy()}";
        }

        /// <summary>
        /// 获取当前时间对应的集合名
        /// </summary>
        /// <returns></returns>
        public string GetCurrentCollectionName()
        {
            var baseName = typeof(TEntity).Name;
            return $"{baseName}_{DateTime.Now.GetDataTableShardingStrategy()}";
        }
          
        /// <summary>
        /// 用于查询时确定目标集合
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public IEnumerable<string> GetQueryCollectionNames(DateTime? startTime, DateTime? endTime)
        {
            var months = new List<string>();
            var current = startTime ?? DateTime.MinValue;
            var end = endTime ?? DateTime.MaxValue;
            var baseName = typeof(TEntity).Name;

            while (current <= end)
            {
                months.Add($"{baseName}_{current.GetDataTableShardingStrategy()}");
                current = current.AddMonths(1);
            }

            return months.Distinct();
        }
    }
}
