using Microsoft.EntityFrameworkCore; using PLCSharp.VVMs.MotionController; using PLCSharp.VVMs.Robots; using System.Text.Json.Nodes; namespace PLCSharp.Models; public sealed record PointField(string Table, Guid Id, string Field) { internal JsonNode Row(JsonNode document) => document[Table].AsArray() .Single(row => Guid.Parse((string)row["ID"]) == Id); internal Type EntityType => Table switch { "DI" => typeof(DI), "DQ" => typeof(DQ), "Cylinders" => typeof(PLCSharp.VVMs.Cylinders.Cylinder), "Axes" => typeof(Axis), "InterpolationGroups" => typeof(InterpolationGroup), "AxisPoints" => typeof(AxisPoint), "RobotPoints" => typeof(RobotPoint), "Matrices" => typeof(Matrix), "RobotMatrices" => typeof(RobotMatrix), _ => throw new ArgumentException("未知点位配置表。") }; } public sealed partial class ProjectDirectory { private static readonly string[] MatrixPointFields = ["StartName", "XEndName", "YEndName"]; internal ProjectRename PlanPointRename(string kind, Guid id, string newName) { if (kind is not ("AxisPoints" or "RobotPoints")) throw new ArgumentException("请选择 AxisPoints 或 RobotPoints。"); var document = JsonNode.Parse(baseline[ConfigPath("points")]); var row = new PointField(kind, id, "Name").Row(document); var recipe = Guid.Parse((string)row["RecipeID"]); var oldName = (string)row["Name"]; if (string.IsNullOrWhiteSpace(newName)) throw new ArgumentException("请输入新点位名称。"); var plan = PlanReferences(kind, id, recipe, oldName, newName) with { MatrixReferences = MatrixLinks(kind, document, row, oldName), RobotScope = kind == "RobotPoints" ? baseline[ConfigPath("hardware")] : null }; ValidatePoint(plan, oldName, newName); return plan; } private static List MatrixLinks(string kind, JsonNode document, JsonNode point, string name) { var table = kind == "AxisPoints" ? "Matrices" : "RobotMatrices"; return (from matrix in document[table].AsArray() where SamePointScope(point, matrix) from field in MatrixPointFields where (string)matrix[field] == name select new PointField(table, Guid.Parse((string)matrix["ID"]), field)).ToList(); } private static bool SamePointScope(JsonNode a, JsonNode b) => Guid.Parse((string)a["RecipeID"]) == Guid.Parse((string)b["RecipeID"]) && string.Equals((string)a["RobotID"], (string)b["RobotID"], StringComparison.OrdinalIgnoreCase); private void ValidatePoint(ProjectRename plan, string before, string after) { if (string.IsNullOrWhiteSpace(before) || string.IsNullOrWhiteSpace(after) || after != after.Trim() || before == after) throw new ArgumentException("请输入不同的新点位名称,首尾不能有空白。"); if (plan.RobotScope != null && baseline[ConfigPath("hardware")] != plan.RobotScope) throw new ProjectConflictException("机器人配置已改变,请重新预览。"); var doc = JsonNode.Parse(baseline[plan.MetadataPath]); var target = new PointField(plan.Kind, plan.ObjectId, "Name").Row(doc); var points = doc[plan.Kind].AsArray().Where(p => SamePointScope(target, p)).ToList(); if ((string)target["Name"] != before || points.Count(p => (string)p["Name"] == before) != 1 || points.Any(p => (string)p["Name"] == after) || MatrixLinks(plan.Kind, doc, target, after).Count != 0 || !MatrixLinks(plan.Kind, doc, target, before).ToHashSet().SetEquals(plan.MatrixReferences)) throw new ProjectConflictException("点位名称或矩阵引用有冲突,请重新核对。"); } private static IEnumerable PointFields(ProjectRename plan) => plan.MatrixReferences.Concat(plan.PointReferences).Concat(plan.CylinderReferences).Prepend(new(plan.Kind, plan.ObjectId, "Name")); private void WritePointFields(ProjectRename plan) { foreach (var f in PointFields(plan)) { var entry = Data.Entry(Data.Find(f.EntityType, f.Id) ?? throw new ProjectConflictException("点位配置已删除。")); if ((string)entry.Property(f.Field).CurrentValue != plan.OldName) throw new ProjectConflictException("点位配置存在未保存的修改。"); // Table and column names come solely from the fixed project schema above. string sql = $"UPDATE \"{f.Table}\" SET \"{f.Field}\" = {{0}} WHERE ID = {{1}} AND \"{f.Field}\" IS {{2}}"; if (Data.Database.ExecuteSqlRaw(sql, plan.NewName, f.Id, plan.OldName) != 1) throw new ProjectConflictException("点位或矩阵已改变,请重新预览。"); } } private void AcceptPointFields(ProjectRename plan) { foreach (var f in PointFields(plan)) { var property = Data.Entry(Data.Find(f.EntityType, f.Id)).Property(f.Field); property.CurrentValue = property.OriginalValue = plan.NewName; } } // The editor displays detached copies. Validate displayed names and matrix links before writing, // then update only the reviewed fields to preserve other UI edits. internal Action PreparePointView(ProjectRename plan, IEnumerable points, IEnumerable matrices) { var live = points.Concat(matrices).Select(e => Data.Entry(e)).ToDictionary( e => (e.Metadata.ClrType, (Guid)e.Property("ID").CurrentValue)); var point = new PointField(plan.Kind, plan.ObjectId, "Name"); var edits = PointFields(plan).ToList(); foreach (var (key, entry) in live) { if (key.Item1 == point.EntityType) { var name = (string)entry.Property("Name").CurrentValue; if (key.Item2 != plan.ObjectId && (name == plan.NewName || name == plan.OldName)) throw new ProjectConflictException("编辑器点位列表存在重名,请先保存。"); continue; } foreach (string field in MatrixPointFields) { var value = (string)entry.Property(field).CurrentValue; if (value == plan.NewName || (value == plan.OldName && !edits.Any(f => f.Id == key.Item2 && f.Field == field))) throw new ProjectConflictException("编辑器矩阵引用存在更改,请先保存。"); } } var properties = edits.Select(f => { if (!live.TryGetValue((f.EntityType, f.Id), out var entry) || (string)entry.Property(f.Field).CurrentValue != plan.OldName) throw new ProjectConflictException("编辑器点位或矩阵引用已改变,请先保存。"); return entry.Property(f.Field); }).ToList(); return () => properties.ForEach(p => p.CurrentValue = plan.NewName); } }