using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using DS.Module.Core;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.TaskInteraction.Method;
using Masuit.Tools.Systems;
namespace DS.WMS.Core.TaskInteraction.Dtos
{
///
/// 任务请求基类
///
public class TaskRequest : IValidatableObject
{
///
/// 业务ID
///
public long BusinessId { get; set; }
///
/// 业务ID,用于批量请求
///
public long[]? Ids { get; set; }
///
/// 业务类型
///
public BusinessType? BusinessType { get; set; }
///
/// 任务类型;该属性为只读
///
[IgnoreDataMember]
public TaskBaseTypeEnum TaskType => string.IsNullOrEmpty(TaskTypeName) ? TaskBaseTypeEnum.NOT_SPECIFIED : Enum.Parse(TaskTypeName);
///
/// 任务类型名称
///
public string TaskTypeName { get; set; } = string.Empty;
///
/// 父级任务类型名称(用于子任务)
///
public string? ParentTaskTypeName { get; set; }
///
/// 父级任务类型;该属性为只读
///
[IgnoreDataMember]
public TaskBaseTypeEnum? ParentTaskType => string.IsNullOrEmpty(ParentTaskTypeName) ? null : Enum.Parse(ParentTaskTypeName);
///
/// 父级任务ID(用于子任务)
///
public long? ParentId { get; set; }
///
/// 父级业务ID(用于子任务)
///
public long? ParentBusinessId { get; set; }
///
/// 附加数据(内部使用)
///
public object? ExtraData { get; set; }
///
/// 是否自动同步更新主任务状态
///
public bool AutoSync { get; set; } = true;
///
/// 任务步骤
///
public List? Steps { get; set; }
///
/// 确定当前对象是否有效
///
/// 验证上下文
///
public virtual IEnumerable Validate(ValidationContext validationContext)
{
//if (BusinessId == 0 && (Ids == null || Ids.Length == 0))
//{
// yield return new ValidationResult($"参数 {nameof(BusinessId)} 与 {nameof(Ids)} 至少指定一项");
//}
if (string.IsNullOrEmpty(TaskTypeName))
{
yield return new ValidationResult($"参数 {nameof(TaskTypeName)} 不能为空");
}
else if (!Enum.TryParse(TaskTypeName, out TaskBaseTypeEnum taskType))
{
yield return new ValidationResult($"参数 {nameof(TaskTypeName)} 值不在有效枚举范围之内");
}
else if (TaskService.IsOrderType(TaskType) && BusinessType == null)
{
yield return new ValidationResult($"任务类型为【{TaskType.GetDescription()}】时,参数 {nameof(BusinessType)} 不能为空");
}
}
}
}