using System; using System.Collections; using System.Diagnostics; using System.Dynamic; using System.Linq; using Abp.Runtime.Caching; using IwbZero.AppServiceBase; using IwbZero.IwbBase; using IwbZero.ToolCommon.LogHelpers; using IwbZero.ToolCommon.StringModel; using WeEngine.CommonDto; using WeEngine.Enum; using WeEngine.Packages; namespace WeEngine.Application.RunEngine { /// /// 演练实施 /// public class EngineRunAppService : IwbZeroAppServiceBase, IEngineRunAppService { public EngineRunAppService(ICacheManager cacheManager, PackageRunner packageRunner) { PackageRunner = packageRunner; CacheManager = cacheManager; } public PackageRunner PackageRunner { get; } /// /// 开启训练营 /// /// /// public void Start(RunStartPackageDto input) { try { this.LogDebug($"---[开启训练营]---> Data:[{input.Obj2String()}]"); if (input.PackageId.IsEmpty()) { CheckErrors("方案包编号不能为空!"); return; } var s = Stopwatch.StartNew(); PackageNode info = GetPackageNode(input.PackageId); s.Stop(); Debug.WriteLine($" ===>{s.Elapsed.TotalSeconds}s ===>{s.Elapsed.TotalMilliseconds}ms"); if (input.GroupNos != null && input.GroupNos.Any()) { foreach (var groupNo in input.GroupNos) { s.Restart(); var package = (PackageNode)info.Clone(); s.Stop(); Debug.WriteLine($" ===>{s.Elapsed.TotalSeconds}s ===>{s.Elapsed.TotalMilliseconds}ms"); package.RunningId = groupNo; package.AssessRoles = input.AssessRoles ?? package.AssessRoles; package.AssessAuto = input.AssessAuto; package.AutoNextRound = input.AutoNextRound; package.RoundScore = input.RoundScore == 0 ? package.RoundScore : input.RoundScore; package.TargetClientId = input.TargetClientId; if (input.Variable.IsNotEmpty()) { Hashtable variables = new Hashtable(); var arr = input.Variable.Split(','); foreach (var v in arr) { var variable = IwbRtVariable.Str2Variable(v); if (!variables.ContainsKey(variable.VarName)) { variables.Add(variable.VarName, variable); } } package.Variables = variables.MergeHashtable(package.Variables); } PackageRunner.Run(package, OperationType.Start); } } } catch (Exception e) { this.LogError(e); CheckErrors("启动失败!"); } } /// /// 重新启动培训营 /// public void ReStart(ReStartPackageDto input) { try { this.LogDebug($"---[重启训练营]---> "); var dataNode = input.RunningInfo.Str2Obj(); this.LogDebug($"=========> Data [{dataNode?.Id}] [{dataNode?.PackageNo}]"); if (dataNode != null) { var packageId = dataNode.PackageNo.IsEmpty() ? input.PackageId : dataNode.PackageNo; var package = GetPackageNode(packageId); if (package != null) { package.TargetClientId = input.TargetClientId; package.AutoNextRound = input.AutoNextRound; package.PackageNodeFrom(dataNode); PackageRunner.ReRun(package); } } } catch (Exception e) { this.LogError(e); CheckErrors("重启失败!"); } } private PackageNode GetPackageNode(string no) { PackageNode info = null; try { var xmlNode = CacheManager.GetPackageXml(no); if (xmlNode == null) { CheckErrors("方案包信息获取失败!"); } info = (PackageNode)new PackageNode().BuildNodeByXmlNode(xmlNode); } catch (Exception e) { CheckErrors($"方案包信息解析失败[{e.Message}]"); } return info; } /// /// 下一轮次(所有组) /// /// /// public void NextRounds(RunGroupPackageDto input) { this.LogDebug($"---[进入下一轮次]---> Data:[{input.Obj2String()}]"); if (input.GroupNos != null && input.GroupNos.Any()) { foreach (var groupNo in input.GroupNos) { var package = GetPackageByRunId(groupNo); try { PackageRunner.Run(package, OperationType.NextRound); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } } } /// /// 下一轮次 /// /// /// public void NextRound(string groupNo) { this.LogDebug($"---[进入下一轮次]--->Data:[groupNo:{groupNo}]"); var package = GetPackageByRunId(groupNo); try { PackageRunner.Run(package, OperationType.NextRound); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } /// /// 下一情景流节点(所有组) /// /// /// public void NextAllFlowNodes(RunStartPackageDto input) { this.LogDebug($"---[下一情景流节点]---> Data:[{input.Obj2String()}]"); if (input.GroupNos != null && input.GroupNos.Any()) { foreach (var groupNo in input.GroupNos) { var package = GetPackageByRunId(groupNo); try { PackageRunner.Run(package, OperationType.NextFlowNodes); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } } } /// /// 下一情景流节点 /// /// /// public void NextGroupFlowNodes(string groupNo) { this.LogDebug($"---[进入下一情景流节点]---> Data:[groupNo:{groupNo}]"); var package = GetPackageByRunId(groupNo); try { PackageRunner.Run(package, OperationType.NextFlowNodes); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } /// /// 下一情景流节点 /// /// 分组编号 /// 情景流流ID /// public void NextFlowNode(string groupNo, string flowId) { this.LogDebug($"---[进入下一情景流节点]---> Data:[groupNo:{groupNo},f:{flowId}]"); var package = GetPackageByRunId(groupNo); try { dynamic obj = new ExpandoObject(); obj.FlowId = flowId; package.ExtendData = obj; PackageRunner.Run(package, OperationType.NextFlowNode); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } /// /// 匹配关键字 /// /// /// /// /// /// public void MatchWord(string groupNo, string behaviorWord, string behaviorRole, string scenePath, bool checkRole) { this.LogDebug($"---[匹配关键字]---> Data:[groupNo:{groupNo},w:{behaviorWord},r:{behaviorRole},s:{scenePath}]"); var package = GetPackageByRunId(groupNo); try { dynamic obj = new ExpandoObject(); obj.BehaviorWord = behaviorWord; obj.BehaviorRole = behaviorRole; obj.ScenePath = scenePath; obj.CheckRole = checkRole; package.ExtendData = obj; PackageRunner.Run(package, OperationType.MatchKey); } catch (Exception e) { this.LogError(e); CheckErrors("操作失败!"); } } /// /// 匹配关键字 /// /// public void MatchWord(RunOperationDto input) { this.LogDebug($"---[匹配关键字]---> Data:[{input.Obj2String()}]"); var package = GetPackageByRunId(input.GroupNo); try { dynamic obj = new ExpandoObject(); obj.BehaviorWord = input.BehaviorWord; obj.BehaviorRole = input.BehaviorRole; obj.ScenePath = input.ScenePath; obj.CheckRole = input.CheckRole; package.ExtendData = obj; var ss = package.NotMatchKeywords; PackageRunner.Run(package, OperationType.MatchKey); this.LogDebug($"---[匹配 =======> 结束]"); } catch (Exception e) { this.LogError(e); this.LogDebug($"---[匹配 =======> ========> 错误]"); CheckErrors("操作失败!"); } } /// /// 查询运行信息 /// /// /// public PackageDataNode QueryPackageDataNode(string id) { try { var package = GetPackageByRunId(id); return package.CurPackageInfo.Convert2DataNode(); } catch (Exception e) { this.LogError(e); } return null; } /// /// 查询运行信息 /// /// /// /// public DataNodeBase QueryPackageDataNodeWithPath(string id, string path = "") { try { var package = GetPackageByRunId(id); var node = path.IsEmpty() ? package.CurPackageInfo : package.CurPackageInfo.GetNodeByPath(path); if (node != null) { return node.ConvertToDataNode(); } } catch (Exception e) { this.LogError(e); } return null; } /// /// 矫正评分 /// /// /// public void CorrectScore(string groupNo, decimal score) { this.LogDebug($"---[矫正评分]---> Data:[groupNo:{groupNo},score:{score}]"); var package = GetPackageByRunId(groupNo); package.CurRoundInfo.CorrectionScore = score; } private PackageNode GetPackageByRunId(string runId) { PackageNode package = null; try { package = (PackageNode)CacheManager.GetCache(EngineCacheName.RunningPackage).GetOrDefault(runId); } catch (Exception e) { this.LogError(e); } if (package == null) { CheckErrors("未查询到正在演练的培训营!"); } return package; } /// /// 非演化情景控制 /// public void NeControl(string groupNo, string ocType) { } } }