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