Bladeren bron

优化检验报告、样品登记保存逻辑

LT069288 3 maanden geleden
bovenliggende
commit
33c600ae8c

+ 19 - 26
UniformMaterialManagementSystem/Models/SampleRegistrationModel.cs

@@ -57,12 +57,10 @@ namespace UniformMaterialManagementSystem.Models
         private string? _telephone = null!;
 
         [ObservableProperty]
-        private InspectApply _inspectApply = null!;
+        private string? _randomTip;
 
-        public SampleRegistrationModel()
-        {
-            Guid = Guid.NewGuid();
-        }
+        [ObservableProperty]
+        private InspectApply _inspectApply = null!;
 
         public SampleRegistrationModel(SampleRegistration sample)
         {
@@ -84,28 +82,23 @@ namespace UniformMaterialManagementSystem.Models
             InspectApply = sample.InspectApply;
         }
 
-        public SampleRegistration ConvertToSampleRegistration()
+        public void ModifySampleRegistration(SampleRegistration sample)
         {
-            SampleRegistration sample = new SampleRegistration
-            {
-                Guid = this.Guid,
-                InspectApplyGuid = this.InspectApplyGuid,
-                SampleNo = this.SampleNo,
-                Department = this.Department,
-                EditTime = this.EditTime,
-                ProductDate = this.ProductDate,
-                BatchNo = this.BatchNo,
-                PacketNo = this.PacketNo,
-                Quantity = this.Quantity,
-                TestingItem = this.TestingItem,
-                SingleIndexItem = this.SingleIndexItem,
-                InspectionOrganization = this.InspectionOrganization,
-                ProductUsers = this.ProductUsers,
-                EditUser = this.EditUser,
-                Telephone = this.Telephone,
-                InspectApply = this.InspectApply,
-            };
-            return sample;
+            sample.Guid = this.Guid;
+            sample.SampleNo = this.SampleNo;
+            sample.InspectApplyGuid = this.InspectApplyGuid;
+            sample.Department = this.Department;
+            sample.EditTime = this.EditTime;
+            sample.ProductDate = this.ProductDate;
+            sample.BatchNo = this.BatchNo;
+            sample.PacketNo = this.PacketNo;
+            sample.Quantity = this.Quantity;
+            sample.TestingItem = this.TestingItem;
+            sample.SingleIndexItem = this.SingleIndexItem;
+            sample.InspectionOrganization = this.InspectionOrganization;
+            sample.ProductUsers = this.ProductUsers;
+            sample.EditUser = this.EditUser;
+            sample.Telephone = this.Telephone;
         }
     }
 }

+ 74 - 116
UniformMaterialManagementSystem/ViewModels/InspectionReportPageViewModel.cs

@@ -1,17 +1,16 @@
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
 using Microsoft.EntityFrameworkCore;
+using Microsoft.Win32;
 using System.Collections.ObjectModel;
 using System.IO;
 using System.Text;
 using System.Windows;
-using Microsoft.Win32;
 using UniformMaterialManagementSystem.Entities;
+using UniformMaterialManagementSystem.Models;
 using UniformMaterialManagementSystem.Services;
-using UniformMaterialManagementSystem.Views;
 using UniformMaterialManagementSystem.Utils;
-using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;
-using UniformMaterialManagementSystem.Models;
+using UniformMaterialManagementSystem.Views;
 
 namespace UniformMaterialManagementSystem.ViewModels
 {
@@ -143,80 +142,69 @@ namespace UniformMaterialManagementSystem.ViewModels
             return false;
         }
 
-        /// <summary>
-        /// 选择行改变事件
-        /// </summary>
-        //[RelayCommand]
-        //private void InspectionReportSelectionChanged(InspectionReport inspectionReport)
-        //{
-        //    AskIsSave();
-
-        //    /* 绑定当前行数据*/
-        //    SelectedInspectionReport = inspectionReport;
-
-        //    //获取合同信息
-        //    var dictionary = GetContractInformation(SelectedInspectionReport);
-
-        //    PurchaseCompanyNames = dictionary.ContainsKey("CompanyNames") ? dictionary["CompanyNames"] : string.Empty;
-
-        //    ContractNos = dictionary.ContainsKey("ContractNos") ? dictionary["ContractNos"] : string.Empty;
-        //}
-
         /// <summary>
         /// 询问是否保存更改
         /// </summary>
