فهرست منبع

材料合同管理代码优化

LT069288 2 ماه پیش
والد
کامیت
705a968bd2
1فایلهای تغییر یافته به همراه64 افزوده شده و 104 حذف شده
  1. 64 104
      UniformMaterialManagementSystem/ViewModels/ContractPageViewModel.cs

+ 64 - 104
UniformMaterialManagementSystem/ViewModels/ContractPageViewModel.cs

@@ -1,6 +1,5 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
-using DocumentFormat.OpenXml.Spreadsheet;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.Win32;
 using System.Collections.ObjectModel;
@@ -348,14 +347,12 @@ namespace UniformMaterialManagementSystem.ViewModels
             var contract = _contractService.Get(x => x.Guid == contractModel.Guid);
             if (contract != null)
             {
-                contract.Company = SelectedMaterialCompany;
-                contract.CompanyGuid = SelectedMaterialCompany.Guid;
-                contract.PurchaseCompany = SelectedProductCompany;
-                contract.PurchaseCompanyGuid = SelectedProductCompany.Guid;
-                contract.ContractNo = ContractNo;
+                contract.CompanyGuid = SelectedMaterialCompany?.Guid ?? Guid.Empty;
+                contract.PurchaseCompanyGuid = SelectedProductCompany?.Guid ?? Guid.Empty;
+                contract.ContractNo = ContractNo ?? string.Empty;
                 contract.SigningDate = SigningDate.GetValueOrDefault();
-                contract.EditUser = EditUser;
-                contract.Telephone = Telephone;
+                contract.EditUser = EditUser ?? string.Empty;
+                contract.Telephone = Telephone ?? string.Empty;
                 contract.Attachment = Attachment;
                 contract.ApplyAttachment = ApplyAttachment;
             }
@@ -420,47 +417,8 @@ namespace UniformMaterialManagementSystem.ViewModels
             var result = MessageBox.Show($"确认删除材料合同号:{SelectedUnExportContract.ContractNo}的材料采购合同吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
             if (result != MessageBoxResult.OK) return;
 
-            _contractService?.Delete(x => x.Guid == SelectedUnExportContract.Guid);
-            _contractService?.SaveChanges();
-
-
-            //var selectedContracts = new List<ContractModel>();
-            //var changedContracts = new List<ContractModel>();
-            //foreach (var contract in UnExportContracts)
-            //{
-            //    if (!contract.IsSelected) continue;
-            //    if (contract.ChangedStatus)
-            //    {
-            //        changedContracts.Add(contract);
-            //    }
-            //    else
-            //    {
-            //        selectedContracts.Add(contract);
-            //    }
-            //}
-
-            //if (selectedContracts.Count + changedContracts.Count == 0)
-            //{
-            //    MessageBox.Show($"没有选中任何行", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
-            //    return;
-            //}
-            //if (selectedContracts.Count == 0)
-            //{
-            //    MessageBox.Show($"变更状态的合同不允许删除,请选择未变更的合同进行删除!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
-            //    return;
-            //}
-
-            ///* 确认删除提示 */
-            //var result = MessageBox.Show($"您选中了{selectedContracts.Count + changedContracts.Count}条合同,将有{changedContracts.Count}条合同被删除,确认继续?(变更状态的合同不允许删除)", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
-            //if (result != MessageBoxResult.OK) return;
-
-            ////删除选中行
-            //foreach (var contract in selectedContracts)
-            //{
-            //    _contractService?.Delete(x => x.Guid == contract.Guid);
-            //}
-
-            //_contractService?.SaveChanges();
+            _contractService.Delete(x => x.Guid == SelectedUnExportContract.Guid);
+            _contractService.SaveChanges();
 
             /* 重新加载数据 */
             LoadUnExportData();
@@ -653,21 +611,21 @@ namespace UniformMaterialManagementSystem.ViewModels
             }
 
             //对导出数据进行加密,用户选择保存位置
-           var path = DataBaseUtil.ExportData();
-           if (string.IsNullOrEmpty(path))
-           {
+            var path = DataBaseUtil.ExportData();
+            if (string.IsNullOrEmpty(path))
+            {
                 //取消保存,将选中合同修改为未导出状态
                 foreach (var contract in contractList)
                 {
                     contract.ExportStatus = false;
                 }
                 return;
-           }
-           
-           MessageBox.Show("材料合同数据包导出成功!");
+            }
 
-           //保存导出状态
-           _contractService.SaveChanges();
+            MessageBox.Show("材料合同数据包导出成功!");
+
+            //保存导出状态
+            _contractService.SaveChanges();
 
             //重新加载数据
             LoadUnExportData();
@@ -682,36 +640,36 @@ namespace UniformMaterialManagementSystem.ViewModels
             var path = DataBaseUtil.ImportData();
             if (string.IsNullOrEmpty(path)) return;
 
