QueryViewModel.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using AutoMapper;
  2. using BizService;
  3. using Microsoft.Extensions.Logging;
  4. using MiniExcelLibs;
  5. using Model.Dto;
  6. using Model.Entities;
  7. using NetTaste;
  8. using Newtonsoft.Json;
  9. using PLCTool.Pdf;
  10. using Prism.Commands;
  11. using Prism.Mvvm;
  12. using Prism.Services.Dialogs;
  13. using QuestPDF;
  14. using QuestPDF.Fluent;
  15. using QuestPDF.Helpers;
  16. using QuestPDF.Infrastructure;
  17. using QuestPDF.Previewer;
  18. using SqlSugar;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Linq;
  23. using System.Reflection.Metadata;
  24. using System.Text;
  25. using System.Threading.Tasks;
  26. using System.Windows;
  27. using WPFDevelopers.Controls.Runtimes.Shell32;
  28. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
  29. namespace PLCTool.ViewModels.BusinessManageViewModel
  30. {
  31. internal class QueryViewModel : BindableBase
  32. {
  33. private readonly IDialogService _dialog;
  34. private readonly IOptionConfigService _optionConfigService;
  35. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  36. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  37. private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
  38. private readonly IBasicDeviceService _iBasicDeviceService;
  39. private readonly ILogger _logger;
  40. private readonly IMapper _mapper;
  41. private readonly IMenuService _menuService;
  42. private List<SchDtlWithResultModel> allConfigList = new List<SchDtlWithResultModel>();//所有方案
  43. private List<SchDtlWithResultModel> conditionConfig = new List<SchDtlWithResultModel>();//符合条件的方案
  44. private const int PageCount = 1; //每一页显示个数
  45. public QueryViewModel(IDialogService dialog, IMenuService menuService, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordDtlService iBizTestRecordDtlService, IBasicDeviceService iBasicDeviceService,IMapper mapper, ILogger logger)
  46. {
  47. _dialog = dialog;
  48. _optionConfigService = optionConfigService;
  49. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  50. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  51. _iBizTestRecordDtlService = iBizTestRecordDtlService;
  52. _iBasicDeviceService = iBasicDeviceService;
  53. _logger = logger;
  54. _mapper = mapper;
  55. _menuService = menuService;
  56. QueryCommand = new DelegateCommand<object>(Query);
  57. ResetCommand = new DelegateCommand<object>(Reset);
  58. ExportCommand = new DelegateCommand<string>(Export);
  59. OnLoadCommand = new DelegateCommand(OnLoad);
  60. CheckDetailCommand = new DelegateCommand<object>(CheckDetail);
  61. PdfReportCommand = new DelegateCommand<object>(CreatePdf);
  62. GetConfigOption();
  63. }
  64. #region 私有方法
  65. /// <summary>
  66. /// pdf报告相关信息
  67. /// </summary>
  68. /// <param name="schDtlId"></param>
  69. /// <returns></returns>
  70. private InvoiceModel GetReportPLCInfo(int schDtlId)
  71. {
  72. //测试方案明细主键ID
  73. InvoiceModel invoiceModel = new InvoiceModel();
  74. invoiceModel.ReportPLCModels = new List<ReportPLCModel>();
  75. //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  76. var findEntity = _basicPlcTestSchemeDtlService.Find(schDtlId);
  77. var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
  78. if (findresult != null)
  79. {
  80. //根据方案id 在方案表中查找方案名 设备名
  81. int schID = Convert.ToInt32(findresult.SchemeId);
  82. var findSch = _basicPlcTestSchemeService.Find(schID);
  83. if (findSch != null)
  84. {
  85. invoiceModel.SchemeName = findSch.scheme_name;
  86. invoiceModel.DeviceName = findSch.devicekind_name;
  87. }
  88. //此测试方案下的所有测试项
  89. var allResult = allConfigList.FindAll(X => X.SchemeName == findSch.scheme_name);
  90. foreach (var each in allResult)
  91. {
  92. long schDetailId = each.SchemeId;
  93. //根据测试方案明细id查找结果
  94. var recordDetail = _iBizTestRecordDtlService.FindRecordDetailBySchDtlID(schDetailId);
  95. if (recordDetail != null)
  96. { //前置项结果解析
  97. string preconStr = recordDetail.precondition_final?.ToString();
  98. if (!string.IsNullOrEmpty(preconStr))
  99. {
  100. JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
  101. foreach (var detail in preconditionModel.DetailInfo)
  102. {
  103. invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
  104. {
  105. ItemName = each.ItemName,
  106. ItemType = each.ItemType,
  107. Type = "前置项",
  108. JudgeType = preconditionModel.ItemType,
  109. SelectLogic = preconditionModel.ItemLogical,
  110. Detail = preconditionModel.Description,
  111. Id = detail.Id,
  112. PlcAddress = detail.PlcAddress,
  113. PlcItem = detail.PlcItem,
  114. PlcValue = detail.PlcValue,
  115. Remark = detail.Remark,
  116. TestTime = detail.TestTime,
  117. RealValue = detail.RealValue,
  118. TestResult = detail.TestResult,
  119. });
  120. }
  121. }
  122. //输入项解析
  123. string inStr = recordDetail.action_final?.ToString();
  124. if (!string.IsNullOrEmpty(inStr))
  125. {
  126. JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
  127. foreach (var detail in inModel.DetailInfo)
  128. {
  129. invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
  130. {
  131. ItemName = each.ItemName,
  132. ItemType = each.ItemType,
  133. Type = "输入项",
  134. JudgeType = inModel.ItemType,
  135. SelectLogic = inModel.ItemLogical,
  136. Detail = inModel.Description,
  137. Id = detail.Id,
  138. PlcAddress = detail.PlcAddress,
  139. PlcItem = detail.PlcItem,
  140. PlcValue = detail.PlcValue,
  141. Remark = detail.Remark,
  142. TestTime = detail.TestTime,
  143. RealValue = detail.RealValue,
  144. TestResult = detail.TestResult,
  145. });
  146. }
  147. }
  148. //输出项解析
  149. string outStr = recordDetail.judgement_result_final?.ToString();
  150. if (!string.IsNullOrEmpty(outStr))
  151. {
  152. JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
  153. foreach (var detail in outModel.DetailInfo)
  154. {
  155. invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
  156. {
  157. ItemName = each.ItemName,
  158. ItemType = each.ItemType,
  159. Type = "结果项",
  160. JudgeType = outModel.ItemType,
  161. SelectLogic = outModel.ItemLogical,
  162. Detail = outModel.Description,
  163. Id = detail.Id,
  164. PlcAddress = detail.PlcAddress,
  165. PlcItem = detail.PlcItem,
  166. PlcValue = detail.PlcValue,
  167. Remark = detail.Remark,
  168. TestTime = detail.TestTime,
  169. RealValue = detail.RealValue,
  170. TestResult = detail.TestResult,
  171. });
  172. }
  173. }
  174. }
  175. }
  176. //判断方案结果是否通过
  177. var testCount = allConfigList.FindAll(X => X.SchemeName == findSch.scheme_name&&X.TestResult=="通过")?.Count;
  178. if(testCount== allResult.Count)
  179. {
  180. invoiceModel.FinalReuslt = "通过";
  181. }
  182. else
  183. {
  184. invoiceModel.FinalReuslt = "不通过";
  185. }
  186. }
  187. return invoiceModel;
  188. }
  189. /// <summary>
  190. /// 生成pdf
  191. /// </summary>
  192. /// <param name="obj"></param>
  193. public void CreatePdf(object obj)
  194. {
  195. int id = Convert.ToInt32(obj);
  196. try
  197. {
  198. Settings.License = LicenseType.Community;
  199. var model = GetReportPLCInfo(id);
  200. var document = new InvoiceDocument(model);
  201. //pdf名称为轴编号+时间
  202. //输出文件
  203. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  204. {
  205. //设置文件类型
  206. //书写规则例如:txt files(*.txt)|*.txt
  207. Filter = "pdf文件|*.pdf",
  208. //设置默认文件名(可以不设置)
  209. FileName = "报表" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now),
  210. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  211. AddExtension = true,
  212. //保存对话框是否记忆上次打开的目录
  213. RestoreDirectory = true
  214. })
  215. {
  216. //点了保存按钮进入
  217. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  218. {
  219. //获得文件路径
  220. string localFilePath = saveFileDialog.FileName.ToString();
  221. document.GeneratePdf(localFilePath);
  222. }
  223. }
  224. }
  225. catch(Exception ex)
  226. {
  227. _logger.LogError(ex.ToString());
  228. }
  229. }
  230. private void CheckDetail(object obj)
  231. {
  232. //测试方案明细主键ID
  233. long id = Convert.ToInt64(obj);
  234. DialogParameters parm = new DialogParameters();
  235. parm.Add("Key", id);
  236. //弹出详情对话框
  237. //弹出详情对话框
  238. _dialog.ShowDialog("TestResultDetailView", parm, async callback =>
  239. {
  240. if (callback.Result == ButtonResult.OK)
  241. {
  242. //更新表格,重新获取
  243. GetContent();
  244. }
  245. });
  246. }
  247. /// <summary>
  248. /// 每次进入页面加载
  249. /// </summary>
  250. private void OnLoad()
  251. {
  252. GetContent();
  253. }
  254. private void Reset(object obj)
  255. {
  256. ScheduleName = string.Empty;
  257. DeviceKindName = string.Empty;
  258. TestName = string.Empty;
  259. SelectTest = string.Empty;
  260. StartTime = DateTime.MinValue;
  261. EndTime = DateTime.MinValue;
  262. }
  263. private void Query(object obj)
  264. {
  265. if (StartTime > EndTime)
  266. {
  267. MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
  268. return;
  269. }
  270. conditionConfig = (from a in allConfigList
  271. where (EndTime == DateTime.MinValue ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
  272. && (string.IsNullOrEmpty(ScheduleName) ? true : (a.SchemeName == ScheduleName))
  273. && (string.IsNullOrEmpty(TestName) ? true : (a.ItemName == TestName))
  274. && (string.IsNullOrEmpty(DeviceKindName) ? true : (a.DeviceKindName == DeviceKindName))
  275. && (string.IsNullOrEmpty(SelectTest) ? true : (a.ItemType == SelectTest))
  276. select a).ToList();
  277. //默认显示的第一页
  278. conditionConfig = conditionConfig.OrderBy(x => x.SchemeName).ThenBy(x => x.SchemeId).ToList();
  279. Getpage();
  280. //BaseConfigList = new ObservableCollection<BasicPlcTestSchemeDto>(conditionConfig);
  281. }
  282. /// <summary>
  283. /// 获取配置
  284. /// </summary>
  285. private void GetConfigOption()
  286. {
  287. var configList = _optionConfigService.QueryList();
  288. var _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  289. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  290. TestKinds = new List<string>();
  291. foreach (var test in tests)
  292. {
  293. TestKinds.Add(test.ContentOption);
  294. }
  295. }
  296. /// <summary>
  297. /// excel导出
  298. /// </summary>
  299. /// <param name="obj"></param>
  300. private void Export(object obj)
  301. {
  302. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  303. {
  304. //设置文件类型
  305. //书写规则例如:txt files(*.txt)|*.txt
  306. Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
  307. //设置默认文件名(可以不设置)
  308. FileName = "测试记录" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now),
  309. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  310. AddExtension = true,
  311. //保存对话框是否记忆上次打开的目录
  312. RestoreDirectory = true
  313. })
  314. {
  315. // Show save file dialog box
  316. //点了保存按钮进入
  317. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  318. {
  319. try
  320. {
  321. //获得文件路径
  322. string localFilePath = saveFileDialog.FileName.ToString();
  323. //string filePathWithName = localFilePath + ".xlsx";
  324. //获取文件内容
  325. MiniExcel.SaveAs(localFilePath, BaseConfigList);
  326. }
  327. catch (Exception ex)
  328. {
  329. _logger.LogError(ex.ToString());
  330. }
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// 获取解决方案
  336. /// </summary>
  337. private void GetContent()
  338. {
  339. //所有测试方案
  340. var schlist = _basicPlcTestSchemeService.QueryList();
  341. var configList = _mapper.Map<List<bas_plc_test_scheme>, List<BasicPlcTestSchemeDto>>(schlist);
  342. //清空集合
  343. BaseConfigList.Clear();
  344. allConfigList.Clear();
  345. conditionConfig.Clear();
  346. //查找测试项名称
  347. foreach (var sch in configList)
  348. {
  349. long schId = sch.SchemeId;
  350. //在解决方案详细表中查找所有的测试项
  351. var items = _basicPlcTestSchemeDtlService.FindAllBySchId(schId);
  352. foreach (var item in items)
  353. {
  354. //上面是测试方案明细
  355. SchDtlWithResultModel basicPlcTestSchemeDto = new SchDtlWithResultModel();
  356. basicPlcTestSchemeDto.SchemeId = item.scheme_dtl_id;
  357. basicPlcTestSchemeDto.SchemeName = sch.SchemeName;
  358. basicPlcTestSchemeDto.DeviceKindName = sch.DeviceKindName;
  359. switch (item.item_type)
  360. {
  361. case 0:
  362. basicPlcTestSchemeDto.ItemType = "前置项";
  363. break;
  364. case 1:
  365. basicPlcTestSchemeDto.ItemType = "PLC点位测试项";
  366. break;
  367. case 2:
  368. basicPlcTestSchemeDto.ItemType = "Robot动作测试";
  369. break;
  370. }
  371. basicPlcTestSchemeDto.ItemName = item.item_name;
  372. basicPlcTestSchemeDto.CreateBy = sch.CreateBy;
  373. basicPlcTestSchemeDto.CreateTime = sch.CreateTime;
  374. //这里是测试项 结果和 测试记录明细状态
  375. var recordDetail=_iBizTestRecordDtlService.FindRecordDetailBySchDtlID(item.scheme_dtl_id);
  376. if(recordDetail != null)
  377. {
  378. basicPlcTestSchemeDto.StartTestTime = recordDetail.start_test_time.Value;
  379. basicPlcTestSchemeDto.FinishTestTime = recordDetail.finish_test_time.Value;
  380. basicPlcTestSchemeDto.TestMode= recordDetail.test_mode;
  381. switch (recordDetail.test_result)
  382. {
  383. case 0:
  384. basicPlcTestSchemeDto.TestResult = "不通过";
  385. break;
  386. case 1:
  387. basicPlcTestSchemeDto.TestResult = "通过";
  388. break;
  389. }
  390. switch (recordDetail.status)
  391. {
  392. case 0:
  393. basicPlcTestSchemeDto.TestStatus = "未测试";
  394. break;
  395. case 99:
  396. basicPlcTestSchemeDto.TestStatus = "已测试";
  397. break;
  398. }
  399. //只显示已测试的项
  400. allConfigList.Add(basicPlcTestSchemeDto);
  401. conditionConfig.Add(basicPlcTestSchemeDto);
  402. }
  403. }
  404. conditionConfig = conditionConfig.OrderBy(x => x.SchemeName).ThenBy(x => x.SchemeId).ToList();
  405. //默认显示的第一页
  406. Getpage();
  407. }
  408. }
  409. /// <summary>
  410. /// 获取页面
  411. /// </summary>
  412. private void Getpage()
  413. {
  414. CurrentPage = 1;
  415. TotalCount = conditionConfig.Count;
  416. CurrentPageChanged();
  417. }
  418. /// <summary>
  419. /// 页面变化
  420. /// </summary>
  421. private void CurrentPageChanged()
  422. {
  423. BaseConfigList.Clear();
  424. foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
  425. {
  426. BaseConfigList.Add(i);
  427. }
  428. }
  429. #endregion
  430. #region 命令绑定
  431. public DelegateCommand<object> QueryCommand { set; get; }
  432. public DelegateCommand<object> ResetCommand { set; get; }
  433. public DelegateCommand<string> ExportCommand { set; get; }
  434. public DelegateCommand OnLoadCommand { set; get; }
  435. public DelegateCommand<object> PdfReportCommand { set; get; }
  436. public DelegateCommand<object> CheckDetailCommand { set; get; }
  437. #endregion
  438. #region 数据绑定
  439. /// <summary>
  440. /// 测试方案编码
  441. /// </summary>
  442. private string scheduleName;
  443. public string ScheduleName
  444. {
  445. get { return scheduleName; }
  446. set { scheduleName = value; RaisePropertyChanged(); }
  447. }
  448. /// <summary>
  449. /// 设备名称
  450. /// </summary>
  451. private string deviceKindName;
  452. public string DeviceKindName
  453. {
  454. get { return deviceKindName; }
  455. set { deviceKindName = value; RaisePropertyChanged(); }
  456. }
  457. /// <summary>
  458. /// 测试项名称
  459. /// </summary>
  460. private string testName;
  461. public string TestName
  462. {
  463. get { return testName; }
  464. set { testName = value; RaisePropertyChanged(); }
  465. }
  466. /// <summary>
  467. /// 测试项类型
  468. /// </summary>
  469. private List<string> testKinds = new List<string>();
  470. public List<string> TestKinds
  471. {
  472. get { return testKinds; }
  473. set { testKinds = value; RaisePropertyChanged(); }
  474. }
  475. /// <summary>
  476. ///
  477. /// </summary>
  478. private string selectTest;
  479. public string SelectTest
  480. {
  481. get { return selectTest; }
  482. set { selectTest = value; RaisePropertyChanged(); }
  483. }
  484. private DateTime startTime = DateTime.Now;
  485. public DateTime StartTime
  486. {
  487. get { return startTime; }
  488. set { startTime = value; RaisePropertyChanged(); }
  489. }
  490. private DateTime endTime = DateTime.Now;
  491. public DateTime EndTime
  492. {
  493. get { return endTime; }
  494. set { endTime = value; RaisePropertyChanged(); }
  495. }
  496. /// <summary>
  497. /// 总条数
  498. /// </summary>
  499. private int totalCount;
  500. public int TotalCount
  501. {
  502. get { return totalCount; }
  503. set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
  504. }
  505. /// <summary>
  506. /// 每页数量
  507. /// </summary>
  508. private int countPerPage = 15;
  509. public int CountPerPage
  510. {
  511. get { return countPerPage; }
  512. set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  513. }
  514. /// <summary>
  515. /// 单前页
  516. /// </summary>
  517. private int currentPage = 1;
  518. public int CurrentPage
  519. {
  520. get { return currentPage; }
  521. set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  522. }
  523. private ObservableCollection<SchDtlWithResultModel> baseConfigList = new ObservableCollection<SchDtlWithResultModel>();
  524. public ObservableCollection<SchDtlWithResultModel> BaseConfigList
  525. {
  526. get { return baseConfigList; }
  527. set { SetProperty(ref baseConfigList, value); }
  528. }
  529. #endregion
  530. }
  531. }