浏览代码

检验申请表导入导出数据包功能

LT069288 2 月之前
父节点
当前提交
0cfc53fe97

+ 115 - 5
UniformMaterialManagementSystem/ViewModels/InspectionRecordPageViewModel.cs

@@ -98,9 +98,9 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void LoadData()
         {
-            SelectedInspectCategory = SelectedInspectCategory == null ? "" : SelectedInspectCategory;
-            SelectedCompany = SelectedCompany == null ? "" : SelectedCompany;
-            var materialName = SelectedMaterial == null ? "" : SelectedMaterial.Name;
+            SelectedInspectCategory = SelectedInspectCategory ?? "";
+            SelectedCompany = SelectedCompany ?? "";
+            var materialName = SelectedMaterial?.Name ?? string.Empty;
 
             var inspectAppliesList = _inspectService.QueryNoTracking(x => x.Year == WorkYear && x.Company.Contains(SelectedCompany) && x.InspCategory.Contains(SelectedInspectCategory) && x.ProductName.Contains(materialName))
                 .Include(x => x.InspectApplyDetails)
@@ -208,18 +208,128 @@ namespace UniformMaterialManagementSystem.ViewModels
         /// 导入报检数据包
         /// </summary>
         [RelayCommand]
-        private void ImportInspectApplyData()
+        private async Task ImportInspectApplyData()
         {
+            var result = MessageBox.Show("您正在导入报检数据包,确认继续?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (result != MessageBoxResult.OK) return;
 
+            //导入并解密db文件
+            var path = DataBaseUtil.ImportData();
+
+            var inspectApplies = DataBaseUtil.QueryAll<InspectApply>(path, p => p
+                .Include(x => x.Material)
+                .Include(x => x.MaterialCompany)
+                .Include(x => x.InspectApplyContractDetails)
+                .Include(x => x.InspectApplyDetails)).Result.ToList();
+
+            var serialNumbers = DataBaseUtil.QueryAll<SerialNumber>(path).Result.ToList();
+
+            var materials = inspectApplies.ToArray().Select(x => x.Material).Distinct().ToArray();
+            var companies = inspectApplies.ToArray().Select(x => x.MaterialCompany).Distinct().ToArray();
+            List<InspectApplyContractDetail> contractDetails = [];
+            List<InspectApplyDetail> details = [];
+            foreach (var inspectApply in inspectApplies)
+            {
+                foreach (var contractDetail in inspectApply.InspectApplyContractDetails)
+                {
+                    contractDetails.Add(contractDetail);
+                }
+                foreach (var detail in inspectApply.InspectApplyDetails)
+                {
+                    details.Add(detail);
+                }
+            }
+
+            //导入相关表数据:材料->企业->检验申请->检验申请合同明细->检验申请明细,流水号表
+            await DataBaseUtil.ImportTable("Materials", materials);
+            await DataBaseUtil.ImportTable("Companies", companies);
+            await DataBaseUtil.ImportTable("InspectApplies", inspectApplies);
+            await DataBaseUtil.ImportTable("InspectApplyContractDetails", contractDetails);
+            await DataBaseUtil.ImportTable("InspectApplyDetails", details);
+            await DataBaseUtil.ImportTable("SerialNumbers", serialNumbers);
+
+            MessageBox.Show("导入报检数据包成功!");
+
+            //重新加载数据
+            LoadData();
         }
 
         /// <summary>
         /// 导出报检数据包
         /// </summary>
         [RelayCommand]
-        private void ExportInspectApplyData()
+        private async Task ExportInspectApplyData()
         {
+            var result = MessageBox.Show("确认导出报检数据包吗?(仅导出“导出状态”为“未导出”的检验申请单)", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (result != MessageBoxResult.OK) return;
+
+            //导出数据包:未导出状态的申请表
+            var inspectApplies = _inspectService.Query(x => !x.ExportStatus)
+                .Include(x => x.Material)
+                .Include(x => x.MaterialCompany)
+                .Include(x => x.InspectApplyContractDetails)
+                .Include(x => x.InspectApplyDetails).ToArray();
+            if (inspectApplies.Length == 0)
+            {
+                MessageBox.Show("不存在可导出的检验申请表!");
+                return;
+            }
+
+            //将导出状态更新为已导出
+            foreach (var inspect in inspectApplies)
+            {
+                inspect.ExportStatus = true;
+            }
+            var success = _inspectService.SaveChanges();
+            if (!success) return;
+
+            var serialNumbers = _serialService.Query().ToArray();
+
+            var materials = inspectApplies.Select(x => x.Material).Distinct().ToArray();
+            var companies = inspectApplies.Select(x => x.MaterialCompany).Distinct().ToArray();
+            List<InspectApplyContractDetail> contractDetails = [];
+            List<InspectApplyDetail> details = [];
+            foreach (var inspectApply in inspectApplies)
+            {
+                foreach (var contractDetail in inspectApply.InspectApplyContractDetails)
+                {
+                    contractDetails.Add(contractDetail);
+                }
+                foreach (var detail in inspectApply.InspectApplyDetails)
+                {
+                    details.Add(detail);
+                }
+            }
+
+            //导入相关表数据:材料->企业->检验申请->检验申请合同明细->检验申请明细,流水号表
+            if (materials is { Length: > 0 })
+            {
+                DataBaseUtil.ExportTable(materials, "Materials", true);
+            }
+            if (companies is { Length: > 0 })
+            {
+                DataBaseUtil.ExportTable(companies, "Companies");
+            }
+            if (inspectApplies is { Length: > 0 })
+            {
+                DataBaseUtil.ExportTable(inspectApplies, "InspectApplies");
+            }
+            if (contractDetails is { Count: > 0 })
+            {
+                DataBaseUtil.ExportTable(contractDetails, "InspectApplyContractDetails");
+            }
+            if (details is { Count: > 0 })
+            {
+                DataBaseUtil.ExportTable(details, "InspectApplyDetails");
+            }
+            if (serialNumbers is { Length: > 0 })
+            {
+                DataBaseUtil.ExportTable(serialNumbers, "SerialNumbers");
+            }
+
+            await DataBaseUtil.ExportData();
 
+            MessageBox.Show("报检数据包导出成功!");
         }
 
         /// <summary>

+ 8 - 2
UniformMaterialManagementSystem/Views/InspectionRecordPage.xaml

@@ -317,7 +317,7 @@
                                     </Button.Template>
                                 </Button>
                                 <Separator />
-                                <Button Command="{Binding ImportInspectApplyDataCommand}">
+                                <Button x:Name="ImportInspectApplyDataButton" Command="{Binding ImportInspectApplyDataCommand}">
                                     <Button.Template>
                                         <ControlTemplate>
                                             <Border
@@ -341,7 +341,7 @@
                                         </ControlTemplate>
                                     </Button.Template>
                                 </Button>
-                                <Button Command="{Binding ExportInspectApplyDataCommand}">
+                                <Button x:Name="ExportInspectApplyDataButton" Command="{Binding ExportInspectApplyDataCommand}">
                                     <Button.Template>
                                         <ControlTemplate>
                                             <Border
@@ -485,6 +485,12 @@
                         Header="录入人"
                         IsColumnFiltered="True"
                         IsReadOnly="True" />
+                    <control:FilterDataGridCheckBoxColumn
+                        Width="100"
+                        Binding="{Binding ExportStatus}"
+                        ElementStyle="{StaticResource CheckBoxColumnElementStyle}"
+                        Header="导出状态"
+                        IsColumnFiltered="True" />
                     <control:FilterDataGridCheckBoxColumn
                         Width="130"
                         Binding="{Binding ReportStatus}"

+ 17 - 0
UniformMaterialManagementSystem/Views/InspectionRecordPage.xaml.cs

@@ -2,6 +2,7 @@
 using Microsoft.Extensions.DependencyInjection;
 using System.Windows.Controls;
 using System.Windows.Media;
+using UniformMaterialManagementSystem.Utils;
 using UniformMaterialManagementSystem.ViewModels;
 
 namespace UniformMaterialManagementSystem.Views
@@ -16,6 +17,22 @@ namespace UniformMaterialManagementSystem.Views
             InitializeComponent();
 
             this.DataContext = App.Current.Services.GetService<InspectionRecordPageViewModel>();
+
+            SetImportAndExportButtonVisible();
+        }
+
+        private void SetImportAndExportButtonVisible()
+        {
+            var roleType = CommonUtil.GetRoleType();
+            switch (roleType)
+            {
+                case RoleType.MaterialCompany:
+                    ImportInspectApplyDataButton.Visibility = Visibility.Collapsed;
+                    break;
+                case RoleType.Supervision:
+                    ExportInspectApplyDataButton.Visibility = Visibility.Collapsed;
+                    break;
+            }
         }
 
         private void InspectTypeText_TextChanged(object sender, TextChangedEventArgs e)