|
@@ -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;
|
|
|
}
|