using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Business.business; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using SlnMesnac.WPF.Views; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace SlnMesnac.WPF.ViewModel { public class ProductionReportViewModel : INotifyPropertyChanged { private ProdPlanInfoService _prodPlanInfoService; private ProdPlanDetailService _prodPlanDetailService; private IBaseStaffService _baseStaffService; private DatabaseHandleBusniess _databaseHandleBusniess; private ProdPLanInfo planInfo; private ProdPlanDetail planDetail; private double complateRate; private double currentAmountDouble; private double newAmountDouble; private double result; #region /// /// 按钮文字转换事件 /// public event PropertyChangedEventHandler PropertyChanged = delegate { }; //刷新委托 public delegate void RefreshDelegate(ProdPLanInfo pLanInfo); public static event RefreshDelegate RefreshDelegateEvent; public static Action? RefrashClearAction; #endregion public ProductionReportViewModel() { _prodPlanDetailService = App.ServiceProvider.GetService(); _prodPlanInfoService = App.ServiceProvider.GetService(); _databaseHandleBusniess = App.ServiceProvider.GetService(); _baseStaffService = App.ServiceProvider.GetService(); ConfirmCommand = new RelayCommand(Confirm); EndPlanCommand = new RelayCommand(EndPlan); ContinueCommand = new RelayCommand(Continue); PauseCommand = new RelayCommand(Pause); EndButtonColor = "Red"; Init(); if (RfidHandleBusniess.theNewAmount != "") { NewAmountText = RfidHandleBusniess.theNewAmount; } } private void Init() { planInfo = _prodPlanInfoService.GetPlanInfoByConditions("", "", "", RfidHandleBusniess.stationCode, "1").FirstOrDefault(); if (planInfo != null) { complateRate = GetComplateRate(Convert.ToDouble(planInfo.CompleteAmount), planInfo.PlanAmount); if (complateRate > -10) { EndButtonColor = "#FF11B514"; } } Refresh(); } private void Refresh() { if (planInfo != null) { planDetail = _prodPlanDetailService.GetPlanDetailsByPlanCode(planInfo.PlanCode).FirstOrDefault(); PlanAmountText = planInfo.PlanAmount.ToString(); CompleteAmountText = planInfo.CompleteAmount.ToString(); } else { HintText = "没有正在执行的工单,请执行工单!"; } } #region 定义属性 /// /// 计划数量Text /// private string planAmountText; public string PlanAmountText { get { return planAmountText; } set { planAmountText = value; OnPropertyChanged("PlanAmountText"); } } /// /// 完成数量Text /// private string completeAmountText; public string CompleteAmountText { get { return completeAmountText; } set { completeAmountText = value; OnPropertyChanged("CompleteAmountText"); } } /// /// 新增数量Text /// private string newAmountText; public string NewAmountText { get { return newAmountText; } set { newAmountText = value; OnPropertyChanged("NewAmountText"); } } /// /// 提示框 /// private string hintText; public string HintText { get { return hintText; } set { hintText = value; OnPropertyChanged("HintText"); } } /// /// 结束按钮颜色 /// private string endButtonColor; public string EndButtonColor { get { return endButtonColor;} set { endButtonColor = value; OnPropertyChanged("EndButtonColor"); } } #endregion /// /// 暂停 /// public ICommand PauseCommand { get; set; } /// /// 继续 /// public ICommand ContinueCommand { get; set; } /// /// 确认命令 /// public ICommand ConfirmCommand { get; private set; } /// /// 确认事件 /// private void Confirm() { // 获取最新工单明细的结束时间 var detail = _databaseHandleBusniess.GetLastDetailEndTime(planInfo.PlanCode); // 如果最新工单明细没有结束时间,跳过时间判断 if (!string.IsNullOrEmpty(detail.EndTime)) { DateTime startTime = DateTime.Parse(detail.EndTime); if ((DateTime.Now - startTime).TotalMinutes <= 15) { HintText = "当前工单距离上次提交不足15分钟!"; return; } } // 如果没有班组当班,直接返回 if (!EmployeeLoginViewModel.isOnDuty) { HintText = "没有班组当班,无法执行此操作!"; return; } // 获取班组长信息 var monitor = _databaseHandleBusniess.GetMonitor(RfidHandleBusniess.stationCode); if (monitor == null) { HintText = "班长未打卡,无法执行此操作!"; return; } // complateRate = GetComplateRate(Convert.ToDouble(planInfo.CompleteAmount), planInfo.PlanAmount); if (complateRate > 10) { HintText = "实际数量已超过计划数量,请结束工单"; return; } // 获取新输入的产量 string newAmount = NewAmountText?.Trim(); if (string.IsNullOrEmpty(newAmount)) { HintText = "不能为空!"; return; } // 验证输入的产量是否为数字 if (!IsNumeric(newAmount)) { HintText = "输入有误,请输入阿拉伯数字!"; return; } // 更新计划信息 RfidHandleBusniess.theNewAmount = newAmount; double currentAmountDouble = Convert.ToDouble(planInfo.CompleteAmount); double newAmountDouble = Convert.ToDouble(newAmount); double result = currentAmountDouble + newAmountDouble; // 设置工单明细 planDetail.CurrentStaffId = monitor.staffId; // 班长ID planDetail.CreatedBy = monitor.staffName; // 班长名称 planDetail.CompleteAmount = newAmountDouble.ToString(); planInfo.CompleteAmount = result.ToString(); planDetail.BeginTime = planDetail.EndTime ?? ExecuteViewModel.theStartTime; planDetail.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); planDetail.BatchNumber += 1; // 保存数据 var realTimes = _databaseHandleBusniess.GetRecordStaffRealTimes(); _prodPlanDetailService.Insert(planDetail); _prodPlanInfoService.Update(planInfo); _databaseHandleBusniess.InsertPlanExecuteUser(planInfo, planDetail, realTimes); // 刷新并更新UI Refresh(); HintText = "已提交!"; RefreshDelegateEvent?.Invoke(planInfo); // 更新完成率 complateRate = GetComplateRate(result, PlanAmountText); if (complateRate > -10) { EndButtonColor = "#FF11B514"; } } // 检查字符串是否为有效数字 private bool IsNumeric(string value) { return value.All(c => char.IsDigit(c) || c == '.'); } /// /// 结束工单命令 /// public ICommand EndPlanCommand { get; private set; } private void EndPlan() { // 判断工单实际产量与目标产量的差距,满足才能执行结束 if (complateRate > -10) { if (EmployeeLoginViewModel.isOnDuty == true) // 是否有班组当班,无当班时操作无效 { if (planDetail != null && planInfo != null) { // planDetail.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //_prodPlanDetailService.Insert(planDetail); planInfo.PlanStatus = "2"; planInfo.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); double planAmountDouble = Convert.ToDouble(planInfo.PlanAmount); double completeAmountDouble = Convert.ToDouble(planInfo.CompleteAmount); ExecuteViewModel.isComplete = true; // 使用 switch-case 来处理完成情况 switch (planAmountDouble.CompareTo(completeAmountDouble)) { case 0: // 完成量等于计划量 planInfo.CompFlag = "0"; // 正常完成 break; case int n when (n > 0): // 完成量小于计划量 planInfo.CompFlag = "1"; // 不足目标产量降级处理 break; case int n when (n < 0): // 完成量大于计划量 planInfo.CompFlag = "2"; // 超额完成 break; } _prodPlanInfoService.Update(planInfo); ExecuteViewModel.stations += 1; } // 关闭窗口 Application.Current.Windows.OfType().First().Close(); RefrashClearAction?.Invoke(0); } } else { MessageBox.Show("完成产量与计划数量差距过大!"); } } /// /// 继续执行 /// /// private void Continue() { //关闭窗口 Application.Current.Windows.OfType().First().Close(); } /// /// 暂停工单 /// private void Pause() { //将当前的工单更新为待执行 planInfo.PlanStatus = "4"; _prodPlanInfoService.Update(planInfo); ExecuteViewModel.isComplete = true; //关闭窗口 Application.Current.Windows.OfType().First().Close(); RefrashClearAction?.Invoke(0); } /// /// 获取完成率 /// /// /// /// private double GetComplateRate(double complateAmount, string planAmount) { double plan = Convert.ToDouble(planAmount); return complateAmount - plan; } public void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }