OptionsController.cs 18 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
using Microsoft.AspNetCore.Mvc;
using Rcs.Application.DTOs;
using Rcs.Application.Services;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Domain.Repositories;
using Rcs.Shared.Options;

namespace Rcs.Api.Controllers
{
    /// <summary>
    /// 下拉选项控制器
    /// 提供统一的下拉数据接口,支持多语言
    /// @author zzy
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class OptionsController : ControllerBase
    {
        private readonly IRobotRepository _robotRepository;
        private readonly IStorageLocationRepository _storageLocationRepository;
        private readonly IStorageAreaRepository _storageAreaRepository;
        private readonly IStorageLocationTypeRepository _storageLocationTypeRepository;
        private readonly IChargingPileRepository _chargingPileRepository;
        private readonly IFieldPathConfigurationService _fieldPathConfigurationService;
        private readonly INetActionPropertysRepository _netActionPropertysRepository;

        public OptionsController(
            IRobotRepository robotRepository,
            IStorageLocationRepository storageLocationRepository,
            IStorageAreaRepository storageAreaRepository,
            IStorageLocationTypeRepository storageLocationTypeRepository,
            IChargingPileRepository chargingPileRepository,
            IFieldPathConfigurationService fieldPathConfigurationService,
            INetActionPropertysRepository netActionPropertysRepository)
        {
            _robotRepository = robotRepository;
            _storageLocationRepository = storageLocationRepository;
            _storageAreaRepository = storageAreaRepository;
            _storageLocationTypeRepository = storageLocationTypeRepository;
            _chargingPileRepository = chargingPileRepository;
            _fieldPathConfigurationService = fieldPathConfigurationService;
            _netActionPropertysRepository = netActionPropertysRepository;
        }
        /// <summary>
        /// 获取所有下拉选项
        /// </summary>
        /// <param name="lang">语言:zh-cn(默认), en</param>
        /// <returns>所有下拉选项</returns>
        [HttpGet]
        public IActionResult GetAllOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                // 机器人管理
                RobotType = EnumOptions.GetOptions<RobotType>(language),
                MovementType = EnumOptions.GetOptions<MovementType>(language),
                RobotStatus = EnumOptions.GetOptions<RobotStatus>(language),
                OnlineStatus = EnumOptions.GetOptions<OnlineStatus>(language),
                OperatingMode = EnumOptions.GetOptions<OperatingMode>(language),
                ProtocolType = EnumOptions.GetOptions<ProtocolType>(language),
                // 地图管理
                MapType = EnumOptions.GetOptions<MapTYPE>(language),
                MapNodeType = EnumOptions.GetOptions<MapNodeTYPE>(language),
                MapResourceType = EnumOptions.GetOptions<MapResourcesTYPE>(language),
                // 动作管理
                ActionCategory = EnumOptions.GetOptions<ActionCategory>(language),
                ActionBlockType = EnumOptions.GetOptions<ActionBlockType>(language),
                ExecutionScope = EnumOptions.GetOptions<ExecutionScope>(language),
                ParameterValueType = EnumOptions.GetOptions<ParameterValueType>(language),
                ParameterSourceType = EnumOptions.GetOptions<ParameterSourceType>(language),
                ResponseWaitType = EnumOptions.GetOptions<ResponseWaitType>(language),
                // 任务模板
                TaskStatus = EnumOptions.GetOptions<Rcs.Domain.Entities.TaskStatus>(language),
                TaskStepType = EnumOptions.GetOptions<TaskStepType>(language),
                StepPropertyType = EnumOptions.GetOptions<StepPropertyType>(language),
                AfterActionType = EnumOptions.GetOptions<ActionType>(language),
                NodeValueType = EnumOptions.GetOptions<NodeValueType>(language),
                // 储位管理
                ShelfType = EnumOptions.GetOptions<ShelfTypeEnum>(language)
            });
        }

        /// <summary>
        /// 获取机器人相关下拉选项
        /// </summary>
        [HttpGet("robot")]
        public IActionResult GetRobotOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                RobotType = EnumOptions.GetOptions<RobotType>(language),
                MovementType = EnumOptions.GetOptions<MovementType>(language),
                RobotStatus = EnumOptions.GetOptions<RobotStatus>(language),
                OnlineStatus = EnumOptions.GetOptions<OnlineStatus>(language),
                OperatingMode = EnumOptions.GetOptions<OperatingMode>(language),
                ProtocolType = EnumOptions.GetOptions<ProtocolType>(language)
            });
        }

        /// <summary>
        /// 获取地图相关下拉选项
        /// </summary>
        [HttpGet("map")]
        public IActionResult GetMapOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                MapType = EnumOptions.GetOptions<MapTYPE>(language),
                MapNodeType = EnumOptions.GetOptions<MapNodeTYPE>(language),
                MapResourceType = EnumOptions.GetOptions<MapResourcesTYPE>(language)
            });
        }

        /// <summary>
        /// 获取动作相关下拉选项
        /// </summary>
        [HttpGet("action")]
        public IActionResult GetActionOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                ActionCategory = EnumOptions.GetOptions<ActionCategory>(language),
                ActionBlockType = EnumOptions.GetOptions<ActionBlockType>(language),
                ExecutionScope = EnumOptions.GetOptions<ExecutionScope>(language),
                ParameterValueType = EnumOptions.GetOptions<ParameterValueType>(language),
                ParameterSourceType = EnumOptions.GetOptions<ParameterSourceType>(language),
                ResponseWaitType = EnumOptions.GetOptions<ResponseWaitType>(language)
            });
        }

        /// <summary>
        /// 获取任务模板相关下拉选项
        /// </summary>
        [HttpGet("task")]
        public IActionResult GetTaskOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                TaskStatus = EnumOptions.GetOptions<Rcs.Domain.Entities.TaskStatus>(language),
                TaskStepType = EnumOptions.GetOptions<TaskStepType>(language),
                StepPropertyType = EnumOptions.GetOptions<StepPropertyType>(language),
                AfterActionType = EnumOptions.GetOptions<ActionType>(language),
                NodeValueType = EnumOptions.GetOptions<NodeValueType>(language)
            });
        }

        /// <summary>
        /// 获取储位相关下拉选项
        /// @author zzy
        /// </summary>
        [HttpGet("storage")]
        public IActionResult GetStorageOptions([FromQuery] string lang = "zh-cn")
        {
            var language = EnumOptions.ParseLanguage(lang);
            return Ok(new
            {
                ShelfType = EnumOptions.GetOptions<ShelfTypeEnum>(language)
            });
        }

        /// <summary>
        /// 获取机器人下拉选项
        /// @author zzy
        /// </summary>
        [HttpGet("robots")]
        public async Task<IActionResult> GetRobotSelectOptions(CancellationToken cancellationToken = default)
        {
            var robots = await _robotRepository.GetActiveRobotsAsync(cancellationToken);
            
            var options = robots?.OrderBy(r => r.RobotCode).Select(r => new { value = r.RobotId, text = $"{r.RobotCode} - {r.RobotName}", code = r.RobotCode });
            return Ok(options);
        }
        /// <summary>
        /// 获取机器人型号下拉选项
        /// @author zzy
        /// </summary>
        [HttpGet("robot-models")]
        public async Task<IActionResult> GetRobotModelsOptions(CancellationToken cancellationToken = default)
        {
            var robots = await _robotRepository.GetRobotsAsync(cancellationToken);
            var options = robots?.Select(r => r.RobotModel).Distinct().Select(g => new { value = g, text = g, code = g });
            return Ok(options);
        }

        /// <summary>
        /// 获取库位下拉选项
        /// @author zzy
        /// </summary>
        [HttpGet("locations")]
        public async Task<IActionResult> GetLocationSelectOptions(CancellationToken cancellationToken = default)
        {
            var locations = await _storageLocationRepository.GetAllAsync(cancellationToken);
            var options = locations?.Where(l => l.IsActive).OrderBy(l => l.LocationName).Select(l => new { value = l.LocationId, text = $"{l.LocationName}", code = l.LocationId });
            return Ok(options);
        }

        /// <summary>
        /// 获取库区下拉选项
        /// @author zzy
        /// </summary>
        [HttpGet("areas")]
        public async Task<IActionResult> GetAreaSelectOptions(CancellationToken cancellationToken = default)
        {
            var areas = await _storageAreaRepository.GetAllAsync(cancellationToken);
            var options = areas?.Where(a => a.IsActive).Select(a => new { value = a.AreaId, text = a.AreaName ?? a.AreaCode, code = a.AreaCode });
            return Ok(options);
        }

        /// <summary>
        /// 获取储位类型下拉选项
        /// @author zzy
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>储位类型选项列表(包含类型ID、类型名称、类型编码)</returns>
        [HttpGet("location-types")]
        public async Task<IActionResult> GetLocationTypeSelectOptions(CancellationToken cancellationToken = default)
        {
            var locationTypes = await _storageLocationTypeRepository.GetAllAsync(cancellationToken);
            var options = locationTypes?
                .Where(t => t.IsActive)
                .OrderBy(t => t.TypeCode)
                .Select(t => new
                {
                    value = t.TypeId,
                    text = $"{t.TypeCode} - {t.TypeName}",
                    code = t.TypeCode
                });
            return Ok(options);
        }

        /// <summary>
        /// 获取充电桩下拉选项
        /// </summary>
        [HttpGet("charging-piles")]
        public async Task<IActionResult> GetChargingPileSelectOptions(CancellationToken cancellationToken = default)
        {
            var piles = await _chargingPileRepository.GetAllAsync(cancellationToken);
            var options = piles?
                .Where(p => p.IsActive)
                .OrderBy(p => p.PileCode)
                .Select(p => new
                {
                    value = p.PileId,
                    text = $"{p.PileCode} - {p.PileName}",
                    code = p.PileCode
                });
            return Ok(options);
        }

        /// <summary>
        /// 根据机器人型号获取储位类型下拉选项
        /// @author zzy
        /// </summary>
        /// <param name="robotModel">机器人型号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>储位类型选项列表(匹配指定机器人型号)</returns>
        [HttpGet("location-types/by-robot-model")]
        public async Task<IActionResult> GetLocationTypeSelectOptionsByRobotModel(
            [FromQuery] string robotModel,
            CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(robotModel))
            {
                return BadRequest(new { message = "机器人型号不能为空" });
            }

            var allTypes = await _storageLocationTypeRepository.GetAllAsync(cancellationToken);
            var options = allTypes?
                .Where(t => t.IsActive && t.RobotModels.Contains(robotModel))
                .Select(t => new
                {
                    value = t.TypeId,
                    text = $"{t.TypeName}({t.TypeCode})",
                    code = t.TypeCode,
                    typeName = t.TypeName,
                    typeCode = t.TypeCode,
                    shelfType = (int)t.ShelfType,
                    robotModels = string.Join(",", t.RobotModels)
                });
            return Ok(options);
        }

        /// <summary>
        /// 获取制造商下拉选项
        /// @author zzy
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>制造商选项列表(从机器人数据中去重获取)</returns>
        [HttpGet("manufacturers")]
        public async Task<IActionResult> GetManufacturerSelectOptions(CancellationToken cancellationToken = default)
        {
            var robots = await _robotRepository.GetRobotsAsync(cancellationToken);
            var options = (robots ?? Enumerable.Empty<Robot>())
                .Where(r => !string.IsNullOrWhiteSpace(r.RobotManufacturer))
                .Select(r => r.RobotManufacturer!)
                .Distinct()
                .OrderBy(m => m)
                .Select(m => new
                {
                    value = m,
                    text = m,
                    code = m
                });
            return Ok(options);
        }

        /// <summary>
        /// 获取动作参数来源路径的字段树(多级下拉框数据源)
        /// @author zzy
        /// </summary>
        /// <param name="sourceType">来源类型(Robot=3,Task=2,Node=4,Edge=5,Context=6)</param>
        /// <param name="lang">语言:zh-cn(默认), en</param>
        /// <returns>字段路径树根节点列表</returns>
        [HttpGet("field-paths")]
        public IActionResult GetFieldPathTree(
            [FromQuery] ParameterSourceType sourceType,
            [FromQuery] string lang = "zh-cn")
        {
            if (!Enum.IsDefined(typeof(ParameterSourceType), sourceType))
            {
                return BadRequest(new { message = $"无效的来源类型: {sourceType}" });
            }

            var fieldTree = _fieldPathConfigurationService.GetFieldPathTree(sourceType, lang);
            return Ok(new
            {
                sourceType = sourceType.ToString(),
                sourceTypeValue = (int)sourceType,
                nodes = fieldTree
            });
        }

        /// <summary>
        /// 批量获取多种来源类型的字段路径树
        /// @author zzy
        /// </summary>
        /// <param name="sourceTypes">来源类型列表(逗号分隔,如:3,2,4)</param>
        /// <param name="lang">语言:zh-cn(默认), en</param>
        /// <returns>多种来源类型的字段路径树映射</returns>
        [HttpGet("field-paths/batch")]
        public IActionResult GetFieldPathTreeBatch(
            [FromQuery] string sourceTypes,
            [FromQuery] string lang = "zh-cn")
        {
            if (string.IsNullOrWhiteSpace(sourceTypes))
            {
                return BadRequest(new { message = "来源类型列表不能为空" });
            }

            var result = new Dictionary<string, object>();

            var typeValues = sourceTypes.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            foreach (var typeValue in typeValues)
            {
                if (int.TryParse(typeValue, out int typeInt) && Enum.IsDefined(typeof(ParameterSourceType), typeInt))
                {
                    var sourceType = (ParameterSourceType)typeInt;
                    var fieldTree = _fieldPathConfigurationService.GetFieldPathTree(sourceType, lang);
                    result[sourceType.ToString()] = new
                    {
                        sourceTypeValue = typeInt,
                        nodes = fieldTree
                    };
                }
            }

            return Ok(result);
        }

        /// <summary>
        /// 获取所有来源类型的字段路径树
        /// @author zzy
        /// </summary>
        /// <param name="lang">语言:zh-cn(默认), en</param>
        /// <returns>所有来源类型的字段路径树映射</returns>
        [HttpGet("field-paths/all")]
        public IActionResult GetAllFieldPathTrees([FromQuery] string lang = "zh-cn")
        {
            var result = new Dictionary<string, object>();

            foreach (ParameterSourceType sourceType in Enum.GetValues(typeof(ParameterSourceType)))
            {
                if (sourceType == ParameterSourceType.Constant)
                    continue; // 常量值类型不需要字段路径

                var fieldTree = _fieldPathConfigurationService.GetFieldPathTree(sourceType, lang);
                result[sourceType.ToString()] = new
                {
                    sourceTypeValue = (int)sourceType,
                    nodes = fieldTree
                };
            }

            return Ok(result);
        }

        /// <summary>
        /// 获取网络动作属性下拉选项
        /// @author zzy
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>网络动作属性选项列表(包含Id、动作名称、描述)</returns>
        [HttpGet("net-actions")]
        public async Task<IActionResult> GetNetActionSelectOptions(CancellationToken cancellationToken = default)
        {
            var netActions = await _netActionPropertysRepository.GetAllAsync(cancellationToken);
            var options = netActions?
                .Where(n => n.IsActive)
                .Select(n => new
                {
                    value = n.NetActionId,
                    text = n.ActionName,
                    code = n.NetActionId
                });
            return Ok(options);
        }
    }
}