using System.Collections.Generic;
using System.Threading.Tasks;
using MDL.Models;
using DAL.Interfaces;
using DB.Database;
using SqlSugar;

namespace DAL.Repositories
{
    /// <summary>
    /// 订单日志仓储实现类
    /// </summary>
    public class OrderLogRepository : IOrderLogRepository
    {
        private readonly ISqlSugarProvider _provider;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="provider">SqlSugar数据库提供程序</param>
        public OrderLogRepository(ISqlSugarProvider provider)
        {
            _provider = provider;
        }
        
        /// <summary>
        /// 获取数据库客户端
        /// </summary>
        private ISqlSugarClient Db => _provider.GetClient();
        
        /// <summary>
        /// 插入订单日志
        /// </summary>
        /// <param name="log">订单日志实体</param>
        /// <returns>插入是否成功</returns>
        public async Task<bool> InsertOrderLogAsync(OrderLogEntity log)
        {
            // 确保表存在
            if (!Db.DbMaintenance.IsAnyTable("order_logs"))
            {
                Db.CodeFirst.InitTables(typeof(OrderLogEntity));
            }
            
            var result = await Db.Insertable(log).ExecuteCommandAsync();
            return result > 0;
        }
        
        /// <summary>
        /// 根据中性面单单号获取订单日志列表
        /// </summary>
        /// <param name="neutralWaybillNumber">中性面单单号</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetOrderLogsByWaybillNumberAsync(string neutralWaybillNumber)
        {
            return await Db.Queryable<OrderLogEntity>()
                .Where(x => x.NeutralWaybillNumber == neutralWaybillNumber)
                .OrderBy(x => x.CreatedAt)
                .ToListAsync();
        }
        
        /// <summary>
        /// 根据尾程跟踪单号获取订单日志列表
        /// </summary>
        /// <param name="finalMileTrackingNumber">尾程跟踪单号</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetOrderLogsByTrackingNumberAsync(string finalMileTrackingNumber)
        {
            return await Db.Queryable<OrderLogEntity>()
                .Where(x => x.FinalMileTrackingNumber == finalMileTrackingNumber)
                .OrderBy(x => x.CreatedAt)
                .ToListAsync();
        }
        
        /// <summary>
        /// 获取所有订单日志列表（分页）
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetAllOrderLogsAsync(int pageIndex, int pageSize)
        {
            return await Db.Queryable<OrderLogEntity>()
                .OrderBy(x => x.CreatedAt, OrderByType.Desc)
                .ToPageListAsync(pageIndex, pageSize);
        }
        
        /// <summary>
        /// 根据操作类型和操作结果获取订单日志列表
        /// </summary>
        /// <param name="operationType">操作类型</param>
        /// <param name="operationResult">操作结果</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetOrderLogsByOperationAsync(string operationType, string operationResult, int pageIndex, int pageSize)
        {
            var query = Db.Queryable<OrderLogEntity>();
            
            if (!string.IsNullOrEmpty(operationType))
            {
                query = query.Where(x => x.OperationType == operationType);
            }
            
            if (!string.IsNullOrEmpty(operationResult))
            {
                query = query.Where(x => x.OperationResult == operationResult);
            }
            
            return await query
                .OrderBy(x => x.CreatedAt, OrderByType.Desc)
                .ToPageListAsync(pageIndex, pageSize);
        }
    }
}
