CopySchViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using AutoMapper;
  2. using BizService;
  3. using ImTools;
  4. using Microsoft.Extensions.Logging;
  5. using Model.Dto;
  6. using Model.Entities;
  7. using PLCTool.Common;
  8. using PLCTool.Models;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Linq;
  17. using System.Net.NetworkInformation;
  18. using System.Security.Cryptography.Xml;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Automation;
  23. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
  24. namespace PLCTool.ViewModels.BasicConfigViewModel
  25. {
  26. internal class CopySchViewModel : BindableBase, IDialogAware
  27. {
  28. private readonly IMapper _mapper;
  29. private readonly ILogger _logger;
  30. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  31. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  32. private string schNameAgo = String.Empty;
  33. public CopySchViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IMapper mapper, ILogger logger)
  34. {
  35. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  36. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  37. _mapper = mapper;
  38. _logger = logger;
  39. SureCommand = new DelegateCommand<string>(Sure);
  40. UpCommand = new DelegateCommand<object>(DataUp);
  41. DownCommand = new DelegateCommand<object>(DataDown);
  42. SelectAllCommand = new DelegateCommand<object>(SelectAll);
  43. UnSelectAllCommand = new DelegateCommand<object>(UnSelecttAll);
  44. }
  45. private void UnSelecttAll(object obj)
  46. {
  47. SchProjectList.ToList().FindAll(p => p.IsSelected = false);
  48. }
  49. private void SelectAll(object obj)
  50. {
  51. SchProjectList.ToList().FindAll(p => p.IsSelected = true);
  52. }
  53. /// <summary>
  54. /// 下移
  55. /// </summary>
  56. /// <param name="obj"></param>
  57. private void DataDown(object obj)
  58. {
  59. int id = Convert.ToInt32(obj);
  60. var findScheExist = SchProjectList.FirstOrDefault(x => x.Id == id);
  61. if (findScheExist != null)
  62. {
  63. int index = SchProjectList.IndexOf(findScheExist);
  64. if(index!=-1)
  65. {
  66. if (index == SchProjectList.Count - 1)
  67. return;
  68. SchProjectList.RemoveAt(index);
  69. index = index + 1;
  70. SchProjectList.Insert(index, findScheExist);
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 上移
  76. /// </summary>
  77. /// <param name="obj"></param>
  78. private void DataUp(object obj)
  79. {
  80. int id = Convert.ToInt32(obj);
  81. var findScheExist = SchProjectList.FirstOrDefault(x=>x.Id==id);
  82. if (findScheExist != null)
  83. {
  84. int index = SchProjectList.IndexOf(findScheExist);
  85. if(index!=-1)
  86. {
  87. if (index == 0)
  88. return;
  89. SchProjectList.RemoveAt(index);
  90. index = index - 1;
  91. SchProjectList.Insert(index, findScheExist);
  92. }
  93. }
  94. }
  95. private void Sure(string obj)
  96. {
  97. //查找是否有这个测试方案
  98. var findExist = _basicPlcTestSchemeService.FindByName(ScheduleName);
  99. if (findExist != null)
  100. {
  101. MessageBoxResult boxResult = MessageBox.Show("已有次测试方案,请填写其他方案名!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  102. return;
  103. }
  104. //查找以前测试方案的信息
  105. var finsSche = _basicPlcTestSchemeService.FindByName(schNameAgo);
  106. if (finsSche != null)
  107. {
  108. //增加测试方案到数据库
  109. bas_plc_test_scheme schDto = new bas_plc_test_scheme();
  110. schDto.scheme_name = ScheduleName;
  111. schDto.device_name = DeviceName;
  112. schDto.create_by = Appsession.UserName;
  113. schDto.create_time = DateTime.Now;
  114. schDto.update_time = DateTime.Now;
  115. schDto.update_by = Appsession.UserName;
  116. _basicPlcTestSchemeService.Add(schDto);
  117. //以前复制的schID
  118. long schID = finsSche.scheme_id;
  119. //查找此方案下的所有的测试项,并增加到测试项详细表中
  120. long? schINewID = _basicPlcTestSchemeService.FindByName(ScheduleName)?.scheme_id;
  121. //这里要注意顺序,所以用了SchProjectList循环
  122. foreach (var sch in SchProjectList)
  123. {
  124. if(sch.IsSelected)
  125. {
  126. int dtlId = Convert.ToInt32(sch.Id);
  127. var unit = _basicPlcTestSchemeDtlService.Find(dtlId);
  128. if (unit != null)
  129. {
  130. bas_plc_test_scheme_dtl schDtlDto = new bas_plc_test_scheme_dtl();
  131. schDtlDto.scheme_id = schINewID;
  132. schDtlDto.item_name = unit.item_name;
  133. schDtlDto.item_type = unit.item_type;
  134. schDtlDto.precondition = unit.precondition;
  135. schDtlDto.precondition_describe = unit.precondition_describe;
  136. schDtlDto.action = unit.action;
  137. schDtlDto.action_describe = unit.action_describe;
  138. schDtlDto.judgement_result = unit.judgement_result;
  139. schDtlDto.judgement_result_describe = unit.judgement_result_describe;
  140. schDtlDto.create_by = Appsession.UserName;
  141. schDtlDto.create_time = DateTime.Now;
  142. schDtlDto.update_time = DateTime.Now;
  143. schDtlDto.update_by = Appsession.UserName;
  144. bool isAddSucc = _basicPlcTestSchemeDtlService.Add(schDtlDto);
  145. }
  146. }
  147. }
  148. _logger.LogInformation($"复制方案成功。方案名{ScheduleName}");
  149. }
  150. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  151. }
  152. public string Title { set; get; } ="复制方案";
  153. public event Action<IDialogResult> RequestClose;
  154. public bool CanCloseDialog()
  155. {
  156. return true;
  157. }
  158. public void OnDialogClosed()
  159. {
  160. }
  161. public void OnDialogOpened(IDialogParameters parameters)
  162. {
  163. var getMsg = parameters.GetValues<string>("Key");
  164. if (getMsg != null)
  165. {
  166. foreach (var item in getMsg)
  167. {
  168. schNameAgo = item.ToString();
  169. ScheduleName = schNameAgo + "_copy";
  170. var findSche = _basicPlcTestSchemeService.FindByName(schNameAgo);
  171. if (findSche != null)
  172. {
  173. //显示设备名称
  174. DeviceName= findSche.device_name;
  175. long schID = findSche.scheme_id;
  176. //查找此方案下的所有的测试项,并增加到表格
  177. var schDtls = _basicPlcTestSchemeDtlService.QueryList()?.FindAll(x => x.scheme_id == schID);
  178. foreach (var unit in schDtls)
  179. {
  180. string itemType = string.Empty;
  181. switch (unit.item_type)
  182. {
  183. case 0:
  184. itemType = "前置项";
  185. break;
  186. case 1:
  187. itemType = "PLC点位测试项";
  188. break;
  189. case 2:
  190. itemType = "Robot动作测试";
  191. break;
  192. }
  193. SchProjectList.Add(new SelectItemModel
  194. {
  195. Id = unit.scheme_dtl_id,
  196. ItemName = unit.item_name,
  197. ItemType = itemType,
  198. }); ;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. #region 命令绑定
  205. public DelegateCommand<string> SureCommand { set; get; }
  206. public DelegateCommand<object> DownCommand { set; get; }
  207. public DelegateCommand<object> UpCommand { set; get; }
  208. public DelegateCommand<object> SelectAllCommand { set; get; }
  209. public DelegateCommand<object> UnSelectAllCommand { set; get; }
  210. #endregion
  211. #region 数据绑定
  212. /// <summary>
  213. /// 测试方案编码
  214. /// </summary>
  215. private string scheduleName;
  216. public string ScheduleName
  217. {
  218. get { return scheduleName; }
  219. set { scheduleName = value; RaisePropertyChanged(); }
  220. }
  221. /// <summary>
  222. /// 设备名称
  223. /// </summary>
  224. private string deviceName;
  225. public string DeviceName
  226. {
  227. get { return deviceName; }
  228. set { deviceName = value; RaisePropertyChanged(); }
  229. }
  230. private ObservableCollection<SelectItemModel> schProjectList = new ObservableCollection<SelectItemModel>();
  231. public ObservableCollection<SelectItemModel> SchProjectList
  232. {
  233. get { return schProjectList; }
  234. set { SetProperty(ref schProjectList, value); }
  235. }
  236. #endregion
  237. }
  238. }