You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
13 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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
/// <summary>
/// 按钮文字转换事件
/// </summary>
public event PropertyChangedEventHandler PropertyChanged = delegate { };
//刷新委托
public delegate void RefreshDelegate(ProdPLanInfo pLanInfo);
public static event RefreshDelegate RefreshDelegateEvent;
#endregion
public ProductionReportViewModel()
{
_prodPlanDetailService = App.ServiceProvider.GetService<ProdPlanDetailService>();
_prodPlanInfoService = App.ServiceProvider.GetService<ProdPlanInfoService>();
_databaseHandleBusniess = App.ServiceProvider.GetService<DatabaseHandleBusniess>();
_baseStaffService = App.ServiceProvider.GetService<IBaseStaffService>();
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.GetRecordStaffAttendancesByConditions("", "", "", RfidHandleBusniess.stationCode, "1").FirstOrDefault();
if (planInfo != null)
{
complateRate = GetComplateRate(Convert.ToDouble(planInfo.CompleteAmount), planInfo.PlanAmount);
if (complateRate > 0.9)
{
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 定义属性
/// <summary>
/// 计划数量Text
/// </summary>
private string planAmountText;
public string PlanAmountText
{
get { return planAmountText; }
set { planAmountText = value; OnPropertyChanged("PlanAmountText"); }
}
/// <summary>
/// 完成数量Text
/// </summary>
private string completeAmountText;
public string CompleteAmountText
{
get { return completeAmountText; }
set { completeAmountText = value; OnPropertyChanged("CompleteAmountText"); }
}
/// <summary>
/// 新增数量Text
/// </summary>
private string newAmountText;
public string NewAmountText
{
get { return newAmountText; }
set { newAmountText = value; OnPropertyChanged("NewAmountText"); }
}
/// <summary>
/// 提示框
/// </summary>
private string hintText;
public string HintText
{
get { return hintText; }
set { hintText = value; OnPropertyChanged("HintText"); }
}
/// <summary>
/// 结束按钮颜色
/// </summary>
private string endButtonColor;
public string EndButtonColor
{
get { return endButtonColor;}
set { endButtonColor = value; OnPropertyChanged("EndButtonColor"); }
}
#endregion
/// <summary>
/// 暂停
/// </summary>
public ICommand PauseCommand { get; set; }
/// <summary>
/// 继续
/// </summary>
public ICommand ContinueCommand { get; set; }
/// <summary>
/// 确认命令
/// </summary>
public ICommand ConfirmCommand { get; private set; }
/// <summary>
/// 确认事件
/// </summary>
private void Confirm()
{
DateTime startTime = DateTime.Parse(ExecuteViewModel.intervalvalTime);
if ((DateTime.Now - startTime).TotalMinutes > 15)
{
if (EmployeeLoginViewModel.isOnDuty == true)//是否有班组当班,无当班时操作无效
{
var monitor = _databaseHandleBusniess.GetMonitor(RfidHandleBusniess.stationCode);
if (monitor != null)
{
string newAmount = NewAmountText;
RfidHandleBusniess.theNewAmount = NewAmountText;
if (newAmount != "")
{
bool isNum = true;
foreach (char x in newAmount)
{
if (!char.IsNumber(x) && x != '.')
{
isNum = false;
}
}
if (isNum)
{
HintText = "请撤回最新工单明细!";
//将新增产量加到实际产量中
currentAmountDouble = Convert.ToDouble(planInfo.CompleteAmount);
newAmountDouble = Convert.ToDouble(newAmount);
result = currentAmountDouble + newAmountDouble;
//planDetail = new ProdPlanDetail();
planDetail.CurrentStaffId = monitor.staffId;//取班长ID
planDetail.CreatedBy = monitor.staffName;//存班长名
planDetail.CompleteAmount = newAmountDouble.ToString();
planInfo.CompleteAmount = result.ToString();
if (planDetail.EndTime == null)//第一条明细
{
planDetail.BeginTime = ExecuteViewModel.theStartTime;
}
else
{
planDetail.BeginTime = planDetail.EndTime;
}
planDetail.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
ExecuteViewModel.intervalvalTime = planDetail.EndTime;
planDetail.BatchNumber += 1;
//ExecuteViewModel.batchNums = planDetail.BatchNumber.ToString();
//保存工单的执行人员
List<RecordStaffRealTime> realTimes = _databaseHandleBusniess.GetRecordStaffRealTimes();
_prodPlanDetailService.Insert(planDetail);
_prodPlanInfoService.Update(planInfo);
_databaseHandleBusniess.InsertPlanExecuteUser(planInfo, planDetail, realTimes);
Refresh();
HintText = "已提交!";
RefreshDelegateEvent?.Invoke(planInfo);
//ExecuteViewModel.isComplete = true;
complateRate = GetComplateRate(result, PlanAmountText);
if (complateRate > 0.9)
{
EndButtonColor = "#FF11B514";
}
}
else
{
//提示框提示错误信息
HintText = "输入有误,请输入阿拉伯数字!";
}
}
else
{
HintText = "不能为空!";
}
}
else
{
HintText = "班长未打卡,无法执行此操作!";
}
}
else
{
HintText = "没有班组当班,无法执行此操作!";
}
}
else
{
HintText = "当前工单距离上次提交不足15分钟";
}
}
/// <summary>
/// 结束工单命令
/// </summary>
public ICommand EndPlanCommand { get; private set; }
private void EndPlan()
{
//判断工单实际产量与目标产量的差距,满足才能执行结束
if (complateRate > 0.9)
{
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;
if (planAmountDouble == completeAmountDouble)
{
planInfo.CompFlag = "0";//正常完成
}
else if (planAmountDouble > completeAmountDouble)
{
planInfo.CompFlag = "1";//不足目标产量降级处理
}
else
{
planInfo.CompFlag = "2";//超额完成
}
_prodPlanInfoService.Update(planInfo);
ExecuteViewModel.stations += 1;
}
//关闭窗口
Application.Current.Windows.OfType<ProductionReportWin>().First().Close();
}
}
else
{
MessageBox.Show("完成产量与计划数量差距过大!");
}
}
/// <summary>
/// 继续执行
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void Continue()
{
//关闭窗口
Application.Current.Windows.OfType<ProductionReportWin>().First().Close();
}
/// <summary>
/// 暂停工单
/// </summary>
private void Pause()
{
//将当前的工单更新为待执行
planInfo.PlanStatus = "4";
_prodPlanInfoService.Update(planInfo);
ExecuteViewModel.isComplete = true;
//关闭窗口
Application.Current.Windows.OfType<ProductionReportWin>().First().Close();
}
/// <summary>
/// 获取完成率
/// </summary>
/// <param name="complateAmount"></param>
/// <param name="planAmount"></param>
/// <returns></returns>
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));
}
}
}