KeyPartManagementViewModel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using LocalhostMES.DataBase;
  2. using LocalhostMES.Enums;
  3. using LocalhostMES.Core;
  4. using LocalhostMES.Helpers;
  5. using LocalhostMES.Models;
  6. using LocalhostMES.ViewModels.Services;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using System.Windows;
  12. namespace LocalhostMES.ViewModels.Tabs
  13. {
  14. public class KeyPartManagementViewModel : BindableBase
  15. {
  16. private readonly IMesWorkspace _workspace;
  17. public KeyPartManagementViewModel(IMesWorkspace workspace)
  18. {
  19. _workspace = workspace;
  20. _workspace.Initialize();
  21. MesDataChangedNotifier.Changed += OnDataChanged;
  22. }
  23. private ObservableCollection<LocalhostPartInfo> _partInfos = new ObservableCollection<LocalhostPartInfo>();
  24. public ObservableCollection<LocalhostPartInfo> PartInfos
  25. {
  26. get => _partInfos;
  27. set => SetProperty(ref _partInfos, value);
  28. }
  29. private LocalhostPartInfo _newPart = new LocalhostPartInfo();
  30. public LocalhostPartInfo NewPart
  31. {
  32. get => _newPart;
  33. set => SetProperty(ref _newPart, value);
  34. }
  35. private LocalhostPartInfo _selectPart;
  36. public LocalhostPartInfo SelectPart
  37. {
  38. get => _selectPart;
  39. set => SetProperty(ref _selectPart, value);
  40. }
  41. private string _statusMessage = string.Empty;
  42. public string StatusMessage
  43. {
  44. get => _statusMessage;
  45. set => SetProperty(ref _statusMessage, value);
  46. }
  47. private DelegateCommand _addPartInfoCommand;
  48. public DelegateCommand AddPartInfoCommand =>
  49. _addPartInfoCommand ?? (_addPartInfoCommand = new DelegateCommand(AddPartInfo));
  50. private DelegateCommand _refPartInfoCommand;
  51. public DelegateCommand RefPartInfoCommand =>
  52. _refPartInfoCommand ?? (_refPartInfoCommand = new DelegateCommand(RefPartInfo));
  53. private DelegateCommand _deletePartInfoCommand;
  54. public DelegateCommand DeletePartInfoCommand =>
  55. _deletePartInfoCommand ?? (_deletePartInfoCommand = new DelegateCommand(DeletePartInfo));
  56. private DelegateCommand<LocalhostPartInfo> _deletePartRowCommand;
  57. public DelegateCommand<LocalhostPartInfo> DeletePartRowCommand =>
  58. _deletePartRowCommand ?? (_deletePartRowCommand = new DelegateCommand<LocalhostPartInfo>(DeletePartRow, p => p != null));
  59. private string _importExcelPath = @"E:\Configuration\零件绑定对应表.xlsx";
  60. public string ImportExcelPath
  61. {
  62. get => _importExcelPath;
  63. set => SetProperty(ref _importExcelPath, value);
  64. }
  65. private DelegateCommand _importFromExcelCommand;
  66. public DelegateCommand ImportFromExcelCommand =>
  67. _importFromExcelCommand ?? (_importFromExcelCommand = new DelegateCommand(ImportFromExcel));
  68. private void AddPartInfo()
  69. {
  70. var pn = (NewPart.PartNum ?? string.Empty).Trim();
  71. var materialCode = (NewPart.MaterialCode ?? string.Empty).Trim();
  72. var stationCode = (NewPart.StationCode ?? string.Empty).Trim();
  73. var positionCode = (NewPart.PositionCode ?? string.Empty).Trim();
  74. var partInfoName = (NewPart.PartInfoName ?? string.Empty).Trim();
  75. if (string.IsNullOrEmpty(pn))
  76. {
  77. _workspace.ShowStatus("请填写关键件号", true);
  78. return;
  79. }
  80. if (string.IsNullOrEmpty(materialCode))
  81. {
  82. _workspace.ShowStatus("请填写物料编码", true);
  83. return;
  84. }
  85. if (string.IsNullOrEmpty(stationCode))
  86. {
  87. _workspace.ShowStatus("请填写工位编码", true);
  88. return;
  89. }
  90. if (string.IsNullOrEmpty(positionCode))
  91. {
  92. _workspace.ShowStatus("请填写站点编码", true);
  93. return;
  94. }
  95. if (string.IsNullOrEmpty(partInfoName))
  96. {
  97. _workspace.ShowStatus("请填写关键件名称", true);
  98. return;
  99. }
  100. if (NewPart.PartQty < 0)
  101. {
  102. _workspace.ShowStatus("数量不能为负数", true);
  103. return;
  104. }
  105. var st = NewPart.StationType;
  106. var toSave = new LocalhostPartInfo
  107. {
  108. StationType = st,
  109. PartNum = pn,
  110. PartQty = NewPart.PartQty,
  111. MaterialCode = materialCode,
  112. StationCode = stationCode,
  113. PositionCode = positionCode,
  114. PartInfoName = partInfoName
  115. };
  116. if (DatabaseHelper.InsertPartInfo(toSave))
  117. {
  118. NewPart = new LocalhostPartInfo { StationType = st };
  119. _workspace.ShowStatus("关键件已保存", false);
  120. RefPartInfo();
  121. }
  122. else
  123. {
  124. _workspace.ShowStatus("保存失败(可能已存在相同工站类型+关键件号)", true);
  125. }
  126. }
  127. private void RefPartInfo()
  128. {
  129. var st = NewPart.StationType;
  130. PartInfos = new ObservableCollection<LocalhostPartInfo>(DatabaseHelper.SelectPartInfo(st));
  131. StatusMessage = $"工站 {st},共 {PartInfos.Count} 条";
  132. }
  133. private void DeletePartInfo()
  134. {
  135. if (SelectPart == null)
  136. {
  137. _workspace.ShowStatus("请先选择要删除的关键件行", true);
  138. return;
  139. }
  140. if (DatabaseHelper.DeletePartInfo(SelectPart.StationType, SelectPart.PartNum))
  141. {
  142. RefPartInfo();
  143. _workspace.ShowStatus("已删除", false);
  144. }
  145. else
  146. {
  147. _workspace.ShowStatus("删除失败", true);
  148. }
  149. }
  150. private void DeletePartRow(LocalhostPartInfo part)
  151. {
  152. if (part == null)
  153. {
  154. return;
  155. }
  156. if (DatabaseHelper.DeletePartInfo(part.StationType, part.PartNum))
  157. {
  158. PartInfos.Remove(part);
  159. _workspace.ShowStatus("已删除", false);
  160. }
  161. else
  162. {
  163. _workspace.ShowStatus("删除失败", true);
  164. }
  165. }
  166. private void ImportFromExcel()
  167. {
  168. try
  169. {
  170. var path = (ImportExcelPath ?? string.Empty).Trim();
  171. var items = KeyPartExcelImporter.BuildPartInfosFromExcel(path);
  172. var affected = DatabaseHelper.InsertPartInfos(items);
  173. _workspace.ShowStatus($"导入完成:构建 {items.Count} 条,入库 {affected} 条", false);
  174. RefPartInfo();
  175. }
  176. catch (Exception ex)
  177. {
  178. _workspace.ShowStatus($"导入失败:{ex.Message}", true);
  179. }
  180. }
  181. private void OnDataChanged(object sender, MesDataChangedEventArgs e)
  182. {
  183. if (!e.Has(MesDataScope.KeyPart))
  184. {
  185. return;
  186. }
  187. Application.Current?.Dispatcher.Invoke(RefPartInfo);
  188. }
  189. }
  190. }