using Aucma.Scada.Business; using Aucma.Scada.Model.domain; using Aucma.Scada.UI.Page.AssemblyPlan; using Aucma.Scada.UI.Page.InventoryInfo; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HighWayIot.Common; using HighWayIot.Config; using LiveCharts; using LiveCharts.Wpf; using MySqlX.XDevAPI.Relational; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Windows; using System.Windows.Media; namespace Aucma.Scada.UI.viewModel.AssemblyPlan { public class AssemblyPlanViewModel : ViewModelBase { private AssemblyPlanBusiness assemblyPlanBusiness = AssemblyPlanBusiness.Instance; private AppConfig appConfig = AppConfig.Instance; private StringChange stringChange = StringChange.Instance; private OutStoreBusiness outStoreBusiness = OutStoreBusiness.Instance; public AssemblyPlanViewModel() { MoveUpCommand = new RelayCommand(obj => MoveUp(obj)); MoveDownCommand = new RelayCommand(obj => MoveDown(obj)); DeletePlanCommand = new RelayCommand(obj => DeletePlan(obj)); NextPassCommand = new RelayCommand(obj => NextPass(obj)); PlanInfoEditCommand = new RelayCommand(PlanInfoEdit); MaterialStatisticsCommand = new RelayCommand(MaterialStatistics); assemblyPlanBusiness.RefreshExecutePlanInfoEvent += RefreshDataGrid; outStoreBusiness.RefreshAssemblyPlanInitEvent += RefreshPlanInfoDataGrid; Init(); } #region 参数定义 /// /// 序号 /// private int no; public int NO { get { return no; } set { no = value; RaisePropertyChanged(nameof(NO)); } } /// /// 工位名称 /// private string stationName = string.Empty; public string StationName { get { return stationName; } set { stationName = value; RaisePropertyChanged(nameof(StationName)); } } /// /// 订单编号 /// private string orderCode = string.Empty; public string OrderCode { get { return orderCode; } set { orderCode = value; RaisePropertyChanged(nameof(OrderCode)); } } /// /// 计划编号 /// private string planCode = string.Empty; public string PlanCode { get { return planCode; } set { planCode = value; RaisePropertyChanged(nameof(PlanCode)); } } /// /// 产品型号 /// private string productModel = string.Empty; public string ProductModel { get { return productModel; } set { productModel = value; RaisePropertyChanged(nameof(ProductModel)); } } /// /// 开始时间 /// private string beginTime = string.Empty; public string BeginTime { get { return beginTime; } set { beginTime = value; RaisePropertyChanged(nameof(BeginTime)); } } /// /// 计划列表DataGrid /// private ObservableCollection planInfoDataGrid; public ObservableCollection PlanInfoDataGrid { get { return planInfoDataGrid; } set { planInfoDataGrid = value; RaisePropertyChanged(() => PlanInfoDataGrid); } } /// /// 日产量柱状图 /// private SeriesCollection achievement = new SeriesCollection(); public SeriesCollection Achievement { get { return achievement; } set { achievement = value; } } /// /// 型号统计柱状图 /// private SeriesCollection modelStatistics = new SeriesCollection(); public SeriesCollection ModelStatistics { get { return modelStatistics; } set { modelStatistics = value; } } /// /// 日产量柱状图X轴日期 /// private List productionHourList; public List ProductionHourList { get { return productionHourList; } set { productionHourList = value; } } /// /// 型号统计柱状图x轴物料类型 /// private List materialNameList; public List MaterialNameList { get { return materialNameList; } set { materialNameList = value; } } /// /// 计划数量 /// private int planAmount = 0; public int PlanAmount { get { return planAmount; } set { planAmount = value; RaisePropertyChanged(nameof(planAmount)); } } /// /// 完成数量 /// public int complateAmout; public int ComplateAmout { get { return complateAmout; } set { complateAmout = value; RaisePropertyChanged(nameof(ComplateAmout)); } } /// /// 差异值 /// public int diffAmount; public int DiffAmount { get { return diffAmount; } set { diffAmount = value; RaisePropertyChanged(nameof(DiffAmount)); } } /// /// 完成率 /// public string compleRoution; public string CompleRoution { get { return compleRoution; } set { compleRoution = value; RaisePropertyChanged(nameof(CompleRoution)); } } #endregion #region 事件定义 /// /// 上移事件 /// public RelayCommand MoveUpCommand { get; set; } /// /// 下移事件 /// public RelayCommand MoveDownCommand { get; set; } /// /// 删除计划 /// public RelayCommand DeletePlanCommand { get; set; } /// /// 下传事件 /// public RelayCommand NextPassCommand { get; set; } public RelayCommand PlanInfoEditCommand { get; set; } public RelayCommand MaterialStatisticsCommand { get; set; } #endregion /// /// 计划上移 /// /// public void MoveUp(object obj) { string info = obj as string; assemblyPlanBusiness.ExecutePlanInfo_MoveUp(planInfoDataGrid.ToList(), info); } /// /// 计划下移 /// /// public void MoveDown(object obj) { string info = obj as string; assemblyPlanBusiness.ExecutePlanInfo_MoveDown(planInfoDataGrid.ToList(), info); } /// /// 删除计划 /// /// public void DeletePlan(object obj) { string info = obj as string; bool result = assemblyPlanBusiness.ExecutePlanInfo_Delete(info); if (result) { OrderCode = string.Empty; PlanCode = string.Empty; ProductModel = string.Empty; BeginTime = string.Empty; MessageBox.Show("执行计划删除成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } else { MessageBox.Show("执行计划删除失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } } /// /// 下传计划 /// /// public void NextPass(object obj) { string info = obj as string; if (info == planCode) { MessageBox.Show("该计划正在执行中,请勿重复下传", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } else { var result = assemblyPlanBusiness.ExecutePlanInfo_NextPass(info, planCode); if (result != null) { RefreshPlanExecute(result); MessageBox.Show("执行计划已下达", "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } else { MessageBox.Show("执行计划下达失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly); } } } public void RefreshDataGrid(List executePlanInfos) { int count = 0; PlanInfoDataGrid = new ObservableCollection(); if (executePlanInfos != null) { executePlanInfos.ForEach( arg => { PlanInfoDataGrid.Add(arg); }); } RefreshStatisticsGauge(executePlanInfos); } /// /// 出库完成刷新计划列表 /// private void RefreshPlanInfoDataGrid() { RefreshExecutePlan(); App.Current.Dispatcher.Invoke((Action)(() => { OrderCode = string.Empty; PlanCode = string.Empty; ProductModel = string.Empty; })); } /// /// 柱状体加载 /// public void Init() { RefreshExecutePlan(); } /// /// 计划维护 /// private void PlanInfoEdit() { PlanInfoEditWindow planInfoEditWindow = new PlanInfoEditWindow(); planInfoEditWindow.ShowDialog(); } private void MaterialStatistics() { MaterialStatisticsWindow materialStatisticsWindow = new MaterialStatisticsWindow(); materialStatisticsWindow.ShowDialog(); } /// /// 刷新计划执行 /// /// private void RefreshPlanExecute(ExecutePlanInfo planInfo) { StationName = appConfig.stationName; OrderCode = planInfo.orderCode; PlanCode = planInfo.executePlanCode; ProductModel = planInfo.materialName; DateTime dateTime = (DateTime)planInfo.beginTime; BeginTime = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 加载正在执行的计划 /// private void RefreshExecutePlan() { var planInfos = assemblyPlanBusiness.GetEexecutePlanInfosByProductLineCode(); if (planInfos != null) { //加载正在执行的计划 planInfos = planInfos.Where(x => x.executeStatus == 2).ToList(); if (planInfos.Count > 0) { ExecutePlanInfo planInfo = planInfos.OrderBy(x => x.executeOrder).First(); RefreshPlanExecute(planInfo); } } } /// /// 刷新统计图表 /// /// private void RefreshStatisticsGauge(List planInfos) { if(planInfos != null) { if (planInfos.Count > 0) { PlanAmount = stringChange.ParseToInt(planInfos.Sum(x => x.planAmount).ToString()); ComplateAmout = stringChange.ParseToInt(planInfos.Sum(x => x.completeAmount).ToString()); DiffAmount = planAmount - complateAmout; double completionRate = (double)ComplateAmout / PlanAmount * 100; // 计算完成率 CompleRoution = completionRate.ToString("0.0"); } else { PlanAmount = 0; ComplateAmout = 0; DiffAmount = 0; CompleRoution = string.Empty; } } RefreshMaterialStats(); RefreshHourAmount(); } /// /// 刷新物料型号统计 /// private void RefreshMaterialStats() { App.Current.Dispatcher.Invoke((Action)(() => { //获取物料型号统计 List materialStats = assemblyPlanBusiness.GetMaterialStats(); if (materialStats != null) { ModelStatistics.Clear(); MaterialNameList = new List(); ChartValues materialAchievement = new ChartValues(); foreach (var item in materialStats) { MaterialNameList.Add(item.MATERIAL_NAME); materialAchievement.Add(Convert.ToDouble(item.PRODUCT_AMOUNT)); } var materialColumn = new ColumnSeries(); materialColumn.DataLabels = true; materialColumn.Title = "型号"; materialColumn.Values = materialAchievement; materialColumn.Foreground = Brushes.White; ModelStatistics.Add(materialColumn); } })); } /// /// 刷新小时产量统计 /// private void RefreshHourAmount() { App.Current.Dispatcher.Invoke((Action)(() => { //获取小时产量 List hourAmount = assemblyPlanBusiness.GetHourAmount(); if (hourAmount != null) { Achievement.Clear(); ProductionHourList = new List(); ChartValues hourAchievement = new ChartValues(); foreach (var item in hourAmount) { ProductionHourList.Add(item.PRODUCT_HOUR); hourAchievement.Add(Convert.ToDouble(item.PRODUCT_AMOUNT)); } var houeColumn = new ColumnSeries(); houeColumn.DataLabels = true; houeColumn.Title = "小时产量"; houeColumn.Values = hourAchievement; houeColumn.Foreground = Brushes.White; Achievement.Add(houeColumn); } })); } } }