Kaynağa Gözat

feat: 发货单导入导出数据包

JaneDoe 2 ay önce
ebeveyn
işleme
2c264ff489

+ 71 - 3
UniformMaterialManagementSystem/ViewModels/DeliveryReceiptViewModel.cs

@@ -15,6 +15,7 @@ using System.Windows.Media.Media3D;
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
 using DocumentFormat.OpenXml.Drawing;
+using DocumentFormat.OpenXml.Drawing.Charts;
 using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
 using DocumentFormat.OpenXml.Spreadsheet;
 using Microsoft.EntityFrameworkCore;
@@ -26,6 +27,7 @@ using UniformMaterialManagementSystem.Models;
 using UniformMaterialManagementSystem.Services;
 using UniformMaterialManagementSystem.Utils;
 using UniformMaterialManagementSystem.Views;
+using DataTable = System.Data.DataTable;
 using Material = UniformMaterialManagementSystem.Entities.Material;
 
 namespace UniformMaterialManagementSystem.ViewModels
@@ -162,7 +164,7 @@ namespace UniformMaterialManagementSystem.ViewModels
                 page.dpShippedDate.IsEnabled = page.fieldShippedMan.IsEnabled = page.fieldShippedTel.IsEnabled = false;
 
                 // 隐藏明细发货和使用按钮
-                page.btnAddDtl.Visibility = page.btnDeleteDtls.Visibility = page.sep2.Visibility = page.btnImportDtl.Visibility = page.btnExportDtl.Visibility = page.btnUseDtl.Visibility = Visibility.Collapsed;
+                page.btnAddDtl.Visibility = page.btnDeleteDtls.Visibility = page.sepDtl.Visibility = page.btnImportDtl.Visibility = page.btnExportDtl.Visibility = page.btnUseDtl.Visibility = Visibility.Collapsed;
 
                 foreach (DataGridColumn col in page.fdgDeliveryDetail.Columns)
                 {
@@ -214,7 +216,7 @@ namespace UniformMaterialManagementSystem.ViewModels
                 page.dpReceivedDate.IsEnabled = page.fieldReceivedMan.IsEnabled = page.fieldReceivedTel.IsEnabled = page.cbReceivedStatus.IsEnabled = false;
 
                 // 隐藏明细发货和接收按钮
-                page.btnAddDtl.Visibility = page.btnDeleteDtls.Visibility = page.sep2.Visibility = page.btnImportDtl.Visibility = page.btnExportDtl.Visibility = page.btnReceiveDtl.Visibility = Visibility.Collapsed;
+                page.btnAddDtl.Visibility = page.btnDeleteDtls.Visibility = page.sepDtl.Visibility = page.btnImportDtl.Visibility = page.btnExportDtl.Visibility = page.btnReceiveDtl.Visibility = Visibility.Collapsed;
 
                 // 隐藏明细表发货字段
                 foreach (DataGridColumn col in page.fdgDeliveryDetail.Columns)
@@ -523,8 +525,73 @@ namespace UniformMaterialManagementSystem.ViewModels
             cbReceivedStatus.SelectedItem = "已复核";
         }
 
+        [RelayCommand]
+        public void ExportThree()
+        {
+
+        }
+
+        [RelayCommand]
+        public void ExportFour()
+        {
+
+        }
+
         #endregion
 
