using System.Collections.Generic;
using System.Threading.Tasks;
using MDL.Models;
using BLL.Interfaces;
using DAL.Interfaces;

namespace BLL.Services
{
    /// <summary>
    /// 订单日志服务实现类
    /// </summary>
    public class OrderLogService : IOrderLogService
    {
        private readonly IOrderLogRepository _orderLogRepository;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="orderLogRepository">订单日志仓储</param>
        public OrderLogService(IOrderLogRepository orderLogRepository)
        {
            _orderLogRepository = orderLogRepository;
        }
        
        /// <summary>
        /// 记录订单操作日志
        /// </summary>
        /// <param name="neutralWaybillNumber">中性面单单号</param>
        /// <param name="finalMileTrackingNumber">尾程跟踪单号</param>
        /// <param name="operationType">操作类型</param>
        /// <param name="operationResult">操作结果</param>
        /// <param name="operationDescription">操作说明</param>
        /// <param name="operator">操作人</param>
        /// <returns>记录是否成功</returns>
        public async Task<bool> RecordOrderLogAsync(string? neutralWaybillNumber, string? finalMileTrackingNumber, string operationType, string operationResult, string operationDescription, string? @operator = null)
        {
            var log = new OrderLogEntity
            {
                NeutralWaybillNumber = neutralWaybillNumber,
                FinalMileTrackingNumber = finalMileTrackingNumber,
                OperationType = operationType,
                OperationResult = operationResult,
                OperationDescription = operationDescription,
                Operator = @operator,
                CreatedAt = System.DateTime.Now
            };
            
            return await _orderLogRepository.InsertOrderLogAsync(log);
        }
        
        /// <summary>
        /// 根据中性面单单号获取订单日志列表
        /// </summary>
        /// <param name="neutralWaybillNumber">中性面单单号</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetOrderLogsByWaybillNumberAsync(string neutralWaybillNumber)
        {
            return await _orderLogRepository.GetOrderLogsByWaybillNumberAsync(neutralWaybillNumber);
        }
        
        /// <summary>
        /// 根据尾程跟踪单号获取订单日志列表
        /// </summary>
        /// <param name="finalMileTrackingNumber">尾程跟踪单号</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetOrderLogsByTrackingNumberAsync(string finalMileTrackingNumber)
        {
            return await _orderLogRepository.GetOrderLogsByTrackingNumberAsync(finalMileTrackingNumber);
        }
        
        /// <summary>
        /// 获取所有订单日志列表（分页）
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>订单日志实体列表</returns>
        public async Task<List<OrderLogEntity>> GetAllOrderLogsAsync(int pageIndex, int pageSize)
        {
            return await _orderLogRepository.GetAllOrderLogsAsync(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)
        {
            return await _orderLogRepository.GetOrderLogsByOperationAsync(operationType, operationResult, pageIndex, pageSize);
        }
    }
}
