C00225155-02/C00225155/MegaRobo.C00225155/MegaRobo.C00225155App/MenuViewModels/FlowConfigViewModel.cs

724 lines
28 KiB
C#
Raw Normal View History

2026-04-13 09:12:49 +00:00
using Common;
using Common.Models;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using MegaRobo.C00225155.AppServer;
using MegaRobo.C00225155.DataAccess;
using MegaRobo.C00225155.Entities;
using MegaRobo.Contract;
using MegaRobo.ControlDevices.Models;
using MegaRobo.Entities;
using MegaRobo.WpfInfrastructure.Abstractions;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
namespace MegaRobo.C00225155App.MenuViewModels
{
public class FlowConfigViewModel : ViewModelBaseEx
{
#region 8
public ObservableCollection<int> BoxIndexList { get; set; } = new ObservableCollection<int>();
private int selectedBoxIndex;
public int SelectedBoxIndex
{
get { return selectedBoxIndex; }
set { SetProperty(ref selectedBoxIndex, value); }
}
private BoxTypeEnum selectedboxtype;
public BoxTypeEnum SelectedBoxType
{
get { return selectedboxtype; }
set {
SetProperty(ref selectedboxtype, value);
if (selectedboxtype == BoxTypeEnum._40mL原液工装)
{
BoxIndexList.Clear(); BoxIndexList.Add(1); BoxIndexList.Add(5);
}
else
{
BoxIndexList.Clear(); BoxIndexList.Add(3); BoxIndexList.Add(4);
BoxIndexList.Add(7); BoxIndexList.Add(8);
}
}
}
#endregion
private ObservableCollection<SampleBottleModel> samplelist = new ObservableCollection<SampleBottleModel>();
private ObservableCollection<SourceLiquidBottleModel> liquidlist = new ObservableCollection<SourceLiquidBottleModel>();
private ObservableCollection<SourcePowderBottleModel> powderlist = new ObservableCollection<SourcePowderBottleModel>();
private ObservableCollection<SourcePowderBottleModel> powderlist_125 = new ObservableCollection<SourcePowderBottleModel>();
private WorkService workService;
private IDataAccessService dataAccessService;
public ProjectProperty ProjectPro
{
get => workService.ProjectPro;
set
{
if (workService.ProjectPro != value)
{
workService.ProjectPro = value;
OnPropertyChanged(nameof(ProjectPro));
}
}
}
#region
public ICommand NewProjectCommand { get; set; }
public ICommand OpenProjectCommand { get; set; }
public ICommand SaveAsCommand { get; set; }
public ICommand SaveCommand { get; set; }
public ICommand ExportExcelCommand { get; set; }
public ICommand ImportExcelCommand { get; set; }
#endregion
#region
/// <summary>
/// 更新指定工装的所有配方
/// </summary>
public ICommand UpdateSampleBoxPropertyCommand { get; set; }
/// <summary>
/// 清空指定工装的所有配方
/// </summary>
public ICommand ClearSampleBoxPropertyCommand { get; set; }
/// <summary>
/// 瓶子都置为空
/// </summary>
public ICommand ResetAllSampleBottlesCommand { get; set; }
/// <summary>
/// 瓶子都置为满瓶子
/// </summary>
public ICommand FillAllSampleBottlesCommand { get; set; }
public ICommand ResetSampleBottlesCommand { get; set; }
#endregion
#region Tip头工装
/// <summary>
/// Tip头工装置为空
/// </summary>
public ICommand ResetAllTipHeadCommand { get; set; }
/// <summary>
/// Tip头工装置为满
/// </summary>
public ICommand FillAllTipHeadCommand { get; set; }
#endregion
// 命令加载工装参数为BoxTypeEnum
public ICommand LoadBoxCommand { get; set; }
public FlowConfigViewModel()
{
}
public override void Initialize(ICoreService coreService, string title, params object[] args)
{
base.Initialize(coreService, title, args);
samplelist = new ObservableCollection<SampleBottleModel>();
for (int i = 0; i < 12; i++)
{
samplelist.Add(new SampleBottleModel()
{
SampleBottleLidState = SampleBottleLidStateEnum.Close,
PosId_InBox = i + 1,
HaveBottle = true,
bDoseFinish = false,
SNCode = string.Empty,
});
}
liquidlist = new ObservableCollection<SourceLiquidBottleModel>();
for (int i = 0; i < 8; i++)
{
liquidlist.Add(new SourceLiquidBottleModel()
{
LiquidBottleLidState = SourceBottleLidStateEnum.Close,
PosId_InBox = i + 1,
HaveBottle = true,
RemainVolume = 0,
SNCode = string.Empty,
SourceLiquidBottleState = SourceBottleStateEnum.Idl,
});
}
powderlist = new ObservableCollection<SourcePowderBottleModel>();
for (int i = 0; i < 4; i++)
{
powderlist.Add(new SourcePowderBottleModel()
{
PosId_InBox = i + 1,
HaveBottle = true,
RemainWeight = 0,
SNCode = string.Empty,
SourcePowderBottleState = SourceBottleStateEnum.Idl,
});
}
powderlist_125 = new ObservableCollection<SourcePowderBottleModel>();
for (int i = 0; i < 2; i++)
{
powderlist_125.Add(new SourcePowderBottleModel()
{
PosId_InBox = i + 1,
HaveBottle = true,
RemainWeight = 0,
SNCode = string.Empty,
SourcePowderBottleState = SourceBottleStateEnum.Idl,
});
}
//InitialProject();
}
protected override void LoadServices()
{
base.LoadServices();
this.workService = this.CoreService.GetServiceInstance<WorkService>();
if (workService != null)
{
workService.ProjectProChanged += OnProjectProChanged;
}
this.dataAccessService = this.CoreService.GetServiceInstance<IDataAccessService>();
}
public new event PropertyChangedEventHandler PropertyChanged;
// 当ProjectPro变更时调用此方法
private void OnProjectProChanged()
{
// 通知视图ProjectPro属性已变更
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ProjectPro)));
}
protected override void LoadDatas()
{
base.LoadDatas();
BoxIndexList.Clear(); //BoxIndexList.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
}
protected override void Register()
{
base.Register();
NewProjectCommand = new RelayCommand(NewProject);
OpenProjectCommand = new RelayCommand(OpenProject);
SaveAsCommand = new RelayCommand(SaveAsProject);
SaveCommand = new RelayCommand(SaveProject);
ImportExcelCommand = new RelayCommand(ImportExcel);
ExportExcelCommand = new RelayCommand(ExportExcel);
UpdateSampleBoxPropertyCommand = new RelayCommand<int>(UpdateSampleBoxProperty);
ClearSampleBoxPropertyCommand = new RelayCommand<int>(ClearSampleBoxProperty);
ResetAllSampleBottlesCommand = new RelayCommand<int>(ClearSampleBottles);
FillAllSampleBottlesCommand = new RelayCommand<int>(FillSampleBottles);
ResetSampleBottlesCommand = new RelayCommand<int>(ResetSampleBottles);
ResetAllTipHeadCommand = new RelayCommand<int>(ResetAllTipHead);
FillAllTipHeadCommand = new RelayCommand<int>(FillAllTipHead);
LoadBoxCommand = new RelayCommand(LoadBox);
}
/// <summary>
/// 设置某类型的工装到选中位置
/// </summary>
private void LoadBox()
{
if (SelectedBoxIndex < 0 || SelectedBoxIndex > 8)
{
MessageBox.Show("请先选择一个缓存工装位置1-8");
return;
}
switch (SelectedBoxType)
{
case BoxTypeEnum._40mL原液工装:
SourceLiquidBottleBoxModel newBox = new SourceLiquidBottleBoxModel
{
FixtureType = SelectedBoxType,
BoxSNCode = string.Empty,
SourceLiquidBottleCollection = liquidlist,
IsDoseFinish = false,
IsEmpty = false,
IsUseFinsh = false,
BoxArea = BoxAreaEnum._托盘存放平台,
BoxId_inDosingStation = selectedBoxIndex
};
// 更新选中位置的工装
ProjectPro.FixtureCacheArea[SelectedBoxIndex - 1] = File_Operator.DeepCopy(newBox);
break;
case BoxTypeEnum._12mL样品工装:
for (int i = 0; i < samplelist.Count; i++)
{
samplelist[i].SampleBottleType = SampleBottleTypeEnum._12mL;
}
SampleBottleBoxModel newBox1 = new SampleBottleBoxModel
{
FixtureType = SelectedBoxType,
BoxSNCode = string.Empty,
SampleBottleList = samplelist,
IsDoseFinish = false,
SampleBottleType = SampleBottleTypeEnum._12mL,
IsEmpty = false,
IsUseFinsh = false,
BoxId_inDosingStation = selectedBoxIndex,
BoxArea = BoxAreaEnum._托盘存放平台
};
// 更新选中位置的工装
ProjectPro.FixtureCacheArea[SelectedBoxIndex - 1] = File_Operator.DeepCopy(newBox1);
//var a = File_Operator.DeepCopyEx(newBox1);
break;
case BoxTypeEnum._5mL样品工装:
for (int i = 0; i < samplelist.Count; i++)
{
samplelist[i].SampleBottleType = SampleBottleTypeEnum._5mL;
}
SampleBottleBoxModel newBox2 = new SampleBottleBoxModel
{
FixtureType = SelectedBoxType,
BoxSNCode = string.Empty,
SampleBottleList = samplelist,
IsDoseFinish = false,
IsEmpty = false,
IsUseFinsh = false,
SampleBottleType = SampleBottleTypeEnum._5mL,
BoxId_inDosingStation = selectedBoxIndex,
BoxArea = BoxAreaEnum._托盘存放平台
};
// 更新选中位置的工装
ProjectPro.FixtureCacheArea[SelectedBoxIndex - 1] = File_Operator.DeepCopy(newBox2);
break;
case BoxTypeEnum._16mL粉末瓶工装:
break;
case BoxTypeEnum._125mL粉末瓶工装:
break;
}
OnPropertyChanged(nameof(ProjectPro));
}
///// <summary>
///// 移除选中位置的工装
///// </summary>
//private void RemoveSelectedBox()
//{
// if (SelectedBoxIndex >= 0 && SelectedBoxIndex < 8)
// {
// ProjectPro.FixtureCacheArea[SelectedBoxIndex] = null;
// }
//}
/// <summary>
/// 按照瓶1配置信息更新样品工装
/// </summary>
/// <param name="sampleboxIdParam"></param>
private void UpdateSampleBoxProperty(int sampleboxIdParam)
{
int index = 0;
index = sampleboxIdParam;
BaseBottleBox box = workService.ProjectPro.FixtureCacheArea.FirstOrDefault(f => f.BoxId_inDosingStation == index);
if (!(box is SampleBottleBoxModel)) return;
SampleBottleBoxModel sampleBottlebox = box as SampleBottleBoxModel;
if (sampleBottlebox != null)
{
for (int i = 0; i < sampleBottlebox.SampleBottleList.Count; i++)
{
SampleBottleModel sampleBottle = File_Operator.DeepCopy(sampleBottlebox.SampleBottleList[0]);
SampleBottleModel needtochangemodel = sampleBottlebox.SampleBottleList.FirstOrDefault(f => f.PosId_InBox == (i + 1));
needtochangemodel.SampleBottleType = sampleBottle.SampleBottleType;
needtochangemodel.MaterialDoseFlowList = sampleBottle.MaterialDoseFlowList;
needtochangemodel.HaveBottle = sampleBottle.HaveBottle;
//needtochangemodel.SampleFunctionFlowList = sampleBottle.SampleFunctionFlowList;
//needtochangemodel.AddLiquidFunctionFlowList = sampleBottle.AddLiquidFunctionFlowList;
}
}
OnPropertyChanged(nameof(ProjectPro));
}
/// <summary>
/// 清空样品工装配置
/// </summary>
/// <param name="sampleboxIdParam"></param>
private void ClearSampleBoxProperty(int sampleboxIdParam)
{
int index = 0;
index = sampleboxIdParam;
BaseBottleBox box = workService.ProjectPro.FixtureCacheArea.FirstOrDefault(f => f.BoxId_inDosingStation == index);
if (!(box is SampleBottleBoxModel)) return;
SampleBottleBoxModel sampleBottlebox = box as SampleBottleBoxModel;
if (sampleBottlebox != null)
{
for (int i = 0; i < sampleBottlebox.SampleBottleList.Count; i++)
{
SampleBottleModel bottle = sampleBottlebox.SampleBottleList.FirstOrDefault(f => f.PosId_InBox == i + 1);
if (bottle != null)
{
bottle.MaterialDoseFlowList.Clear();
bottle.SampleFunctionFlowList.Clear();
bottle.AddLiquidFunctionFlowList.Clear();
}
}
}
OnPropertyChanged(nameof(ProjectPro));
}
private async void ResetSampleBottles(int pos)
{
workService.ProjectPro.FixtureCacheArea[pos - 1] = new BaseBottleBox()
{
BoxArea = BoxAreaEnum._自动加样模块内,
BoxId_inDosingStation = pos,
IsEmpty = true
};
}
/// <summary>
/// 装置为满
/// </summary>
/// <param name="sampleboxIdParam"></param>
private void FillSampleBottles(int sampleboxIdParam)
{
int index = 0;
index = sampleboxIdParam;
BaseBottleBox box = workService.ProjectPro.FixtureCacheArea.FirstOrDefault(f => f.BoxId_inDosingStation == index);
if (!(box is SampleBottleBoxModel)) return;
SampleBottleBoxModel sampleBottlebox = box as SampleBottleBoxModel;
if (sampleBottlebox != null)
{
for (int i = 0; i < sampleBottlebox.SampleBottleList.Count; i++)
{
SampleBottleModel model = sampleBottlebox.SampleBottleList.FirstOrDefault(f => f.PosId_InBox == i + 1);
model.HaveBottle = true;
}
}
OnPropertyChanged(nameof(ProjectPro));
}
/// <summary>
/// 将样品工装置为空
/// </summary>
/// <param name="sampleboxIdParam"></param>
private void ClearSampleBottles(int sampleboxIdParam)
{
int index = 0;
index = sampleboxIdParam;
BaseBottleBox box = workService.ProjectPro.FixtureCacheArea.FirstOrDefault(f => f.BoxId_inDosingStation == index);
if (!(box is SampleBottleBoxModel)) return;
SampleBottleBoxModel sampleBottlebox = box as SampleBottleBoxModel;
if (sampleBottlebox != null)
{
for (int i = 0; i < sampleBottlebox.SampleBottleList.Count; i++)
{
SampleBottleModel model = sampleBottlebox.SampleBottleList.FirstOrDefault(f => f.PosId_InBox == i + 1);
model.HaveBottle = false;
}
}
OnPropertyChanged(nameof(ProjectPro));
}
private async void ResetAllTipHead(int id)
{
int index = 0;
index = id;
TipBoxModel tipBox = workService.ProjectPro.TipHeadArea.FirstOrDefault(c => c.BoxId_inDosingStation == index);
if (tipBox != null)
{
foreach (var tip in tipBox.TipItems)
{
tip.IsAvailable = false;
}
tipBox.HaveBox = false;
tipBox.IsEmpty = true;
//await workService.stationService_Dose.UpdateTipBox(tipBox);
}
}
private async void FillAllTipHead(int id)
{
int index = 0;
index = id;
TipBoxModel tipBox = workService.ProjectPro.TipHeadArea.FirstOrDefault(c => c.BoxId_inDosingStation == index);
tipBox.BoxArea = BoxAreaEnum._Tip托盘存放平台;
if (tipBox != null)
{
foreach (var tip in tipBox.TipItems)
{
tip.IsAvailable = true;
}
tipBox.HaveBox = true;
tipBox.IsEmpty = false;
//await workService.stationService_Dose.UpdateTipBox(tipBox);
}
}
private void InitialProject()
{
string projectAddress = IniHelper.IniRead(Application.StartupPath + "\\projectOpen.ini", "OpenProject", "LastOpenProject", "");
if (!string.IsNullOrWhiteSpace(projectAddress) && File.Exists(projectAddress))
{
bool readOk = false;
workService.ProjectPro = File_Operator.Xml_Serialize(projectAddress, false, workService.ProjectPro, out readOk);
if (readOk)
{
workService.ProjectPro.LastOpenProject = projectAddress;
workService.ProjectPro.NowOpenProjectFile = projectAddress.Substring(projectAddress.LastIndexOf("\\") + 1).Trim();
}
else
{
//InitialProjectProperty();
}
}
else
{
//InitialProjectProperty();
}
OnPropertyChanged(nameof(ProjectPro));
}
private void InitialProjectProperty()
{
//加载传送平台 7个工装
workService.ProjectPro.TransferArea.Clear();
for (int i = 0; i < 7; i++)
{
workService.ProjectPro.TransferArea.Add(new BaseBottleBox()
{
//FixtureType = BoxTypeEnum._40mL原液工装,
BoxId_inDoseDnload = i + 1,
BoxId_inDoseUpload = i + 1,
IsEmpty = true,
IsDoseFinish = false,
IsUseFinsh = false,
BoxArea = BoxAreaEnum._手套箱传送平台,
});
}
workService.ProjectPro.FixtureCacheArea.Clear();
//加载工装暂存区域 8个工装 暂定6个区域可用
for (int i = 0; i < 8; i++)
{
workService.ProjectPro.FixtureCacheArea.Add(new BaseBottleBox()
{
BoxId_inDosingStation = i + 1,
BoxArea = BoxAreaEnum._托盘存放平台,
IsEmpty = true,
IsDoseFinish = false,
IsUseFinsh = false,
});
}
// 初始化10个粉末瓶 暂存位置
workService.ProjectPro.PowderHeaderCacheArea.Clear();
for (int i = 0; i < 10; i++)
{
workService.ProjectPro.PowderHeaderCacheArea.Add(new SourcePowderBottleModel
{
HaveBottle = false,
Pos_InDosePowderHC = i + 1,
SNCode = string.Empty,
PowderBottleType = PowderBottleTypeEnum._125mL,
SourcePowderBottleState = SourceBottleStateEnum.Idl,
OriginWeight = 0,
});
}
// 3个tip头工装
workService.ProjectPro.TipHeadArea.Clear();
for (int i = 0; i < 3; i++)
{
TipBoxModel tipBox = new TipBoxModel()
{
TipType = i switch
{
0 => TipTypeEnum._1000UL,
1 => TipTypeEnum._300UL,
2 => TipTypeEnum._50UL,
_ => TipTypeEnum._300UL
},
HaveBox = false,
UseIndex = 0,
BoxId_inDosingStation = i + 1,
IsEmpty = true, // 初始无工装(可根据需求调整)
};
// 添加96个可用Tip头避免序列化空引用
for (int j = 0; j < 8 * 12; j++)
{
tipBox.TipItems.Add(new TipHeadItem { IsAvailable = true, UseIndex = j + 1 });
}
workService.ProjectPro.TipHeadArea.Add(tipBox);
}
OnPropertyChanged(nameof(ProjectPro));
}
/// <summary>
/// 新建项目文件
/// </summary>
private void NewProject()
{
workService.ProjectPro.LastOpenProject = string.Empty;
workService.ProjectPro.NowOpenProjectFile = string.Empty;
InitialProjectProperty();
}
/// <summary>
/// 打开项目文件
/// </summary>
private void OpenProject()
{
OpenFileDialog dlgopen = new OpenFileDialog();
dlgopen.Filter = "pro文件(*.pro)|*.pro";
if (!Directory.Exists(Application.StartupPath + @"\Projects")) Directory.CreateDirectory(Application.StartupPath + @"\Projects");
dlgopen.InitialDirectory = Application.StartupPath + @"\Projects";
if (dlgopen.ShowDialog() == DialogResult.OK)
{
ProjectProperty project = new ProjectProperty();
bool readOk = false;
project = File_Operator.Xml_Serialize(dlgopen.FileName, false, project, out readOk);
if (readOk)
{
workService.ProjectPro = project;
workService.ProjectPro.LastOpenProject = dlgopen.FileName;
IniHelper.IniWrite(Application.StartupPath + "\\projectOpen.ini", "OpenProject", "LastOpenProject", dlgopen.FileName);
workService.ProjectPro.NowOpenProjectFile = dlgopen.FileName.Substring(dlgopen.FileName.LastIndexOf("\\") + 1).Trim();
OnPropertyChanged(nameof(ProjectPro));
WeakReferenceMessenger.Default.Send(new DesktopAlertMessage { Content = "打开项目文件成功!" });
}
}
}
/// <summary>
/// 项目文件另存为
/// </summary>
private void SaveAsProject()
{
try
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "pro文件(*.pro)|*.pro";
if (!Directory.Exists(Application.StartupPath + @"\Projects")) Directory.CreateDirectory(Application.StartupPath + @"\Projects");
dlg.InitialDirectory = Application.StartupPath + @"\Projects";
if (dlg.ShowDialog() == DialogResult.OK)
{
workService.ProjectPro.LastOpenProject = dlg.FileName;
IniHelper.IniWrite(Application.StartupPath + "\\projectOpen.ini", "OpenProject", "LastOpenProject", dlg.FileName);
workService.ProjectPro.NowOpenProjectFile = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\\") + 1).Trim();
bool bReadOk = false;
File_Operator.Xml_Serialize<ProjectProperty>(workService.ProjectPro.LastOpenProject, true, workService.ProjectPro, out bReadOk);
Application.DoEvents();
WeakReferenceMessenger.Default.Send(new DesktopAlertMessage { Content = "另存成功!" });
}
else
{
return;
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 保存项目文件
/// </summary>
private void SaveProject()
{
try
{
if (string.IsNullOrEmpty(workService.ProjectPro.LastOpenProject) || !File.Exists(workService.ProjectPro.LastOpenProject))
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "pro文件(*.pro)|*.pro";
if (!Directory.Exists(Application.StartupPath + @"\Projects")) Directory.CreateDirectory(Application.StartupPath + @"\Projects");
dlg.InitialDirectory = Application.StartupPath + @"\Projects";
if (dlg.ShowDialog() == DialogResult.OK)
{
workService.ProjectPro.LastOpenProject = dlg.FileName;
IniHelper.IniWrite(Application.StartupPath + "\\projectOpen.ini", "OpenProject", "LastOpenProject", dlg.FileName);
workService.ProjectPro.NowOpenProjectFile = dlg.FileName.Substring(dlg.FileName.LastIndexOf("\\") + 1).Trim();
}
else
{
return;
}
}
bool bReadOk = false;
File_Operator.Xml_Serialize<ProjectProperty>(workService.ProjectPro.LastOpenProject, true, workService.ProjectPro, out bReadOk);
Application.DoEvents();
WeakReferenceMessenger.Default.Send(new DesktopAlertMessage { Content = "保存成功!" });
}
catch (Exception)
{
throw;
}
}
private void ImportExcel()
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择要导入的Excel文件";
openFileDialog.Filter = "Excel文件(*.xlsx;*.xls)|*.xlsx;*.xls";
openFileDialog.Multiselect = false; // 只允许选择一个文件
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
IWorkbook workbook;
using (FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
// 根据Excel文件的格式.xlsx或.xls选择不同的加载方式
if (openFileDialog.FileName.EndsWith(".xlsx"))
{
workbook = new XSSFWorkbook(fileStream);
}
else
{
workbook = new HSSFWorkbook(fileStream);
}
//解析excel文档
}
OnPropertyChanged(nameof(ProjectPro));
WeakReferenceMessenger.Default.Send(new DesktopAlertMessage { Content = "导入配方Excel文件成功" });
}
}
catch (Exception)
{
throw;
}
}
private void ExportExcel()
{
}
}
}