|
@@ -0,0 +1,341 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+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 UniformMaterialManagementSystem.Entities;
|
|
|
+using UniformMaterialManagementSystem.Models;
|
|
|
+using UniformMaterialManagementSystem.Services;
|
|
|
+using UniformMaterialManagementSystem.Utils;
|
|
|
+using UniformMaterialManagementSystem.Views;
|
|
|
+using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
|
|
+
|
|
|
+namespace UniformMaterialManagementSystem.ViewModels
|
|
|
+{
|
|
|
+ public partial class SampleRegistrationPageViewModel : ObservableObject
|
|
|
+ {
|
|
|
+ [ObservableProperty]
|
|
|
+ private List<int> _years = [];
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private int _selectedYear = DateTime.Now.Year;
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private ObservableCollection<SampleRegistrationModel> _sampleRegistrations = [];
|
|
|
+
|
|
|
+ [ObservableProperty]
|
|
|
+ private SampleRegistrationModel? _selectedSampleRegistration;
|
|
|
+
|
|
|
+ private readonly IDataBaseService<SampleRegistration> _sampleService;
|
|
|
+ private readonly IDataBaseService<InspectApply> _inspectApplyService;
|
|
|
+ private readonly IDataBaseService<InspectionOrganization> _organizationService;
|
|
|
+
|
|
|
+ public SampleRegistrationPageViewModel(IDataBaseService<SampleRegistration> sampleService, IDataBaseService<InspectApply> inspectApplyService, IDataBaseService<InspectionOrganization> organizationService)
|
|
|
+ {
|
|
|
+ _sampleService = sampleService;
|
|
|
+ _inspectApplyService = inspectApplyService;
|
|
|
+ _organizationService = organizationService;
|
|
|
+
|
|
|
+ //初始化年份
|
|
|
+ for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year + 1; i++)
|
|
|
+ {
|
|
|
+ Years.Add(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ //加载数据
|
|
|
+ LoadData();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 加载数据
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void LoadData()
|
|
|
+ {
|
|
|
+ var sampleRegistrations = _sampleService.Query()
|
|
|
+ .Include(x => x.InspectApply)
|
|
|
+ .ThenInclude(x => x.Material)
|
|
|
+ .Where(x => x.InspectApply.Year == SelectedYear)
|
|
|
+ .ToList();
|
|
|
+
|
|
|
+ SampleRegistrations.Clear();
|
|
|
+ foreach (var sample in sampleRegistrations)
|
|
|
+ {
|
|
|
+ SampleRegistrations.Add(new SampleRegistrationModel(sample));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 选择行改变事件
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void SampleRegistrationSelectionChanged(SampleRegistrationModel sample)
|
|
|
+ {
|
|
|
+ //询问是否保存修改内容
|
|
|
+ AskIsSave();
|
|
|
+
|
|
|
+ //切换行
|
|
|
+ SelectedSampleRegistration = sample;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新增
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void AddSampleRegistration()
|
|
|
+ {
|
|
|
+ /* 打开窗口,展示抽样状态未生成的检验申请列表,选择检验申请,携带检验申请数据 */
|
|
|
+ SelectInspectApplyWindowViewModel viewModel =
|
|
|
+ new SelectInspectApplyWindowViewModel(_inspectApplyService, "SampleRegistration");
|
|
|
+ SelectInspectApplyWindow inspectApplyWindow = new SelectInspectApplyWindow(viewModel);
|
|
|
+
|
|
|
+ var result = inspectApplyWindow.ShowDialog();
|
|
|
+ if (result == null || !(bool)result) return;
|
|
|
+
|
|
|
+ var inspectApply = inspectApplyWindow.DataGridMain.SelectedItem as InspectApply;
|
|
|
+ if (inspectApply == null) return;
|
|
|
+
|
|
|
+ //新增抽样登记并设置默认值
|
|
|
+ SampleRegistrationModel sampleRegistration = new SampleRegistrationModel
|
|
|
+ {
|
|
|
+ InspectApply = inspectApply,
|
|
|
+ SampleNo = inspectApply.ApplyNo.Replace("申请", ""),
|
|
|
+ EditTime = DateTime.Now,
|
|
|
+ ProductDate = DateTime.Now,
|
|
|
+ BatchNo = inspectApply.BatchNo,
|
|
|
+ TestingItem = "全检",
|
|
|
+ Department = App.CurrentUser!.SupervisionUnit.Name,
|
|
|
+ EditUser = App.CurrentUser!.UserName,
|
|
|
+ };
|
|
|
+
|
|
|
+ SampleRegistrations.Add(sampleRegistration);
|
|
|
+
|
|
|
+ //选中当前行
|
|
|
+ SelectedSampleRegistration = sampleRegistration;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 保存
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void SaveSampleRegistration()
|
|
|
+ {
|
|
|
+ if (SelectedSampleRegistration == null) return;
|
|
|
+
|
|
|
+ //自定义校验
|
|
|
+ var validateDictionary = CustomValidate();
|
|
|
+ if (validateDictionary.ContainsKey("result") && !string.IsNullOrEmpty(validateDictionary["result"]))
|
|
|
+ {
|
|
|
+ MessageBox.Show(validateDictionary["result"],"提示",MessageBoxButton.OK,MessageBoxImage.Error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //对新增或修改的抽样登记单进行保存
|
|
|
+ var sampleRegistration = _sampleService.Get(x => x.Guid == SelectedSampleRegistration.Guid);
|
|
|
+ if (sampleRegistration == null) //新增
|
|
|
+ {
|
|
|
+ sampleRegistration = SelectedSampleRegistration.ConvertToSampleRegistration();
|
|
|
+ _sampleService.Insert(sampleRegistration);
|
|
|
+ }
|
|
|
+ else //修改
|
|
|
+ {
|
|
|
+ ModifySampleRegistrationFromModel(sampleRegistration, SelectedSampleRegistration);
|
|
|
+ }
|
|
|
+
|
|
|
+ var state = _sampleService.Entry(sampleRegistration);
|
|
|
+
|
|
|
+ //反写检验申请单的抽样送检状态
|
|
|
+ var inspectApply = _inspectApplyService.Get(x => x.Guid == sampleRegistration.InspectApplyGuid);
|
|
|
+ if (inspectApply != null)
|
|
|
+ {
|
|
|
+ inspectApply.SampleStatus = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ var result = _sampleService.SaveChanges();
|
|
|
+
|
|
|
+ if (result)
|
|
|
+ {
|
|
|
+ MessageBox.Show("保存成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 导出登记表
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void ExportSampleRegistration()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 随机抽取检测机构
|
|
|
+ /// </summary>
|
|
|
+ [RelayCommand]
|
|
|
+ private void RandomInspectionOrg()
|
|
|
+ {
|
|
|
+ if (SelectedSampleRegistration == null) return;
|
|
|
+
|
|
|
+ //获取检测机构数量
|
|
|
+ var organizations = _organizationService.QueryNoTracking(x => x.IsEnabled).OrderBy(x => x.OrderNo).ToList();
|
|
|
+ if (organizations.Count == 0)
|
|
|
+ {
|
|
|
+ MessageBox.Show("没有要抽取的检测机构,请先维护检测机构信息!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成随机数
|
|
|
+ int random = RandomHelper.CreateRandom(organizations.Count);
|
|
|
+ int count = random == 0 ? organizations.Count : random;
|
|
|
+
|
|
|
+ MessageBox.Show($"系统生成随机数为{random},将使用第{count}家检测机构。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+
|
|
|
+ InspectionOrganization? organization = organizations.Find(x => x.OrderNo == count);
|
|
|
+ if (organization == null)
|
|
|
+ {
|
|
|
+ MessageBox.Show($"第{count}家检测机构不存在,请确保检测机构序号正确!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.AppendLine("1.名称:" + organization.Name);
|
|
|
+ sb.AppendLine("2.地址:" + organization.Address);
|
|
|
+ sb.AppendLine("3.联系人:" + organization.Contacts);
|
|
|
+ sb.AppendLine("4.联系电话:" + organization.Telephone);
|
|
|
+
|
|
|
+ SelectedSampleRegistration.InspectionOrganization = sb.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 询问是否保存
|
|
|
+ /// </summary>
|
|
|
+ private void AskIsSave()
|
|
|
+ {
|
|
|
+ if (SelectedSampleRegistration == null) return;
|
|
|
+ var sampleRegistration = _sampleService.Get(x => x.Guid == SelectedSampleRegistration.Guid);
|
|
|
+
|
|
|
+ bool isNew = false;
|
|
|
+ EntityState state = EntityState.Unchanged;
|
|
|
+ if (sampleRegistration == null)
|
|
|
+ {
|
|
|
+ isNew = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ModifySampleRegistrationFromModel(sampleRegistration, SelectedSampleRegistration);
|
|
|
+
|
|
|
+ state = _sampleService.Entry(sampleRegistration);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isNew || state == EntityState.Modified) //新增或修改
|
|
|
+ {
|
|
|
+ //询问是否保存
|
|
|
+ var result = MessageBox.Show($"抽样登记表编号:【{SelectedSampleRegistration.SampleNo}】数据已修改,是否保存?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
|
+ if (result == MessageBoxResult.Yes)
|
|
|
+ {
|
|
|
+ //保存修改
|
|
|
+ SaveSampleRegistration();
|
|
|
+ }
|
|
|
+ else if (result == MessageBoxResult.No)
|
|
|
+ {
|
|
|
+ if (isNew)
|
|
|
+ {
|
|
|
+ SampleRegistrations.Remove(SelectedSampleRegistration);
|
|
|
+ }
|
|
|
+ else if (state == EntityState.Modified)
|
|
|
+ {
|
|
|
+ _sampleService.SetEntryState(sampleRegistration, EntityState.Unchanged);
|
|
|
+ ModifyModelFromSampleRegistration(SelectedSampleRegistration, sampleRegistration);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Dictionary<string, string> CustomValidate()
|
|
|
+ {
|
|
|
+ Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
+ StringBuilder errorMessage = new StringBuilder();
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.Department))
|
|
|
+ {
|
|
|
+ errorMessage.Append("单位不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (SelectedSampleRegistration?.EditTime == null)
|
|
|
+ {
|
|
|
+ errorMessage.Append("时间不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (SelectedSampleRegistration?.ProductDate == null)
|
|
|
+ {
|
|
|
+ errorMessage.Append("样品生产日期不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.PacketNo))
|
|
|
+ {
|
|
|
+ errorMessage.Append("包号不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (SelectedSampleRegistration?.Quantity <= 0)
|
|
|
+ {
|
|
|
+ errorMessage.Append("数量不允许小于等于0!\n");
|
|
|
+ }
|
|
|
+ if (SelectedSampleRegistration?.TestingItem == "单项指标" && string.IsNullOrEmpty(SelectedSampleRegistration?.SingleIndexItem))
|
|
|
+ {
|
|
|
+ errorMessage.Append("检测项目为“单项指标”,单项指标内容不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.InspectionOrganization))
|
|
|
+ {
|
|
|
+ errorMessage.Append("检测机构不允许为空,请抽取检测机构!\n");
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.ProductUsers))
|
|
|
+ {
|
|
|
+ errorMessage.Append("生产企业人员不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.EditUser))
|
|
|
+ {
|
|
|
+ errorMessage.Append("承办人不允许为空!\n");
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(SelectedSampleRegistration?.Telephone))
|
|
|
+ {
|
|
|
+ errorMessage.Append("联系电话不允许为空!\n");
|
|
|
+ }
|
|
|
+ result.Add("result", errorMessage.ToString());
|
|
|
+ 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;
|
|
|
+ model.EditTime = sampleRegistration.EditTime;
|
|
|
+ model.ProductDate = sampleRegistration.ProductDate;
|
|
|
+ model.BatchNo = sampleRegistration.BatchNo;
|
|
|
+ model.PacketNo = sampleRegistration.PacketNo;
|
|
|
+ model.Quantity = sampleRegistration.Quantity;
|
|
|
+ model.TestingItem = sampleRegistration.TestingItem;
|
|
|
+ model.SingleIndexItem = sampleRegistration.SingleIndexItem;
|
|
|
+ model.InspectionOrganization = sampleRegistration.InspectionOrganization;
|
|
|
+ model.ProductUsers = sampleRegistration.ProductUsers;
|
|
|
+ model.EditUser = sampleRegistration.EditUser;
|
|
|
+ model.Telephone = sampleRegistration.Telephone;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|