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

namespace NPP.SmartSchedue.Api.Repositories.Work;

/// <summary>
/// 工作任务FL人员关联仓储
/// </summary>
public class WorkOrderFLPersonnelRepository : AppRepositoryBase<WorkOrderFLPersonnelEntity>, IWorkOrderFLPersonnelRepository
{
    public WorkOrderFLPersonnelRepository(UnitOfWorkManagerCloud uowm) : base(uowm)
    {
    }

    /// <summary>
    /// 根据工作任务ID获取FL人员列表
    /// </summary>
    /// <param name="workOrderId">工作任务ID</param>
    /// <returns>FL人员列表</returns>
    public async Task<List<WorkOrderFLPersonnelEntity>> GetByWorkOrderIdAsync(long workOrderId)
    {
        return await Select
            .Where(x => x.WorkOrderId == workOrderId)
            .ToListAsync();
    }

    /// <summary>
    /// 根据FL人员ID获取工作任务列表
    /// </summary>
    /// <param name="flPersonnelId">FL人员ID</param>
    /// <returns>工作任务列表</returns>
    public async Task<List<WorkOrderFLPersonnelEntity>> GetByPersonnelIdAsync(long flPersonnelId)
    {
        return await Select
            .Where(x => x.FLPersonnelId == flPersonnelId)
            .ToListAsync();
    }

    /// <summary>
    /// 批量添加工作任务FL人员关联
    /// </summary>
    /// <param name="workOrderId">工作任务ID</param>
    /// <param name="flPersonnelIds">FL人员ID列表</param>
    /// <param name="flPersonnelNames">FL人员姓名列表</param>
    /// <returns>添加结果</returns>
    public async Task<bool> BatchAddAsync(long workOrderId, List<long> flPersonnelIds, List<string> flPersonnelNames)
    {
        if (flPersonnelIds?.Any() != true)
            return true;

        try
        {
            var entities = new List<WorkOrderFLPersonnelEntity>();
            for (int i = 0; i < flPersonnelIds.Count; i++)
            {
                entities.Add(new WorkOrderFLPersonnelEntity
                {
                    WorkOrderId = workOrderId,
                    FLPersonnelId = flPersonnelIds[i],
                    FLPersonnelName = i < flPersonnelNames?.Count ? flPersonnelNames[i] : string.Empty
                });
            }

            await InsertAsync(entities);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 根据工作任务ID删除所有FL人员关联
    /// </summary>
    /// <param name="workOrderId">工作任务ID</param>
    /// <returns>删除结果</returns>
    public async Task<bool> DeleteByWorkOrderIdAsync(long workOrderId)
    {
        try
        {
            await DeleteAsync(x => x.WorkOrderId == workOrderId);
            return true;
        }
        catch
        {
            return false;
        }
    }

    /// <summary>
    /// 更新工作任务的FL人员关联
    /// </summary>
    /// <param name="workOrderId">工作任务ID</param>
    /// <param name="flPersonnelIds">新的FL人员ID列表</param>
    /// <param name="flPersonnelNames">新的FL人员姓名列表</param>
    /// <returns>更新结果</returns>
    public async Task<bool> UpdateByWorkOrderIdAsync(long workOrderId, List<long> flPersonnelIds, List<string> flPersonnelNames)
    {
        try
        {
            // 先删除现有关联
            await DeleteByWorkOrderIdAsync(workOrderId);
            
            // 再添加新关联
            if (flPersonnelIds?.Any() == true)
            {
                await BatchAddAsync(workOrderId, flPersonnelIds, flPersonnelNames);
            }
            
            return true;
        }
        catch
        {
            return false;
        }
    }
}