123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- using AutoMapper;
- using BizService;
- using Microsoft.Extensions.Logging;
- using MiniExcelLibs;
- using Model.Dto;
- using Model.Entities;
- using PLCTool.Pdf;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using QuestPDF.Infrastructure;
- using QuestPDF;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using QuestPDF.Fluent;
- namespace PLCTool.ViewModels.BusinessManageViewModel
- {
-
- public class ResultQueryViewModel : BindableBase
- {
- private readonly IBasicDeviceService _iBasicDeviceService;
- private readonly IBasicDeviceKindService _iBasicDeviceKindService;
- private readonly IBasicProjectService _iBasicProjectService;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
- private readonly IBizTestRecordService _iBizTestRecordService;
- private readonly IBasicPlcTestSchemeDtlService _iBasicPlcTestSchemeDtlService;
- private readonly IMapper _mapper;
- private readonly IDialogService _dialog;
- private readonly ILogger _logger;
- private List<DeviceDtlWithResultModel> allDeviceList = new List<DeviceDtlWithResultModel>();//所有方案
- private List<DeviceDtlWithResultModel> conditionDevices = new List<DeviceDtlWithResultModel>();//符合条件的方案
- public ResultQueryViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBizTestRecordDtlService iBizTestRecordDtlService, IBizTestRecordService iBizTestRecordService, IBasicPlcTestSchemeDtlService iBasicPlcTestSchemeDtlService, IMapper mapper, IDialogService dialog, ILogger logger)
- {
- _iBasicDeviceService = iBasicDeviceService;
- _iBasicDeviceKindService = iBasicDeviceKindService;
- _iBasicProjectService = iBasicProjectService;
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _iBizTestRecordService= iBizTestRecordService;
- _iBizTestRecordDtlService= iBizTestRecordDtlService;
- _iBasicPlcTestSchemeDtlService = iBasicPlcTestSchemeDtlService;
- _mapper = mapper;
- _dialog = dialog;
- _logger = logger;
- QueryCommand = new DelegateCommand<object>(Query);
- ExportCommand = new DelegateCommand<string>(Export);
- CheckDetailCommand = new DelegateCommand<object>(CheckDetail);
- ResetCommand = new DelegateCommand<object>(Reset);
- OnLoadCommand = new DelegateCommand(OnLoad);
- PdfReportCommand = new DelegateCommand<object>(CreatePdf);
- UnqualifiedCommand = new DelegateCommand<object>(RetryTest);
- }
- #region 私有方法
- /// <summary>
- /// 重新测试不合格项
- /// </summary>
- /// <param name="obj"></param>
- private void RetryTest(object obj)
- {
- object[] multiObj = obj as object[];
- //测试方案明细主键ID
- long id = (long)multiObj[0];
- long deviceId = (long)multiObj[1];
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", id);
- parm.Add("Key2", deviceId);
- //弹出详情对话框
- //弹出详情对话框
- _dialog.ShowDialog("ManualTestView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- //更新表格,重新获取
- }
- });
- }
- /// <summary>
- /// 加载页面
- /// </summary>
- private void OnLoad()
- {
- DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind();
- ProjectNameList = _iBasicProjectService.FindAllProject();
- GetProjectConfig();
- }
- private void Reset(object obj)
- {
- DeviceNo = string.Empty;
- DeviceName = string.Empty;
- }
- /// <summary>
- /// 查看详情
- /// </summary>
- /// <param name="obj"></param>
- 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>
- /// <param name="obj"></param>
- private void Query(object obj)
- {
- conditionDevices = (from a in allDeviceList
- where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
- && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
- //&& (string.IsNullOrEmpty(DeviceKindName) ? true : (a.DeviceKindId == DeviceKindName))
- // && (string.IsNullOrEmpty(ProjectName) ? true : (a.ProjectId == ProjectName))
- select a).ToList();
- //默认显示的第一页
- conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
- Getpage();
- }
- private void Export(string obj)
- {
- using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
- {
- //设置文件类型
- //书写规则例如:txt files(*.txt)|*.txt
- Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
- //设置默认文件名(可以不设置)
- FileName = "项目配置表",
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- // Show save file dialog box
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- try
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //获取文件内容
- MiniExcel.SaveAs(localFilePath, DeviceResultItemList);
- }
- catch (Exception ex)
- {
- _logger.LogError(ex.ToString());
- }
- }
- }
- }
- /// <summary>
- /// 获取所有项目
- /// </summary>
- private void GetProjectConfig()
- {
- allDeviceList.Clear();
- conditionDevices.Clear();
- //所有测试方案
- var schlist = _basicPlcTestSchemeService.QueryList();
- var schDtoList = _mapper.Map<List<bas_plc_test_scheme>, List<BasicPlcTestSchemeDto>>(schlist);
- //测试记录中的所有设备
- var deviceIds=_iBizTestRecordService.QueryList().Select(X => X.device_id).Distinct();
- foreach (var deviceId in deviceIds)
- {
- var deviceMsg = _iBasicDeviceService.Find(((int)deviceId));
- if(deviceMsg != null)
- {
- //查找哦记录有的方案
- var recordMsgs=_iBizTestRecordService.FindRecordByDeviceId(deviceId);
- foreach ( var sch in recordMsgs)
- {
- string deviceKindName = _iBasicDeviceKindService.Find((int)deviceMsg.device_kind_id)?.devicekind_name;
- string projectName = _iBasicProjectService.Find((int)deviceMsg.project_id)?.project_name;
- long schId=(long)_basicPlcTestSchemeService.FindByNameAndType(sch.scheme_name, deviceKindName)?.scheme_id;
- allDeviceList.Add(new DeviceDtlWithResultModel()
- {
- RecordId = sch.record_id,
- DeviceId = deviceMsg.device_id,
- DeviceNo = deviceMsg.device_no,
- DeviceName = deviceMsg.device_name,
- DeviceKindName = deviceKindName,
- ProjectName = projectName,
- SchemeName = sch.scheme_name,
- SchemeId= schId,
- StartTestTime = sch.start_test_time.Value,
- }); ; ;
- conditionDevices.Add(new DeviceDtlWithResultModel()
- {
- RecordId = sch.record_id,
- DeviceId = deviceMsg.device_id,
- DeviceNo = deviceMsg.device_no,
- DeviceName = deviceMsg.device_name,
- DeviceKindName = deviceKindName,
- ProjectName = projectName,
- SchemeName = sch.scheme_name,
- SchemeId = schId,
- StartTestTime = sch.start_test_time.Value,
- });
- }
-
- }
-
-
-
- }
- conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
- Getpage();
- }
- /// <summary>
- /// 获取页面
- /// </summary>
- private void Getpage()
- {
- CurrentPage = 1;
- TotalCount = conditionDevices.Count;
- CurrentPageChanged();
- }
- /// <summary>
- /// 页面变化
- /// </summary>
- private void CurrentPageChanged()
- {
- DeviceResultItemList.Clear();
- foreach (var i in conditionDevices.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
- {
- DeviceResultItemList.Add(i);
- }
- }
- /// <summary>
- /// 生成pdf
- /// </summary>
- /// <param name="obj"></param>
- public void CreatePdf(object obj)
- {
- object[] multiObj = obj as object[];
- //测试方案名称和设备id
- string schemeName = (string)multiObj[0];
- long deviceId = (long)multiObj[1];
- //查找recordid
- long recordId=(long)_iBizTestRecordService.FindRecorddByDeviceIdAndSchname((int)deviceId, schemeName)?.record_id;
- //查找详细记录
- var recordDtls = _iBizTestRecordDtlService.FindRecordDetailByRecordID(recordId);
- string deviceName = _iBasicDeviceService.Find((int)deviceId).device_name;
- try
- {
- Settings.License = LicenseType.Community;
- var model = GetReportPLCInfo(schemeName,deviceName, recordDtls);
- 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());
- }
- }
- /// <summary>
- /// pdf报告相关信息
- /// </summary>
- /// <param name="schDtlId"></param>
- /// <returns></returns>
- private InvoiceModel GetReportPLCInfo(string schName,string deviceName,List<biz_test_record_dtl> recordDtls)
- {
- //测试方案明细主键ID
- InvoiceModel invoiceModel = new InvoiceModel();
- invoiceModel.ReportPLCModels = new List<ReportPLCModel>();
- invoiceModel.SchemeName = schName;
- invoiceModel.DeviceName = deviceName;
- foreach (var each in recordDtls)
- {
- long recordId = each.record_id.Value;
- long schDtlid = each.scheme_dtl_id;
-
- string schItemName = _iBasicPlcTestSchemeDtlService.Find((int)schDtlid)?.item_name;
- string schItemType = string.Empty;
- int typeNo=(int) _iBasicPlcTestSchemeDtlService.Find((int)schDtlid)?.item_type;
- switch (typeNo)
- {
- case 0:
- schItemType = "前置项";
- break;
- case 1:
- schItemType = "PLC点位测试项";
- break;
- case 2:
- schItemType = "Robot动作测试";
- break;
- }
- //根据测试结果明细
- if (each != null)
- { //前置项结果解析
- string preconStr = each.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 =schItemName,
- ItemType = schItemType,
- 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 = each.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 = schItemName,
- ItemType = schItemType,
- 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 = each.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 = schItemName,
- ItemType = schItemType,
- 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 = allDeviceList.FindAll(X => X.SchemeName == findSch.scheme_name && X.TestResult == "通过")?.Count;
- //if (testCount == allResult.Count)
- //{
- // invoiceModel.FinalReuslt = "通过";
- //}
- //else
- //{
- // invoiceModel.FinalReuslt = "不通过";
- //}
-
- return invoiceModel;
- }
- #endregion
- #region 命令绑定
- public DelegateCommand<object> QueryCommand { set; get; }
- public DelegateCommand<string> ExportCommand { set; get; }
- public DelegateCommand<object> ResetCommand { set; get; }
- public DelegateCommand OnLoadCommand { set; get; }
- public DelegateCommand<object> CheckDetailCommand { set; get; }
- public DelegateCommand<object> UnqualifiedCommand { set; get; }
-
- public DelegateCommand<object> PdfReportCommand { set; get; }
- #endregion
- #region 数据绑定
- private ObservableCollection<DeviceDtlWithResultModel> deviceResultItemList = new ObservableCollection<DeviceDtlWithResultModel>();
- public ObservableCollection<DeviceDtlWithResultModel> DeviceResultItemList
- {
- get { return deviceResultItemList; }
- set { deviceResultItemList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备编号
- /// </summary>
- private string devicedNo;
- public string DeviceNo
- {
- get { return devicedNo; }
- set { devicedNo = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型名称
- /// </summary>
- private string deviceName;
- public string DeviceName
- {
- get { return deviceName; }
- set { deviceName = value; RaisePropertyChanged(); }
- }
- private string projectName;
- public string ProjectName
- {
- get { return projectName; }
- set { projectName = value; RaisePropertyChanged(); }
- }
- private string deviceKindName;
- public string DeviceKindName
- {
- get { return deviceKindName; }
- set { deviceKindName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 项目名称
- /// </summary>
- private List<string> projectNameList;
- public List<string> ProjectNameList
- {
- get { return projectNameList; }
- set { projectNameList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型
- /// </summary>
- private List<string> deviceKindNameList;
- public List<string> DeviceKindNameList
- {
- get { return deviceKindNameList; }
- set { deviceKindNameList = 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(); }
- }
- #endregion
- }
- }
|