Ver código fonte

检验报告保存功能

LT069288 4 meses atrás
pai
commit
6f2d2c0edf

+ 2 - 0
UniformMaterialManagementSystem/Services/DataBaseService.cs

@@ -129,6 +129,8 @@ namespace UniformMaterialManagementSystem.Services
 
         public EntityState Entry(TEntity entity) => context.Entry(entity).State;
 
+        public void SetEntryState(TEntity entity, EntityState state) => context.Entry(entity).State = state;
+
         #endregion
 
         #region Save

+ 2 - 0
UniformMaterialManagementSystem/Services/IDataBaseService.cs

@@ -55,6 +55,8 @@ namespace UniformMaterialManagementSystem.Services
 
         EntityState Entry(TEntity entity);
 
+        void SetEntryState(TEntity entity, EntityState state);
+
         #endregion
 
         #region Aggregate

+ 1 - 2
UniformMaterialManagementSystem/ViewModels/InspectionRecordPageViewModel.cs

@@ -77,9 +77,8 @@ namespace UniformMaterialManagementSystem.ViewModels
                 Companies.Add(row.Name);
             }
             //初始化检验类别
-            InspectCategories.Add("首件检验");
+            InspectCategories.Add("报样检验");
             InspectCategories.Add("首批检验");
-            InspectCategories.Add("生产过程");
             InspectCategories.Add("出厂检验");
 
             //初始化年份

+ 287 - 19
UniformMaterialManagementSystem/ViewModels/InspectionReportPageViewModel.cs

@@ -1,13 +1,9 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
-using DocumentFormat.OpenXml.Bibliography;
 using Microsoft.EntityFrameworkCore;
+using System.Collections.ObjectModel;
+using System.Text;
+using System.Windows;
 using UniformMaterialManagementSystem.Entities;
 using UniformMaterialManagementSystem.Services;
 using UniformMaterialManagementSystem.Views;
@@ -29,11 +25,45 @@ namespace UniformMaterialManagementSystem.ViewModels
         [ObservableProperty]
         private string? _contractNos;
 
+        [ObservableProperty]
+        private List<int> _years = [];
+
+        [ObservableProperty]
+        private int _selectedYear = DateTime.Now.Year;
+
+        private InspectionReportDetail? _selectedInspectionReportDetail;
+
         private readonly IDataBaseService<InspectionReport> _inspectionReportService;
 
-        public InspectionReportPageViewModel(IDataBaseService<InspectionReport> inspectionReportService)
+        private readonly IDataBaseService<InspectionReportDetail> _inspectionReportDetailService;
+
+        private readonly IDataBaseService<InspectApply> _inspectApplyService;
+
+        public InspectionReportPageViewModel(IDataBaseService<InspectionReport> inspectionReportService, IDataBaseService<InspectionReportDetail> inspectionReportDetailService, IDataBaseService<InspectApply> inspectApplyService)
         {
             _inspectionReportService = inspectionReportService;
+            _inspectionReportDetailService = inspectionReportDetailService;
+            _inspectApplyService = inspectApplyService;
+
+            //初始化年份
+            int currYear = DateTime.Now.Year;
+            for (int i = currYear - 10; i <= currYear + 1; i++)
+            {
+                Years.Add(i);
+            }
+
+            /* 加载数据 */
+            LoadData();
+        }
+
+        /// <summary>
+        /// 加载数据
+        /// </summary>
+        [RelayCommand]
+        private void LoadData()
+        {
+            //询问是否保存
+            AskIsSave();
 
             var inspectionReports = _inspectionReportService.Query()
                 .Include(x => x.InspectionReportDetails)
@@ -41,17 +71,47 @@ namespace UniformMaterialManagementSystem.ViewModels
                 .ThenInclude(x => x.Material)
                 .Include(x => x.InspectApply)
                 .ThenInclude(x => x.InspectApplyContractDetails)
+                .Where(x => x.InspectApply.Year == SelectedYear)
                 .ToList();
 
+            InspectionReports.Clear();
             foreach (var report in inspectionReports)
             {
                 InspectionReports.Add(report);
             }
+        }
 
