using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
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 ProdPLanInfo planInfo;
private ProdPlanDetail planDetail;
#region
///
/// 按钮文字转换事件
///
public event PropertyChangedEventHandler PropertyChanged = delegate { };
//刷新委托
public delegate void RefreshDelegate(ProdPLanInfo pLanInfo);
public static event RefreshDelegate RefreshDelegateEvent;
#endregion
public ProductionReportViewModel()
{
_prodPlanDetailService = App.ServiceProvider.GetService();
_prodPlanInfoService = App.ServiceProvider.GetService();
ConfirmCommand = new RelayCommand(Confirm);
EndPlanCommand = new RelayCommand(EndPlan);
Init();
}
private void Init()
{
planInfo = _prodPlanInfoService.GetRecordStaffAttendancesByConditions("", "", "", "","1").FirstOrDefault();
Refresh();
}
private void Refresh()
{
if (planInfo != null)
{
planDetail = _prodPlanDetailService.GetPlanDetailsByPlanCode(planInfo.PlanCode);
PlanAmountText = planDetail.PlanAmount.ToString();
CompleteAmountText = planDetail.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"); }
}
#endregion
///
/// 确认命令
///
public ICommand ConfirmCommand { get; private set; }
///
/// 确认事件
///
private void Confirm()
{
string newAmount = NewAmountText;
if (newAmount != null)
{
bool isNum = true;
foreach (char x in newAmount)
{
if (!char.IsNumber(x) && x != '.')
{
isNum = false;
}
}
if (isNum)
{
//将新增产量加到实际产量中
double currentAmountDouble = Convert.ToDouble(planDetail.CompleteAmount);
double newAmountDouble = Convert.ToDouble(newAmount);
double result = currentAmountDouble + newAmountDouble;
planDetail.CompleteAmount = result.ToString();
planInfo.CompleteAmount = result.ToString();
_prodPlanDetailService.Update(planDetail);
_prodPlanInfoService.Update(planInfo);
Refresh();
NewAmountText = null;
HintText = "已提交!";
RefreshDelegateEvent?.Invoke(planInfo);
ExecuteViewModel.isComplete = true;
}
else
{
//提示框提示错误信息
HintText = "输入有误,请输入阿拉伯数字!";
}
}
else
{
HintText = "不能为空!";
}
}
///
/// 结束工单命令
///
public ICommand EndPlanCommand { get; private set; }
private void EndPlan()
{
planDetail.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
_prodPlanDetailService.Update(planDetail);
planInfo.PlanStatus = "2";
planInfo.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
double planAmountDouble = Convert.ToDouble(planDetail.PlanAmount);
double completeAmountDouble = Convert.ToDouble(planDetail.CompleteAmount);
if(planAmountDouble == completeAmountDouble)
{
planInfo.CompFlag = "0";//正常完成
}
else if(planAmountDouble > completeAmountDouble)
{
planInfo.CompFlag = "1";//不足目标产量降级处理
}
else
{
planInfo.CompFlag = "2";//超额完成
}
_prodPlanInfoService.Update(planInfo);
//关闭窗口
Application.Current.Windows.OfType().First().Close();
}
public void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}