FieldPathConfigurationService.cs 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using System.Text;
using Rcs.Application.DTOs;
using Rcs.Domain.Attributes;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Shared.Options;

namespace Rcs.Application.Services
{
    /// <summary>
    /// 字段路径配置服务
    /// 提供动作参数来源路径的多级下拉框数据源配置(通过反射动态获取)
    /// @author zzy
    /// </summary>
    public interface IFieldPathConfigurationService
    {
        /// <summary>
        /// 获取指定来源类型的字段路径树
        /// </summary>
        /// <param name="sourceType">来源类型</param>
        /// <param name="language">语言(zh-cn/en)</param>
        /// <returns>字段路径树根节点列表</returns>
        List<FieldPathTreeNodeDto> GetFieldPathTree(ParameterSourceType sourceType, string language = "zh-cn");
    }

    /// <summary>
    /// 字段路径配置服务实现
    /// @author zzy
    /// </summary>
    public class FieldPathConfigurationService : IFieldPathConfigurationService
    {
        // 最大递归深度,防止循环引用
        private const int MaxDepth = 5;

        // 排除的属性列表(基类属性、不需要的字段等)
        private static readonly HashSet<string> ExcludedProperties = new(StringComparer.OrdinalIgnoreCase)
        {
            "DomainEvents", "NodeStates", "EdgeStates"
        };

        // 支持的嵌套导航属性(需要展开子字段的属性)
        private static readonly Dictionary<string, Type> SupportedNavigationProperties = new(StringComparer.OrdinalIgnoreCase)
        {
            // Robot 相关
            { "Map", typeof(Map) },
            { "MapNode", typeof(MapNode) },
            // Task 相关
            { "Robot", typeof(Robot) },
            { "BeginLocation", typeof(StorageLocation) },
            { "EndLocation", typeof(StorageLocation) },
            { "TaskTemplate", typeof(TaskTemplate) },
            // Node 相关
            { "StorageLocationType", typeof(StorageLocationType) },
            // Edge 相关
            { "StartNode", typeof(MapNode) },
            { "EndNode", typeof(MapNode) }
        };

        public List<FieldPathTreeNodeDto> GetFieldPathTree(ParameterSourceType sourceType, string language = "zh-cn")
        {
            return sourceType switch
            {
                ParameterSourceType.Robot => GetEntityFieldTree<Robot>(language),
                ParameterSourceType.Task => GetEntityFieldTree<RobotTask>(language),
                ParameterSourceType.Node => GetEntityFieldTree<MapNode>(language),
                ParameterSourceType.Edge => GetEntityFieldTree<MapEdge>(language),
                ParameterSourceType.Context => GetContextFieldTree(language),
                _ => new List<FieldPathTreeNodeDto>()
            };
        }

        /// <summary>
        /// 通过反射获取实体类的字段树
        /// @author zzy
        /// </summary>
        /// <param name="language">语言</param>
        /// <param name="parentPath">父路径</param>
        /// <param name="currentDepth">当前递归深度</param>
        /// <param name="visitedTypes">已访问的实体类型集合,防止循环引用</param>
        private static List<FieldPathTreeNodeDto> GetEntityFieldTree<T>(
            string language,
            string parentPath = "",
            int currentDepth = 0,
            HashSet<Type>? visitedTypes = null)
        {
            var result = new List<FieldPathTreeNodeDto>();
            var entityType = typeof(T);

            // 初始化已访问类型集合
            visitedTypes ??= new HashSet<Type>();

            // 如果当前实体类型已被访问过,直接返回空结果,防止循环引用
            if (visitedTypes.Contains(entityType))
                return result;

            // 标记当前实体类型为已访问
            visitedTypes.Add(entityType);

            var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var property in properties)
            {
                // 跳过排除的属性
                if (ExcludedProperties.Contains(property.Name))
                    continue;

                // 跳过索引器属性
                if (property.GetIndexParameters().Length > 0)
                    continue;

                // 跳过没有 public setter 的属性(只读属性,除了集合导航属性)
                if (property.SetMethod == null && !typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
                    continue;

                var propertyPath = string.IsNullOrEmpty(parentPath) ? property.Name : $"{parentPath}.{property.Name}";
                var displayName = GetPropertyDisplayName(property);
                var propertyType = GetPropertyTypeString(property.PropertyType);

                // 检查是否为导航属性(关联对象)
                if (IsNavigationProperty(property, out var navigationType))
                {
                    // 如果达到最大深度或导航类型为空,不再递归
                    if (currentDepth >= MaxDepth || navigationType == null)
                    {
                        continue; // 不添加导航属性节点本身,跳过
                    }
                    else
                    {
                        // 直接展开导航属性的字段,不创建父节点
                        // 例如:Task.Robot -> 直接添加 Task.Robot.RobotId, Task.Robot.Status 等
                        var expandedFields = GetEntityFieldTreeByType(
                            navigationType,
                            language,
                            propertyPath,
                            currentDepth + 1,
                            visitedTypes);

                        result.AddRange(expandedFields);
                    }
                }
                // 检查是否为集合类型(暂不展开)
                else if (IsCollectionType(property.PropertyType))
                {
                    continue; // 跳过集合类型
                }
                // 检查是否为枚举类型
                else if (property.PropertyType.IsEnum)
                {
                    result.Add(new FieldPathTreeNodeDto
                    {
                        Name = property.Name,
                        DisplayName = displayName,
                        Type = "enum",
                        Path = propertyPath,
                        HasChildren = false,
                        Children = null,
                        EnumValues = GetEnumOptions(property.PropertyType, language)
                    });
                }
                // 普通属性
                else
                {
                    result.Add(new FieldPathTreeNodeDto
                    {
                        Name = property.Name,
                        DisplayName = displayName,
                        Type = propertyType,
                        Path = propertyPath,
                        HasChildren = false,
                        Children = null,
                        EnumValues = null
                    });
                }
            }

            // 回溯:移除当前类型,允许其他路径再次访问(例如:Task.Map 和 Robot.Map 可以都存在)
            visitedTypes.Remove(entityType);

            return result;
        }

