Selaa lähdekoodia

新增检验报告录入模块

LT069288 3 kuukautta sitten
vanhempi
commit
644e457a57

+ 219 - 19
UniformMaterialManagementSystem/ViewModels/InspectionReportInputPageViewModel.cs

@@ -1,26 +1,30 @@
-using System.Collections.ObjectModel;
-using System.Text;
-using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
 using Microsoft.EntityFrameworkCore;
+using System.Collections.ObjectModel;
+using System.ComponentModel.DataAnnotations;
+using System.Text;
+using UniformMaterialManagementSystem.Custom;
 using UniformMaterialManagementSystem.Entities;
 using UniformMaterialManagementSystem.Services;
 
 namespace UniformMaterialManagementSystem.ViewModels
 {
-    public partial class InspectionReportInputPageViewModel : ObservableObject
+    public partial class InspectionReportInputPageViewModel : ObservableValidator
     {
         [ObservableProperty]
-        private string _reportNo;
+        private string _reportNo = string.Empty;
 
         [ObservableProperty]
-        private string _department;
+        [Required(ErrorMessage = "检验部门不能为空")]
+        private string _department = string.Empty;
 
         [ObservableProperty]
+        [Required(ErrorMessage = "检验时间不能为空")]
         private DateTime _reportTime;
 
         [ObservableProperty]
-        private string _inspectCategory;
+        private string _inspectCategory = string.Empty;
 
         [ObservableProperty]
         private string? _purchaseCompanyNames;
@@ -29,21 +33,36 @@ namespace UniformMaterialManagementSystem.ViewModels
         private string? _contractNos;
 
         [ObservableProperty]
-        private string? _reportBasis;
+        [Required(ErrorMessage = "检验依据不能为空")]
+        private string _reportBasis = string.Empty;
 
         [ObservableProperty]
         private bool _isSample;
 
         [ObservableProperty]
-        private string _reportDesc;
+        [Required(ErrorMessage = "检验情况及主要问题不能为空")]
+        private string _reportDesc = string.Empty;
 
         [ObservableProperty]
-        private string _conclusion;
+        private string _conclusion = string.Empty;
 
         [ObservableProperty]
-        private string _conclusionDesc;
+        [Required(ErrorMessage = "检验结论不能为空")]
+        private string _conclusionDesc = string.Empty;
 
+        [ObservableProperty]
+        private string _editUser = string.Empty;
+
+        [ObservableProperty]
+        private ObservableCollection<InspectionReportDetail> _inspectionReportDetails = [];
 
+        private InspectionReportDetail? _selectedInspectionReportDetail;
+
+        [ObservableProperty]
+        private List<string> _jobCategories = [];
+
+        [ObservableProperty]
+        private List<string> _userNames = [];
 
         [ObservableProperty]
         private ObservableCollection<InspectApply> _inspectApplies = [];
@@ -53,9 +72,24 @@ namespace UniformMaterialManagementSystem.ViewModels
 
         private readonly IDataBaseService<InspectApply> _inspectApplyService;
 
-        public InspectionReportInputPageViewModel(IDataBaseService<InspectApply> inspectApplyService)
+        private readonly IDataBaseService<InspectionReport> _inspectionReportService;
+
+        public InspectionReportInputPageViewModel(IDataBaseService<InspectApply> inspectApplyService, IDataBaseService<User> userService,IDataBaseService<InspectionReport> inspectionReportService)
         {
             _inspectApplyService = inspectApplyService;
+            _inspectionReportService = inspectionReportService;
+
+            //检验组
+            JobCategories.Add("组长");
+            JobCategories.Add("成员");
+
+            //姓名
+            var users = userService.QueryNoTracking(
+                x => x.IsEnabled && x.SupervisionUnitGuid == App.CurrentUser.SupervisionUnitGuid).ToList();
+            foreach (var user in users)
+            {
+                UserNames.Add(user.UserName);
+            }
 
             LoadData();
         }
@@ -68,7 +102,7 @@ namespace UniformMaterialManagementSystem.ViewModels
         {
             var inspectApplyList = _inspectApplyService.QueryNoTracking()
                 .Include(x => x.Material)
-                .Include(x=>x.InspectApplyContractDetails)
+                .Include(x => x.InspectApplyContractDetails)
                 .Where(x => x.Year == App.CurrentUser.WorkYear && !x.ReportStatus).ToList();
 
             InspectApplies.Clear();
@@ -84,18 +118,167 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void SaveInspectionReport()
         {
-            int i = 0;
+            //校验是否选择检验申请单
+            if (SelectedInspectApply == null)
+            {
+                MessageEx.ShowWarningDialog("请双击检验申请表行数据进行新增,再执行保存!");
+                return;
+            }
+
+            //校验所有属性
+            ValidateAllProperties();
+            if (HasErrors)
+            {
+                return;
+            }
+
+            //校验验收组
+            Dictionary<string, string> dictionary = CustomValidate();
+            if (dictionary.ContainsKey("result") && !string.IsNullOrEmpty(dictionary["result"]))
+            {
+                MessageEx.ShowWarningDialog(dictionary["result"]);
+                return;
+            }
+
+            //新增检验报告
+            AddInspectionReportAndDetail();
+
+            //反写检验申请表的检验报告状态
+            var inspect = _inspectApplyService.Get(x => x.Guid == SelectedInspectApply.Guid);
+            if (inspect == null) return;
+            inspect.ReportStatus = true;
+
+            var result = _inspectionReportService.SaveChanges();
+            if (result)
+            {
+                MessageEx.ShowInformation("保存成功!");
+            }
+
+            //重新加载检验申请列表
+            LoadData();
+
+            //清空录入内容
+            ClearInput();
+        }
+
+        private void ClearInput()
+        {
+            SelectedInspectApply = null;
+
+            ReportNo = string.Empty;
+            Department = string.Empty;
+            ReportTime = DateTime.Now;
+            InspectCategory = string.Empty;
+            PurchaseCompanyNames = string.Empty;
+            ContractNos = string.Empty;
+            ReportBasis = string.Empty;
+            IsSample = false;
+            Conclusion = string.Empty;
+            ReportDesc = string.Empty;
+            ConclusionDesc = string.Empty;
+            EditUser = string.Empty;
+            InspectionReportDetails.Clear();
+        }
+
+        private void AddInspectionReportAndDetail()
+        {
+            InspectionReport report = new()
+            {
+                InspectApplyGuid = SelectedInspectApply.Guid,
+                ReportNo = ReportNo,
+                Department = Department,
+                ReportTime = ReportTime,
+                ReportBasis = ReportBasis,
+                IsSample = IsSample,
+                ReportDesc = ReportDesc,
+                Conclusion = Conclusion,
+                ConclusionDesc = ConclusionDesc,
+                EditUser = EditUser,
+                InspectionReportDetails = []
+            };
+
+            foreach (var detail in InspectionReportDetails)
+            {
+                report.InspectionReportDetails.Add(detail);
+            }
+
+            _inspectionReportService.Insert(report);
         }
 
         /// <summary>
-        /// 选择检验申请单行改变事件
+        /// 自定义校验
         /// </summary>
-        [RelayCommand]
-        private void InspectApplySelectionChanged(InspectApply inspectApply)
+        private Dictionary<string, string> CustomValidate()
         {
-            SelectedInspectApply = inspectApply;
+            Dictionary<string, string> result = new Dictionary<string, string>();
+            StringBuilder errorMessage = new StringBuilder();
+
+            //校验验收组
+            var details = 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 = 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;
         }
 
+        /// <summary>
+        /// 选择检验申请单行改变事件
+        /// </summary>
+        //[RelayCommand]
+        //private void InspectApplySelectionChanged(InspectApply inspectApply)
+        //{
+        //    SelectedInspectApply = inspectApply;
+        //}
+
         /// <summary>
         /// 鼠标双击事件
         /// </summary>
@@ -115,7 +298,7 @@ namespace UniformMaterialManagementSystem.ViewModels
             Conclusion = "合格";
             ReportDesc = "检验情况及主要问题";
             ConclusionDesc = "请录入检验结论";
-
+            EditUser = App.CurrentUser.UserName;
         }
 
         /// <summary>
@@ -124,7 +307,12 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void AddInspectionReportDetail()
         {
+            InspectionReportDetail detail = new InspectionReportDetail()
+            {
+                SupervisionUnit = App.CurrentUser.SupervisionUnit.Name,
+            };
 
+            InspectionReportDetails.Add(detail);
         }
 
         /// <summary>
@@ -133,7 +321,19 @@ namespace UniformMaterialManagementSystem.ViewModels
         [RelayCommand]
         private void RemoveInspectionReportDetail()
         {
+            if (_selectedInspectionReportDetail == null)
+            {
+                MessageEx.ShowWarningDialog("请选中一行,再执行删除操作!");
+                return;
+            }
 
+            InspectionReportDetails.Remove(_selectedInspectionReportDetail);
+        }
+
+        [RelayCommand]
+        private void SelectedInspectionReportDetailChanged(InspectionReportDetail detail)
+        {
+            _selectedInspectionReportDetail = detail;
         }
 
         private string GetPurchaseCompanyNames(InspectApply inspectApply)

+ 2 - 15
UniformMaterialManagementSystem/Views/InspectionReport/InspectionReportInputPage.xaml

@@ -750,7 +750,7 @@
                             ColumnHeaderStyle="{StaticResource CustomColumnHeaderStyle}"
                             HeadersVisibility="All"
                             HorizontalGridLinesBrush="LightSlateGray"
-                            ItemsSource="{Binding SelectedInspectionReport.InspectionReportDetails, Mode=TwoWay}"
+                            ItemsSource="{Binding InspectionReportDetails, Mode=TwoWay}"
                             RowHeaderStyle="{StaticResource CustomRowHeaderStyle}"
                             RowStyle="{StaticResource CustomRowStyle}"
                             SelectionMode="Single"
@@ -798,13 +798,6 @@
                                     </DataGridTemplateColumn.CellEditingTemplate>
                                 </DataGridTemplateColumn>
 
-                                <!--<DataGridTextColumn
-                                Width="100"
-                                Binding="{Binding JobCategory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
-                                EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
-                                ElementStyle="{StaticResource TextColumnElementStyle}"
-                                Header="检验组" />-->
-
                                 <DataGridTextColumn
                                     Width="300"
                                     Binding="{Binding SupervisionUnit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
@@ -812,12 +805,6 @@
                                     ElementStyle="{StaticResource TextColumnElementStyle}"
                                     Header="单位" />
 
-                                <!--<DataGridTextColumn
-                                Width="100"
-                                Binding="{Binding Inspector, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
-                                EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
-                                ElementStyle="{StaticResource TextColumnElementStyle}"
-                                Header="姓名" />-->
                                 <DataGridTemplateColumn Width="100" Header="姓名">
                                     <DataGridTemplateColumn.CellTemplate>
                                         <DataTemplate>
@@ -860,7 +847,7 @@
                     <TextBox
                         Grid.Row="11"
                         Grid.Column="1"
-                        Text="{Binding SelectedInspectionReport.EditUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
+                        Text="{Binding EditUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                 </Grid>
             </ScrollViewer>
         </Grid>