| | |
| | | |
| | | namespace WIDESEAWCS_Common.TaskEnum |
| | | { |
| | | // 定义一个静态类TaskEnumHelper,用于处理枚举类型 |
| | | public static class TaskEnumHelper |
| | | { |
| | | // 获取枚举类型的索引列表 |
| | | public static List<int> GetEnumIndexList(this Type type) |
| | | { |
| | | // 如果type为空,抛出ArgumentNullException异常 |
| | | if (type is null) throw new ArgumentNullException("type"); |
| | | // 如果type不是枚举类型,返回空列表 |
| | | if (!type.IsEnum) return new List<int>(); |
| | | // 返回枚举类型的索引列表 |
| | | return Enum.GetValues(type).Cast<int>().ToList(); |
| | | } |
| | | |
| | | // 根据任务类型获取任务类型组 |
| | | public static TaskTypeGroup GetTaskTypeGroup(this int taskType) |
| | | { |
| | | if (!int.TryParse(Enum.Parse<TaskOutboundTypeEnum>(taskType.ToString()).ToString(), out int result)) |
| | | // 将任务类型转换为字符串 |
| | | string taskTypeStr = taskType.ToString(); |
| | | // 尝试将任务类型转换为TaskOutboundTypeEnum枚举类型,如果成功,返回OutbondGroup |
| | | if (!int.TryParse(Enum.Parse<TaskOutboundTypeEnum>(taskTypeStr).ToString(), out int result)) |
| | | { |
| | | return TaskTypeGroup.OutbondGroup; |
| | | } |
| | | else if (!int.TryParse(Enum.Parse<TaskInStatusEnum>(taskType.ToString()).ToString(), out result)) |
| | | // 尝试将任务类型转换为TaskInStatusEnum枚举类型,如果成功,返回InboundGroup |
| | | else if (!int.TryParse(Enum.Parse<TaskInStatusEnum>(taskTypeStr).ToString(), out result)) |
| | | { |
| | | return TaskTypeGroup.InboundGroup; |
| | | } |
| | | else if (!int.TryParse(Enum.Parse<TaskRelocationTypeEnum>(taskType.ToString()).ToString(), out result)) |
| | | // 尝试将任务类型转换为TaskRelocationTypeEnum枚举类型,如果成功,返回RelocationGroup |
| | | else if (!int.TryParse(Enum.Parse<TaskRelocationTypeEnum>(taskTypeStr).ToString(), out result)) |
| | | { |
| | | return TaskTypeGroup.RelocationGroup; |
| | | } |
| | | else if (!int.TryParse(Enum.Parse<TaskOtherTypeEnum>(taskType.ToString()).ToString(), out result)) |
| | | // 尝试将任务类型转换为TaskOtherTypeEnum枚举类型,如果成功,返回OtherGroup |
| | | else if (!int.TryParse(Enum.Parse<TaskOtherTypeEnum>(taskTypeStr).ToString(), out result)) |
| | | { |
| | | return TaskTypeGroup.OtherGroup; |
| | | } |
| | | // 如果以上转换都不成功,抛出NotImplementedException异常 |
| | | else |
| | | { |
| | | throw new NotImplementedException(); |
| | | } |
| | | } |
| | | |
| | | // 获取下一个未完成的任务状态 |
| | | public static int GetNextNotCompletedStatus<T>(this int currentStatus) where T : Enum |
| | | { |
| | | // 获取枚举类型 |
| | | Type type = typeof(T); |
| | | // 如果type为空,抛出ArgumentNullException异常 |
| | | if (type is null) throw new ArgumentNullException(); |
| | | // 如果type不是枚举类型,返回0 |
| | | if (!type.IsEnum) return 0; |
| | | // 如果type是TaskInStatusEnum枚举类型 |
| | | if (type == typeof(TaskInStatusEnum)) |
| | | { |
| | | // 获取TaskInStatusEnum枚举类型的索引列表 |
| | | List<int> taskInboundTypes = type.GetEnumIndexList(); |
| | | // 返回大于当前状态且小于InFinish状态的索引 |
| | | return taskInboundTypes.Where(x => x > currentStatus && x < (int)TaskInStatusEnum.InFinish).OrderBy(x => x).FirstOrDefault(); |
| | | } |
| | | // 如果type是TaskOutStatusEnum枚举类型 |
| | | else if (type == typeof(TaskOutStatusEnum)) |
| | | { |
| | | // 获取TaskOutStatusEnum枚举类型的索引列表 |
| | | return type.GetEnumIndexList().Where(x => x > currentStatus && x < (int)TaskOutStatusEnum.OutFinish).OrderBy(x => x).FirstOrDefault(); |
| | | } |
| | | // 如果以上条件都不满足,抛出NotImplementedException异常 |
| | | else |
| | | { |
| | | throw new NotImplementedException(); |