+        [RelayCommand]
+        public void ExportDB()
+        {
+            // 需要存在 bin\DataBase 目录,且目录下存在一份 .db 文件,需要存在表
+
+            // 导出主表到 bin\DataBase 目录下的 .db:根据主键判断,存在数据则更新,不存在则插入
+            var deliveries = DeliveryReceipts;
+            DataBaseUtil.ExportTable(deliveries, "DeliveryReceipts"); // 数据库表名输错会抛出异常,但在本方法里获取不到抛出异常
+
+            // 必须先导出主表,否则明细插入数据库时报错:Foreign Key constraint failed
+            List<DeliveryReceiptDetail> details = new List<DeliveryReceiptDetail>();
+            foreach (var delivery in DeliveryReceipts)
+            {
+                details.AddRange(delivery.DeliveryReceiptDetails);
+            }
+            DataBaseUtil.ExportTable(details, "DeliveryReceiptDetails");
+
+            // 将选择的数据导出为加密数据包
+            string exportPath = DataBaseUtil.ExportData();
+            if (!string.IsNullOrEmpty(exportPath))
+            {
+                MessageBox.Show("导出成功。");
+            }
+        }
+
+        [RelayCommand]
+        public async Task ImportDB()
+        {
+            // 从加密数据包中获取出 .db 放到 path 目录下(bin\DataBase 目录)
+            string path = DataBaseUtil.ImportData();
+            if (string.IsNullOrEmpty(path)) { return; }
+
+            // 新增发货单
+            var deliveries = DataBaseUtil.QueryAll<DeliveryReceipt>(path, x => x.Include(x => x.DeliveryReceiptDetails))
+                                         .Result;
+            await DataBaseUtil.ImportTable("DeliveryReceipts", deliveries);
+
+            // 新增发货单明细 - 方法一:
+            List<DeliveryReceiptDetail> details = new List<DeliveryReceiptDetail>();
+            foreach (var d in deliveries)
+            {
+                details.AddRange(d.DeliveryReceiptDetails);
+            }
+            await DataBaseUtil.ImportTable("DeliveryReceiptDetails", details);
+
+            // 新增发货单明细 - 方法二:
+            //var drDetails = DataBaseUtil.QueryAll<DeliveryReceiptDetail>(path)
+            //                             .Result;
+            //await DataBaseUtil.ImportTable("DeliveryReceiptDetails", drDetails);
+
+            MessageBox.Show("导入成功");
+        }
+
         #region 子表事件
 
         public void FdgDeliveryDetail_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
@@ -1380,7 +1447,8 @@ namespace UniformMaterialManagementSystem.ViewModels
             }
 
             // 检查发运数量不能超出可发运数量
-            decimal hasShippedQty = _service.Query(x => x.ProductName.Equals(CurrDeliveryReceipt!.ProductName) && x.ContractNo.Equals(CurrDeliveryReceipt.ContractNo) && !x.Guid.Equals(CurrDeliveryReceipt.Guid)).Select(x => x.ShippedQty)
+            decimal hasShippedQty = _service.Query(x => x.ProductName.Equals(CurrDeliveryReceipt!.ProductName) && x.ContractNo.Equals(CurrDeliveryReceipt.ContractNo) && !x.Guid.Equals(CurrDeliveryReceipt.Guid))
+                                    .Select(x => x.ShippedQty)
                                     .ToList() // 必须转换为 List<decimal> 才不抛异常
                                     .Sum();
             decimal currQty = CurrDeliveryReceipt.ShippedQty;

+ 16 - 40
UniformMaterialManagementSystem/Views/DeliveryReceiptControl.xaml

@@ -262,7 +262,7 @@
             </b:EventTrigger>
         </b:Interaction.Triggers>
 
-        <!-- 发货单工具栏 -->
+        <!-- 主表工具栏 -->
         <Border x:Name="toolBarDelivery"
                 Grid.Row="0" 
                 Grid.Column="0"
@@ -305,43 +305,20 @@
                         Tag="{x:Static utils:RegularFontUtil.Arrow_Turn_Right_48}"
                         Template="{StaticResource CustomToolBarButtomTemplate}"
                         Command="{Binding ExportFourCommand}" />
