SopPageViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using LogoForceTestApp.Modules.MainModule.Models;
  2. using Newtonsoft.Json;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using Repository;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Data;
  19. using System.Windows.Forms;
  20. using System.Windows.Media.Imaging;
  21. using System.Xml.Linq;
  22. using static System.Net.Mime.MediaTypeNames;
  23. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  24. {
  25. public class SopPageViewModel : BindableBase
  26. {
  27. IDialogService dialogService;//弹窗
  28. private readonly IRepository repository;
  29. private readonly IEventAggregator eventAggregator;
  30. public string ProName { get; set; }
  31. public DelegateCommand AddCommand1 { get; set; }
  32. public DelegateCommand AddCommand2 { get; set; }
  33. public DelegateCommand AddCommand3 { get; set; }
  34. public DelegateCommand SaveSopCmm { get; set; }
  35. public DelegateCommand ClearChart { get; set; }
  36. public ObservableCollection<LocalFile> FileInfos { get; set; }
  37. public ObservableCollection<LocalFile2> FileInfos2 { get; set; }
  38. public ObservableCollection<LocalFile3> FileInfos3 { get; set; }
  39. public DelegateCommand AddPic { get; set; }
  40. public SopPageViewModel(IDialogService dialogService, IRepository repository, IEventAggregator eventAggregator)
  41. {
  42. FileInfos = new ObservableCollection<LocalFile>();
  43. FileInfos2 = new ObservableCollection<LocalFile2>();
  44. FileInfos3 = new ObservableCollection<LocalFile3>();
  45. AddCommand1 = new DelegateCommand(Add1);
  46. AddCommand2 = new DelegateCommand(Add2);
  47. AddCommand3 = new DelegateCommand(Add3);
  48. ClearChart = new DelegateCommand(ClearCha);
  49. AddPic = new DelegateCommand(OpenPicFile);
  50. this.dialogService = dialogService;
  51. this.repository = repository;
  52. this.eventAggregator = eventAggregator;
  53. SaveSopCmm = new DelegateCommand(SaveSop);
  54. }
  55. public void ClearCha()
  56. {
  57. FileInfos.Clear();
  58. FileInfos2.Clear();
  59. FileInfos3.Clear();
  60. System.Windows.Controls.DataGrid dataGrid = new System.Windows.Controls.DataGrid();
  61. dataGrid.Items.Clear();
  62. }
  63. private void SaveSop()
  64. {
  65. Task.Run(() =>
  66. {
  67. #region 数据库之间的关联
  68. //SopFile sopFile = new SopFile();
  69. //sopFile.ProductName = ProName;
  70. //sopFile.ID = Guid.NewGuid();
  71. //repository.Add(sopFile);
  72. ////sopFile.LocalFile3Db = localFile3Dbs;
  73. ////sopFile.LocalFile2Db = localFile2Dbs;
  74. ////sopFile.LocalFileDb = localFileDbs;
  75. //foreach (var item in localFile3Dbs)
  76. //{
  77. // item.SopFile=sopFile;
  78. // repository.Update(item);
  79. //}
  80. //foreach (var item in localFile2Dbs)
  81. //{
  82. // item.SopFile = sopFile;
  83. // repository.Update(item);
  84. //}
  85. //foreach (var item in localFileDbs)
  86. //{
  87. // item.SopFile = sopFile;
  88. // repository.Update(item);
  89. //}
  90. #endregion
  91. GenaralFile genaralFile = new GenaralFile();
  92. genaralFile.FileInfos = FileInfos.ToList();
  93. genaralFile.FileInfos2 = FileInfos2.ToList();
  94. genaralFile.FileInfos3 = FileInfos3.ToList();
  95. genaralFile.ProductName = ProName;
  96. var json = JsonConvert.SerializeObject(genaralFile, Formatting.Indented);
  97. string sPath = @"D:\作业指导书\";
  98. if (!Directory.Exists(sPath))
  99. {
  100. Directory.CreateDirectory(sPath);
  101. }
  102. var path = sPath + $"{ProName}.json";
  103. using (var sw = new StreamWriter(path))
  104. {
  105. sw.Write(json);
  106. sw.Close();
  107. }
  108. System.Windows.Forms.MessageBox.Show("保存成功:" + ProName);
  109. eventAggregator.GetEvent<FileChangedEvent>().Publish();
  110. //CopyFolder(sPath, "//192.168.1.11/作业指导书/");
  111. });
  112. }
  113. #region 添加表格
  114. //private List<LocalFile3Db> localFile3Dbs = new();
  115. //private List<LocalFile2Db> localFile2Dbs = new();
  116. //private List<LocalFileDb> localFileDbs = new();
  117. private void Add3()
  118. {
  119. dialogService.ShowDialog("ChartPage3", c =>
  120. {
  121. var param = c.Parameters;
  122. var equipment = param.GetValue<string>("c1");
  123. var before = param.GetValue<string>("c2");
  124. var inn = param.GetValue<string>("c3");
  125. var after = param.GetValue<string>("c4");
  126. var lookOut = param.GetValue<string>("c5");
  127. if (c.Result.ToString() == "OK")
  128. {
  129. FileInfos3.Add(new LocalFile3
  130. {
  131. Equipment = equipment,
  132. Before = before,
  133. Inn = inn,
  134. After = after,
  135. LookOut = lookOut
  136. });
  137. //var sopFile = repository.GetFirstDefault<SopFile>(c => c.ProductName == ProName);
  138. //var cha3 = new LocalFile3Db()
  139. //{
  140. // Id = Guid.NewGuid(),
  141. // Equipment = equipment,
  142. // Before = before,
  143. // Inn = inn,
  144. // After = after,
  145. // LookOut = lookOut,
  146. //};
  147. //localFile3Dbs.Add(cha3);
  148. //repository.Add(cha3);
  149. }
  150. });
  151. }
  152. private void Add2()
  153. {
  154. dialogService.ShowDialog("ChartPage2", c =>
  155. {
  156. var param = c.Parameters;
  157. var num = param.GetValue<string>("c1");
  158. var materialNum = param.GetValue<string>("c2");
  159. var materialName = param.GetValue<string>("c3");
  160. var count = param.GetValue<string>("c4");
  161. var unit = param.GetValue<string>("c5");
  162. if (c.Result.ToString() == "OK")
  163. {
  164. FileInfos2.Add(new LocalFile2
  165. {
  166. Num = num,
  167. MaterialNum = materialNum,
  168. MaterialName = materialName,
  169. Count = count,
  170. Unit = unit
  171. });
  172. //var cha2 = new LocalFile2Db()
  173. //{
  174. // Id = Guid.NewGuid(),
  175. // Num = num,
  176. // MaterialNum = materialNum,
  177. // MaterialName = materialName,
  178. // Count = count,
  179. // Unit = unit
  180. //};
  181. //localFile2Dbs.Add(cha2);
  182. //repository.Add(cha2);
  183. }
  184. });
  185. }
  186. private void Add1()
  187. {
  188. dialogService.ShowDialog("ChartPage1", c =>
  189. {
  190. var param = c.Parameters;
  191. var fileNum = param.GetValue<string>("c1");
  192. var edition = param.GetValue<string>("c2");
  193. var date = param.GetValue<string>("c3");
  194. var type = param.GetValue<string>("c4");
  195. var people = param.GetValue<string>("c5");
  196. var name = param.GetValue<string>("c6");
  197. var process = param.GetValue<string>("c7");
  198. var workTime = param.GetValue<string>("c8");
  199. if (c.Result.ToString() == "OK")
  200. {
  201. FileInfos.Add(new LocalFile
  202. {
  203. FileNum = fileNum,
  204. Edition = edition,
  205. Date = date,
  206. Type = type,
  207. People = people,
  208. Name = name,
  209. Process = process,
  210. WorkTime = workTime
  211. });
  212. //var cha1 = new LocalFileDb()
  213. //{
  214. // Id = Guid.NewGuid(),
  215. // FileNum = fileNum,
  216. // Edition = edition,
  217. // Date = date,
  218. // Type = type,
  219. // People = people,
  220. // Name = name,
  221. // Process = process,
  222. // WorkTime = workTime
  223. //};
  224. //localFileDbs.Add(cha1);
  225. //repository.Add(cha1);
  226. }
  227. });
  228. }
  229. #endregion
  230. #region 显示图片
  231. public void OpenPicFile()
  232. {
  233. FolderBrowserDialog dialog = new FolderBrowserDialog();
  234. dialog.Description = "请选择文件路径";
  235. string imgPath = "";
  236. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  237. {
  238. imgPath = dialog.SelectedPath + @"\";
  239. LoadPicture(imgPath);
  240. }
  241. }
  242. private ObservableCollection<PictureInfo> imageList = new ObservableCollection<PictureInfo>();
  243. public ObservableCollection<PictureInfo> ImageList
  244. {
  245. get { return imageList; }
  246. set { imageList = value; }
  247. }
  248. object lockobj = new object();
  249. /// <summary>
  250. /// 加载缺陷图片
  251. /// </summary>
  252. /// <param name="strPath"></param>
  253. public void LoadPicture(string strPath)
  254. {
  255. ImageList.Clear();
  256. BindingOperations.EnableCollectionSynchronization(ImageList, lockobj);
  257. string folderFullName = strPath;//图片文件夹路径
  258. Task.Run(() =>
  259. {
  260. DirectoryInfo TheFolder = new DirectoryInfo(folderFullName);
  261. var res = TheFolder.GetFiles();
  262. for (int i = 0; i < res.Length; i++)
  263. {
  264. PictureInfo pictureInfo = new PictureInfo();
  265. pictureInfo.PicturePath = res[i].DirectoryName + @"\" + res[i].Name;
  266. Uri uri = new Uri(pictureInfo.PicturePath, UriKind.Absolute);
  267. //BitmapImage myimg =ImageProcess.GetBitImage(uri);
  268. pictureInfo.Info = res[i].Name;
  269. ImageList.Add(pictureInfo);
  270. }
  271. });
  272. }
  273. #endregion
  274. /// <summary>
  275. /// 复制文件夹中内容
  276. /// </summary>
  277. /// <param name="sourceFolder">源文件夹</param>
  278. /// <param name="destFolder">目标文件夹</param>
  279. public static void CopyFolder(string sourceFolder, string destFolder)
  280. {
  281. // 检查目标文件夹是否存在,如果不存在则创建
  282. if (!Directory.Exists(destFolder))
  283. {
  284. Directory.CreateDirectory(destFolder);
  285. }
  286. // 获取源文件夹中的所有文件的完整路径
  287. string[] files = Directory.GetFiles(sourceFolder);
  288. // 遍历所有文件
  289. foreach (string file in files)
  290. {
  291. // 获取文件信息
  292. FileInfo fileInfo = new FileInfo(file);
  293. // 构造目标文件的路径
  294. string newPath = Path.Combine(destFolder, fileInfo.Name);
  295. // 将文件复制到目标文件夹,如果目标文件已存在则覆盖
  296. File.Copy(file, newPath, true);
  297. }
  298. // 获取源文件夹中的所有子文件夹
  299. string[] dirs = Directory.GetDirectories(sourceFolder);
  300. // 递归复制所有子文件夹
  301. foreach (string dir in dirs)
  302. {
  303. // 获取子文件夹的名称
  304. string dirName = Path.GetFileName(dir);
  305. // 构造目标子文件夹的路径
  306. string newDirPath = Path.Combine(destFolder, dirName);
  307. // 递归调用自身来复制子文件夹
  308. CopyFolder(dir, newDirPath);
  309. }
  310. }
  311. }
  312. public class LocalFile : BindableBase
  313. {
  314. public string FileNum { get; set; }
  315. public string Edition { get; set; }
  316. public string Date { get; set; }
  317. public string Type { get; set; }
  318. public string People { get; set; }
  319. public string Name { get; set; }
  320. public string Process { get; set; }
  321. public string WorkTime { get; set; }
  322. }
  323. public class LocalFile2 : BindableBase
  324. {
  325. public string Num { get; set; }
  326. public string MaterialNum { get; set; }
  327. public string MaterialName { get; set; }
  328. public string Count { get; set; }
  329. public string Unit { get; set; }
  330. }
  331. public class LocalFile3 : BindableBase
  332. {
  333. public string Equipment { get; set; }
  334. public string Before { get; set; }
  335. public string Inn { get; set; }
  336. public string After { get; set; }
  337. public string LookOut { get; set; }
  338. }
  339. public class GenaralFile
  340. {
  341. public string ProductName { get; set; }
  342. public List<LocalFile> FileInfos { get; set; }
  343. public List<LocalFile2> FileInfos2 { get; set; }
  344. public List<LocalFile3> FileInfos3 { get; set; }
  345. }
  346. }