SimpleTaskViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Mapster;
  2. using MaterialDesignThemes.Wpf;
  3. using Prism.Commands;
  4. using Prism.Services.Dialogs;
  5. using ProjectManagementSystem.Common.Config;
  6. using ProjectManagementSystem.Common.Extenions;
  7. using ProjectManagementSystem.Common.Models;
  8. using ProjectManagementSystem.Common.Service;
  9. using ProjectManagementSystemView.Infrastructure;
  10. using ProjectManagementSystemView.Infrastructure.Models;
  11. using PropertyChanged;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace ProjectManagementSystemView.ViewModels.Dialogs
  19. {
  20. [AddINotifyPropertyChangedInterface]
  21. public class SimpleTaskViewModel : IDialogHostAware
  22. {
  23. public ObservableCollection<string> WareHouseCodeCollection1 { get; set; }
  24. public ObservableCollection<string> LocationCodeCollection1 { get; set; }
  25. public ObservableCollection<string> MaterialIdCollection { get; set; }
  26. public ObservableCollection<string> WareHouseCodeCollection2 { get; set; }
  27. public ObservableCollection<string> LocationCodeCollection2 { get; set; }
  28. public string Title { get; set; }
  29. public bool IsTaskCall { get; set; }
  30. public bool IsTaskTransport { get; set; }
  31. [OnChangedMethod(nameof(OnSourceWareHouseCodeChanged))]
  32. public string SourceWareHouseCode { get; set; }
  33. [OnChangedMethod(nameof(OnSourceLocationCodeChanged))]
  34. public string SourceLocationCode { get; set; }
  35. public string SourceMaterialId { get; set; }
  36. [OnChangedMethod(nameof(OnTargetWareHouseCodeChanged))]
  37. public string TargetWareHouseCode { get; set; }
  38. public string TargetLocationCode { get; set; }
  39. public string DialogHostName { get; set; }
  40. public DelegateCommand SaveCommand { get; set; }
  41. public DelegateCommand CancelCommand { get; set; }
  42. public SimpleTaskViewModel()
  43. {
  44. WareHouseCodeCollection1 = new ObservableCollection<string>();
  45. LocationCodeCollection1 = new ObservableCollection<string>();
  46. MaterialIdCollection = new ObservableCollection<string>();
  47. WareHouseCodeCollection2 = new ObservableCollection<string>();
  48. LocationCodeCollection2 = new ObservableCollection<string>();
  49. SaveCommand = new DelegateCommand(Save);
  50. CancelCommand = new DelegateCommand(Cancel);
  51. }
  52. private void OnSourceWareHouseCodeChanged()
  53. {
  54. LocationCodeCollection1.Clear();
  55. var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { WareHouseCode = SourceWareHouseCode });
  56. var dataList = res?.data as List<LocationPropertyDto>;
  57. dataList?.ForEach(p => LocationCodeCollection1.Add(p.LocationCode));
  58. LocationCodeCollection1.Add("");
  59. MaterialIdCollection.Clear();
  60. var mDataList = dataList.Where(d => !string.IsNullOrEmpty(d.MaterialId)).Select(d => d.MaterialId).Distinct().ToList();
  61. mDataList.ForEach(p => MaterialIdCollection.Add(p));
  62. MaterialIdCollection.Add("");
  63. }
  64. private void OnTargetWareHouseCodeChanged()
  65. {
  66. LocationCodeCollection2.Clear();
  67. var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { WareHouseCode = TargetWareHouseCode });
  68. var dataList = res?.data as List<LocationPropertyDto>;
  69. dataList?.ForEach(p => LocationCodeCollection2.Add(p.LocationCode));
  70. LocationCodeCollection2.Add("");
  71. }
  72. private void OnSourceLocationCodeChanged()
  73. {
  74. if (string.IsNullOrEmpty(SourceLocationCode))
  75. {
  76. OnSourceWareHouseCodeChanged();
  77. return;
  78. }
  79. var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { LocationCode = SourceLocationCode });
  80. var dataList = res?.data as List<LocationPropertyDto>;
  81. var data = dataList?.FirstOrDefault();
  82. if (data != null)
  83. {
  84. MaterialIdCollection.Clear();
  85. MaterialIdCollection.Add(data.MaterialId);
  86. SourceMaterialId = data.MaterialId;
  87. }
  88. }
  89. /// <summary>
  90. /// 取消
  91. /// </summary>
  92. private void Cancel()
  93. {
  94. if (DialogHost.IsDialogOpen(DialogHostName))
  95. DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No)); //取消返回NO告诉操作结束
  96. }
  97. /// <summary>
  98. /// 确定
  99. /// </summary>
  100. private void Save()
  101. {
  102. if (DialogHost.IsDialogOpen(DialogHostName))
  103. {
  104. var model1 = new VmLocationProperty()
  105. {
  106. WareHouseCode = SourceWareHouseCode,
  107. LocationCode = SourceLocationCode,
  108. MaterialId = SourceMaterialId
  109. };
  110. var model2 = new VmLocationProperty()
  111. {
  112. WareHouseCode = TargetWareHouseCode,
  113. LocationCode = TargetLocationCode
  114. };
  115. //确定时,把编辑的实体返回并且返回OK
  116. DialogParameters param = new DialogParameters();
  117. param.Add("Value1", model1);
  118. param.Add("Value2", model2);
  119. DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param));
  120. }
  121. }
  122. public void OnDialogOpend(IDialogParameters parameters)
  123. {
  124. if (parameters.ContainsKey("Value1") && parameters.ContainsKey("Value2"))
  125. {
  126. Title = parameters.GetValue<string>("Title");
  127. var data1 = parameters.GetValue<VmLocationProperty>("Value1");
  128. var data2 = parameters.GetValue<VmLocationProperty>("Value2");
  129. WareHouseCodeCollection1.Clear();
  130. LocationCodeCollection1.Clear();
  131. MaterialIdCollection.Clear();
  132. WareHouseCodeCollection2.Clear();
  133. LocationCodeCollection2.Clear();
  134. IsTaskCall = data1 == null && data2 != null;
  135. IsTaskTransport = data1 != null && data2 == null;
  136. if (IsTaskTransport)
  137. {
  138. WareHouseCodeCollection1.Add(data1.WareHouseCode);
  139. LocationCodeCollection1.Add(data1.LocationCode);
  140. MaterialIdCollection.Add(data1.MaterialId);
  141. SourceWareHouseCode = data1.WareHouseCode;
  142. SourceLocationCode = data1.LocationCode;
  143. SourceMaterialId = data1.MaterialId;
  144. var targetLocations = data1.LocationCode.GetLocationMember("目标仓位").ToValueArray<string>();
  145. if (targetLocations.Length > 0)
  146. {
  147. foreach (var location in targetLocations)
  148. {
  149. LocationCodeCollection2.Add(location);
  150. }
  151. TargetLocationCode = targetLocations.First();
  152. }
  153. else
  154. {
  155. var arr = data1.LocationCode.GetLocationMember("目标仓库").ToValueArray<string>();
  156. for (int i = 0; i < arr.Length; i++)
  157. {
  158. WareHouseCodeCollection2.Add(arr[i]);
  159. }
  160. if (arr.Length > 0)
  161. {
  162. TargetWareHouseCode = arr[0];
  163. }
  164. }
  165. }
  166. if (IsTaskCall)
  167. {
  168. var sourceLocations = data2.LocationCode.GetLocationMember("源仓位").ToValueArray<string>();
  169. if (sourceLocations.Length > 0)
  170. {
  171. foreach (var location in sourceLocations)
  172. {
  173. LocationCodeCollection1.Add(location);
  174. }
  175. SourceLocationCode = sourceLocations.First();
  176. }
  177. else
  178. {
  179. var arr = data2.LocationCode.GetLocationMember("源仓库").ToValueArray<string>();
  180. for (int i = 0; i < arr.Length; i++)
  181. {
  182. WareHouseCodeCollection1.Add(arr[i]);
  183. }
  184. if (arr.Length > 0)
  185. {
  186. SourceWareHouseCode = arr[0];
  187. }
  188. }
  189. WareHouseCodeCollection2.Add(data2.WareHouseCode);
  190. LocationCodeCollection2.Add(data2.LocationCode);
  191. TargetWareHouseCode = data2.WareHouseCode;
  192. TargetLocationCode = data2.LocationCode;
  193. }
  194. }
  195. }
  196. }
  197. }