-        //private void AskIsSave()
-        //{
-        //    var changed = IsCurrentReportChanged();
-        //    if (changed)
-        //    {
-        //        var result = MessageBox.Show($"检验报告编号【{SelectedInspectionReport?.ReportNo}】已修改,是否保存修改?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
-        //        if (result == MessageBoxResult.Yes)
-        //        {
-        //            //保存上一行的修改
-        //            SaveInspectionReport();
-        //        }
-        //        else
-        //        {
-        //            //放弃更改
-        //            var state = _inspectionReportService.Entry(SelectedInspectionReport);
-        //            if (state == EntityState.Detached) //新增行直接移除
-        //            {
-        //                InspectionReports.Remove(SelectedInspectionReport);
-        //            }
-
-        //            _inspectionReportService.SetEntryState(SelectedInspectionReport, EntityState.Unchanged);
-
-        //            var details = _inspectionReportDetailService.GetAll(x => x.InspectionReportGuid == SelectedInspectionReport.Guid).ToList();
-        //            SelectedInspectionReport.InspectionReportDetails.Clear();
-        //            foreach (var detail in details) //重新将数据库的数据赋值过来,并设置跟踪状态Unchanged
-        //            {
-        //                SelectedInspectionReport.InspectionReportDetails.Add(detail);
-        //                _inspectionReportDetailService.SetEntryState(detail, EntityState.Unchanged);
-        //            }
-        //        }
-        //    }
-        //}
-
-        /// <summary>
-        /// 根据检验申请-合同信息拼接采购机构名称
-        /// </summary>
-        private Dictionary<string, string> GetContractInformation(InspectionReport? inspectionReport)
+        private bool AskIsSave()
         {
-            Dictionary<string, string> dictionary = new Dictionary<string, string>();
-            StringBuilder purchaseCompanyNames = new StringBuilder();
-            StringBuilder contractNos = new StringBuilder();
-            if (inspectionReport == null) return dictionary;
-            var contractDetails = inspectionReport.InspectApply.InspectApplyContractDetails;
-            foreach (var detail in contractDetails)
+            if (SelectedInspectionReport == null) return true;
+            var isChanged = IsCurrentReportChanged(SelectedInspectionReport);
+            if (!isChanged) return true;
+
+            var result = MessageBox.Show($"检验报告编号【{SelectedInspectionReport?.ReportNo}】已修改,是否保存修改?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
+            if (result == MessageBoxResult.Yes)
+            {
+                Dictionary<string, string> dictionary = CustomValidate();
+                if (dictionary.ContainsKey("result") && !string.IsNullOrEmpty(dictionary["result"]))
+                {
+                    MessageBox.Show(dictionary["result"], "错误", MessageBoxButton.OK, MessageBoxImage.Error);
+                    return false;
+                }
+                //保存修改
+                SaveInspectionReport();
+            }
+            else if (result == MessageBoxResult.No) //放弃更改
             {
-                purchaseCompanyNames.Append(detail.PurchaseCompany).Append("\n");
-                contractNos.Append(detail.ContractNo).Append("\n");
+                var inspectionReport = _inspectionReportService.Get(x => x.Guid == SelectedInspectionReport!.Guid);
+
+                if (inspectionReport == null) //新增行直接移除
+                {
+                    if (SelectedInspectionReport != null) InspectionReports.Remove(SelectedInspectionReport);
+                }
+                else
+                {
+                    _inspectionReportService.SetEntryState(inspectionReport, EntityState.Unchanged);
+
+                    if (SelectedInspectionReport != null)
+                    {
+                        ModifyModelFromInspectReport(SelectedInspectionReport, inspectionReport);
+
+                        var details = _inspectionReportDetailService
+                            .GetAll(x => x.InspectionReportGuid == SelectedInspectionReport.Guid).ToList();
+                        SelectedInspectionReport.InspectionReportDetails.Clear();
+                        foreach (var detail in details) //重新将数据库的数据赋值过来,并设置跟踪状态Unchanged
+                        {
+                            SelectedInspectionReport.InspectionReportDetails.Add(detail);
+                            _inspectionReportDetailService.SetEntryState(detail, EntityState.Unchanged);
+                        }
+                    }
+                }
             }
-            dictionary.Add("CompanyNames", purchaseCompanyNames.ToString());
-            dictionary.Add("ContractNos", contractNos.ToString());
+            return true;
+        }
 
-            return dictionary;
+        private void ModifyModelFromInspectReport(InspectionReportModel model, InspectionReport report)
+        {
+            model.ReportNo  = report.ReportNo;
+            model.Department = report.Department;
+            model.ReportTime = report.ReportTime;
+            model.ReportBasis = report.ReportBasis;
+            model.IsSample = report.IsSample;
+            model.ReportDesc = report.ReportDesc;
+            model.Conclusion = report.Conclusion;
+            model.ConclusionDesc = report.ConclusionDesc;
+            model.EditUser = report.EditUser;
+            model.InspectionReportDetails = report.InspectionReportDetails;
         }
 
         /// <summary>
@@ -225,7 +213,7 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         public void AddInspectionReport()
         {
-            //AskIsSave();
+            if (!AskIsSave()) return;
 
             /* 打开窗口,展示检验报告状态未生成的检验申请列表,选择检验申请,携带检验申请数据 */
             SelectInspectApplyWindowViewModel viewModel =
@@ -248,7 +236,7 @@ namespace UniformMaterialManagementSystem.ViewModels
                 ReportTime = DateTime.Now,
                 ReportBasis = "依据" + inspectApply.Material.NormName,
                 Conclusion = "合格",
-                EditUser = App.CurrentUser!.UserName,
+                EditUser = App.CurrentUser.UserName,
                 InspectionReportDetails = []
             };
 
@@ -319,14 +307,13 @@ namespace UniformMaterialManagementSystem.ViewModels
             var inspectionReport = _inspectionReportService.Get(x => x.Guid == SelectedInspectionReport.Guid);
             if (inspectionReport == null) //新增行
             {
-                InspectionReport newInspectionReport = new InspectionReport(); 
+                InspectionReport newInspectionReport = new InspectionReport();
                 SelectedInspectionReport.ModifyInspectionReport(newInspectionReport);
                 _inspectionReportService.Insert(newInspectionReport);
             }
             else  //判断是否修改
             {
                 SelectedInspectionReport.ModifyInspectionReport(inspectionReport);
-                var state = _inspectionReportService.Entry(inspectionReport);//主表修改状态
 
                 ////处理明细表新增行(无法跟踪明细表的新增行)
                 var existReportDetails = _inspectionReportDetailService.GetAll(x => x.InspectionReportGuid == inspectionReport.Guid).ToList();
@@ -340,35 +327,11 @@ namespace UniformMaterialManagementSystem.ViewModels
                 }
             }
 
