123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- using AutoMapper;
- using BizService;
- using Microsoft.Extensions.Logging;
- using MiniExcelLibs;
- using Model.Dto;
- using Model.Entities;
- using NetTaste;
- using Newtonsoft.Json;
- using PLCTool.Pdf;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using QuestPDF;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- using QuestPDF.Previewer;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection.Metadata;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using WPFDevelopers.Controls.Runtimes.Shell32;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
- namespace PLCTool.ViewModels.BusinessManageViewModel
- {
- internal class QueryViewModel : BindableBase
- {
- private readonly IDialogService _dialog;
- private readonly IOptionConfigService _optionConfigService;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
- private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
- private readonly IBasicDeviceService _iBasicDeviceService;
- private readonly ILogger _logger;
- private readonly IMapper _mapper;
- private readonly IMenuService _menuService;
- private List<SchDtlWithResultModel> allConfigList = new List<SchDtlWithResultModel>();//所有方案
- private List<SchDtlWithResultModel> conditionConfig = new List<SchDtlWithResultModel>();//符合条件的方案
- private const int PageCount = 1; //每一页显示个数
- public QueryViewModel(IDialogService dialog, IMenuService menuService, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordDtlService iBizTestRecordDtlService, IBasicDeviceService iBasicDeviceService,IMapper mapper, ILogger logger)
- {
- _dialog = dialog;
- _optionConfigService = optionConfigService;
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
- _iBizTestRecordDtlService = iBizTestRecordDtlService;
- _iBasicDeviceService = iBasicDeviceService;
- _logger = logger;
- _mapper = mapper;
- _menuService = menuService;
- QueryCommand = new DelegateCommand<object>(Query);
- ResetCommand = new DelegateCommand<object>(Reset);
- ExportCommand = new DelegateCommand<string>(Export);
- OnLoadCommand = new DelegateCommand(OnLoad);
- CheckDetailCommand = new DelegateCommand<object>(CheckDetail);
- PdfReportCommand = new DelegateCommand<object>(CreatePdf);
- GetConfigOption();
-
- }
- #region 私有方法
- /// <summary>
- /// pdf报告相关信息
- /// </summary>
- /// <param name="schDtlId"></param>
- /// <returns></returns>
- private InvoiceModel GetReportPLCInfo(int schDtlId)
- {
- //测试方案明细主键ID
-
- InvoiceModel invoiceModel = new InvoiceModel();
-
- invoiceModel.ReportPLCModels = new List<ReportPLCModel>();
- //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
- var findEntity = _basicPlcTestSchemeDtlService.Find(schDtlId);
- var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
- if (findresult != null)
- {
- //根据方案id 在方案表中查找方案名 设备名
- int schID = Convert.ToInt32(findresult.SchemeId);
- var findSch = _basicPlcTestSchemeService.Find(schID);
- if (findSch != null)
- {
- invoiceModel.SchemeName = findSch.scheme_name;
- invoiceModel.DeviceName = findSch.devicekind_name;
- }
- //此测试方案下的所有测试项
- var allResult = allConfigList.FindAll(X => X.SchemeName == findSch.scheme_name);
- foreach (var each in allResult)
- {
- long schDetailId = each.SchemeId;
-
- //根据测试方案明细id查找结果
- var recordDetail = _iBizTestRecordDtlService.FindRecordDetailBySchDtlID(schDetailId);
- if (recordDetail != null)
- { //前置项结果解析
- string preconStr = recordDetail.precondition_final?.ToString();
- if (!string.IsNullOrEmpty(preconStr))
- {
- JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
- foreach (var detail in preconditionModel.DetailInfo)
- {
- invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
- {
- ItemName = each.ItemName,
- ItemType = each.ItemType,
- Type = "前置项",
- JudgeType = preconditionModel.ItemType,
- SelectLogic = preconditionModel.ItemLogical,
- Detail = preconditionModel.Description,
- Id = detail.Id,
- PlcAddress = detail.PlcAddress,
- PlcItem = detail.PlcItem,
- PlcValue = detail.PlcValue,
- Remark = detail.Remark,
- TestTime = detail.TestTime,
- RealValue = detail.RealValue,
- TestResult = detail.TestResult,
- });
- }
- }
- //输入项解析
- string inStr = recordDetail.action_final?.ToString();
- if (!string.IsNullOrEmpty(inStr))
- {
- JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
- foreach (var detail in inModel.DetailInfo)
- {
- invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
- {
- ItemName = each.ItemName,
- ItemType = each.ItemType,
- Type = "输入项",
- JudgeType = inModel.ItemType,
- SelectLogic = inModel.ItemLogical,
- Detail = inModel.Description,
- Id = detail.Id,
- PlcAddress = detail.PlcAddress,
- PlcItem = detail.PlcItem,
- PlcValue = detail.PlcValue,
- Remark = detail.Remark,
- TestTime = detail.TestTime,
- RealValue = detail.RealValue,
- TestResult = detail.TestResult,
- });
- }
- }
- //输出项解析
- string outStr = recordDetail.judgement_result_final?.ToString();
- if (!string.IsNullOrEmpty(outStr))
- {
- JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
- foreach (var detail in outModel.DetailInfo)
- {
- invoiceModel.ReportPLCModels.Add(new ReportPLCModel()
- {
- ItemName = each.ItemName,
- ItemType = each.ItemType,
- Type = "结果项",
- JudgeType = outModel.ItemType,
- SelectLogic = outModel.ItemLogical,
- Detail = outModel.Description,
- Id = detail.Id,
- PlcAddress = detail.PlcAddress,
- PlcItem = detail.PlcItem,
- PlcValue = detail.PlcValue,
- Remark = detail.Remark,
- TestTime = detail.TestTime,
- RealValue = detail.RealValue,
- TestResult = detail.TestResult,
- });
- }
- }
- }
- }
- //判断方案结果是否通过
- var testCount = allConfigList.FindAll(X => X.SchemeName == findSch.scheme_name&&X.TestResult=="通过")?.Count;
- if(testCount== allResult.Count)
- {
- invoiceModel.FinalReuslt = "通过";
- }
- else
- {
- invoiceModel.FinalReuslt = "不通过";
- }
- }
- return invoiceModel;
- }
- /// <summary>
- /// 生成pdf
- /// </summary>
- /// <param name="obj"></param>
- public void CreatePdf(object obj)
- {
- int id = Convert.ToInt32(obj);
- try
- {
- Settings.License = LicenseType.Community;
- var model = GetReportPLCInfo(id);
- var document = new InvoiceDocument(model);
- //pdf名称为轴编号+时间
- //输出文件
- using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
- {
- //设置文件类型
- //书写规则例如:txt files(*.txt)|*.txt
- Filter = "pdf文件|*.pdf",
- //设置默认文件名(可以不设置)
- FileName = "报表" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now),
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- document.GeneratePdf(localFilePath);
- }
- }
-
-
- }
- catch(Exception ex)
- {
- _logger.LogError(ex.ToString());
- }
-
- }
- private void CheckDetail(object obj)
- {
- //测试方案明细主键ID
- long id = Convert.ToInt64(obj);
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", id);
- //弹出详情对话框
- //弹出详情对话框
- _dialog.ShowDialog("TestResultDetailView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //更新表格,重新获取
- GetContent();
- }
- });
- }
- /// <summary>
- /// 每次进入页面加载
- /// </summary>
- private void OnLoad()
- {
- GetContent();
- }
- private void Reset(object obj)
- {
- ScheduleName = string.Empty;
- DeviceKindName = string.Empty;
- TestName = string.Empty;
- SelectTest = string.Empty;
- StartTime = DateTime.MinValue;
- EndTime = DateTime.MinValue;
- }
- private void Query(object obj)
- {
- if (StartTime > EndTime)
- {
- MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- conditionConfig = (from a in allConfigList
- where (EndTime == DateTime.MinValue ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
- && (string.IsNullOrEmpty(ScheduleName) ? true : (a.SchemeName == ScheduleName))
- && (string.IsNullOrEmpty(TestName) ? true : (a.ItemName == TestName))
- && (string.IsNullOrEmpty(DeviceKindName) ? true : (a.DeviceKindName == DeviceKindName))
- && (string.IsNullOrEmpty(SelectTest) ? true : (a.ItemType == SelectTest))
- select a).ToList();
- //默认显示的第一页
- conditionConfig = conditionConfig.OrderBy(x => x.SchemeName).ThenBy(x => x.SchemeId).ToList();
- Getpage();
- //BaseConfigList = new ObservableCollection<BasicPlcTestSchemeDto>(conditionConfig);
- }
- /// <summary>
- /// 获取配置
- /// </summary>
- private void GetConfigOption()
- {
- var configList = _optionConfigService.QueryList();
- var _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
- var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
- TestKinds = new List<string>();
- foreach (var test in tests)
- {
- TestKinds.Add(test.ContentOption);
- }
- }
-
-
- /// <summary>
- /// excel导出
- /// </summary>
- /// <param name="obj"></param>
- private void Export(object obj)
- {
- using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
- {
- //设置文件类型
- //书写规则例如:txt files(*.txt)|*.txt
- Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
- //设置默认文件名(可以不设置)
- FileName = "测试记录" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now),
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- // Show save file dialog box
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- try
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //string filePathWithName = localFilePath + ".xlsx";
- //获取文件内容
- MiniExcel.SaveAs(localFilePath, BaseConfigList);
- }
- catch (Exception ex)
- {
- _logger.LogError(ex.ToString());
- }
- }
- }
- }
- /// <summary>
- /// 获取解决方案
- /// </summary>
- private void GetContent()
- {
- //所有测试方案
- var schlist = _basicPlcTestSchemeService.QueryList();
- var configList = _mapper.Map<List<bas_plc_test_scheme>, List<BasicPlcTestSchemeDto>>(schlist);
- //清空集合
- BaseConfigList.Clear();
- allConfigList.Clear();
- conditionConfig.Clear();
- //查找测试项名称
- foreach (var sch in configList)
- {
- long schId = sch.SchemeId;
- //在解决方案详细表中查找所有的测试项
- var items = _basicPlcTestSchemeDtlService.FindAllBySchId(schId);
- foreach (var item in items)
- {
- //上面是测试方案明细
- SchDtlWithResultModel basicPlcTestSchemeDto = new SchDtlWithResultModel();
- basicPlcTestSchemeDto.SchemeId = item.scheme_dtl_id;
- basicPlcTestSchemeDto.SchemeName = sch.SchemeName;
- basicPlcTestSchemeDto.DeviceKindName = sch.DeviceKindName;
- switch (item.item_type)
- {
- case 0:
- basicPlcTestSchemeDto.ItemType = "前置项";
- break;
- case 1:
- basicPlcTestSchemeDto.ItemType = "PLC点位测试项";
- break;
- case 2:
- basicPlcTestSchemeDto.ItemType = "Robot动作测试";
- break;
- }
- basicPlcTestSchemeDto.ItemName = item.item_name;
- basicPlcTestSchemeDto.CreateBy = sch.CreateBy;
- basicPlcTestSchemeDto.CreateTime = sch.CreateTime;
- //这里是测试项 结果和 测试记录明细状态
- var recordDetail=_iBizTestRecordDtlService.FindRecordDetailBySchDtlID(item.scheme_dtl_id);
- if(recordDetail != null)
- {
- basicPlcTestSchemeDto.StartTestTime = recordDetail.start_test_time.Value;
- basicPlcTestSchemeDto.FinishTestTime = recordDetail.finish_test_time.Value;
- basicPlcTestSchemeDto.TestMode= recordDetail.test_mode;
- switch (recordDetail.test_result)
- {
- case 0:
- basicPlcTestSchemeDto.TestResult = "不通过";
- break;
- case 1:
- basicPlcTestSchemeDto.TestResult = "通过";
- break;
- }
- switch (recordDetail.status)
- {
- case 0:
- basicPlcTestSchemeDto.TestStatus = "未测试";
- break;
- case 99:
- basicPlcTestSchemeDto.TestStatus = "已测试";
- break;
- }
- //只显示已测试的项
- allConfigList.Add(basicPlcTestSchemeDto);
- conditionConfig.Add(basicPlcTestSchemeDto);
- }
-
- }
- conditionConfig = conditionConfig.OrderBy(x => x.SchemeName).ThenBy(x => x.SchemeId).ToList();
- //默认显示的第一页
- Getpage();
- }
- }
- /// <summary>
- /// 获取页面
- /// </summary>
- private void Getpage()
- {
- CurrentPage = 1;
- TotalCount = conditionConfig.Count;
- CurrentPageChanged();
- }
- /// <summary>
- /// 页面变化
- /// </summary>
- private void CurrentPageChanged()
- {
- BaseConfigList.Clear();
- foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
- {
- BaseConfigList.Add(i);
- }
- }
- #endregion
- #region 命令绑定
- public DelegateCommand<object> QueryCommand { set; get; }
- public DelegateCommand<object> ResetCommand { set; get; }
-
- public DelegateCommand<string> ExportCommand { set; get; }
- public DelegateCommand OnLoadCommand { set; get; }
-
- public DelegateCommand<object> PdfReportCommand { set; get; }
- public DelegateCommand<object> CheckDetailCommand { set; get; }
- #endregion
- #region 数据绑定
- /// <summary>
- /// 测试方案编码
- /// </summary>
- private string scheduleName;
- public string ScheduleName
- {
- get { return scheduleName; }
- set { scheduleName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备名称
- /// </summary>
- private string deviceKindName;
- public string DeviceKindName
- {
- get { return deviceKindName; }
- set { deviceKindName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项名称
- /// </summary>
- private string testName;
- public string TestName
- {
- get { return testName; }
- set { testName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项类型
- /// </summary>
- private List<string> testKinds = new List<string>();
- public List<string> TestKinds
- {
- get { return testKinds; }
- set { testKinds = value; RaisePropertyChanged(); }
- }
- /// <summary>
- ///
- /// </summary>
- private string selectTest;
- public string SelectTest
- {
- get { return selectTest; }
- set { selectTest = value; RaisePropertyChanged(); }
- }
- private DateTime startTime = DateTime.Now;
- public DateTime StartTime
- {
- get { return startTime; }
- set { startTime = value; RaisePropertyChanged(); }
- }
- private DateTime endTime = DateTime.Now;
- public DateTime EndTime
- {
- get { return endTime; }
- set { endTime = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 总条数
- /// </summary>
- private int totalCount;
- public int TotalCount
- {
- get { return totalCount; }
- set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 每页数量
- /// </summary>
- private int countPerPage = 15;
- public int CountPerPage
- {
- get { return countPerPage; }
- set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 单前页
- /// </summary>
- private int currentPage = 1;
- public int CurrentPage
- {
- get { return currentPage; }
- set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- private ObservableCollection<SchDtlWithResultModel> baseConfigList = new ObservableCollection<SchDtlWithResultModel>();
- public ObservableCollection<SchDtlWithResultModel> BaseConfigList
- {
- get { return baseConfigList; }
- set { SetProperty(ref baseConfigList, value); }
- }
- #endregion
- }
- }
|