133 lines
6.0 KiB
C#
133 lines
6.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using PLCSharp.VVMs.Homepage;
|
|
using PLCSharp.VVMs.MotionController;
|
|
using PLCSharp.VVMs.Vision;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace PLCSharp.Models;
|
|
|
|
public sealed record NestedName(string Kind, string OwnerName, string SegmentName)
|
|
{
|
|
internal string Table => Kind switch
|
|
{
|
|
"ControlMeasurements" => "CustomControls", "VisionLocalVariables" => "VisionFunctions",
|
|
"InterpolationSegments" or "InterpolationPoints" => "InterpolationGroups",
|
|
_ => throw new ArgumentException("未知子对象类型。")
|
|
};
|
|
internal string Column => Kind switch
|
|
{
|
|
"ControlMeasurements" => "RangeValues", "VisionLocalVariables" => "VisionParams", _ => "Interpolations"
|
|
};
|
|
internal JsonNode OwnerRow(JsonNode doc, Guid id) => doc[Table].AsArray().Single(r => Guid.Parse((string)r["ID"]) == id);
|
|
internal static T Select<T>(IEnumerable<T> list, Func<T, string> name, string before, string after)
|
|
{
|
|
var matches = list.Where(o => name(o) == before).Take(2).ToList();
|
|
if (matches.Count != 1 || after != null && list.Any(o => name(o) == after))
|
|
throw new ProjectConflictException("子对象不存在、位置已改变或在所属范围内重名。");
|
|
return matches[0];
|
|
}
|
|
|
|
private void Patch(JsonNode column, string before, string after, bool write)
|
|
{
|
|
JsonNode segment = null;
|
|
var list = Kind == "VisionLocalVariables" ? column["Variables"].AsArray() : column.AsArray();
|
|
if (Kind == "InterpolationPoints")
|
|
{
|
|
segment = Select(list, r => (string)r["Name"], SegmentName, null);
|
|
list = JsonNode.Parse((string)segment["SerializedInterpolations"]).AsArray();
|
|
}
|
|
var target = Select(list, r => (string)r["Name"], before, after);
|
|
if (!write) return;
|
|
target["Name"] = after;
|
|
if (segment != null) segment["SerializedInterpolations"] = list.ToJsonString();
|
|
}
|
|
|
|
internal void Check(ProjectRename plan, JsonNode doc, string before, string after) =>
|
|
Patch(OwnerRow(doc, plan.ObjectId)[Column], before, after, false);
|
|
internal void EditDocument(ProjectRename plan, JsonNode doc) =>
|
|
Patch(OwnerRow(doc, plan.ObjectId)[Column], plan.OldName, plan.NewName, true);
|
|
internal string EditColumn(string raw, ProjectRename plan)
|
|
{
|
|
var column = JsonNode.Parse(raw);
|
|
Patch(column, plan.OldName, plan.NewName, true);
|
|
return column.ToJsonString();
|
|
}
|
|
}
|
|
|
|
public sealed partial class ProjectDirectory
|
|
{
|
|
internal NestedName DescribeMember(string kind, Guid id, string segmentName)
|
|
{
|
|
var member = new NestedName(kind, null, segmentName);
|
|
var target = ProjectInspection.Query(this, "get", new() { ["kind"] = member.Table, ["id"] = id.ToString() });
|
|
return member with { OwnerName = (string)target["Name"] };
|
|
}
|
|
|
|
private ProjectRename PlanNestedRename(string kind, Guid id, string before, string segment, string after)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(before) || string.IsNullOrWhiteSpace(after) || after != after.Trim() || after == before)
|
|
throw new ArgumentException("请给出子对象的原名称及不同的新名称。");
|
|
var nested = DescribeMember(kind, id, segment);
|
|
var data = JsonNode.Parse(baseline[ProjectRename.PathFor(kind, Guid.Empty)]);
|
|
var row = nested.OwnerRow(data, id);
|
|
var recipe = Guid.Parse((string)row["RecipeID"]);
|
|
var plan = PlanReferences(kind, id, recipe, before, after, nested);
|
|
nested.Check(plan, data, before, after);
|
|
if (kind == "VisionLocalVariables") PlanLocalBindings(plan, row);
|
|
return plan;
|
|
}
|
|
|
|
internal static Action PrepareNestedView(ProjectRename plan, object owner)
|
|
{
|
|
Action rename;
|
|
if (owner is VisionFunction f)
|
|
{
|
|
var v = NestedName.Select(f.Params.Variables, x => x.Name, plan.OldName, plan.NewName);
|
|
rename = () => v.Name = plan.NewName;
|
|
}
|
|
else if (owner is CustomControl control)
|
|
{
|
|
var v = NestedName.Select(control.RangeValues, x => x.Name, plan.OldName, plan.NewName);
|
|
rename = () => v.Name = plan.NewName;
|
|
}
|
|
else
|
|
{
|
|
var group = (InterpolationGroup)owner;
|
|
if (plan.Kind == "InterpolationSegments")
|
|
{
|
|
var v = NestedName.Select(group.Interpolations, x => x.Name, plan.OldName, plan.NewName);
|
|
rename = () => v.Name = plan.NewName;
|
|
}
|
|
else
|
|
{
|
|
var segment = NestedName.Select(group.Interpolations, x => x.Name, plan.Nested.SegmentName, null);
|
|
var v = NestedName.Select(segment.InterpolationPoints, x => x.Name, plan.OldName, plan.NewName);
|
|
rename = () => v.Name = plan.NewName;
|
|
}
|
|
}
|
|
var steps = owner is VisionFunction function ? PrepareSteps(plan, [function]) : null;
|
|
return () => { rename(); steps?.Invoke(); };
|
|
}
|
|
|
|
private Action WriteNested(ProjectRename plan)
|
|
{
|
|
var n = plan.Nested;
|
|
var id = plan.ObjectId;
|
|
object owner = n.Table switch
|
|
{
|
|
"CustomControls" => Data.CustomControls.Find(id), "VisionFunctions" => Data.VisionFunctions.Find(id),
|
|
_ => Data.InterpolationGroups.Find(id)
|
|
};
|
|
var entry = Data.Entry(owner);
|
|
var property = entry.Property("Serialized" + n.Column);
|
|
var raw = (string)entry.GetDatabaseValues()[property.Metadata];
|
|
var next = n.EditColumn(raw, plan);
|
|
// Table and column are fixed by NestedName, all data values are parameters.
|
|
string sql = $"UPDATE \"{n.Table}\" SET \"{n.Column}\" = {{0}} WHERE ID = {{1}} AND \"{n.Column}\" = {{2}}";
|
|
var update = PrepareNestedView(plan with { VisionReferences = [] }, owner);
|
|
if (Data.Database.ExecuteSqlRaw(sql, next, id, raw) != 1) throw new ProjectConflictException("子对象配置已改变。");
|
|
var steps = WriteVisionSteps(plan);
|
|
return () => { update(); property.OriginalValue = property.CurrentValue; steps(); };
|
|
}
|
|
}
|