user_lt 1 éve
szülő
commit
f9aa1b717a

+ 1 - 0
BlankApp1/BlankApp1/PLCTool.csproj

@@ -25,6 +25,7 @@
     <PackageReference Include="NLog.Wpf.RichTextBox" Version="1.0.2" />
     <PackageReference Include="NlogViewer" Version="0.7.0" />
     <PackageReference Include="Prism.DryIoc" Version="8.1.97" />
+    <PackageReference Include="QuestPDF" Version="2023.12.1" />
     <PackageReference Include="SqlSugarCore" Version="5.1.4.113" />
     <PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
     <PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview3" />

+ 165 - 0
BlankApp1/BlankApp1/Pdf/InvoiceDocument.cs

@@ -0,0 +1,165 @@
+using Model.Dto;
+using PLCTool.Common;
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+using QuestPDF.Infrastructure;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
+
+namespace PLCTool.Pdf
+{
+    public class InvoiceDocument : IDocument
+    {
+        static string logoPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images", "Logo.png");
+        public static Image LogoImage { get; } = Image.FromFile(logoPath);
+
+        public InvoiceModel Model { get; }
+
+        public InvoiceDocument(InvoiceModel model)
+        {
+            Model = model;
+        }
+
+        public DocumentMetadata GetMetadata() => DocumentMetadata.Default;
+
+        public void Compose(IDocumentContainer container)
+        {
+            container
+                .Page(page =>
+                {
+                    page.Margin(50);
+
+                    page.Header().Element(ComposeHeader);
+                    page.Content().Element(ComposeContent);
+
+                    page.Footer().AlignCenter().Text(text =>
+                    {
+                        text.CurrentPageNumber();
+                        text.Span(" / ");
+                        text.TotalPages();
+                    });
+                });
+        }
+
+        void ComposeHeader(IContainer container)
+        {
+            container.Row(row =>
+            {
+                row.RelativeItem().Column(column =>
+                {
+
+                    column.Item().AlignCenter().Text($"PLC点表测试报表").FontFamily("simhei").Style(TextStyle.Default.FontSize(30));
+
+                    
+                });
+                
+                row.ConstantItem(100).Image(LogoImage);
+            });
+        }
+
+        void ComposeContent(IContainer container)
+        {
+            container.PaddingVertical(40).Column(column =>
+            {
+                column.Spacing(20);
+                column.Item().Row(row =>
+                {
+                    row.RelativeItem().Text($"测试方案名称:{Model.SchemeName}").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    row.ConstantItem(50);
+                    row.RelativeItem().Text($"设备名称:{Model.DeviceName}").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                });
+
+                column.Item().Row(row =>
+                {
+                    row.RelativeItem().Text($"测试人员:{Appsession.UserName}").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    row.ConstantItem(50);
+                    row.RelativeItem().Text($"测试结果:{Model.FinalReuslt}").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                });
+
+             
+                //表格部分
+                column.Item().Element(ComposeTable);
+
+               
+
+
+       
+            });
+        }
+
+        void ComposeTable(IContainer container)
+        {
+            var headerStyle = TextStyle.Default.SemiBold();
+
+            container.Table(table =>
+            {
+                table.ColumnsDefinition(columns =>
+                {
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+                    columns.RelativeColumn();
+
+                });
+
+                table.Header(header =>
+                {
+                    header.Cell().Text("测试项名称").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().Text("类型").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().Text("测试项类型").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("明细判定逻辑").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("描述").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("PLC地址").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("PLC变量名").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("判定值").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("实际值").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("结果").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    header.Cell().AlignCenter().Text("测试时间").FontFamily("simhei").Style(TextStyle.Default.FontSize(10));
+                    //标题画一条黑线
+                    header.Cell().ColumnSpan(11).PaddingTop(5).BorderBottom(1).BorderColor(Colors.Black);
+
+                });
+
+                foreach (var item in Model.ReportPLCModels)
+                {
+                    var index = Model.ReportPLCModels.IndexOf(item) + 1;
+
+                    //table.Cell().Element(CellStyle).Text($"{index}");
+                    table.Cell().Element(CellStyle).AlignCenter().Text(item.ItemName).FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text(item.Type).FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.JudgeType}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.SelectLogic}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.Detail}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.PlcAddress}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.PlcItem}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.PlcValue}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.RealValue}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.TestResult}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    table.Cell().Element(CellStyle).AlignCenter().Text($"{item.TestTime}").FontFamily("simhei").Style(TextStyle.Default.FontSize(8)); ;
+                    //static IContainer CellStyle(IContainer container)
+                    //{
+                    //    return container.BorderBottom(1).PaddingVertical(4);
+                    //}
+
+                    static IContainer CellStyle(IContainer container) => container.BorderBottom(1).BorderColor(Colors.Grey.Lighten2).PaddingVertical(5);
+                }
+            });
+        }
+
+     
+
+    }
+
+ 
+}

