using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using GummingCommon; using GummingEntity; using GummingSupport; using GummingControl; using System.Text; using GummingBusiness; using Newtonsoft.Json; using GummingLine; namespace Gumming { public class FormulaCPViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public FormulaCPView View { get; set; } private bool IsConfirm; private bool isLoading; #endregion #region constructor public FormulaCPViewModule() { View = new FormulaCPView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } private void InitializeCommands() { AddStepCommand = new DelegateCommand(OnAddStepCommand); InsertStepCommand = new DelegateCommand(OnInsertStepCommand); DeleteStepCommand = new DelegateCommand(OnDeleteStepCommand); SaveStepCommand = new DelegateCommand(OnSaveStepCommand); AllowCommand = new DelegateCommand(OnAllowCommand); SelectStepCommand = new DelegateCommand(OnSelectStepCommand); CopyStepCommand = new DelegateCommand(OnCopyStepCommand); DisabledCommand = new DelegateCommand(OnDisabledCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; AllowEdit = false; CreateBy = Global.CurrentUserCode; HardFormulaCPs = new ObservableCollection(); //默认追加六步空白步骤 InitializeEmptyFormula(false); } ~FormulaCPViewModule() { } #endregion #region Binding Properties public string WindowTitle { get; private set; } private ObservableCollection _HardFormulaCPs; public ObservableCollection HardFormulaCPs { get { return _HardFormulaCPs; } set { _HardFormulaCPs = value; OnPropertyChanged("HardFormulaCPs"); } } private HardFormulaCPEntity _SelectedStep; public HardFormulaCPEntity SelectedStep { get { return _SelectedStep; } set { _SelectedStep = value; OnPropertyChanged("SelectedStep"); } } private string _FormulaName; public string FormulaName { get { return _FormulaName; } set { _FormulaName = value; OnPropertyChanged("FormulaName"); } } private string _CreateBy; public string CreateBy { get { return _CreateBy; } set { _CreateBy = value; OnPropertyChanged("CreateBy"); } } private HardStationType _SelectedFormulaType; public HardStationType SelectedFormulaType { get { return _SelectedFormulaType; } set { _SelectedFormulaType = value; OnPropertyChanged("SelectedFormulaType"); } } private HardFormulaEntity _SelectedFormula; public HardFormulaEntity SelectedFormula { get { return _SelectedFormula; } set { _SelectedFormula = value; OnPropertyChanged("SelectedFormula"); } } private bool _AllowEdit; public bool AllowEdit { get { return _AllowEdit; } set { _AllowEdit = value; OnPropertyChanged("AllowEdit"); } } #endregion #region Binding Commands private ICommand _AddStepCommand; public ICommand AddStepCommand { get { return _AddStepCommand; } set { _AddStepCommand = value; OnPropertyChanged("AddStepCommand"); } } private ICommand _DeleteStepCommand; public ICommand DeleteStepCommand { get { return _DeleteStepCommand; } set { _DeleteStepCommand = value; OnPropertyChanged("DeleteStepCommand"); } } private ICommand _SaveStepCommand; public ICommand SaveStepCommand { get { return _SaveStepCommand; } set { _SaveStepCommand = value; OnPropertyChanged("SaveStepCommand"); } } private ICommand _AllowCommand; public ICommand AllowCommand { get { return _AllowCommand; } set { _AllowCommand = value; OnPropertyChanged("AllowCommand"); } } private ICommand _InsertStepCommand; public ICommand InsertStepCommand { get { return _InsertStepCommand; } set { _InsertStepCommand = value; OnPropertyChanged("InsertStepCommand"); } } private ICommand _SelectStepCommand; public ICommand SelectStepCommand { get { return _SelectStepCommand; } set { _SelectStepCommand = value; OnPropertyChanged("SelectStepCommand"); } } private ICommand _CopyStepCommand; public ICommand CopyStepCommand { get { return _CopyStepCommand; } set { _CopyStepCommand = value; OnPropertyChanged("CopyStepCommand"); } } private ICommand _DisabledCommand; public ICommand DisabledCommand { get { return _DisabledCommand; } set { _DisabledCommand = value; OnPropertyChanged("DisabledCommand"); } } #endregion #region Private Methods public void InitializeEmptyFormula(bool canEdit) { FormulaName = string.Empty; AllowEdit = canEdit; HardFormulaCPs.Clear(); //默认追加六步空白步骤 for (int i = 0; i < 6; i++) { HardFormulaCPEntity cp = new HardFormulaCPEntity(); cp.StepIndex = i + 1; cp.Empty = false; cp.Copy = (HardFormulaCPEntity)cp.Clone(); HardFormulaCPs.Add(cp); } } public void StartLoadTestItem(string formulaId) { try { isLoading = true; HardFormulaCPs = new ObservableCollection(); var record = HardFormulaCPDA.Load(new PagerEntity() { PageIndex = pagenumber, Rows = rows, Conditions = " and FormulaId='"+ formulaId + "' " }); List logs = (List)record.rows; View.Dispatcher.Invoke((Action)(() => { foreach (var s in logs) { s.Copy = (HardFormulaCPEntity)s.Clone(); HardFormulaCPs.Add(s); } OnPropertyChanged("HartFormulaCP"); isLoading = false; })); } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } finally { CloseProgressView(); } } private void OnAddStepCommand(Object sender) { if (HardFormulaCPs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } var cp = new HardFormulaCPEntity(); cp.StepIndex = HardFormulaCPs.Count + 1; cp.Empty = false; cp.Copy = (HardFormulaCPEntity)cp.Clone(); HardFormulaCPs.Add(cp); } private void OnInsertStepCommand(Object sender) { if (HardFormulaCPs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行插入!"); return; } int curIndex = HardFormulaCPs.IndexOf(SelectedStep); var cp = new HardFormulaCPEntity(); cp.Empty = false; cp.Copy = (HardFormulaCPEntity)cp.Clone(); HardFormulaCPs.Insert(curIndex, cp); for (int i = 0; i < HardFormulaCPs.Count; i++) { HardFormulaCPs[i].StepIndex = i + 1; } } private void OnDeleteStepCommand(Object sender) { if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行删除!"); return; } HardFormulaCPs.Remove(SelectedStep); for (int i = 0; i < HardFormulaCPs.Count; i++) { HardFormulaCPs[i].StepIndex = i + 1; } } public void OnSaveStepCommand(Object sender) { if (string.IsNullOrWhiteSpace(FormulaName)) { ShowMessageBox("配方名称不能为空"); return; } HardFormulaEntity hfe = new HardFormulaEntity(); hfe.FormulaCode = FormulaName; hfe.FormulaName = FormulaName; hfe.CreateBy = Global.CurrentUserCode; hfe.StationCode = SelectedFormulaType.StationCode; hfe.StationName = SelectedFormulaType.StationName; hfe.StationType = (int)SelectedFormulaType.StationType; hfe.RecId = SelectedFormula?.RecId; hfe.RecId = HardFormulaDA.Save(hfe, HardFormulaCPs.ToList()); hfe.CreateTime = DateTime.Now; SelectedFormula = hfe; AllowEdit = false; for (int i = 0; i < HardFormulaCPs.Count; i++) { HardFormulaCPs[i].Copy = (HardFormulaCPEntity)HardFormulaCPs[i].Clone(); } AppEventAggregator.GetEventAggregator().GetEvent().Publish(new NotifyEventParameter() { NotifyType = CallbackType.Formula, NotifyContent = SelectedFormula }); } private void OnAllowCommand(Object sender) { AllowEdit = true; } private void OnDisabledCommand(Object sender) { AllowEdit = false; StartLoadTestItem(SelectedFormula?.RecId); } private void OnSelectStepCommand(Object sender) { } private void OnCopyStepCommand(Object sender) { if (HardFormulaCPs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行拷贝!"); return; } var cp = (HardFormulaCPEntity)SelectedStep.Clone(); cp.Copy = (HardFormulaCPEntity)cp.Clone(); HardFormulaCPs.Add(cp); for (int i = 0; i < HardFormulaCPs.Count; i++) { HardFormulaCPs[i].StepIndex = i + 1; } } #endregion } }