        /// <summary>
        /// 根据类型获取字段树(用于递归)
        /// @author zzy
        /// </summary>
        private static List<FieldPathTreeNodeDto> GetEntityFieldTreeByType(
            Type entityType,
            string language,
            string parentPath,
            int currentDepth,
            HashSet<Type>? visitedTypes = null)
        {
            var method = typeof(FieldPathConfigurationService).GetMethod(
                nameof(GetEntityFieldTree),
                BindingFlags.NonPublic | BindingFlags.Static);

            if (method == null)
                return new List<FieldPathTreeNodeDto>();

            var genericMethod = method.MakeGenericMethod(entityType);
            // 确保 visitedTypes 不为 null
            visitedTypes ??= new HashSet<Type>();
            var parameters = new object[] { language, parentPath, currentDepth, visitedTypes };

            return genericMethod.Invoke(null, parameters) as List<FieldPathTreeNodeDto>
                   ?? new List<FieldPathTreeNodeDto>();
        }

        /// <summary>
        /// 获取上下文字段树(手动定义,因为上下文不是实体类)
        /// @author zzy
        /// </summary>
        private static List<FieldPathTreeNodeDto> GetContextFieldTree(string language)
        {
            return new List<FieldPathTreeNodeDto>
            {
                new FieldPathTreeNodeDto
                {
                    Name = "CurrentTime",
                    DisplayName = "当前时间",
                    Type = "DateTime",
                    Path = "CurrentTime",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "CurrentDate",
                    DisplayName = "当前日期",
                    Type = "DateTime",
                    Path = "CurrentDate",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "TaskCount",
                    DisplayName = "任务总数",
                    Type = "int",
                    Path = "TaskCount",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "RobotCount",
                    DisplayName = "机器人总数",
                    Type = "int",
                    Path = "RobotCount",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "IdleRobotCount",
                    DisplayName = "空闲机器人数量",
                    Type = "int",
                    Path = "IdleRobotCount",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "BusyRobotCount",
                    DisplayName = "忙碌机器人数量",
                    Type = "int",
                    Path = "BusyRobotCount",
                    HasChildren = false
                },
                new FieldPathTreeNodeDto
                {
                    Name = "ErrorRobotCount",
                    DisplayName = "异常机器人数量",
                    Type = "int",
                    Path = "ErrorRobotCount",
                    HasChildren = false
                }
            };
        }

        /// <summary>
        /// 检查属性是否为导航属性
        /// @author zzy
        /// </summary>
        private static bool IsNavigationProperty(PropertyInfo property, out Type? navigationType)
        {
            navigationType = null;

            // 检查是否在支持的导航属性列表中
            if (SupportedNavigationProperties.TryGetValue(property.Name, out var supportedType))
            {
                navigationType = supportedType;
                return true;
            }

            // 检查是否有 ForeignKey 特性
            var foreignKeyAttr = property.GetCustomAttribute<ForeignKeyAttribute>();
            if (foreignKeyAttr != null)
            {
                navigationType = property.PropertyType;
                return true;
            }

            // 检查是否为虚拟导航属性(EF Core 导航属性通常标记为 virtual)
            if (property.GetMethod?.IsVirtual == true)
            {
                // 确保不是集合类型
                if (!IsCollectionType(property.PropertyType))
                {
                    navigationType = property.PropertyType;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 检查是否为集合类型
        /// @author zzy
        /// </summary>
        private static bool IsCollectionType(Type type)
        {
            if (type == typeof(string))
                return false;

            return typeof(System.Collections.IEnumerable).IsAssignableFrom(type)
                   && type.IsGenericType
                   && type.GetGenericTypeDefinition() != typeof(Nullable<>);
        }

        /// <summary>
        /// 获取属性显示名称
        /// @author zzy
        /// </summary>
        private static string GetPropertyDisplayName(PropertyInfo property)
        {
            // 优先使用 Display 特性
            var displayAttr = property.GetCustomAttribute<DisplayAttribute>();
            if (displayAttr != null && !string.IsNullOrWhiteSpace(displayAttr.Name))
                return displayAttr.Name;

            // 其次使用 EnumDescription 特性(如果是枚举)
            if (property.PropertyType.IsEnum)
            {
                var enumDescAttr = property.PropertyType.GetCustomAttribute<EnumDescriptionAttribute>();
                if (enumDescAttr != null && !string.IsNullOrWhiteSpace(enumDescAttr.ZhCn))
                    return enumDescAttr.ZhCn;
            }

            // 最后使用属性名称的分词形式
            return SplitCamelCase(property.Name);
        }

        /// <summary>
        /// 获取属性类型字符串
        /// @author zzy
        /// </summary>
        private static string GetPropertyTypeString(Type type)
        {
            // 处理可空类型
            var underlyingType = Nullable.GetUnderlyingType(type) ?? type;

            // 基础类型映射
            if (underlyingType == typeof(Guid))
                return "Guid";
            if (underlyingType == typeof(string))
                return "string";
            if (underlyingType == typeof(int) || underlyingType == typeof(int?))
                return "int";
            if (underlyingType == typeof(long) || underlyingType == typeof(long?))
                return "long";
            if (underlyingType == typeof(double) || underlyingType == typeof(double?))
                return "double";
            if (underlyingType == typeof(float) || underlyingType == typeof(float?))
                return "float";
            if (underlyingType == typeof(decimal) || underlyingType == typeof(decimal?))
                return "decimal";
            if (underlyingType == typeof(bool) || underlyingType == typeof(bool?))
                return "bool";
            if (underlyingType == typeof(DateTime) || underlyingType == typeof(DateTime?))
                return "DateTime";
            if (underlyingType.IsEnum)
                return "enum";

            return "object";
        }

        /// <summary>
        /// 获取枚举类型的选项列表
        /// @author zzy
        /// </summary>
        private static List<EnumValueDto>? GetEnumOptions(Type enumType, string language)
        {
            if (!enumType.IsEnum)
                return null;

            try
            {
                var lang = EnumOptions.ParseLanguage(language);

                // 使用反射调用泛型方法
                var getOptionsMethod = typeof(EnumOptions).GetMethod(
                    nameof(EnumOptions.GetOptions),
                    BindingFlags.Public | BindingFlags.Static);

                if (getOptionsMethod == null)
                    return null;

                var genericMethod = getOptionsMethod.MakeGenericMethod(enumType);
                var options = genericMethod.Invoke(null, new object[] { lang }) as System.Collections.IEnumerable;

                if (options == null)
                    return null;

                var result = new List<EnumValueDto>();
                foreach (var option in options)
                {
                    var optionType = option.GetType();
                    var valueProperty = optionType.GetProperty("Value");
                    var labelProperty = optionType.GetProperty("Label");

                    if (valueProperty != null && labelProperty != null)
                    {
                        var value = valueProperty.GetValue(option);
                        var label = labelProperty.GetValue(option);

                        result.Add(new EnumValueDto
                        {
                            Value = value != null ? Convert.ToInt32(value) : 0,
                            DisplayName = label?.ToString() ?? string.Empty,
                            Name = value != null ? (Enum.GetName(enumType, value) ?? string.Empty) : string.Empty
                        });
                    }
                }

                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 将驼峰命名拆分为可读格式
        /// @author zzy
        /// </summary>
        private static string SplitCamelCase(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return input;

            var sb = new StringBuilder();
            sb.Append(input[0]);

            for (int i = 1; i < input.Length; i++)
            {
                if (char.IsUpper(input[i]))
                {
                    sb.Append(' ');
                }
                sb.Append(input[i]);
            }

            return sb.ToString();
        }
    }
}