-             var contracts = DataBaseUtil.QueryAll<Contract>(path, q =>
-                            q.Include(x => x.Company)
-                                .Include(x => x.PurchaseCompany)
-                                .Include(x=>x.ContractDetails)
-                                .ThenInclude(x=>x.Material)).Result.ToList();
-
-             var materialCompanies = contracts.Select(x => x.Company).Distinct().ToArray();
-             var productCompanies = contracts.Select(x => x.PurchaseCompany).Distinct().ToArray();
-             var companies = materialCompanies.Concat(productCompanies).ToArray();
-             List<ContractDetail> details = [];
-             foreach (var contract in contracts)
-             {
-                 foreach (var detail in contract.ContractDetails)
-                 {
-                     details.Add(detail);
-                 }
-             }
-
-             var materials = details.ToArray().Select(x => x.Material).Distinct().ToArray();
-
-             //导入数据:(材料+成品)企业->合同->材料->合同明细
-             await DataBaseUtil.ImportTable("Companies", companies);
-             await DataBaseUtil.ImportTable("Contracts", contracts);
-             await DataBaseUtil.ImportTable("Materials", materials);
-             await DataBaseUtil.ImportTable("ContractDetails", details);
-
-             MessageBox.Show("材料合同数据包导入成功!");
-
-             //重新加载数据
-             LoadData();
+            var contracts = DataBaseUtil.QueryAll<Contract>(path, q =>
+                           q.Include(x => x.Company)
+                               .Include(x => x.PurchaseCompany)
+                               .Include(x => x.ContractDetails)
+                               .ThenInclude(x => x.Material)).Result.ToList();
+
+            var materialCompanies = contracts.Select(x => x.Company).Distinct().ToArray();
+            var productCompanies = contracts.Select(x => x.PurchaseCompany).Distinct().ToArray();
+            var companies = materialCompanies.Concat(productCompanies).ToArray();
+            List<ContractDetail> details = [];
+            foreach (var contract in contracts)
+            {
+                foreach (var detail in contract.ContractDetails)
+                {
+                    details.Add(detail);
+                }
+            }
+
+            var materials = details.ToArray().Select(x => x.Material).Distinct().ToArray();
+
+            //导入数据:(材料+成品)企业->合同->材料->合同明细
+            await DataBaseUtil.ImportTable("Companies", companies);
+            await DataBaseUtil.ImportTable("Contracts", contracts);
+            await DataBaseUtil.ImportTable("Materials", materials);
+            await DataBaseUtil.ImportTable("ContractDetails", details);
+
+            MessageBox.Show("材料合同数据包导入成功!");
+
+            //重新加载数据
+            LoadData();
         }
 
         /// <summary>
@@ -882,30 +840,32 @@ namespace UniformMaterialManagementSystem.ViewModels
             Contract contract = new Contract
             {
                 Year = App.CurrentUser!.WorkYear,
-                ContractNo = ContractNo == null ? "" : ContractNo,
-                CompanyGuid = SelectedMaterialCompany.Guid,
-                PurchaseCompanyGuid = SelectedProductCompany.Guid,
+                ContractNo = ContractNo ?? string.Empty,
+                CompanyGuid = SelectedMaterialCompany?.Guid ?? Guid.Empty,
+                PurchaseCompanyGuid = SelectedProductCompany?.Guid ?? Guid.Empty,
                 SigningDate = SigningDate.GetValueOrDefault().Date,
                 Attachment = Attachment,
                 ApplyAttachment = ApplyAttachment,
-                EditUser = EditUser,
+                EditUser = EditUser ?? string.Empty,
                 EditDate = DateTime.Now,
-                Telephone = Telephone,
+                Telephone = Telephone ?? string.Empty,
 
                 ContractDetails = new ObservableCollection<ContractDetail>()
             };
             foreach (var detail in ContractDetails)
             {
-                ContractDetail contractDetail = new ContractDetail();
-                contractDetail.ContractGuid = contract.Guid;
-                contractDetail.MaterialGuid = detail.Material.Guid;
-                contractDetail.ContractQty = detail.ContractQty;
-                contractDetail.ReduceQuantity = detail.ReduceQuantity;
-                contractDetail.DeliveryTime = detail.DeliveryTime;
-                contractDetail.InspectedQty = detail.InspectedQty;
-                contractDetail.InspectStatus = detail.InspectStatus;
-                contractDetail.UnitPrice = detail.UnitPrice;
-                contractDetail.ShippedQuantity = 0;
+                ContractDetail contractDetail = new ContractDetail
+                {
+                    ContractGuid = contract.Guid,
+                    MaterialGuid = detail.Material.Guid,
+                    ContractQty = detail.ContractQty,
+                    ReduceQuantity = detail.ReduceQuantity,
+                    DeliveryTime = detail.DeliveryTime,
+                    InspectedQty = detail.InspectedQty,
+                    InspectStatus = detail.InspectStatus,
+                    UnitPrice = detail.UnitPrice,
+                    ShippedQuantity = 0
+                };
 
                 contract.ContractDetails.Add(contractDetail);
             }
@@ -1059,11 +1019,11 @@ namespace UniformMaterialManagementSystem.ViewModels
             if (result != MessageBoxResult.OK) return;
 
             //更改变更状态,重置导出状态为未导出
-            var contract = _contractService?.Get(x => x.Guid == SelectedContract.Guid);
+            var contract = _contractService.Get(x => x.Guid == SelectedContract.Guid);
             if (contract == null) return;
             contract.ChangedStatus = true;
             contract.ExportStatus = false;
-            _contractService?.SaveChanges();
+            _contractService.SaveChanges();
 
             //重新加载数据
             LoadData();