-
-                <!--<WrapPanel Orientation="Horizontal">
-                    <StackPanel Orientation="Horizontal">
-                        <TextBlock Text="检验申请编号:" 
-                                   Width="90"
-                                   Style="{StaticResource CustomTextBlockStyle}"  />
-
-                        <custom:AutoCompleteBox x:Name="autoListBoxNo"
-                            Background="White"
-                            Width="275"
-                            Margin="0 2 0 2"
-                            ItemsSource="{Binding InspectApplyNos}" />
-                    </StackPanel>
-                    <StackPanel Orientation="Horizontal">
-                        <TextBlock Text="产品名称:" 
-                                   Width="90"
-                                   Style="{StaticResource CustomTextBlockStyle}"  />
-                        <custom:AutoCompleteBox x:Name="autoListBoxMate"
-                            Background="White"
-                            Width="275"
-                            Margin="0 2 0 2"
-                            ItemsSource="{Binding ProductNames}"
-                            SelectedItem="{Binding SelectedProductName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
-                    </StackPanel>
-
-                    <Button Content="查询"
-                            Margin="5 0 5 0"
-                            Tag="{x:Static utils:RegularFontUtil.Search_32}"
-                            Template="{StaticResource CustomToolBarButtomTemplate}"
-                            Command="{Binding QueryCommand}"
-                            CommandParameter="{Binding ElementName=autoListBoxNo}">
-                    </Button>
-                </WrapPanel>-->
+                <Separator x:Name="sep2" />
+                <Button Content="导出数据包" 
+                        Tag="{x:Static utils:RegularFontUtil.Arrow_Export_Up_24}"
+                        Template="{StaticResource CustomToolBarButtomTemplate}"
+                        Command="{Binding ExportDBCommand}" />
+                <Button Content="导入数据包" 
+                        Tag="{x:Static utils:RegularFontUtil.Arrow_Download_24}"
+                        Template="{StaticResource CustomToolBarButtomTemplate}"
+                        Command="{Binding ImportDBCommand}" />
+                
             </ToolBar>
         </Border>
 
-        <!-- 发货单主表:主表不允许编辑,只能选择一行后在下方编辑界面编辑 -->
+        <!-- 主表 -->
         <!-- 正常显示行标题:ShowRowsCount="True" 
                             RowHeaderWidth="30"-->
         <fdg:FilterDataGrid x:Name="fdgDelivery"
@@ -463,7 +440,7 @@
         <!-- 水平拖动分割线 -->
         <GridSplitter Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Height="5"  HorizontalAlignment="Stretch" />
 
-        <!-- 发货单信息 -->
+        <!-- 发货单信息界面 -->
         <Border Grid.Row="3" 
                 Grid.RowSpan="2"
                 Grid.Column="0"
@@ -735,7 +712,7 @@
             </Grid>
         </Border>
 
-        <!-- 发货单明细工具栏 -->
+        <!-- 明细工具栏 -->
         <Border Grid.Row="3" 
                 Grid.Column="1" 
                 BorderBrush="Gray"
@@ -754,8 +731,7 @@
                         Template="{StaticResource CustomToolBarButtomTemplate}"
                         Command="{Binding DeleteDetailsCommand}"
                         CommandParameter="{Binding ElementName=fdgDeliveryDetail}" />
-                <Separator x:Name="sep2" />
-                <!-- Arrow_Turn_Right_48 -->
+                <Separator x:Name="sepDtl" />
                 <Button x:Name="btnImportDtl"
                         Content="导入"
                         Tag="{x:Static utils:RegularFontUtil.Arrow_Circle_Down_32}"
@@ -780,7 +756,7 @@
             </ToolBar>
         </Border>
 
-        <!-- todo 没有行的时候左边不显示行标题,必须有行之后才会出现行标题,列宽会变化 -->
+        <!-- todo 没有行的时候左边不显示行标题,必须有行之后才会出现行标题 -->
         <!-- todo 当按比例设置标题宽度时,单元格的竖向边框会部分消失 -->
         <fdg:FilterDataGrid x:Name="fdgDeliveryDetail"
                             Grid.Row="4"