+ 105 - 0
BlankApp1/BlankApp1/Pdf/InvoiceModel.cs

@@ -0,0 +1,105 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Pdf
+{
+    public class InvoiceModel
+    {
+        public string SchemeName { get; set; }
+        /// <summary>
+        /// Desc:设备名称
+        /// Default:
+        /// Nullable:True
+        /// </summary>           
+        public string DeviceName { get; set; }
+
+        /// <summary>
+        /// 整个测试方案测试结果
+        /// </summary>
+        public string FinalReuslt { get; set; }
+
+        public List<ReportPLCModel> ReportPLCModels { get; set; }
+     
+    }
+
+    public class ReportPLCModel
+    {     /// <summary>
+          /// 测试项名称
+          /// </summary>
+        public string ItemName { get; set; }
+        /// <summary>
+        /// 测试项类型
+        /// </summary>
+        public string ItemType { get; set; }
+
+        /// <summary>
+        /// 前置项  输入项 输出项
+        /// </summary>
+        public string Type { get; set; }
+        /// <summary>
+        /// 类型
+        /// </summary>
+        public string JudgeType { get; set; }
+        /// <summary>
+        /// 判定逻辑
+        /// </summary>
+        public string SelectLogic { get; set; }
+        /// <summary>
+        /// 描述
+        /// </summary>
+        public string Detail { get; set; }
+        public long Id { get; set; }
+
+
+        /// <summary>
+        /// Desc:
+        /// Default:PLC变量名
+        /// Nullable:True
+        /// </summary>           
+        public  string PlcItem { get; set; }
+
+
+
+        /// <summary>
+        /// Desc:
+        /// Default:
+        /// Nullable:True
+        /// </summary>           
+        public string PlcAddress { get; set; }
+
+
+        public string PlcValue { get; set; }
+
+
+        /// <summary>
+        /// plc实时值
+        /// </summary>
+        public string RealValue { get; set; }
+
+
+
+        /// <summary>
+        /// 测试时间(数据库中没有的字段,方便测试时绑定使用)
+        /// </summary>
+        public DateTime TestTime { get; set; }
+
+
+        /// <summary>
+        /// 测试结果(数据库中没有的字段,方便测试时绑定使用)
+        /// </summary>
+        public string TestResult { get; set; }
+        /// <summary>
+        /// Desc:
+        /// Default:单个变量的描述
+        /// Nullable:True
+        /// </summary>           
+        public string Remark { get; set; }
+    }
+}
+
+   
+   
+

+ 215 - 1
BlankApp1/BlankApp1/ViewModels/BusinessManageViewModel/QueryViewModel.cs

@@ -4,9 +4,18 @@ 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;
@@ -15,6 +24,7 @@ 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
 {
@@ -48,15 +58,218 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
             ExportCommand = new DelegateCommand<string>(Export);
             OnLoadCommand = new DelegateCommand(OnLoad);
             CheckDetailCommand = new DelegateCommand<object>(CheckDetail);
+            PdfReportCommand = new DelegateCommand<object>(CreatePdf);
             GetConfigOption();
           
         }
 
 
 