+        /// <summary>
+        /// 判断单据是否有修改
+        /// </summary>
+        private bool IsSelectedChanged()
+        {
+            if (SelectedInspectionReport == null) return false;
+            var state = _inspectionReportService.Entry(SelectedInspectionReport);
+            if (state != EntityState.Unchanged) return true;
 
+            var reportDetails = SelectedInspectionReport.InspectionReportDetails;
+            var existDetails = _inspectionReportDetailService.Query(x => x.InspectionReportGuid == SelectedInspectionReport.Guid).ToList();
 
+            foreach (var detail in reportDetails)  //新增行或修改行操作
+            {
+                var detailState = _inspectionReportDetailService.Entry(detail);
+                if (detailState != EntityState.Unchanged)
+                {
+                    return true;
+                }
+            }
 
+            foreach (var detail in existDetails) //删除行操作
+            {
+                var detailState = _inspectionReportDetailService.Entry(detail);
+                if (detailState == EntityState.Deleted)
+                {
+                    return true;
+                }
+            }
 
+            return false;
         }
 
         /// <summary>
@@ -60,17 +120,52 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void InspectionReportSelectionChanged(InspectionReport inspectionReport)
         {
+            AskIsSave();
+
+            /* 绑定当前行数据*/
             SelectedInspectionReport = inspectionReport;
 
             //获取合同信息
             var dictionary = GetContractInformation(SelectedInspectionReport);
-            if (dictionary.ContainsKey("CompanyNames"))
-            {
-                PurchaseCompanyNames = dictionary["CompanyNames"];
-            }
-            if (dictionary.ContainsKey("ContractNos"))
+
+            PurchaseCompanyNames = dictionary.ContainsKey("CompanyNames") ? dictionary["CompanyNames"] : string.Empty;
+
+            ContractNos = dictionary.ContainsKey("ContractNos") ? dictionary["ContractNos"] : string.Empty;
+        }
+
+        /// <summary>
+        /// 询问是否保存更改
+        /// </summary>
+        private void AskIsSave()
+        {
+            var changed = IsSelectedChanged();
+            if (changed)
             {
-                ContractNos = dictionary["ContractNos"];
+                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);
+                    }
+                }
             }
         }
 
@@ -101,6 +196,8 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         public void AddInspectionReport()
         {
+            AskIsSave();
+
             /* 打开窗口,展示检验报告状态未生成的检验申请列表,选择检验申请,携带检验申请数据 */
             SelectInspectApplyToReportWindow inspectApplyWindow = new SelectInspectApplyToReportWindow();
             var result = inspectApplyWindow.ShowDialog();
@@ -117,7 +214,9 @@ namespace UniformMaterialManagementSystem.ViewModels
                 Department = App.CurrentUser!.SupervisionUnit.Name,
                 ReportTime = DateTime.Now,
                 ReportBasis = "依据" + inspectApply.Material.NormName,
-                EditUser = App.CurrentUser!.UserName
+                Conclusion = "合格",
+                EditUser = App.CurrentUser!.UserName,
+                InspectionReportDetails = []
             };
 
             InspectionReports.Add(inspectionReport);
@@ -134,15 +233,184 @@ namespace UniformMaterialManagementSystem.ViewModels
         {
             if (SelectedInspectionReport == null) return;
 
-            SelectedInspectionReport.InspectionReportDetails ??= [];
-
             InspectionReportDetail newDetail = new InspectionReportDetail
             {
                 InspectionReportGuid = SelectedInspectionReport.Guid
             };
-            SelectedInspectionReport.InspectionReportDetails.Add(newDetail);
+
+            SelectedInspectionReport.InspectionReportDetails.Add(newDetail); //sql执行update操作,有异常
+        }
+
+        /// <summary>
+        /// 验收组选中行改变事件
+        /// </summary>
+        [RelayCommand]
+        private void SelectedInspectionReportDetailChanged(InspectionReportDetail reportDetail)
+        {
+            _selectedInspectionReportDetail = reportDetail;
+        }
+
+        /// <summary>
+        /// 删除验收组
+        /// </summary>
+        [RelayCommand]
+        private void RemoveInspectionReportDetail()
+        {
+            if (_selectedInspectionReportDetail == null)
+            {
+                MessageBox.Show("请先选中一行再进行操作!");
+                return;
+            }
+
+            SelectedInspectionReport?.InspectionReportDetails.Remove(_selectedInspectionReportDetail);
+        }
+
+        /// <summary>
+        /// 保存检验报告
+        /// </summary>
+        [RelayCommand]
+        private void SaveInspectionReport()
+        {
+            if (SelectedInspectionReport == null) return;
+
+            /* 自定义校验 */
+            Dictionary<string, string> dictionary = CustomValidate();
+            if (dictionary.ContainsKey("result") && !string.IsNullOrEmpty(dictionary["result"]))
+            {
+                MessageBox.Show(dictionary["result"], "错误", MessageBoxButton.OK, MessageBoxImage.Error);
+                return;
+            }
+
+            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)
+            {
+                inpecApply.ReportStatus = true;
+            }
+
+            var result = _inspectionReportService.SaveChanges();
+            if (result)
+            {
+                MessageBox.Show("保存成功!");
+            }
         }
 
