using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hotline.BatchTask;
using Hotline.Share.Dtos.BatchTask;
using Hotline.Share.Enums.BatchTask;
using Mapster;
using XF.Domain.Exceptions;

namespace Hotline.Application.Mappers
{
    public class BatchTaskMapperConfigs : IRegister
    {
        public void Register(TypeAdapterConfig config)
        {
            config.ForType<Apptask, ApptaskProgressDto>()
                .BeforeMapping((s, d) =>
                {
                    if (!s.ApptaskItems.Any())
                        throw new UserFriendlyException("任务明细无数据");
                })
                .Map(d => d.Total, s => s.ApptaskItems.Count)
                .Map(d => d.Waiting, s => s.ApptaskItems.Count(i => i.TaskStatus == ETaskStatus.Waiting))
                .Map(d => d.Processing, s => s.ApptaskItems.Count(i => i.TaskStatus == ETaskStatus.Processing))
                .Map(d => d.Succeeded, s => s.ApptaskItems.Count(i => i.TaskStatus == ETaskStatus.Succeeded))
                .Map(d => d.Failed, s => s.ApptaskItems.Count(i => i.TaskStatus == ETaskStatus.Failed));

        }
    }
}
