261 lines
9.5 KiB
C#
261 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace CadParamPluging.Common
|
|
{
|
|
[Serializable]
|
|
public class DropdownDisplayNameOverride
|
|
{
|
|
public string Category { get; set; }
|
|
public string Value { get; set; }
|
|
public string DisplayName { get; set; }
|
|
}
|
|
|
|
public static class DropdownOptionCategory
|
|
{
|
|
public const string DeliveryStatus = "DeliveryStatus";
|
|
public const string ProcessMethod = "ProcessMethod";
|
|
public const string StructuralFeature = "StructuralFeature";
|
|
public const string SpecialCondition = "SpecialCondition";
|
|
}
|
|
|
|
public class DropdownOptions
|
|
{
|
|
public List<string> DeliveryStatuses { get; set; }
|
|
public List<string> ProcessMethods { get; set; }
|
|
public List<string> StructuralFeatures { get; set; }
|
|
public List<string> SpecialConditions { get; set; }
|
|
public List<DropdownDisplayNameOverride> DisplayNameOverrides { get; set; }
|
|
|
|
|
|
public DropdownOptions()
|
|
{
|
|
DeliveryStatuses = new List<string>();
|
|
ProcessMethods = new List<string>();
|
|
StructuralFeatures = new List<string>();
|
|
SpecialConditions = new List<string>();
|
|
DisplayNameOverrides = new List<DropdownDisplayNameOverride>();
|
|
}
|
|
|
|
public static DropdownOptions CreateDefault()
|
|
{
|
|
return new DropdownOptions
|
|
{
|
|
DeliveryStatuses = new List<string> { "毛料态", "车加工" },
|
|
ProcessMethods = new List<string> { "轧制", "自由锻" },
|
|
StructuralFeatures = new List<string> { "环形", "方体", "轴杆", "饼盘" },
|
|
SpecialConditions = new List<string> { "无", "中心冲孔", "非中心冲孔", "有圆头", "圆轴", "方轴", "无圆头" }
|
|
};
|
|
}
|
|
|
|
public DropdownOptions Clone()
|
|
{
|
|
return new DropdownOptions
|
|
{
|
|
DeliveryStatuses = new List<string>(DeliveryStatuses ?? new List<string>()),
|
|
ProcessMethods = new List<string>(ProcessMethods ?? new List<string>()),
|
|
StructuralFeatures = new List<string>(StructuralFeatures ?? new List<string>()),
|
|
SpecialConditions = new List<string>(SpecialConditions ?? new List<string>()),
|
|
DisplayNameOverrides = (DisplayNameOverrides ?? new List<DropdownDisplayNameOverride>())
|
|
.Where(x => x != null)
|
|
.Select(x => new DropdownDisplayNameOverride
|
|
{
|
|
Category = x.Category,
|
|
Value = x.Value,
|
|
DisplayName = x.DisplayName
|
|
})
|
|
.ToList()
|
|
};
|
|
}
|
|
|
|
public void Normalize()
|
|
{
|
|
DeliveryStatuses = NormalizeList(DeliveryStatuses);
|
|
ProcessMethods = NormalizeList(ProcessMethods);
|
|
StructuralFeatures = NormalizeList(StructuralFeatures);
|
|
SpecialConditions = NormalizeList(SpecialConditions);
|
|
DisplayNameOverrides = NormalizeOverrides(DisplayNameOverrides);
|
|
}
|
|
|
|
public string GetDisplayName(string category, string rawValue)
|
|
{
|
|
var raw = (rawValue ?? string.Empty).Trim();
|
|
if (raw.Length == 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var normalizedCategory = NormalizeCategory(category);
|
|
var match = (DisplayNameOverrides ?? new List<DropdownDisplayNameOverride>())
|
|
.FirstOrDefault(x =>
|
|
x != null
|
|
&& string.Equals(NormalizeCategory(x.Category), normalizedCategory, StringComparison.OrdinalIgnoreCase)
|
|
&& string.Equals((x.Value ?? string.Empty).Trim(), raw, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var display = (match != null ? match.DisplayName : null) ?? raw;
|
|
display = display.Trim();
|
|
return display.Length == 0 ? raw : display;
|
|
}
|
|
|
|
public void SetDisplayNameOverride(string category, string rawValue, string displayName)
|
|
{
|
|
var normalizedCategory = NormalizeCategory(category);
|
|
var raw = (rawValue ?? string.Empty).Trim();
|
|
var display = (displayName ?? string.Empty).Trim();
|
|
if (raw.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (DisplayNameOverrides == null)
|
|
{
|
|
DisplayNameOverrides = new List<DropdownDisplayNameOverride>();
|
|
}
|
|
|
|
DisplayNameOverrides = (DisplayNameOverrides ?? new List<DropdownDisplayNameOverride>())
|
|
.Where(x =>
|
|
x != null
|
|
&& (!string.Equals(NormalizeCategory(x.Category), normalizedCategory, StringComparison.OrdinalIgnoreCase)
|
|
|| !string.Equals((x.Value ?? string.Empty).Trim(), raw, StringComparison.OrdinalIgnoreCase)))
|
|
.ToList();
|
|
|
|
if (display.Length == 0 || string.Equals(display, raw, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return;
|
|
}
|
|
|
|
DisplayNameOverrides.Add(new DropdownDisplayNameOverride
|
|
{
|
|
Category = normalizedCategory,
|
|
Value = raw,
|
|
DisplayName = display
|
|
});
|
|
}
|
|
|
|
public List<string> GetValues(string category)
|
|
{
|
|
switch (NormalizeCategory(category))
|
|
{
|
|
case DropdownOptionCategory.DeliveryStatus:
|
|
return DeliveryStatuses;
|
|
case DropdownOptionCategory.ProcessMethod:
|
|
return ProcessMethods;
|
|
case DropdownOptionCategory.StructuralFeature:
|
|
return StructuralFeatures;
|
|
case DropdownOptionCategory.SpecialCondition:
|
|
return SpecialConditions;
|
|
default:
|
|
return new List<string>();
|
|
}
|
|
}
|
|
|
|
private static List<string> NormalizeList(IEnumerable<string> values)
|
|
{
|
|
var result = new List<string>();
|
|
if (values == null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var raw in values)
|
|
{
|
|
var v = (raw ?? string.Empty).Trim();
|
|
if (v.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (seen.Add(v))
|
|
{
|
|
result.Add(v);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static List<DropdownDisplayNameOverride> NormalizeOverrides(IEnumerable<DropdownDisplayNameOverride> values)
|
|
{
|
|
var result = new List<DropdownDisplayNameOverride>();
|
|
if (values == null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (var raw in values)
|
|
{
|
|
if (raw == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var category = NormalizeCategory(raw.Category);
|
|
var value = (raw.Value ?? string.Empty).Trim();
|
|
var display = (raw.DisplayName ?? string.Empty).Trim();
|
|
if (category.Length == 0 || value.Length == 0 || display.Length == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (string.Equals(value, display, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var key = category + "|" + value;
|
|
if (!seen.Add(key))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result.Add(new DropdownDisplayNameOverride
|
|
{
|
|
Category = category,
|
|
Value = value,
|
|
DisplayName = display
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static string NormalizeCategory(string category)
|
|
{
|
|
var value = (category ?? string.Empty).Trim();
|
|
if (value.Length == 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
if (string.Equals(value, DropdownOptionCategory.DeliveryStatus, StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(value, "DeliveryStatuses", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return DropdownOptionCategory.DeliveryStatus;
|
|
}
|
|
|
|
if (string.Equals(value, DropdownOptionCategory.ProcessMethod, StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(value, "ProcessMethods", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return DropdownOptionCategory.ProcessMethod;
|
|
}
|
|
|
|
if (string.Equals(value, DropdownOptionCategory.StructuralFeature, StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(value, "StructuralFeatures", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return DropdownOptionCategory.StructuralFeature;
|
|
}
|
|
|
|
if (string.Equals(value, DropdownOptionCategory.SpecialCondition, StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(value, "SpecialConditions", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return DropdownOptionCategory.SpecialCondition;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|