+        /// <summary>
+        /// 自定义校验
+        /// </summary>
+        private Dictionary<string, string> CustomValidate()
+        {
+            Dictionary<string, string> result = new Dictionary<string, string>();
+            StringBuilder errorMessage = new StringBuilder();
+
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.Department))
+            {
+                errorMessage.Append("检验部门不允许为空!\n");
+            }
+            if (SelectedInspectionReport?.ReportTime == null)
+            {
+                errorMessage.Append("检验时间不允许为空!\n");
+            }
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.ReportBasis))
+            {
+                errorMessage.Append("检验依据不允许为空!\n");
+            }
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.ReportDesc))
+            {
+                errorMessage.Append("检验情况及主要问题不允许为空!\n");
+            }
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.Conclusion))
+            {
+                errorMessage.Append("检验结论不允许为空!\n");
+            }
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.ConclusionDesc))
+            {
+                errorMessage.Append("检验结论不允许为空!\n");
+            }
+            if (string.IsNullOrEmpty(SelectedInspectionReport?.EditUser))
+            {
+                errorMessage.Append("承办人不允许为空!\n");
+            }
+
+            //校验验收组
+            var details = SelectedInspectionReport?.InspectionReportDetails;
+            if (details == null || details.Count == 0)
+            {
+                errorMessage.Append("验收组不允许为空!\n");
+            }
+            else
+            {
+                int leaderCount = 0;
+                int memberCount = 0;
+                for (int i = 0; i < details.Count; i++)
+                {
+                    var detail = details[i];
+                    if (string.IsNullOrEmpty(detail.JobCategory))
+                    {
+                        errorMessage.Append($"验收组中第{i + 1}行检验组不允许为空!\n");
+                    }
+                    if (string.IsNullOrEmpty(detail.SupervisionUnit))
+                    {
+                        errorMessage.Append($"验收组中第{i+1}行单位不允许为空!\n");
+                    }
+                    if (string.IsNullOrEmpty(detail.Inspector))
+                    {
+                        errorMessage.Append($"验收组中第{i + 1}行姓名不允许为空!\n");
+                    }
+
+                    if (detail.JobCategory == "组长")
+                    {
+                        leaderCount++;
+                    }
+                    else if (detail.JobCategory == "成员")
+                    {
+                        memberCount++;
+                    }
+                }
 
+                if (leaderCount != 1)
+                {
+                    errorMessage.Append("验收组中至少包含1名组长,不能超过1名组长!\n");
+                }
+
+                if (memberCount < 1)
+                {
+                    errorMessage.Append("验收组中至少包含1名成员!\n");
+                }
+            }
+
+            var isRepeat = SelectedInspectionReport?.InspectionReportDetails.GroupBy(x => x.Inspector).Any(g => g.Count() > 1);
+            if (isRepeat != null && (bool)isRepeat)
+            {
+                errorMessage.Append("验收组姓名有重复,请检查!\n");
+            }
+
+            result.Add("result", errorMessage.ToString());
+            return result;
+        }
     }
 }

+ 1 - 1
UniformMaterialManagementSystem/Views/ContractPage.xaml