+
+
         #region 私有方法
 
+        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.device_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名称为轴编号+时间
+                string strDt = "报表" + string.Format("{0:yyyyMMddHHmm}", DateTime.Now)+ ".pdf"; ;
+                string pathToPDF = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "PDF", strDt);
+               
+                //输出文件
+
+                document.GeneratePdf(pathToPDF);
+            }
+            catch(Exception ex)
+            {
+                _logger.LogError(ex.ToString());
+            }
+        
+
 
+            //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)
+            //    {
+            //        try
+            //        {
+            //            //获得文件路径
+            //            string localFilePath = saveFileDialog.FileName.ToString();
+
+
+            //            document.GeneratePdf(localFilePath);
+            //        }
+            //        catch (Exception ex)
+            //        {
+
+            //        }
+
+
+            //    }
+
+            //}
+
+        }
 
         private void CheckDetail(object obj)
         {
@@ -291,7 +504,8 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
         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 数据绑定

+ 1 - 1
BlankApp1/BlankApp1/Views/BasicConfigView/CopySchView.xaml

@@ -24,7 +24,7 @@
         <Grid Grid.Row="1">
             <DataGrid  Grid.Row="2"  ColumnWidth="*" AutoGenerateColumns="False" HeadersVisibility="All" CanUserAddRows="False"  SelectionUnit="FullRow" SelectionMode="Single"   RowHeaderWidth="0"
              ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"  RowHeaderStyle="{StaticResource RowHeaderStyle}" RowStyle="{StaticResource DataGridRowtyle}"  AlternationCount="2"
-            ItemsSource="{Binding SchProjectList}" IsReadOnly="True" Padding="0"  >
+            ItemsSource="{Binding SchProjectList}" IsReadOnly="True" Margin="5" Padding="0"  >
                
                 <DataGrid.Columns >
                     <DataGridTemplateColumn CanUserResize="False" Width="60">

+ 11 - 5
BlankApp1/BlankApp1/Views/BusinessManageView/QueryView.xaml

@@ -78,18 +78,24 @@
                 <DataGridTextColumn Header="测试结果" Binding="{Binding TestResult}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                 <DataGridTextColumn Header="状态" Binding="{Binding TestStatus}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                 <DataGridTextColumn Header="创建者" Binding="{Binding CreateBy}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
-                <DataGridTemplateColumn Header="操作" Width="160"  CellStyle="{StaticResource MyDataGridCellStyle}">
+                <DataGridTemplateColumn Header="操作" Width="180"  CellStyle="{StaticResource MyDataGridCellStyle}">
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
-                            <UniformGrid Columns="1">
+                            <UniformGrid Columns="2">
                                 <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
-                                        Command="{Binding DataContext.CheckDetailCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding SchemeId}" Cursor="Hand" Margin="0,0,10,0" >
+                                        Command="{Binding DataContext.CheckDetailCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding SchemeId}" Cursor="Hand" Margin="0,0,5,0" >
 
                                     <StackPanel Orientation="Horizontal">
-                                        <TextBlock  Text="查看测试详情" VerticalAlignment="Center" Foreground="Blue"/>
+                                        <TextBlock  Text="查看详情" VerticalAlignment="Center" Foreground="Blue"/>
+                                    </StackPanel>
+                                </Button>
+                                <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
+                 Command="{Binding DataContext.PdfReportCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding SchemeId}" Cursor="Hand" Margin="0,0,5,0" >
+
+                                    <StackPanel Orientation="Horizontal">
+                                        <TextBlock  Text="生成pdf报表" VerticalAlignment="Center" Foreground="Blue"/>
                                     </StackPanel>
                                 </Button>
-                                
 
                             </UniformGrid>
                         </DataTemplate>