543 lines
18 KiB
C#
543 lines
18 KiB
C#
using EPLAN.Harness.Common.Extensions;
|
||
using EPLAN.Harness.Core.Controls;
|
||
using EPLAN.Harness.ProjectCore.Report;
|
||
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;
|
||
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<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;
|
||
/// <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 WireFlagType _flagType;
|
||
public WireFlagType FlagType
|
||
{
|
||
get => _flagType;
|
||
set
|
||
{
|
||
_flagType = value;
|
||
OnPropertyChanged(nameof(FlagType));
|
||
}
|
||
}
|
||
#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);
|
||
}
|
||
|
||
public Task<List<ReportModel>> LoadReportDataAsync(IEnumerable<BaseReportEntry> data, List<ReportColumn> columns)
|
||
{
|
||
var reportDatas = new ConcurrentBag<ReportModel>();
|
||
//foreach (var entry in data)
|
||
data.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);
|
||
if (property != null)
|
||
{
|
||
property.SetValue(obj, value ?? "");
|
||
}
|
||
}
|
||
reportDatas.Add(obj);
|
||
}
|
||
);
|
||
return Task.FromResult(reportDatas.ToList());
|
||
}
|
||
|
||
|
||
|
||
public void LoadReportData(IEnumerable<BaseReportEntry> data, List<ReportColumn> columns)
|
||
{
|
||
DataColumns = [];
|
||
//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)
|
||
// });
|
||
//}
|
||
|
||
//DataColumns.Add(new DataGridTextColumn()
|
||
//{
|
||
// Header = "变色管信息",
|
||
// Binding = new System.Windows.Data.Binding("DiscolorationDesc")
|
||
//});
|
||
|
||
//Data = [];
|
||
//var reportDatas = new ConcurrentBag<ReportModel>();
|
||
////foreach (var entry in data)
|
||
//data.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);
|
||
// if (property != null)
|
||
// {
|
||
// property.SetValue(obj, value ?? "");
|
||
// }
|
||
// }
|
||
// reportDatas.Add(obj);
|
||
// }
|
||
//);
|
||
//if (reportDatas != null && reportDatas.Any())
|
||
//{
|
||
// reportDatas.ToList().ForEach(Data.Add);
|
||
//}
|
||
//UpdateDataGridColumns([.. DataColumns], DataGridType.Originial);
|
||
//StuffedData = [];
|
||
//StuffData().ForEach(item =>
|
||
//{
|
||
// if (item != null)
|
||
// {
|
||
// StuffedData.Add(item);
|
||
// }
|
||
//});
|
||
//OnPropertyChanged(nameof(StuffedDatas));
|
||
|
||
}
|
||
|
||
public async Task<List<StuffedDataModel>> StuffData(List<ReportModel> data)
|
||
{
|
||
var datas = new ConcurrentBag<StuffedDataModel>();
|
||
var isAllCe = true;
|
||
var isL1 = false;
|
||
var isL2 = false;
|
||
var isL3 = false;
|
||
var isPe = false;
|
||
//foreach (var item in Data)
|
||
data.AsParallel().WithDegreeOfParallelism(8).ForAll(item =>
|
||
{
|
||
try
|
||
{
|
||
|
||
|
||
if (item == null) return;
|
||
//var data = item.MemoryClone<ReportModel>();
|
||
//new ReportModel
|
||
//{
|
||
// Project = item.Project,
|
||
// Document = item.Document,
|
||
// CableName = item.CableName,
|
||
// PartNumber = item.PartNumber,
|
||
// ManufacturerCompany = item.ManufacturerCompany,
|
||
// Imprint = item.Imprint,
|
||
// Length = item.Length,
|
||
// OutsideDiameter = item.OutsideDiameter,
|
||
// CoreDiameter = item.CoreDiameter,
|
||
// ERPNr = item.ERPNr,
|
||
// DisplayColor = item.DisplayColor,
|
||
// DisplayStripeColor = item.DisplayStripeColor,
|
||
// WireName = item.WireName,
|
||
// WireTwisted = item.WireTwisted,
|
||
// ConnectorFrom = item.ConnectorFrom,
|
||
// ConnectorFromERPNr = item.ConnectorFromERPNr,
|
||
//};
|
||
|
||
var data = new StuffedDataModel();
|
||
|
||
if (!string.IsNullOrEmpty(item.PartNumber))
|
||
{
|
||
if (!item.PartNumber.Contains("-CE/"))
|
||
{
|
||
isAllCe = false;
|
||
}
|
||
}
|
||
if (!string.IsNullOrEmpty(item.Imprint))
|
||
{
|
||
if (item.Imprint.StartsWith("L1"))
|
||
{
|
||
isL1 = true;
|
||
}
|
||
|
||
if (item.Imprint.StartsWith("L2"))
|
||
{
|
||
isL2 = true;
|
||
}
|
||
|
||
if (item.Imprint.StartsWith("L3"))
|
||
{
|
||
isL3 = true;
|
||
}
|
||
|
||
if (item.Imprint.StartsWith("PE"))
|
||
{
|
||
isPe = true;
|
||
}
|
||
}
|
||
if (!string.IsNullOrEmpty(item.Imprint) && item.Imprint.Contains("&"))
|
||
{
|
||
var splitImprint = item.Imprint.Split('&');
|
||
if (splitImprint != null)
|
||
{
|
||
data.WireNumber = item.Imprint = splitImprint[0];
|
||
data.DiscolorationDesc = splitImprint[1];
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
data.WireNumber = item.Imprint;
|
||
}
|
||
data.Imprint = item.Imprint;
|
||
data.WireColor = GetWireColorCode(item.DisplayColor, item.DisplayStripeColor);
|
||
//if (string.IsNullOrEmpty(data.WireColor))
|
||
//{
|
||
// data.IsError = true;
|
||
// data.CheckedMsg += "线色不符合规范\r\n";
|
||
//}
|
||
data.WireModel = item.PartNumber;
|
||
data.WireCode = item.ERPNr;
|
||
data.WireName = item.WireName;
|
||
//if (string.IsNullOrEmpty(data.WireCode))
|
||
//{
|
||
// data.IsError = true;
|
||
// data.CheckedMsg += "线材编码为空\r\n";
|
||
//}
|
||
data.FrontTerminalModel = item.ConnectorFrom;
|
||
data.FrontTerminalMaterialCode = item.ConnectorFromERPNr;
|
||
//if (string.IsNullOrEmpty(data.FrontTerminalMaterialCode))
|
||
//{
|
||
// data.IsError = true;
|
||
// data.CheckedMsg += "前端子物料编码为空\r\n";
|
||
//}
|
||
data.FrontStripLength = item.StripLengthFrom;
|
||
data.RearTerminalModel = item.ConnectorTo;
|
||
data.RearTerminalMaterialCode = item.ConnectorToERPNr;
|
||
//if (string.IsNullOrEmpty(data.RearTerminalMaterialCode))
|
||
//{
|
||
// data.IsError = true;
|
||
// data.CheckedMsg = "后端子物料编码为空\r\n";
|
||
//}
|
||
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
|
||
data.WireLength = item.Length.Replace(" mm", "").Trim();
|
||
//data.WireNumber = item.Imprint;
|
||
data.Quantity = 1;
|
||
//"RV-"&A2&"-"&"1×"&J2&"-"&L2&"-"&"16×N2"&"-"&M2
|
||
data.Model = $"RV-{data.WireColor}-1x{data.CrossSection}-{data.WireLength}-16×N2-{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();
|
||
}
|
||
|
||
|
||
//if (datas.Any(it => it.Model == data.Model))
|
||
//{
|
||
|
||
// var existsItem = datas.First(it => it.Model == data.Model);
|
||
// if (existsItem != null)
|
||
// {
|
||
// existsItem.Quantity += 1;
|
||
// }
|
||
//}
|
||
//else
|
||
{
|
||
if (data != null)
|
||
datas.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>>([.. datas]);
|
||
}
|
||
/// <summary>
|
||
/// 获取颜色代号
|
||
/// TODO : 改成从excel中读取基本资料
|
||
/// </summary>
|
||
/// <param name="displayColor"></param>
|
||
/// <param name="displayStripeColor"></param>
|
||
/// <returns></returns>
|
||
private static string GetWireColorCode(string displayColor, string displayStripeColor = null)
|
||
{
|
||
|
||
string colorCode;
|
||
|
||
if (displayColor == "黄色" && displayStripeColor == "绿色")
|
||
{
|
||
return "GNYE";
|
||
}
|
||
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 (crossSection == "16")
|
||
{
|
||
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/定制";
|
||
}
|
||
else if (crossSection == "0.3" || crossSection == "0.5" || crossSection == "0.75" || crossSection == "1")
|
||
{
|
||
return "白色号码管φ2.5/定制";
|
||
}
|
||
else if (crossSection == "1.5")
|
||
{
|
||
return "白色号码管φ3/定制";
|
||
}
|
||
else if (crossSection == "2.5")
|
||
{
|
||
return "白色号码管φ4/定制";
|
||
}
|
||
else if (crossSection == "4")
|
||
{
|
||
return "白色号码管φ5/定制";
|
||
}
|
||
else if (crossSection == "6")
|
||
{
|
||
return "白色号码管φ6/定制";
|
||
}
|
||
else if (crossSection == "10")
|
||
{
|
||
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>();
|
||
//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 = numberTubeSpec,
|
||
NumberTubeMaterialNo = ExcelHelper.GetNumberTube(numberTubeSpec),
|
||
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
|
||
};
|
||
seqNo++;
|
||
dataList.Add(exportModel);
|
||
}
|
||
);
|
||
if (dataList != null && dataList.Any())
|
||
{
|
||
dataList.ForEach(ExportData.Add);
|
||
}
|
||
}
|
||
|
||
} |