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 FormulaCAViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public FormulaCAView View { get; set; } private bool IsConfirm; private bool isLoading; #endregion #region constructor public FormulaCAViewModule() { View = new FormulaCAView(); 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; HardFormulaCAs = new ObservableCollection(); //默认追加六步空白步骤 InitializeEmptyFormula(false); } ~FormulaCAViewModule() { } #endregion #region Binding Properties public string WindowTitle { get; private set; } private ObservableCollection _HardFormulaCAs; public ObservableCollection HardFormulaCAs { get { return _HardFormulaCAs; } set { _HardFormulaCAs = value; OnPropertyChanged("HardFormulaCAs"); } } private HardFormulaCAEntity _SelectedStep; public HardFormulaCAEntity 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; HardFormulaCAs.Clear(); //默认追加六步空白步骤 for (int i = 0; i < 1; i++) { HardFormulaCAEntity ca = new HardFormulaCAEntity(); ca.StepIndex = i + 1; ca.Copy = (HardFormulaCAEntity)ca.Clone(); HardFormulaCAs.Add(ca); } } public void StartLoadTestItem(string formulaId) { try { isLoading = true; HardFormulaCAs = new ObservableCollection(); var record = HardFormulaCADA.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 = (HardFormulaCAEntity)s.Clone(); HardFormulaCAs.Add(s); } OnPropertyChanged("HartFormulaCA"); isLoading = false; })); } catch (Exception ex) { LogService.WriteErrorLog(ex); ShowErrorBox("Error:" + ex.Message); return; } finally { CloseProgressView(); } } private void OnAddStepCommand(Object sender) { if (HardFormulaCAs.Count == 20) { ShowMessageBox("步骤不能超过1条"); return; } var ca = new HardFormulaCAEntity(); ca.StepIndex = HardFormulaCAs.Count + 1; ca.Copy = (HardFormulaCAEntity)ca.Clone(); HardFormulaCAs.Add(ca); } private void OnInsertStepCommand(Object sender) { if (HardFormulaCAs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行插入!"); return; } int curIndex = HardFormulaCAs.IndexOf(SelectedStep); var ca = new HardFormulaCAEntity(); ca.Copy = (HardFormulaCAEntity)ca.Clone(); HardFormulaCAs.Insert(curIndex, ca); for (int i = 0; i < HardFormulaCAs.Count; i++) { HardFormulaCAs[i].StepIndex = i + 1; } } private void OnDeleteStepCommand(Object sender) { if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行删除!"); return; } HardFormulaCAs.Remove(SelectedStep); for (int i = 0; i < HardFormulaCAs.Count; i++) { HardFormulaCAs[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, HardFormulaCAs.ToList()); hfe.CreateTime = DateTime.Now; SelectedFormula = hfe; AllowEdit = false; for (int i = 0; i < HardFormulaCAs.Count; i++) { HardFormulaCAs[i].Copy = (HardFormulaCAEntity)HardFormulaCAs[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 (HardFormulaCAs.Count == 20) { ShowMessageBox("步骤不能超过20条"); return; } if (SelectedStep == null) { ShowMessageBox("请选择一个步骤再进行拷贝!"); return; } var ca = (HardFormulaCAEntity)SelectedStep.Clone(); ca.Copy = (HardFormulaCAEntity)ca.Clone(); HardFormulaCAs.Add(ca); for (int i = 0; i < HardFormulaCAs.Count; i++) { HardFormulaCAs[i].StepIndex = i + 1; } } #endregion } }