@@ -221,7 +221,7 @@
                                             </ControlTemplate>
                                         </Button.Template>
                                     </Button>
-                                    <Button Command="{Binding SaveContractCommand}">
+                                    <Button x:Name="SaveButton" Command="{Binding SaveContractCommand}" Click="SaveButton_Click">
                                         <Button.Template>
                                             <ControlTemplate>
                                                 <Border

+ 6 - 1
UniformMaterialManagementSystem/Views/ContractPage.xaml.cs

@@ -52,6 +52,11 @@ namespace UniformMaterialManagementSystem.Views
             }
         }
 
+        private void SaveButton_Click(object sender, RoutedEventArgs e)
+        {
+            DataGridMain.CommitEdit();
+        }
+
         //private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
         //{
         //    if (bool.Parse(IsCompanyTextBox.Text))
@@ -62,7 +67,7 @@ namespace UniformMaterialManagementSystem.Views
         //    {
         //        MaterialCompanyComboBox.IsEnabled = true;
         //    }
-            
+
         //}
     }
 }

+ 17 - 13
UniformMaterialManagementSystem/Views/InspectionReportPage.xaml

@@ -341,7 +341,7 @@
                                 </ControlTemplate>
                             </Button.Template>
                         </Button>
-                        <Button Command="{Binding SaveInspectApplyCommand}">
+                        <Button Command="{Binding SaveInspectionReportCommand}">
                             <Button.Template>
                                 <ControlTemplate>
                                     <Border
@@ -418,7 +418,7 @@
                     <TextBox
                         Grid.Row="2"
                         Grid.Column="1"
-                        Text="{Binding SelectedInspectionReport.Department}" />
+                        Text="{Binding SelectedInspectionReport.Department, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                     <TextBlock
                         Grid.Row="2"
                         Grid.Column="2"
@@ -427,7 +427,7 @@
                     <DatePicker
                         Grid.Row="2"
                         Grid.Column="3"
-                        Text="{Binding SelectedInspectionReport.ReportTime}" />
+                        Text="{Binding SelectedInspectionReport.ReportTime, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                     <TextBlock
                         Grid.Row="3"
                         Grid.Column="0"
@@ -564,7 +564,7 @@
                         Grid.Row="7"
                         Grid.Column="1"
                         Grid.ColumnSpan="3"
-                        Text="{Binding SelectedInspectionReport.ReportBasis}" />
+                        Text="{Binding SelectedInspectionReport.ReportBasis, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                     <TextBlock
                         Grid.Row="8"
                         Grid.Column="0"
@@ -607,7 +607,7 @@
                             x:Name="IsSampleTextBox"
                             Grid.Row="0"
                             Grid.Column="2"
-                            Text="{Binding SelectedInspectionReport.IsSample}"
+                            Text="{Binding SelectedInspectionReport.IsSample, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             TextChanged="IsSampleTextBox_TextChanged"
                             Visibility="Collapsed" />
                         <TextBox
@@ -618,7 +618,7 @@
                             HorizontalContentAlignment="Left"
                             VerticalContentAlignment="Top"
                             AcceptsReturn="True"
-                            Text="{Binding SelectedInspectionReport.ReportDesc}"
+                            Text="{Binding SelectedInspectionReport.ReportDesc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             TextWrapping="Wrap"
                             VerticalScrollBarVisibility="Auto" />
                     </Grid>
@@ -647,15 +647,18 @@
                             Grid.Column="0"
                             Padding="10,0,10,0"
                             VerticalContentAlignment="Center"
+                            Checked="ConclusionGroupRadioButton_Checked"
                             Content="合格"
                             Foreground="Green"
-                            GroupName="ConclusionGroup" />
+                            GroupName="ConclusionGroup"
+                            IsChecked="True" />
                         <RadioButton
                             x:Name="SecondQualifiedRadioButton"
                             Grid.Row="0"
                             Grid.Column="1"
                             Padding="10,0,10,0"
                             VerticalContentAlignment="Center"
+                            Checked="ConclusionGroupRadioButton_Checked"
                             Content="合格(初测不合格,复测合格)"
                             Foreground="DarkOrange"
                             GroupName="ConclusionGroup" />