-            //var state = _inspectionReportService.Entry(SelectedInspectionReport);
-            //switch (state)
-            //{
-            //    case EntityState.Detached:
-            //    case EntityState.Added:
-            //        _inspectionReportService.Insert(SelectedInspectionReport);
-            //        break;
-
-            //    case EntityState.Modified:
-            //        _inspectionReportService.Update(SelectedInspectionReport);
-            //        break;
-            //}
-
-            ////处理明细表新增行(无法跟踪明细表的新增行)
-            //var existReportDetails = _inspectionReportDetailService.GetAll(x => x.InspectionReportGuid == SelectedInspectionReport.Guid).ToList();
-            //foreach (var detail in SelectedInspectionReport.InspectionReportDetails)
-            //{
-            //    var existDetail = existReportDetails.FirstOrDefault(x => x.Guid == detail.Guid);
-            //    if (existDetail == null)
-            //    {
-            //        _inspectionReportDetailService.Insert(detail);
-            //    }
-            //}
-
             /* 反写检验申请单的检验报告状态为true */
-            var inpecApply = _inspectApplyService.Get(x => x.Guid == SelectedInspectionReport.InspectApplyGuid);
-            if (inpecApply != null)
+            var inspectApply = _inspectApplyService.Get(x => x.Guid == SelectedInspectionReport.InspectApplyGuid);
+            if (inspectApply != null)
             {
-                inpecApply.ReportStatus = true;
+                inspectApply.ReportStatus = true;
             }
 
             var result = _inspectionReportService.SaveChanges();
