ResultQueryViewModel.cs 22 KB

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