WmsQueryViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using Mapster;
  2. using Prism.Commands;
  3. using Prism.Events;
  4. using Prism.Services.Dialogs;
  5. using ProjectManagementSystem.Common.Config;
  6. using ProjectManagementSystem.Common.Extenions;
  7. using ProjectManagementSystem.Common.Function;
  8. using ProjectManagementSystem.Common.Logger;
  9. using ProjectManagementSystem.Common.Models;
  10. using ProjectManagementSystem.Common.Models.Crms;
  11. using ProjectManagementSystem.Common.Service;
  12. using ProjectManagementSystem.Language;
  13. using ProjectManagementSystemView.Infrastructure;
  14. using ProjectManagementSystemView.Infrastructure.Events;
  15. using ProjectManagementSystemView.Infrastructure.Extensions;
  16. using ProjectManagementSystemView.Infrastructure.Models;
  17. using PropertyChanged;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Collections.ObjectModel;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. namespace ProjectManagementSystemView.ViewModels
  25. {
  26. [AddINotifyPropertyChangedInterface]
  27. public class WmsQueryViewModel
  28. {
  29. private List<string> wareHouses;
  30. protected readonly IDialogHostService dialogService;
  31. public bool IsVisible { get; set; }
  32. [OnChangedMethod(nameof(ReloadLayout))]
  33. public string WareHouseCode { get; set; }
  34. public VmLocationProperty SelectedVmData { get; set; }
  35. public ObservableCollection<VmLocationProperty> VmDatas { get; set; } = new ObservableCollection<VmLocationProperty>();
  36. public ObservableCollection<string> WareHouseCollection { get; set; } = new ObservableCollection<string>();
  37. public DelegateCommand LoadedCommand { get; private set; }
  38. public DelegateCommand ModifyCommand { get; private set; }
  39. public DelegateCommand<string> TaskCallCommand { get; private set; }
  40. public DelegateCommand<string> TaskTransportCommand { get; private set; }
  41. public DelegateCommand ReleaseAgvCommand { get; private set; }
  42. public WmsQueryViewModel(IDialogHostService dialogService, IEventAggregator eventAggregator)
  43. {
  44. this.dialogService = dialogService;
  45. var wareHouseFilters = AppSetting.TryGetValue<string>("WareHouseFilter1").ToValueArray<string>().ToList();
  46. var wareHouseList = ExcelConfig.Instance.RouteConfigList
  47. .Where(d => !string.IsNullOrEmpty(d.Area))
  48. .Where(d =>
  49. {
  50. if (wareHouseFilters.Count == 0) return true;
  51. return wareHouseFilters.Contains(d.Area);
  52. })
  53. .GroupBy(d => d.Area)
  54. .Select(d => d.Key)
  55. .OrderBy(d => wareHouseFilters.IndexOf(d));
  56. wareHouses = wareHouseFilters.Count == 0 ?
  57. wareHouseList.OrderBy(d => d).ToList() :
  58. wareHouseList.ToList();
  59. wareHouses.ForEach(p => WareHouseCollection.Add(p));
  60. if (wareHouseFilters.Count == 0)
  61. {
  62. WareHouseCollection.Add(string.Empty);
  63. WareHouseCode = WareHouseCollection.Last();
  64. }
  65. else
  66. {
  67. WareHouseCode = WareHouseCollection.First();
  68. }
  69. LoadedCommand = new DelegateCommand(Loaded);
  70. ModifyCommand = new DelegateCommand(ModifyAsync);
  71. TaskCallCommand = new DelegateCommand<string>((arg) => SimpleTaskAddAsync(arg, null, SelectedVmData));
  72. TaskTransportCommand = new DelegateCommand<string>((arg) => SimpleTaskAddAsync(arg, SelectedVmData, null));
  73. ReleaseAgvCommand = new DelegateCommand(ReleaseAgv);
  74. ResourceService.Current.CultureChanged += d => ReloadLayout();
  75. eventAggregator.GetEvent<UpdateViewEvent>().Subscribe(Read);
  76. }
  77. private void Read()
  78. {
  79. if (!IsVisible) return;
  80. try
  81. {
  82. var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { Area = WareHouseCode });
  83. var dataList = res?.data as List<LocationPropertyDto>;
  84. UpdateVmDatas(dataList);
  85. }
  86. catch (Exception ex)
  87. {
  88. HandyControl.Controls.Growl.Error(ex.Message);
  89. }
  90. }
  91. protected virtual void UpdateVmDatas(List<LocationPropertyDto> dataList)
  92. {
  93. VmDatas.Update(dataList, (s, v) => s.LocationCode == v.LocationCode);
  94. }
  95. protected virtual void Loaded()
  96. {
  97. }
  98. protected virtual void ReloadLayout()
  99. {
  100. VmDatas.Clear();
  101. }
  102. private async void ModifyAsync()
  103. {
  104. try
  105. {
  106. if (SelectedVmData == null) return;
  107. DialogParameters param = new DialogParameters();
  108. param.Add("Value", SelectedVmData);
  109. var dialogResult = await dialogService.ShowDialog("ModifyWmsView", param);
  110. if (dialogResult.Result == ButtonResult.OK)
  111. {
  112. var data = dialogResult.Parameters.GetValue<VmLocationProperty>("Value");
  113. UpdateLocation(data);
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  119. }
  120. }
  121. private async void SimpleTaskAddAsync(string arg, VmLocationProperty value1, VmLocationProperty value2)
  122. {
  123. try
  124. {
  125. if (value1 == null && value2 == null) return;
  126. if (value1 != null && value2 == null)
  127. {
  128. var temp = value1.LocationCode.GetLocationMember("目标仓库");
  129. var temp2 = value1.LocationCode.GetLocationMember("目标仓位");
  130. if (string.IsNullOrEmpty(temp) && string.IsNullOrEmpty(temp2))
  131. {
  132. HandyControl.Controls.Growl.Info($"{value1.LocationCode} {Langs.TargetWarehouseNotConfigured}");
  133. return;
  134. }
  135. }
  136. if (value1 == null && value2 != null)
  137. {
  138. var temp = value2.LocationCode.GetLocationMember("源仓库");
  139. var temp2 = value2.LocationCode.GetLocationMember("源仓位");
  140. if (string.IsNullOrEmpty(temp) && string.IsNullOrEmpty(temp2))
  141. {
  142. HandyControl.Controls.Growl.Info($"{value2.LocationCode} {Langs.SourceWarehouseNotConfigured}");
  143. return;
  144. }
  145. }
  146. DialogParameters param = new DialogParameters();
  147. param.Add("Title", arg);
  148. param.Add("Value1", value1);
  149. param.Add("Value2", value2);
  150. var dialogResult = await dialogService.ShowDialog("SimpleTaskView", param);
  151. if (dialogResult.Result == ButtonResult.OK)
  152. {
  153. try
  154. {
  155. var data1 = dialogResult.Parameters.GetValue<VmLocationProperty>("Value1");
  156. var data2 = dialogResult.Parameters.GetValue<VmLocationProperty>("Value2");
  157. var dto = new PmsTaskInfoDto();
  158. dto.WareHouseCode = data1.WareHouseCode;
  159. dto.LocationCode = data1.LocationCode;
  160. dto.MaterialId = data1.MaterialId;
  161. dto.TargetWareHouseCode = data2.WareHouseCode;
  162. dto.TargetLocationCode = data2.LocationCode;
  163. var result = PmsCustomApi.Post<object>("TaskAdd", dto);
  164. if (result.code == 20000)
  165. {
  166. HandyControl.Controls.Growl.Success(result.message);
  167. }
  168. else
  169. {
  170. HandyControl.Controls.Growl.Warning(result.message);
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  176. }
  177. }
  178. }
  179. catch (Exception ex)
  180. {
  181. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  182. }
  183. }
  184. private void UpdateLocation(VmLocationProperty locationData)
  185. {
  186. try
  187. {
  188. if (string.IsNullOrEmpty(locationData?.LocationCode)) return;
  189. var dto = locationData.Adapt<LocationPropertyUpdateDto>();
  190. var result = PmsCustomApi.Post<object>("UpdateLocationProperty", dto);
  191. if (result.code == 20000)
  192. {
  193. HandyControl.Controls.Growl.Success(result.message);
  194. }
  195. else
  196. {
  197. HandyControl.Controls.Growl.Warning(result.message);
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  203. }
  204. }
  205. private void ReleaseAgv()
  206. {
  207. try
  208. {
  209. var dto = new { LocationCode = SelectedVmData?.LocationCode };
  210. var result = PmsCustomApi.Post<object>("ReleaseAgv", dto);
  211. if (result.code == 20000)
  212. {
  213. HandyControl.Controls.Growl.Success(result.message);
  214. }
  215. else
  216. {
  217. HandyControl.Controls.Growl.Warning(result.message);
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. CLog.Instance.SystemLog.WriteExceptionCaller(ex);
  223. }
  224. }
  225. }
  226. }