@@ -378,8 +341,6 @@ namespace UniformMaterialManagementSystem.ViewModels
             }
         }
 
-       
-
         /// <summary>
         /// 导出检验报告
         /// </summary>
@@ -388,12 +349,7 @@ namespace UniformMaterialManagementSystem.ViewModels
         {
             if (SelectedInspectionReport == null) return;
 
-            bool isChanged = IsCurrentReportChanged(SelectedInspectionReport);
-            if (isChanged)
-            {
-                MessageBox.Show($"检验报告编号【{SelectedInspectionReport.ReportNo}】未保存,请保存后再导出!","提示",MessageBoxButton.OK,MessageBoxImage.Warning);
-                return;
-            }
+            if (!AskIsSave()) return;
 
             const string templateFilePath = "Template\\军需物资质量监督检验报告模板.docx";
             SaveFileDialog saveFileDialog = new SaveFileDialog()
@@ -419,10 +375,12 @@ namespace UniformMaterialManagementSystem.ViewModels
 
         private Dictionary<string, string> CreateDictionary(InspectionReportModel inspectionReport)
         {
-            Dictionary<string, string> dictionary = new Dictionary<string, string>();
-            dictionary.Add("ReportNo", inspectionReport.ReportNo);
-            dictionary.Add("Department", inspectionReport.Department);
-            dictionary.Add("ReportTime", inspectionReport.ReportTime.ToString("yyyy-MM-dd"));
+            Dictionary<string, string> dictionary = new()
+            {
+                { "ReportNo", inspectionReport.ReportNo },
+                { "Department", inspectionReport.Department },
+                { "ReportTime", inspectionReport.ReportTime.ToString("yyyy-MM-dd") }
+            };
             //检验类别
             switch (inspectionReport.InspectApply.InspCategory)
             {
@@ -462,7 +420,7 @@ namespace UniformMaterialManagementSystem.ViewModels
             //验收组
             for (int i = 0; i < 5; i++)
             {
-                if (i< inspectionReport.InspectionReportDetails.Count)
+                if (i < inspectionReport.InspectionReportDetails.Count)
                 {
                     var detail = inspectionReport.InspectionReportDetails[i];
                     dictionary.Add("JobCategory" + i, detail.JobCategory);
@@ -477,7 +435,7 @@ namespace UniformMaterialManagementSystem.ViewModels
                 }
             }
 
-            dictionary.Add("EditUser",inspectionReport.EditUser);
+            dictionary.Add("EditUser", inspectionReport.EditUser);
 
             return dictionary;
         }

+ 84 - 87
UniformMaterialManagementSystem/ViewModels/SampleRegistrationPageViewModel.cs

@@ -1,21 +1,16 @@
-using System;
-using System.Collections.Generic;
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.Win32;
 using System.Collections.ObjectModel;
 using System.IO;
-using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 using System.Windows;
-using CommunityToolkit.Mvvm.ComponentModel;
-using CommunityToolkit.Mvvm.Input;
-using Microsoft.EntityFrameworkCore;
-using Microsoft.Win32;
 using UniformMaterialManagementSystem.Entities;
 using UniformMaterialManagementSystem.Models;
 using UniformMaterialManagementSystem.Services;
 using UniformMaterialManagementSystem.Utils;
 using UniformMaterialManagementSystem.Views;
-using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
 
 namespace UniformMaterialManagementSystem.ViewModels
 {
@@ -59,6 +54,9 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void LoadData()
         {
+            //询问是否保存
+            if (!AskIsSave()) return;
+
             var sampleRegistrations = _sampleService.Query()
                 .Include(x => x.InspectApply)
                 .ThenInclude(x => x.Material)
@@ -72,28 +70,20 @@ namespace UniformMaterialManagementSystem.ViewModels
             }
         }
 
-        /// <summary>
-        /// 选择行改变事件
-        /// </summary>
-        //[RelayCommand]
-        //private void SampleRegistrationSelectionChanged(SampleRegistrationModel sample)
-        //{
-        //    //询问是否保存修改内容
-        //    AskIsSave();
-
-        //    //切换行
-        //    SelectedSampleRegistration = sample;
-        //}
-
         /// <summary>
         /// 新增
         /// </summary>
         [RelayCommand]
         private void AddSampleRegistration()
         {
+            if (SelectedSampleRegistration != null)
+            {
+                //询问是否保存
+                if (!AskIsSave()) return;
+            }
+
             /* 打开窗口,展示抽样状态未生成的检验申请列表,选择检验申请,携带检验申请数据 */
-            SelectInspectApplyWindowViewModel viewModel =
-                new SelectInspectApplyWindowViewModel(_inspectApplyService, "SampleRegistration");
+            SelectInspectApplyWindowViewModel viewModel = new SelectInspectApplyWindowViewModel(_inspectApplyService, "SampleRegistration");
             SelectInspectApplyWindow inspectApplyWindow = new SelectInspectApplyWindow(viewModel);
 
             var result = inspectApplyWindow.ShowDialog();
@@ -103,22 +93,24 @@ namespace UniformMaterialManagementSystem.ViewModels
             if (inspectApply == null) return;
 
             //新增抽样登记并设置默认值
-            SampleRegistrationModel sampleRegistration = new SampleRegistrationModel
+            SampleRegistration sampleRegistration = new SampleRegistration
             {
                 InspectApply = inspectApply,
+                InspectApplyGuid = inspectApply.Guid,
                 SampleNo = inspectApply.ApplyNo.Replace("申请", ""),
                 EditTime = DateTime.Now,
                 ProductDate = DateTime.Now,
                 BatchNo = inspectApply.BatchNo,
                 TestingItem = "全检",
                 Department = App.CurrentUser!.SupervisionUnit.Name,
-                EditUser = App.CurrentUser!.UserName,
+                EditUser = App.CurrentUser.UserName,
             };
+            SampleRegistrationModel model = new SampleRegistrationModel(sampleRegistration);
 
-            SampleRegistrations.Add(sampleRegistration);
+            SampleRegistrations.Add(model);
 
             //选中当前行
-            SelectedSampleRegistration = sampleRegistration;
+            SelectedSampleRegistration = model;
         }
 
         /// <summary>
@@ -141,16 +133,15 @@ namespace UniformMaterialManagementSystem.ViewModels
             var sampleRegistration = _sampleService.Get(x => x.Guid == SelectedSampleRegistration.Guid);
             if (sampleRegistration == null)  //新增
             {
-                sampleRegistration = SelectedSampleRegistration.ConvertToSampleRegistration();
+                sampleRegistration = new SampleRegistration();
+                SelectedSampleRegistration.ModifySampleRegistration(sampleRegistration);
                 _sampleService.Insert(sampleRegistration);
             }
             else  //修改
             {
-                ModifySampleRegistrationFromModel(sampleRegistration, SelectedSampleRegistration);
+                SelectedSampleRegistration.ModifySampleRegistration(sampleRegistration);
             }
 
-            var state = _sampleService.Entry(sampleRegistration);
-
             //反写检验申请单的抽样送检状态
             var inspectApply = _inspectApplyService.Get(x => x.Guid == sampleRegistration.InspectApplyGuid);
             if (inspectApply != null)
@@ -164,7 +155,6 @@ namespace UniformMaterialManagementSystem.ViewModels
             {
                 MessageBox.Show("保存成功!");
             }
-
         }
 
         /// <summary>
@@ -178,6 +168,9 @@ namespace UniformMaterialManagementSystem.ViewModels
                 MessageBox.Show("请先选中一行抽样登记表,再执行导出操作!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                 return;
             }
+            //询问是否保存
+            if (!AskIsSave()) return;
+
             const string templateFilePath = "Template\\被装材料产品抽样信息登记表模板.docx";
             SaveFileDialog saveFileDialog = new SaveFileDialog()
             {
@@ -212,7 +205,8 @@ namespace UniformMaterialManagementSystem.ViewModels
             var organizations = _organizationService.QueryNoTracking(x => x.IsEnabled).OrderBy(x => x.OrderNo).ToList();
             if (organizations.Count == 0)
             {
-                MessageBox.Show("没有要抽取的检测机构,请先维护检测机构信息!");
+                //MessageBox.Show("没有要抽取的检测机构,请先维护检测机构信息!");
+                SelectedSampleRegistration.RandomTip = "没有要抽取的检测机构,请先维护检测机构信息!";
                 return;
             }
 
@@ -220,12 +214,14 @@ namespace UniformMaterialManagementSystem.ViewModels
             int random = RandomHelper.CreateRandom(organizations.Count);
             int count = random == 0 ? organizations.Count : random;
 
-            MessageBox.Show($"系统生成随机数为{random},将使用第{count}家检测机构。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+            //MessageBox.Show($"系统生成随机数为{random},将使用第{count}家检测机构。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+            SelectedSampleRegistration.RandomTip = $"系统生成随机数为{random},将使用第{count}家检测机构。";
 
             InspectionOrganization? organization = organizations.Find(x => x.OrderNo == count);
             if (organization == null)
             {
-                MessageBox.Show($"第{count}家检测机构不存在,请确保检测机构序号正确!");
+                //MessageBox.Show($"第{count}家检测机构不存在,请确保检测机构序号正确!");
+                SelectedSampleRegistration.RandomTip = $"第{count}家检测机构不存在,请确保检测机构序号正确!";
                 return;
             }
 
@@ -239,48 +235,63 @@ namespace UniformMaterialManagementSystem.ViewModels
         }
 
         /// <summary>
-        /// 询问是否保存
+        ///判断单据是否有修改,true:已修改 false:未修改
         /// </summary>
-        private void AskIsSave()
+        private bool IsCurrentSampleChanged(SampleRegistrationModel sampleRegistrationModel)
         {
-            if (SelectedSampleRegistration == null) return;
-            var sampleRegistration = _sampleService.Get(x => x.Guid == SelectedSampleRegistration.Guid);
-
-            bool isNew = false;
-            EntityState state = EntityState.Unchanged;
-            if (sampleRegistration == null)
+            var sampleRegistration = _sampleService.Get(x => x.Guid == sampleRegistrationModel.Guid);
+            if (sampleRegistration == null) //新增
             {
-                isNew = true;
+                return true;
             }
             else
             {
-                ModifySampleRegistrationFromModel(sampleRegistration, SelectedSampleRegistration);
-
-                state = _sampleService.Entry(sampleRegistration);
+                sampleRegistrationModel.ModifySampleRegistration(sampleRegistration);
+                var state = _sampleService.Entry(sampleRegistration);
+                if (state == EntityState.Modified)
+                {
+                    return true;
+                }
             }
+            return false;
+        }
 
-            if (isNew || state == EntityState.Modified) //新增或修改
+        /// <summary>
+        /// 询问是否保存
+        /// </summary>
+        private bool AskIsSave()
+        {
+            if (SelectedSampleRegistration == null) return true;
+            var isChanged = IsCurrentSampleChanged(SelectedSampleRegistration);
+            if (!isChanged) return true;
+            var result = MessageBox.Show($"抽样登记表【{SelectedSampleRegistration.SampleNo}】已修改,是否保存修改?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
+
+            if (result == MessageBoxResult.Yes)
             {
-                //询问是否保存
-                var result = MessageBox.Show($"抽样登记表编号:【{SelectedSampleRegistration.SampleNo}】数据已修改,是否保存?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
-                if (result == MessageBoxResult.Yes)
+                //自定义校验
+                var validateDictionary = CustomValidate();
+                if (validateDictionary.ContainsKey("result") && !string.IsNullOrEmpty(validateDictionary["result"]))
                 {
-                    //保存修改
-                    SaveSampleRegistration();
+                    MessageBox.Show(validateDictionary["result"], "提示", MessageBoxButton.OK, MessageBoxImage.Error);
+                    return false;
                 }
-                else if (result == MessageBoxResult.No)
+                SaveSampleRegistration();
+            }
+            else if (result == MessageBoxResult.No)
+            {
+                var sampleRegistration = _sampleService.Get(x => x.Guid == SelectedSampleRegistration.Guid);
+                if (sampleRegistration == null)//新增
                 {
-                    if (isNew)
-                    {
-                        SampleRegistrations.Remove(SelectedSampleRegistration);
-                    }
-                    else if (state == EntityState.Modified)
-                    {
-                        _sampleService.SetEntryState(sampleRegistration, EntityState.Unchanged);
-                        ModifyModelFromSampleRegistration(SelectedSampleRegistration, sampleRegistration);
-                    }
+                    SampleRegistrations.Remove(SelectedSampleRegistration);
+                }
+                else  //修改
+                {
+                    _sampleService.SetEntryState(sampleRegistration, EntityState.Unchanged);
+                    ModifyModelFromSampleRegistration(SelectedSampleRegistration, sampleRegistration);
                 }
             }
+
+            return true;
         }
 
         private Dictionary<string, string> CustomValidate()
@@ -332,22 +343,6 @@ namespace UniformMaterialManagementSystem.ViewModels
             return result;
         }
 
-        private void ModifySampleRegistrationFromModel(SampleRegistration sampleRegistration, SampleRegistrationModel model)
-        {
-            sampleRegistration.Department = model.Department;
-            sampleRegistration.EditTime = model.EditTime;
-            sampleRegistration.ProductDate = model.ProductDate;
-            sampleRegistration.BatchNo = model.BatchNo;
-            sampleRegistration.PacketNo = model.PacketNo;
-            sampleRegistration.Quantity = model.Quantity;
-            sampleRegistration.TestingItem = model.TestingItem;
-            sampleRegistration.SingleIndexItem = model.SingleIndexItem;
-            sampleRegistration.InspectionOrganization = model.InspectionOrganization;
-            sampleRegistration.ProductUsers = model.ProductUsers;
-            sampleRegistration.EditUser = model.EditUser;
-            sampleRegistration.Telephone = model.Telephone;
-        }
-
         private void ModifyModelFromSampleRegistration(SampleRegistrationModel model, SampleRegistration sampleRegistration)
         {
             model.Department = sampleRegistration.Department;
@@ -366,10 +361,12 @@ namespace UniformMaterialManagementSystem.ViewModels
 
         private Dictionary<string, string> CreateDictionary(SampleRegistrationModel sampleRegistration)
         {
-            Dictionary<string, string> dictionary = new Dictionary<string, string>();
-            dictionary.Add("SampleNo", sampleRegistration.SampleNo);
-            dictionary.Add("Department", sampleRegistration.Department);
-            dictionary.Add("EditTime", sampleRegistration.EditTime.ToString("yyyy年MM月dd日"));
+            Dictionary<string, string> dictionary = new()
+            {
+                { "SampleNo", sampleRegistration.SampleNo },
+                { "Department", sampleRegistration.Department },
+                { "EditTime", sampleRegistration.EditTime.ToString("yyyy年MM月dd日") }
+            };
             //检验类别
             switch (sampleRegistration.InspectApply.InspCategory)
             {
@@ -412,11 +409,11 @@ namespace UniformMaterialManagementSystem.ViewModels
 
                     break;
             }
-            dictionary.Add("SingleIndexItem", sampleRegistration.SingleIndexItem == null ? "" : sampleRegistration.SingleIndexItem);
+            dictionary.Add("SingleIndexItem", sampleRegistration.SingleIndexItem ?? "");
             dictionary.Add("InspectionOrganization", sampleRegistration.InspectionOrganization);
-            dictionary.Add("ProductUsers", sampleRegistration.ProductUsers == null ? "" : sampleRegistration.ProductUsers);
+            dictionary.Add("ProductUsers", sampleRegistration.ProductUsers ?? "");
             dictionary.Add("EditUser", sampleRegistration.EditUser);
-            dictionary.Add("Telephone", sampleRegistration.Telephone == null ? "" : sampleRegistration.Telephone);
+            dictionary.Add("Telephone", sampleRegistration.Telephone ?? "");
 
             return dictionary;
         }

+ 8 - 0
UniformMaterialManagementSystem/Views/SampleRegistrationPage.xaml

@@ -664,6 +664,14 @@
                             </ControlTemplate>
                         </Button.Template>
                     </Button>
+                    <TextBlock
+                        Grid.Row="8"
+                        Grid.Column="2"
+                        Grid.ColumnSpan="2"
+                        HorizontalAlignment="Left"
+                        Foreground="DimGray"
+                        Text="{Binding SelectedSampleRegistration.RandomTip, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                        TextWrapping="Wrap" />
                     <TextBlock
                         Grid.Row="9"
                         Grid.Column="0"