@@ -665,6 +668,7 @@
                             Grid.Column="2"
                             Padding="10,0,10,0"
                             VerticalContentAlignment="Center"
+                            Checked="ConclusionGroupRadioButton_Checked"
                             Content="不合格"
                             Foreground="Red"
                             GroupName="ConclusionGroup" />
@@ -672,7 +676,7 @@
                             x:Name="ConclusionTextBox"
                             Grid.Row="0"
                             Grid.Column="2"
-                            Text="{Binding SelectedInspectionReport.Conclusion}"
+                            Text="{Binding SelectedInspectionReport.Conclusion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             TextChanged="ConclusionTextBox_TextChanged"
                             Visibility="Collapsed" />
                         <TextBox
@@ -683,7 +687,7 @@
                             HorizontalContentAlignment="Left"
                             VerticalContentAlignment="Top"
                             AcceptsReturn="True"
-                            Text="{Binding SelectedInspectionReport.ConclusionDesc}"
+                            Text="{Binding SelectedInspectionReport.ConclusionDesc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             TextWrapping="Wrap"
                             VerticalScrollBarVisibility="Auto" />
                     </Grid>
@@ -733,7 +737,7 @@
                                             </ControlTemplate>
                                         </Button.Template>
                                     </Button>
-                                    <Button Command="{Binding}">
+                                    <Button Command="{Binding RemoveInspectionReportDetailCommand}">
                                         <Button.Template>
                                             <ControlTemplate>
                                                 <Border
@@ -772,7 +776,7 @@
                             ColumnHeaderStyle="{StaticResource CustomColumnHeaderStyle}"
                             HeadersVisibility="All"
                             HorizontalGridLinesBrush="LightSlateGray"
-                            ItemsSource="{Binding SelectedInspectionReport.InspectionReportDetails}"
+                            ItemsSource="{Binding SelectedInspectionReport.InspectionReportDetails, Mode=TwoWay}"
                             RowHeaderStyle="{StaticResource CustomRowHeaderStyle}"
                             RowStyle="{StaticResource CustomRowStyle}"
                             SelectionMode="Single"
@@ -780,7 +784,7 @@
                             <!--  选择行事件  -->
                             <b:Interaction.Triggers>
                                 <b:EventTrigger EventName="SelectionChanged">
-                                    <b:InvokeCommandAction Command="{Binding}" CommandParameter="{Binding ElementName=DataDetail, Path=SelectedItem}" />
+                                    <b:InvokeCommandAction Command="{Binding SelectedInspectionReportDetailChangedCommand}" CommandParameter="{Binding ElementName=DataGridDetail, Path=SelectedItem}" />
                                 </b:EventTrigger>
                             </b:Interaction.Triggers>
 
@@ -856,7 +860,7 @@
                     <TextBox
                         Grid.Row="11"
                         Grid.Column="1"
-                        Text="{Binding SelectedInspectionReport.EditUser}" />
+                        Text="{Binding SelectedInspectionReport.EditUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                 </Grid>
             </ScrollViewer>
 

+ 14 - 1
UniformMaterialManagementSystem/Views/InspectionReportPage.xaml.cs

@@ -44,7 +44,7 @@ namespace UniformMaterialManagementSystem.Views
         private void IsSampleTextBox_TextChanged(object sender, TextChangedEventArgs e)
         {
             var isSample = IsSampleTextBox.Text;
-            if (bool.Parse(isSample))
+            if (!string.IsNullOrEmpty(isSample) && bool.Parse(isSample))
             {
                 IsSampleRadioButton.IsChecked = true;
             }
@@ -70,5 +70,18 @@ namespace UniformMaterialManagementSystem.Views
                     break;
             }
         }
+
+        private void ConclusionGroupRadioButton_Checked(object sender, System.Windows.RoutedEventArgs e)
+        {
+            var radioButton = (RadioButton)sender;
+            if (radioButton.GroupName == "ConclusionGroup")
+            {
+                if (radioButton.Content != null && ConclusionTextBox != null)
+                {
+                    ConclusionTextBox.Text = radioButton.Content.ToString();
+                }
+            }
+        }
+
     }
 }