| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using LocalhostMES.DataBase;
- using LocalhostMES.Enums;
- using LocalhostMES.Core;
- using LocalhostMES.Helpers;
- using LocalhostMES.Models;
- using LocalhostMES.ViewModels.Services;
- using Prism.Commands;
- using Prism.Mvvm;
- using System;
- using System.Collections.ObjectModel;
- using System.Windows;
- namespace LocalhostMES.ViewModels.Tabs
- {
- public class KeyPartManagementViewModel : BindableBase
- {
- private readonly IMesWorkspace _workspace;
- public KeyPartManagementViewModel(IMesWorkspace workspace)
- {
- _workspace = workspace;
- _workspace.Initialize();
- MesDataChangedNotifier.Changed += OnDataChanged;
- }
- private ObservableCollection<LocalhostPartInfo> _partInfos = new ObservableCollection<LocalhostPartInfo>();
- public ObservableCollection<LocalhostPartInfo> PartInfos
- {
- get => _partInfos;
- set => SetProperty(ref _partInfos, value);
- }
- private LocalhostPartInfo _newPart = new LocalhostPartInfo();
- public LocalhostPartInfo NewPart
- {
- get => _newPart;
- set => SetProperty(ref _newPart, value);
- }
- private LocalhostPartInfo _selectPart;
- public LocalhostPartInfo SelectPart
- {
- get => _selectPart;
- set => SetProperty(ref _selectPart, value);
- }
- private string _statusMessage = string.Empty;
- public string StatusMessage
- {
- get => _statusMessage;
- set => SetProperty(ref _statusMessage, value);
- }
- private DelegateCommand _addPartInfoCommand;
- public DelegateCommand AddPartInfoCommand =>
- _addPartInfoCommand ?? (_addPartInfoCommand = new DelegateCommand(AddPartInfo));
- private DelegateCommand _refPartInfoCommand;
- public DelegateCommand RefPartInfoCommand =>
- _refPartInfoCommand ?? (_refPartInfoCommand = new DelegateCommand(RefPartInfo));
- private DelegateCommand _deletePartInfoCommand;
- public DelegateCommand DeletePartInfoCommand =>
- _deletePartInfoCommand ?? (_deletePartInfoCommand = new DelegateCommand(DeletePartInfo));
- private DelegateCommand<LocalhostPartInfo> _deletePartRowCommand;
- public DelegateCommand<LocalhostPartInfo> DeletePartRowCommand =>
- _deletePartRowCommand ?? (_deletePartRowCommand = new DelegateCommand<LocalhostPartInfo>(DeletePartRow, p => p != null));
- private string _importExcelPath = @"E:\Configuration\零件绑定对应表.xlsx";
- public string ImportExcelPath
- {
- get => _importExcelPath;
- set => SetProperty(ref _importExcelPath, value);
- }
- private DelegateCommand _importFromExcelCommand;
- public DelegateCommand ImportFromExcelCommand =>
- _importFromExcelCommand ?? (_importFromExcelCommand = new DelegateCommand(ImportFromExcel));
- private void AddPartInfo()
- {
- var pn = (NewPart.PartNum ?? string.Empty).Trim();
- var materialCode = (NewPart.MaterialCode ?? string.Empty).Trim();
- var stationCode = (NewPart.StationCode ?? string.Empty).Trim();
- var positionCode = (NewPart.PositionCode ?? string.Empty).Trim();
- var partInfoName = (NewPart.PartInfoName ?? string.Empty).Trim();
- if (string.IsNullOrEmpty(pn))
- {
- _workspace.ShowStatus("请填写关键件号", true);
- return;
- }
- if (string.IsNullOrEmpty(materialCode))
- {
- _workspace.ShowStatus("请填写物料编码", true);
- return;
- }
- if (string.IsNullOrEmpty(stationCode))
- {
- _workspace.ShowStatus("请填写工位编码", true);
- return;
- }
- if (string.IsNullOrEmpty(positionCode))
- {
- _workspace.ShowStatus("请填写站点编码", true);
- return;
- }
- if (string.IsNullOrEmpty(partInfoName))
- {
- _workspace.ShowStatus("请填写关键件名称", true);
- return;
- }
- if (NewPart.PartQty < 0)
- {
- _workspace.ShowStatus("数量不能为负数", true);
- return;
- }
- var st = NewPart.StationType;
- var toSave = new LocalhostPartInfo
- {
- StationType = st,
- PartNum = pn,
- PartQty = NewPart.PartQty,
- MaterialCode = materialCode,
- StationCode = stationCode,
- PositionCode = positionCode,
- PartInfoName = partInfoName
- };
- if (DatabaseHelper.InsertPartInfo(toSave))
- {
- NewPart = new LocalhostPartInfo { StationType = st };
- _workspace.ShowStatus("关键件已保存", false);
- RefPartInfo();
- }
- else
- {
- _workspace.ShowStatus("保存失败(可能已存在相同工站类型+关键件号)", true);
- }
- }
- private void RefPartInfo()
- {
- var st = NewPart.StationType;
- PartInfos = new ObservableCollection<LocalhostPartInfo>(DatabaseHelper.SelectPartInfo(st));
- StatusMessage = $"工站 {st},共 {PartInfos.Count} 条";
- }
- private void DeletePartInfo()
- {
- if (SelectPart == null)
- {
- _workspace.ShowStatus("请先选择要删除的关键件行", true);
- return;
- }
- if (DatabaseHelper.DeletePartInfo(SelectPart.StationType, SelectPart.PartNum))
- {
- RefPartInfo();
- _workspace.ShowStatus("已删除", false);
- }
- else
- {
- _workspace.ShowStatus("删除失败", true);
- }
- }
- private void DeletePartRow(LocalhostPartInfo part)
- {
- if (part == null)
- {
- return;
- }
- if (DatabaseHelper.DeletePartInfo(part.StationType, part.PartNum))
- {
- PartInfos.Remove(part);
- _workspace.ShowStatus("已删除", false);
- }
- else
- {
- _workspace.ShowStatus("删除失败", true);
- }
- }
- private void ImportFromExcel()
- {
- try
- {
- var path = (ImportExcelPath ?? string.Empty).Trim();
- var items = KeyPartExcelImporter.BuildPartInfosFromExcel(path);
- var affected = DatabaseHelper.InsertPartInfos(items);
- _workspace.ShowStatus($"导入完成:构建 {items.Count} 条,入库 {affected} 条", false);
- RefPartInfo();
- }
- catch (Exception ex)
- {
- _workspace.ShowStatus($"导入失败:{ex.Message}", true);
- }
- }
- private void OnDataChanged(object sender, MesDataChangedEventArgs e)
- {
- if (!e.Has(MesDataScope.KeyPart))
- {
- return;
- }
- Application.Current?.Dispatcher.Invoke(RefPartInfo);
- }
- }
- }
|