605 lines
20 KiB
C#
605 lines
20 KiB
C#
using EPLAN.Harness.Common.Extensions;
|
||
using EPLAN.Harness.Core.Controls;
|
||
using EPLAN.Harness.Core.Settings;
|
||
using EPLAN.Harness.ProjectCore.Report;
|
||
using Sinvo.EplanHpD.Plugin.WPFUI.Enum;
|
||
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
|
||
using Sinvo.EplanHpD.Plugin.WPFUI.Utils;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Controls;
|
||
using static Sinvo.EplanHpD.Plugin.WPFUI.MainWindow;
|
||
|
||
namespace Sinvo.EplanHpD.Plugin.WPFUI.ViewModel;
|
||
|
||
public partial class MainViewModel : INotifyPropertyChanged
|
||
{
|
||
#region Properties
|
||
public UpdateDataGridColumns UpdateDataGridColumns;
|
||
|
||
private ObservableCollection<ReportModel> data;
|
||
private ObservableCollection<StuffedDataModel> stuffedData;
|
||
private ObservableCollection<ExportModel> exportData;
|
||
private ObservableCollection<DataGridColumn> dataColumns;
|
||
private ObservableCollection<StuffedDataModel> searchedData;
|
||
public ObservableCollection<ReportModel> Data
|
||
{
|
||
get => data;
|
||
set
|
||
{
|
||
data = value;
|
||
OnPropertyChanged(nameof(Data));
|
||
}
|
||
}
|
||
|
||
public ObservableCollection<StuffedDataModel> StuffedData
|
||
{
|
||
get => stuffedData;
|
||
set
|
||
{
|
||
stuffedData = value;
|
||
OnPropertyChanged(nameof(StuffedData));
|
||
}
|
||
}
|
||
|
||
public ObservableCollection<ExportModel> ExportData
|
||
{
|
||
get => exportData;
|
||
set
|
||
{
|
||
exportData = value;
|
||
OnPropertyChanged(nameof(ExportData));
|
||
}
|
||
}
|
||
|
||
public ObservableCollection<StuffedDataModel> SearchedData
|
||
{
|
||
get => searchedData;
|
||
set
|
||
{
|
||
searchedData = value;
|
||
OnPropertyChanged(nameof(SearchedData));
|
||
}
|
||
}
|
||
|
||
public ObservableCollection<DataGridColumn> DataColumns
|
||
{
|
||
get => dataColumns;
|
||
set
|
||
{
|
||
dataColumns = value;
|
||
OnPropertyChanged(nameof(DataColumns));
|
||
}
|
||
}
|
||
|
||
private bool _isRequireCe;
|
||
public bool IsRequireCe
|
||
{
|
||
get => _isRequireCe;
|
||
set
|
||
{
|
||
_isRequireCe = value;
|
||
OnPropertyChanged(nameof(IsRequireCe));
|
||
}
|
||
}
|
||
private string _projectNo;
|
||
public string ProjectNo
|
||
{
|
||
get => _projectNo;
|
||
set
|
||
{
|
||
_projectNo = value;
|
||
OnPropertyChanged(nameof(ProjectNo));
|
||
}
|
||
}
|
||
|
||
private bool _isUseDiscoloration = true;
|
||
/// <summary>
|
||
/// 是否使用变色管
|
||
/// </summary>
|
||
public bool IsUseDiscoloration
|
||
{
|
||
get => _isUseDiscoloration;
|
||
set
|
||
{
|
||
_isUseDiscoloration = value;
|
||
OnPropertyChanged(nameof(IsUseDiscoloration));
|
||
}
|
||
}
|
||
private string _mechanismNo;
|
||
/// <summary>
|
||
/// 机构号
|
||
/// </summary>
|
||
public string MechanismNo
|
||
{
|
||
get => _mechanismNo;
|
||
set
|
||
{
|
||
_mechanismNo = value;
|
||
OnPropertyChanged(nameof(MechanismNo));
|
||
}
|
||
}
|
||
private string _mechanismName;
|
||
/// <summary>
|
||
/// 机构名称
|
||
/// </summary>
|
||
public string MechanismName
|
||
{
|
||
get => _mechanismName;
|
||
set
|
||
{
|
||
_mechanismName = value;
|
||
OnPropertyChanged(nameof(MechanismName));
|
||
}
|
||
}
|
||
|
||
private bool _onlyShowError;
|
||
/// <summary>
|
||
/// 机构名称
|
||
/// </summary>
|
||
public bool OnlyShowError
|
||
{
|
||
get => _onlyShowError;
|
||
set
|
||
{
|
||
_onlyShowError = value;
|
||
OnPropertyChanged(nameof(OnlyShowError));
|
||
}
|
||
}
|
||
|
||
private WireFlagType _flagType;
|
||
public WireFlagType FlagType
|
||
{
|
||
get => _flagType;
|
||
set
|
||
{
|
||
_flagType = value;
|
||
OnPropertyChanged(nameof(FlagType));
|
||
FormUISettings.Instance.SetValue("FlagType", $"{FlagType}");
|
||
FormUISettings.Instance.SaveSingleton();
|
||
}
|
||
}
|
||
|
||
private ExportFileNameType _nameType;
|
||
public ExportFileNameType NameType
|
||
{
|
||
get => _nameType;
|
||
set
|
||
{
|
||
_nameType = value;
|
||
OnPropertyChanged(nameof(NameType));
|
||
FormUISettings.Instance.SetValue("NameType", $"{NameType}");
|
||
FormUISettings.Instance.SaveSingleton();
|
||
}
|
||
}
|
||
private string _customFileName;
|
||
public string CustomFileName
|
||
{
|
||
get => _customFileName;
|
||
set
|
||
{
|
||
_customFileName = value;
|
||
OnPropertyChanged(nameof(CustomFileName));
|
||
}
|
||
}
|
||
private bool _toSourceAndMinSelf;
|
||
public bool ToSourceAndMinSelf
|
||
{
|
||
get => _toSourceAndMinSelf;
|
||
set
|
||
{
|
||
_toSourceAndMinSelf = value;
|
||
OnPropertyChanged(nameof(ToSourceAndMinSelf));
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
public Task<List<DataGridTextColumn>> GetColumns(List<ReportColumn> columns)
|
||
{
|
||
var dataColumns = new List<DataGridTextColumn>();
|
||
foreach (var column in columns)
|
||
{
|
||
//Trace.WriteLine($"/// {column.Text}\r\n public string {column.ID} {{get;set;}} ");
|
||
|
||
dataColumns.Add(new DataGridTextColumn()
|
||
{
|
||
Header = column.Text,
|
||
Binding = new System.Windows.Data.Binding(column.ID)
|
||
});
|
||
}
|
||
return Task.FromResult(dataColumns);
|
||
}
|
||
private void LoadSavedUISettings()
|
||
{
|
||
var savedFlagType = FormUISettings.Instance.GetValue("FlagType") ?? WireFlagType.Dual;
|
||
if (System.Enum.IsDefined(typeof(WireFlagType), savedFlagType))
|
||
{
|
||
FlagType = (WireFlagType)System.Enum.Parse(typeof(WireFlagType), savedFlagType.ToString());
|
||
}
|
||
else
|
||
{
|
||
FlagType = WireFlagType.Dual;
|
||
}
|
||
var savedNameType = FormUISettings.Instance.GetValue("NameType") ?? ExportFileNameType.Mechanism;
|
||
if (System.Enum.IsDefined(typeof(ExportFileNameType), savedNameType))
|
||
{
|
||
NameType = (ExportFileNameType)System.Enum.Parse(typeof(ExportFileNameType), savedNameType.ToString());
|
||
}
|
||
else
|
||
{
|
||
NameType = ExportFileNameType.Mechanism;
|
||
}
|
||
}
|
||
public Task<List<ReportModel>> LoadReportDataAsync(IEnumerable<BaseReportEntry> data, List<ReportColumn> columns)
|
||
{
|
||
LoadSavedUISettings();
|
||
|
||
var reportDatas = new ConcurrentBag<ReportModel>();
|
||
//foreach (var entry in data)
|
||
data.Where(it =>
|
||
it.OrigOcc == "OccWire"
|
||
&& !(it?.Properties["WireName"]?.GetDisplayValue()?.StartsWith("导线") ?? false)
|
||
)
|
||
.AsParallel()
|
||
.WithDegreeOfParallelism(8)
|
||
.ForAll(entry =>
|
||
{
|
||
var obj = new ReportModel();
|
||
foreach (var column in columns)
|
||
{
|
||
if (!entry.Properties.ContainsKey(column.ColumnID)) continue;
|
||
var value = entry.Properties[column.ColumnID].GetDisplayValue();
|
||
|
||
var property = typeof(ReportModel).GetProperty(column.ID);
|
||
property?.SetValue(obj, value ?? "");
|
||
|
||
}
|
||
obj.OccPartId = entry.ID;
|
||
reportDatas.Add(obj);
|
||
}
|
||
);
|
||
return Task.FromResult(reportDatas.ToList());
|
||
}
|
||
/// <summary>
|
||
/// 清理数据
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
public async Task<List<StuffedDataModel>> StuffData(List<ReportModel> datas)
|
||
{
|
||
var
|
||
stuffedDatas = new ConcurrentBag<StuffedDataModel>();
|
||
//var isAllCe = true;
|
||
//var isL1 = false;
|
||
//var isL2 = false;
|
||
//var isL3 = false;
|
||
//var isPe = false;
|
||
//foreach (var item in Data)
|
||
datas.AsParallel().WithDegreeOfParallelism(8).ForAll(item =>
|
||
{
|
||
try
|
||
{
|
||
if (item == null) return;
|
||
|
||
var data = new StuffedDataModel();
|
||
|
||
data.OccPartId = item.OccPartId;
|
||
|
||
// 不再识别印记内容 取导线名称 20250327
|
||
var imprint = item.WireName;
|
||
|
||
if (!string.IsNullOrEmpty(imprint) && imprint.Contains("&"))
|
||
{
|
||
var splitImprint = imprint.Split('&');
|
||
if (splitImprint != null)
|
||
{
|
||
// 同时将印记内容拆分成两部分 imprint 只取第一部分
|
||
data.WireNumber = imprint = splitImprint[0];
|
||
data.DiscolorationDesc = splitImprint[1];
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
data.WireNumber = imprint;
|
||
}
|
||
data.Imprint = imprint;
|
||
data.WireColor = GetWireColorCode(item.DisplayColor, item.DisplayStripeColor);
|
||
|
||
data.WireModel = item.PartNumber.Contains(":") ? item.PartNumber.Split(':')[0] : item.PartNumber;
|
||
|
||
data.WireCode = item.ERPNr;
|
||
data.WireName = item.WireName;
|
||
|
||
data.FrontTerminalModel = item.ConnectorFrom?.Contains(":") ?? false ? item.ConnectorFrom.Split(':')[0] : item.ConnectorFrom ?? "";
|
||
data.FrontTerminalMaterialCode = item.ConnectorFromERPNr;
|
||
|
||
data.FrontStripLength = item.StripLengthFrom;
|
||
|
||
data.RearTerminalModel = item.ConnectorTo?.Contains(":") ?? false ? item.ConnectorTo.Split(':')[0] : item.ConnectorTo ?? "";
|
||
|
||
data.RearTerminalMaterialCode = item.ConnectorToERPNr;
|
||
|
||
data.RearStripLength = item.StripLengthTo;
|
||
|
||
//MID(输出报表!J2,1,FIND(" mm2",输出报表!J2)-1)
|
||
data.CrossSection = item.CoreDiameter.Replace(" mm2", "").Trim();
|
||
//=ROUND(MID(输出报表!H2,1,FIND(" mm",输出报表!H2)-1),0)+0
|
||
if (double.TryParse(item.Length.Replace(" mm", "").Trim(), out double wireLength))
|
||
data.WireLength = $"{Math.Round(wireLength, 0)}";
|
||
else
|
||
{
|
||
data.WireLength = string.Empty;
|
||
}
|
||
//data.WireNumber = item.Imprint;
|
||
data.Quantity = 1;
|
||
//"RV-"&A2&"-"&"1×"&J2&"-"&L2&"-"&"16×N2"&"-"&M2
|
||
//data.Model = $@"RV-{data.WireColor}-1×【Replace】{data.CrossSection}-{data.WireLength}-16×N2-{data.WireNumber}";
|
||
//data.Model = string.Format("RV-{0}-1{4}{1}-{2}-16×N2-{3}", data.WireColor, data.CrossSection, data.WireLength, data.WireNumber, "×");
|
||
data.Model = GenWireModel(data.WireColor, data.CrossSection, data.WireLength, data.WireNumber);
|
||
//data.IsChecked = true;
|
||
if (!string.IsNullOrEmpty(data.DiscolorationDesc))
|
||
{
|
||
var results = ExcelHelper.GetInsulationSoftSleeveTable(data.DiscolorationDesc);
|
||
var insulationModels = results as InsulationModel[] ?? results.ToArray();
|
||
if (insulationModels.Any())
|
||
{
|
||
data.Insulation = insulationModels.First();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
data.Insulation = new InsulationModel();
|
||
}
|
||
var numberTubeModel = GetNumberTubeSpecification(data.CrossSection, data.WireColor);
|
||
var numberTube = ExcelHelper.GetNumberTube(numberTubeModel);
|
||
if (numberTube != null)
|
||
{
|
||
|
||
data.NumberTubeSpec = numberTube.TubeModel;
|
||
data.NumberTubeMaterialNo = numberTube.TubeMaterialCode;
|
||
}
|
||
|
||
{
|
||
if (data != null)
|
||
stuffedDatas.Add(data);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Trace.WriteLine($"{ex}");
|
||
}
|
||
});
|
||
|
||
|
||
//IsRequireCe = isAllCe;
|
||
//IsUseDiscoloration = isL1 && isL2 && isL3 && isPe;
|
||
//Trace.WriteLine($"isAllCE: {isAllCe}");
|
||
//Trace.WriteLine($"isSDIProject: {isL1 && isL2 && isL3 && isPe}");
|
||
|
||
return await Task.FromResult<List<StuffedDataModel>>([.. stuffedDatas]);
|
||
}
|
||
/// <summary>
|
||
/// 获取颜色代号
|
||
/// </summary>
|
||
/// <param name="displayColor">L3</param>
|
||
/// <param name="displayStripeColor">M3</param>
|
||
/// <returns></returns>
|
||
private static string GetWireColorCode(string displayColor, string displayStripeColor = null)
|
||
{
|
||
/*
|
||
=IF(AND(输出报表!L3="黄色"
|
||
输出报表!M3="绿色")
|
||
"GNYE"
|
||
IF(AND(输出报表!L3="蓝色"
|
||
输出报表!M3="白色")
|
||
"BUWH"
|
||
IF(输出报表!L3="红色"
|
||
"RD"
|
||
IF(输出报表!L3="黄色"
|
||
"YE"
|
||
IF(输出报表!L3="蓝色"
|
||
"BU"
|
||
IF(输出报表!L3="绿色"
|
||
"GN"
|
||
IF(输出报表!L3="黑色"
|
||
"BK"
|
||
IF(输出报表!L3="灰色"
|
||
"GR"
|
||
IF(输出报表!L3="白色"
|
||
"WH"
|
||
IF(输出报表!L3="棕色"
|
||
"BN"
|
||
IF(输出报表!L3="橙色"
|
||
"OG"
|
||
IF(输出报表!L3="紫色"
|
||
"VT"
|
||
IF(输出报表!L3="绿/黄双色"
|
||
"GNYE"
|
||
IF(输出报表!L3="浅蓝色"
|
||
"LBU"
|
||
))))))))))))))
|
||
*/
|
||
string colorCode;
|
||
|
||
if (displayColor == "黄色" && displayStripeColor == "绿色")
|
||
{
|
||
return "GNYE";
|
||
}
|
||
else if (displayColor == "蓝色" && displayStripeColor == "白色")
|
||
{
|
||
return "BUWH";
|
||
}
|
||
else if (displayColor == "浅蓝色")
|
||
{
|
||
return "LBU";
|
||
}
|
||
else
|
||
{
|
||
colorCode = displayColor switch
|
||
{
|
||
"红色" => "RD",
|
||
"黄色" => "YE",
|
||
"蓝色" => "BU",
|
||
"绿色" => "GN",
|
||
"黑色" => "BK",
|
||
"灰色" => "GR",
|
||
"白色" => "WH",
|
||
"棕色" => "BN",
|
||
"橙色" => "OG",
|
||
"紫色" => "VT",
|
||
"绿/黄双色" => "GNYE",
|
||
"浅蓝色" => "BU",
|
||
_ => "",
|
||
};
|
||
}
|
||
return colorCode;
|
||
}
|
||
/// <summary>
|
||
/// 获取号码管规格
|
||
/// </summary>
|
||
public string GetNumberTubeSpecification(string crossSection, string colorCode)
|
||
{
|
||
if (double.TryParse(crossSection, out double crossSectionDouble))
|
||
if (crossSectionDouble >= 25)
|
||
{
|
||
if (colorCode == "YE") return "黄色热缩管φ12/定制";
|
||
if (colorCode == "GN") return "绿色热缩管φ12/定制";
|
||
if (colorCode == "RD") return "红色热缩管φ12/定制";
|
||
if (colorCode == "BU") return "蓝色热缩管φ12/定制";
|
||
if (colorCode == "GNYE") return "黄绿色热缩管φ12/定制";
|
||
if (colorCode == "BK") return "白色号码管φ8/定制";
|
||
}
|
||
//else if (crossSection == "0.3" || crossSection == "0.5" || crossSection == "0.75" || crossSection == "1")
|
||
else if (crossSectionDouble < 1.5)
|
||
{
|
||
return "白色号码管φ2.5/定制";
|
||
}
|
||
//else if (crossSection == "1.5")
|
||
else if (crossSectionDouble >= 1.5 && crossSectionDouble < 2.5)
|
||
{
|
||
return "白色号码管φ3/定制";
|
||
}
|
||
//else if (crossSection == "2.5")
|
||
else if (crossSectionDouble >= 2.5 && crossSectionDouble < 4)
|
||
|
||
{
|
||
return "白色号码管φ4/定制";
|
||
}
|
||
//else if (crossSection == "4")
|
||
else if (crossSectionDouble >= 4 && crossSectionDouble < 6)
|
||
|
||
{
|
||
return "白色号码管φ5/定制";
|
||
}
|
||
//else if (crossSection == "6")
|
||
else if (crossSectionDouble >= 6 && crossSectionDouble < 10)
|
||
|
||
{
|
||
return "白色号码管φ6/定制";
|
||
}
|
||
//else if (crossSection == "10")
|
||
else if (crossSectionDouble >= 10 && crossSectionDouble < 25)
|
||
|
||
{
|
||
return "白色号码管φ8/定制";
|
||
}
|
||
|
||
return string.Empty;
|
||
}
|
||
|
||
public event PropertyChangedEventHandler PropertyChanged;
|
||
|
||
private void OnPropertyChanged(string propertyName)
|
||
{
|
||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||
}
|
||
|
||
|
||
public void GenExportData()
|
||
{
|
||
if (StuffedData.Any(it => !it.IsIgnore && (!it.IsChecked || it.IsError)))
|
||
{
|
||
FlexMessageBox.Info("存在未检查通过或是未做检查,不允许生成!");
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(MechanismNo) || string.IsNullOrEmpty(MechanismName))
|
||
{
|
||
FlexMessageBox.Info("机构信息未完善,不允许生成!");
|
||
return;
|
||
}
|
||
ExportData = [];
|
||
int seqNo = 1;
|
||
var dataList = new List<ExportModel>();
|
||
var nowDateStr = DateTime.Now;//.ToString("yyyy-MM-dd");
|
||
var nowDeliveryDateStr = DateTime.Now.AddDays(7);//.ToString("yyyy-MM-dd");
|
||
//foreach (var stuffedDataModel in StuffedData)
|
||
StuffedData.AsParallel().WithDegreeOfParallelism(1).ForAll(stuffedDataModel =>
|
||
{
|
||
//var numberTubeSpec = GetNumberTubeSpecification(stuffedDataModel.CrossSection, stuffedDataModel.WireColor);
|
||
var exportModel = new ExportModel
|
||
{
|
||
SeqNo = seqNo.ToString(),
|
||
MechanismName = MechanismName,
|
||
MechanismNo = MechanismNo,
|
||
DrawNo = stuffedDataModel.Model,
|
||
MaterialCode = "",
|
||
WireOrTubeSpec = stuffedDataModel.WireModel,
|
||
WireOrTubeMaterialNo = stuffedDataModel.WireCode,
|
||
WireOrTubeLength = Math.Round(double.Parse(stuffedDataModel.WireLength), 0),
|
||
NumberTubeSpec = stuffedDataModel.NumberTubeSpec,
|
||
NumberTubeMaterialNo = stuffedDataModel.NumberTubeMaterialNo,
|
||
NumberTubeContent = stuffedDataModel.Imprint,
|
||
FrontTerminalModel = stuffedDataModel.FrontTerminalModel,
|
||
FrontTerminalMaterialNo = stuffedDataModel.FrontTerminalMaterialCode,
|
||
FrontTerminalStripLength = stuffedDataModel.FrontStripLength,
|
||
RearTerminalModel = stuffedDataModel.RearTerminalModel,
|
||
RearTerminalMaterialNo = stuffedDataModel.RearTerminalMaterialCode,
|
||
RearTerminalStripLength = stuffedDataModel.RearStripLength,
|
||
InsulationModel = stuffedDataModel.Insulation?.Specification,
|
||
InsulationMaterialNo = stuffedDataModel.Insulation?.MaterialCode,
|
||
InsulationQuantity = string.IsNullOrEmpty(stuffedDataModel.Insulation?.MaterialCode) ? "" : "2",
|
||
OrderDate = nowDateStr,
|
||
DeliveryDate = nowDeliveryDateStr,
|
||
|
||
IsIgnoreError = stuffedDataModel.IsIgnore ? "是" : string.Empty
|
||
};
|
||
seqNo++;
|
||
dataList.Add(exportModel);
|
||
}
|
||
);
|
||
if (dataList != null && dataList.Any())
|
||
{
|
||
dataList.ForEach(ExportData.Add);
|
||
}
|
||
}
|
||
public void SearchByWireName(string wireName)
|
||
{
|
||
SearchedData.Clear();
|
||
if (string.IsNullOrEmpty(wireName))
|
||
{
|
||
stuffedData.ForEach(SearchedData.Add);
|
||
}
|
||
else
|
||
{
|
||
stuffedData.Where(it => it.WireName.Contains(wireName)
|
||
|| (it.RearTerminalModel?.Contains(wireName) ?? false)
|
||
|| (it.FrontTerminalModel?.Contains(wireName) ?? false)
|
||
)
|
||
.Where(it => it.IsError == OnlyShowError || !OnlyShowError)
|
||
.ForEach(SearchedData.Add);
|
||
}
|
||
}
|
||
|
||
private string GenWireModel(string wireColor, string crossSection, string wireLength, string wireNumber)
|
||
{
|
||
return $@"RV-{wireColor}-1×{crossSection}-{wireLength}-16×N2-{wireNumber}";
|
||
}
|
||
|
||
} |