using GalaSoft.MvvmLight.Command; using Microsoft.AspNetCore.Components.Forms; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Protocols; using SlnMesnac.Business.business; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Repository.service; using SlnMesnac.WPF.Page; using SlnMesnac.WPF.Views; using SqlSugar; using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Configuration; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Documents; using System.Windows.Input; namespace SlnMesnac.WPF.ViewModel { public class ExecuteViewModel : INotifyPropertyChanged { public ObservableCollection OrderCodeComboBoxItems { get; set; } public ObservableCollection PlanCodeComboBoxItems { get; set; } public ObservableCollection MaterialNameComboBoxItems { get; set; } private ProdPlanInfoService _prodPlanInfoService; private List prodPlanInfos; private ProdPlanDetailService _prodPlanDetailService; private IRecordStaffAttendanceService _recordStaffAttendanceService; private string StationCode; public static bool isComplete = true; private ProdPLanInfo pLanInfo; /// /// 按钮文字转换事件 /// public event PropertyChangedEventHandler PropertyChanged = delegate { }; /// /// 检索命令 /// public ICommand SearchCommand { get; private set; } /// /// 换班命令 /// public ICommand HandoverCommand { get; private set; } /// /// 报工命令 /// public ICommand ProductionReportCommand { get; private set; } /// /// 执行命令 /// public ICommand ExecuteCommand { get; private set; } #region public delegate void RefreshDelegate(ProdPLanInfo pLanInfo); public static event RefreshDelegate? RefreshEvent; #endregion public ExecuteViewModel() { _prodPlanInfoService = App.ServiceProvider.GetService(); _prodPlanDetailService = App.ServiceProvider.GetService(); _recordStaffAttendanceService = App.ServiceProvider.GetService(); IConfigurationBuilder configurationBuilder = new ConfigurationBuilder() .SetBasePath(System.AppContext.BaseDirectory) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = configurationBuilder.Build(); pLanInfo = _prodPlanInfoService.GetRecordStaffAttendancesByConditions("", "", "", "", "1").FirstOrDefault(); Refresh(pLanInfo); // 从配置文件中获取ProductLineNameTextBlock的值 stationTextBlock = configuration.GetSection("AppConfig")["ProductLineName"]; StationCode = configuration.GetSection("AppConfig")["ProductLineCode"]; HandoverCommand = new RelayCommand(Handover); ProductionReportCommand = new RelayCommand(ProductionReport); SearchCommand = new RelayCommand(Search); ExecuteCommand = new RelayCommand(Execute); ProductionReportViewModel.RefreshDelegateEvent += Refresh; } /// /// 换班弹窗 /// private void Handover() { var handOverWin = new HandOverWin(); handOverWin.WindowStartupLocation = WindowStartupLocation.CenterScreen; // 让窗体出现在屏幕中央 handOverWin.ShowDialog();//窗体出现后禁止后面的用户控件 } /// /// 报工弹窗 /// private void ProductionReport() { var reportWin = new ProductionReportWin(); reportWin.WindowStartupLocation = WindowStartupLocation.CenterScreen; // 让窗体出现在屏幕中央 reportWin.ShowDialog();//窗体出现后禁止后面的用户控件 } /// /// 检索事件 /// private void Search() { // 在这里执行其他操作,可以通过InputText获取用户输入的信息 //Console.WriteLine("用户输入的信息:" + OrderCodeTextBox + PlanCodeTextBox + MaterialCodeTextBox); //ProductLineNameTextBlock = ConfigurationManager.AppSettings["ProductLineNameTextBlock"]; List list = _prodPlanInfoService.GetRecordStaffAttendancesByConditions(OrderCodeTextBox, PlanCodeTextBox, MaterialCodeTextBox, StationCode,"0"); System.Windows.Application.Current.Dispatcher.Invoke((Action)(async () => { ProdPLanInfoDataGrid.Clear(); list.ForEach(item => { ProdPLanInfoDataGrid.Add(item); }); })); } // 新增的执行事件 private void Execute() { if (isComplete) { // 将当前记录存为实体,可以通过parameter获取当前记录的信息 string orderCode = _selectedRow.OrderCode.ToString(); string planCode = _selectedRow.PlanCode.ToString(); pLanInfo = _prodPlanInfoService.GetRecordStaffAttendancesByConditions(orderCode, planCode, "", "", "0").First(); RecordStaffAttendance currentRecord = _recordStaffAttendanceService.GetLastestOnRecord(); RecordStaffAttendance nextRecord = _recordStaffAttendanceService.GetLastestOffRecord(); // 向detail表里插入一条数据 ProdPlanDetail prodPlanDetail = new ProdPlanDetail { PlanCode = pLanInfo.PlanCode, MaterialCode = pLanInfo.MaterialCode, PlanAmount = pLanInfo.PlanAmount, CompleteAmount = pLanInfo.CompleteAmount, BeginTime = DateTime.Now.ToString(), CurrentStaffId = currentRecord.StaffId, //NextStaffId = nextRecord.StaffId, }; _prodPlanDetailService.Insert(prodPlanDetail); //按钮文字变成执行中并锁定,其他的订单执行按钮也禁用 pLanInfo.BeginTime = DateTime.Now.ToString(); pLanInfo.PlanStatus = "1"; _prodPlanInfoService.Update(pLanInfo); Search(); //查明细表显示出来 Refresh(pLanInfo); isComplete = false; } } //刷新明细 private void Refresh(ProdPLanInfo pLanInfo) { if(pLanInfo != null) { ProdPlanDetail planDetail = _prodPlanDetailService.GetPlanDetailsByPlanCode(pLanInfo.PlanCode); PlanCodeText = planDetail.PlanCode; OrderCodeText = pLanInfo.OrderCode; MaterialCodeText = planDetail.MaterialCode; StationCodeText = pLanInfo.StationCode; System.Windows.Application.Current.Dispatcher.Invoke((Action)(async () => { ProdPLanDetailDataGrid.Clear(); ProdPLanDetailDataGrid.Add(planDetail); })); } } #region /// /// 订单 /// private string orderCodeTextBox; public string OrderCodeTextBox { get { return orderCodeTextBox; } set { orderCodeTextBox = value; OnPropertyChanged("OrderCodeTextBox"); } } /// /// 工单 /// private string planCodeTextBox; public string PlanCodeTextBox { get { return planCodeTextBox; } set { planCodeTextBox = value; OnPropertyChanged("PlanCodeTextBox"); } } /// /// 原料 /// private string materialCodeTextBox; public string MaterialCodeTextBox { get { return materialCodeTextBox; } set { materialCodeTextBox = value; OnPropertyChanged("MaterialNameTextBox"); } } /// /// 工位 /// private string stationTextBlock; public string StationTextBox { get { return stationTextBlock; } set { stationTextBlock = value; new PropertyChangedEventArgs(nameof(StationTextBox)); } } /// /// PlanDataGrid /// private ObservableCollection prodPLanInfoDataGrid = new ObservableCollection(); public ObservableCollection ProdPLanInfoDataGrid { get { return prodPLanInfoDataGrid; } set { prodPLanInfoDataGrid = value; OnPropertyChanged("ProdPLanInfoDataGrid"); } } /// /// DetailDataGrid /// private ObservableCollection prodPLanDetailDataGrid = new ObservableCollection(); public ObservableCollection ProdPLanDetailDataGrid { get { return prodPLanDetailDataGrid; } set { ProdPLanDetailDataGrid = value; OnPropertyChanged("ProdPLanDetailDataGrid"); } } /// /// 选中行 /// public ProdPLanInfo _selectedRow; public ProdPLanInfo SelectedRow { get { return _selectedRow; } set { _selectedRow = value; OnPropertyChanged(nameof(SelectedRow)); } } /// /// 订单Text /// private string orderCodeText; public string OrderCodeText { get { return orderCodeText; } set { orderCodeText = value; OnPropertyChanged("OrderCodeText"); } } /// /// 工单Text /// private string planCodeText; public string PlanCodeText { get { return planCodeText; } set { planCodeText = value; OnPropertyChanged("PlanCodeText"); } } /// /// 原料Text /// private string materialCodeText; public string MaterialCodeText { get { return materialCodeText; } set { materialCodeText = value; OnPropertyChanged("MaterialCodeText"); } } /// /// 订单 /// private string stationCodeText; public string StationCodeText { get { return stationCodeText; } set { stationCodeText = value; OnPropertyChanged("StationCodeText"); } } #endregion public void OnPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }