using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NPP.SmartSchedue.Api.Contracts;
using NPP.SmartSchedue.Api.Contracts.Domain.Work;
using NPP.SmartSchedue.Api.Contracts.Core.Enums;
using NPP.SmartSchedue.Api.Core.Repositories;
using ZhonTai.Admin.Core.Db.Transaction;

namespace NPP.SmartSchedue.Api.Repositories.Work;

/// <summary>
/// 工作任务仓储
/// </summary>
public class WorkOrderRepository : AppRepositoryBase<WorkOrderEntity>, IWorkOrderRepository
{
    public WorkOrderRepository(UnitOfWorkManagerCloud uowm) : base(uowm)
    {
    }

    /// <summary>
    /// 根据日期获取工作任务列表
    /// </summary>
    public async Task<List<WorkOrderEntity>> GetByDateAsync(DateTime date)
    {
        return await Select
            .Where(w => w.WorkOrderDate.Date == date.Date)
            .ToListAsync();
    }

    /// <summary>
    /// 根据人员ID和日期范围获取工作任务列表
    /// </summary>
    public async Task<List<WorkOrderEntity>> GetByPersonnelAndDateRangeAsync(long personnelId, DateTime startDate, DateTime endDate)
    {
        return await Select
            .Where(w => w.AssignedPersonnelId == personnelId)
            .Where(w => w.WorkOrderDate >= startDate && w.WorkOrderDate <= endDate)
            .ToListAsync();
    }

    /// <summary>
    /// 根据状态获取工作任务列表
    /// </summary>
    public async Task<List<WorkOrderEntity>> GetByStatusAsync(WorkOrderStatusEnum status)
    {
        return await Select
            .Where(w => w.Status == (int)status)
            .ToListAsync();
    }

    /// <summary>
    /// 根据班次ID获取工作任务列表
    /// </summary>
    public async Task<List<WorkOrderEntity>> GetByShiftAndDateAsync(long shiftId, DateTime date)
    {
        return await Select
            .Where(w => w.ShiftId == shiftId && w.WorkOrderDate.Date == date.Date)
            .ToListAsync();
    }

    /// <summary>
    /// 根据工序ID获取工作任务列表
    /// </summary>
    public async Task<List<WorkOrderEntity>> GetByProcessAndDateRangeAsync(long processId, DateTime startDate, DateTime endDate)
    {
        return await Select
            .Where(w => w.ProcessId == processId)
            .Where(w => w.WorkOrderDate >= startDate && w.WorkOrderDate <= endDate)
            .ToListAsync();
    }

    /// <summary>
    /// 检查任务代码是否存在
    /// </summary>
    public async Task<bool> ExistsWorkOrderCodeAsync(string workOrderCode, long? excludeId = null)
    {
        var query = Select.Where(w => w.WorkOrderCode == workOrderCode);
        
        if (excludeId.HasValue)
            query = query.Where(w => w.Id != excludeId.Value);
            
        return await query.AnyAsync();
    }

    /// <summary>
    /// 生成任务代码
    /// </summary>
    public string GenerateWorkOrderCode(string projectNumber, string shiftCode, string processCode)
    {
        return $"{projectNumber}_{shiftCode}_{processCode}";
    }

    /// <summary>
    /// 获取人员在指定日期范围内的任务数量
    /// </summary>
    public async Task<int> GetPersonnelTaskCountByDateRangeAsync(long personnelId, DateTime startDate, DateTime endDate)
    {
        var count = await Select
            .Where(w => w.AssignedPersonnelId == personnelId)
            .Where(w => w.WorkOrderDate >= startDate && w.WorkOrderDate <= endDate)
            .CountAsync();
        return (int)count;
    }
} 