RecipeManageViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.Dialog;
  4. using Aitex.Core.UI.MVVM;
  5. using Aitex.Core.Utilities;
  6. using Caliburn.Micro.Core;
  7. using MECF.Framework.Common.DataCenter;
  8. using MECF.Framework.Common.RecipeCenter;
  9. using CyberX8_Core;
  10. using CyberX8_MainPages.PMs;
  11. using CyberX8_Themes.UserControls;
  12. using Prism.Mvvm;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.ComponentModel;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Runtime.Remoting.Contexts;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows;
  24. using System.Windows.Input;
  25. namespace CyberX8_MainPages.ViewModels
  26. {
  27. public class RecipeManageViewModel : BindableBase
  28. {
  29. #region 常量
  30. private const string ENGINEERING = "Engineering";
  31. private const string PRODUCTION = "Production";
  32. private const string HVD = "hvd";
  33. #endregion
  34. #region 内部变量
  35. /// <summary>
  36. /// Recipe节点字典
  37. /// </summary>
  38. private Dictionary<string, RecipeNode> _recipeNodeDic = new Dictionary<string, RecipeNode>();
  39. private Dictionary<string, RecipeNode> _recipeName = new Dictionary<string, RecipeNode>();
  40. /// <summary>
  41. /// Recipe节点集合
  42. /// </summary>
  43. private ObservableCollection<RecipeNode> _recipeNodes;
  44. /// <summary>
  45. /// Archive节点集合
  46. /// </summary>
  47. private ObservableCollection<RecipeNode> _archiveNodes;
  48. /// <summary>
  49. /// uiRecipeManager
  50. /// </summary>
  51. private UiRecipeManager _uiRecipeManager = new UiRecipeManager();
  52. // 选中文件名
  53. private List<string> _filePaths = new List<string>();
  54. private List<string> _fileNames = new List<string>();
  55. //目录文件
  56. List<string> ProductionFileList = new List<string>();
  57. private SequenceRecipe _recipe;
  58. #endregion
  59. #region 属性
  60. public SequenceRecipe SeqRecipe
  61. {
  62. get { return _recipe; }
  63. set { SetProperty(ref _recipe, value); }
  64. }
  65. public List<string> FilePaths
  66. {
  67. get { return _filePaths; }
  68. set { SetProperty(ref _filePaths, value); }
  69. }
  70. public List<string> FileNames
  71. {
  72. get { return _fileNames; }
  73. set { SetProperty(ref _fileNames, value); }
  74. }
  75. public ObservableCollection<RecipeNode> RecipeNodes
  76. {
  77. get { return _recipeNodes; }
  78. set { SetProperty(ref _recipeNodes, value); }
  79. }
  80. public ObservableCollection<RecipeNode> ArchiveNodes
  81. {
  82. get { return _archiveNodes; }
  83. set { SetProperty(ref _archiveNodes, value); }
  84. }
  85. #endregion
  86. #region 指令
  87. [IgnorePropertyChange]
  88. public ICommand OperationCommand
  89. {
  90. get;
  91. private set;
  92. }
  93. [IgnorePropertyChange]
  94. public ICommand SelectedCommand
  95. {
  96. get;
  97. private set;
  98. }
  99. [IgnorePropertyChange]
  100. public ICommand PromoteCommand { get; private set; }
  101. [IgnorePropertyChange]
  102. public ICommand DeleteRecipeCommand { get; private set; }
  103. #endregion
  104. /// <summary>
  105. /// 构造函数
  106. /// </summary>
  107. public RecipeManageViewModel()
  108. {
  109. OperationCommand = new DelegateCommand<object>(SelectionChangedAction);
  110. PromoteCommand = new DelegateCommand<object>(Promote);
  111. DeleteRecipeCommand=new DelegateCommand<object>(DeleteFile);
  112. }
  113. /// <summary>
  114. /// 加载数据
  115. /// </summary>
  116. public void LoadData(string system)
  117. {
  118. List<string> directoryList = new List<string>();
  119. directoryList.Add(ENGINEERING);
  120. directoryList.Add(PRODUCTION);
  121. RecipeNodes = _uiRecipeManager.GetRecipeByDirectoryList(directoryList);
  122. directoryList.Clear();
  123. directoryList.Add("Archive");
  124. ArchiveNodes = _uiRecipeManager.GetRecipeByDirectoryList(directoryList);
  125. _recipeNodeDic.Clear();
  126. _recipeName.Clear();
  127. InitializeDictionary(RecipeNodes);
  128. InitializeDictionary(ArchiveNodes);
  129. checkSeqRecipe(RecipeNodes);
  130. FilePaths.Clear();
  131. FileNames.Clear();
  132. }
  133. /// <summary>
  134. /// 初始化字典
  135. /// </summary>
  136. /// <param name="nodes"></param>
  137. private void InitializeDictionary(ObservableCollection<RecipeNode> nodes)
  138. {
  139. if (nodes != null)
  140. {
  141. foreach (var node in nodes)
  142. {
  143. if (node.NodeType == RecipeNodeType.File)
  144. {
  145. _recipeNodeDic[node.Name] = node;
  146. }
  147. if ((node.NodeType == RecipeNodeType.File) && (node.RecipeLocation == "Production"))
  148. {
  149. _recipeName[node.FileName] = node;
  150. }
  151. InitializeDictionary(node.Children);
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 操作
  157. /// </summary>
  158. /// <param name="param"></param>
  159. private void SelectionChangedAction(object param)
  160. {
  161. if (param != null)
  162. {
  163. if (_recipeNodeDic.ContainsKey(param.ToString()))
  164. {
  165. }
  166. else
  167. {
  168. if (param.ToString() == ENGINEERING)
  169. {
  170. }
  171. else
  172. {
  173. }
  174. }
  175. }
  176. }
  177. private void checkSeqRecipe(ObservableCollection<RecipeNode> nodes)
  178. {
  179. //检验sequence文件是否合法
  180. if (nodes != null)
  181. {
  182. foreach (var node in nodes)
  183. {
  184. if((node.NodeType == RecipeNodeType.Directory) && (node.Name == PRODUCTION))
  185. {
  186. node.FileName = PRODUCTION;
  187. }
  188. if ((node.NodeType == RecipeNodeType.Directory) && (node.Name == ENGINEERING))
  189. {
  190. node.FileName = ENGINEERING;
  191. }
  192. if ((node.NodeType == RecipeNodeType.File) && (node.RecipeLocation == "Production") && node.FileName.Contains("seq.rcp"))
  193. {
  194. if (node.FileName.Contains("seq.rcp"))
  195. {
  196. bool isValid = true;
  197. SeqRecipe = _uiRecipeManager.LoadRecipe<SequenceRecipe>(node.RecipeFullFileName);
  198. if (SeqRecipe != null && SeqRecipe.Recipes.Count != 0)
  199. {
  200. foreach (string recipe in SeqRecipe.Recipes)
  201. {
  202. if (!(_recipeName.Keys.Contains(recipe)))
  203. {
  204. isValid = false;
  205. }
  206. }
  207. if (!isValid)
  208. {
  209. node.NodeType = RecipeNodeType.Error;
  210. }
  211. }
  212. }
  213. }
  214. checkSeqRecipe(node.Children);
  215. }
  216. }
  217. }
  218. private void Promote(object param)
  219. {
  220. string SelectedFileNames = null;
  221. if (FilePaths.Count == 0)
  222. {
  223. return;
  224. }
  225. foreach (var fileItem in FilePaths)
  226. {
  227. string[] _content = fileItem.Split('\\');
  228. string projectName = _content[_content.Length - 2];
  229. if ("Production".Equals(projectName))
  230. {
  231. MessageBox.Show($"所选文件包含Production文件,无法执行该操作!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
  232. return;
  233. }
  234. string _fileName = _content[_content.Length - 1];
  235. SelectedFileNames += _fileName + '\r';
  236. ////检验sequence文件是否合法
  237. //if (_fileName.Contains("seq.rcp"))
  238. //{
  239. // bool isValid = true;
  240. // string _notValidFileNames = null;
  241. // SeqRecipe = _uiRecipeManager.LoadRecipe<SequenceRecipe>(fileItem);
  242. // if (SeqRecipe != null && SeqRecipe.Recipes.Count != 0)
  243. // {
  244. // foreach (string recipe in SeqRecipe.Recipes)
  245. // {
  246. // if (!(FileNames.Contains(recipe)))
  247. // {
  248. // isValid = false;
  249. // _notValidFileNames += recipe + ',';
  250. // }
  251. // }
  252. // if (!isValid)
  253. // {
  254. // MessageBox.Show($"未选中{_fileName}中的{_notValidFileNames}文件!", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
  255. // return;
  256. // }
  257. // }
  258. //}
  259. //检验文件是否已存在,已存在是否覆盖文件
  260. foreach (string item in _recipeName.Keys)
  261. {
  262. if (item == _fileName)
  263. {
  264. if (MessageBox.Show($"{_fileName} 已经存在是否覆盖?", "保存", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  265. {
  266. continue;
  267. }
  268. else
  269. {
  270. return;
  271. }
  272. }
  273. }
  274. }
  275. if (MessageBox.Show($"确认需要将{SelectedFileNames}文件移至production吗?", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  276. {
  277. SaveFile(FilePaths);
  278. }
  279. }
  280. private void SaveFile(List<string> FilePaths)
  281. {
  282. _uiRecipeManager.PromoteRecipe(FilePaths);
  283. LoadData("");
  284. }
  285. private void DeleteFile(object param)
  286. {
  287. string FileNames = null;
  288. if (FilePaths.Count == 0)
  289. {
  290. return;
  291. }
  292. if (MessageBox.Show($"确认删除{FileNames}文件吗?", "确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  293. {
  294. foreach (var fileItem in FilePaths)
  295. {
  296. string[] _content = fileItem.Split('\\');
  297. string _fileName = _content[_content.Length - 1];
  298. string _directoryName = _content[_content.Length - 2];
  299. FileNames += _directoryName + '/' + _fileName + '\r';
  300. //if (!(File.Exists(fileItem)))
  301. //{
  302. // //MessageBox.Show($"不存在文件{_directoryName}/{_fileName}!", "Aitex", MessageBoxButton.OK, MessageBoxImage.Error);
  303. // MessageBox.Show($"不存在文件{fileItem}!", "Aitex", MessageBoxButton.OK, MessageBoxImage.Error);
  304. // return;
  305. //}
  306. _uiRecipeManager.DeleteRecipeByFullPath(fileItem);
  307. }
  308. }
  309. LoadData("");
  310. }
  311. }
  312. }