You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
14 KiB
C#

using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Aucma.Scada.UI.Common;
using Aucma.Scada.UI.Page.AssemblyPlan;
using Aucma.Scada.UI.Page.InventoryInfo;
using Aucma.Scada.UI;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveCharts;
using LiveCharts.Wpf;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace Aucma.Scada.UI.ViewModel.AssemblyPlan
{
public partial class AssemblyPlanViewModel : ObservableObject
{
private AssemblyPlanBusiness assemblyPlanBusiness = new AssemblyPlanBusiness();// AssemblyPlanBusiness.Instance;
private IExecutePlanInfoServices _executePlanInfoServices;
private AppConfig appConfig = new AppConfig();//AppConfig.Instance;
//private OutStoreBusiness outStoreBusiness = OutStoreBusiness.Instance;
public AssemblyPlanViewModel()
{
_executePlanInfoServices = App.ServiceProvider.GetService<IExecutePlanInfoServices>();
//assemblyPlanBusiness.RefreshExecutePlanInfoEvent += RefreshDataGrid;
//outStoreBusiness.RefreshAssemblyPlanInitEvent += RefreshPlanInfoDataGrid;
Init();
}
#region 参数定义
/// <summary>
/// 工位名称
/// </summary>
private string stationName = string.Empty;
public string StationName
{
get => stationName;
set => SetProperty(ref stationName, value);
}
/// <summary>
/// 订单编号
/// </summary>
private string orderCode = string.Empty;
public string OrderCode
{
get => orderCode;
set => SetProperty(ref orderCode, value);
}
/// <summary>
/// 计划编号
/// </summary>
private string planCode = string.Empty;
public string PlanCode
{
get => planCode;
set => SetProperty(ref planCode, value);
}
/// <summary>
/// 产品型号
/// </summary>
private string productModel = string.Empty;
public string ProductModel
{
get => productModel;
set => SetProperty(ref productModel, value);
}
/// <summary>
/// 开始时间
/// </summary>
private string beginTime = string.Empty;
public string BeginTime
{
get => beginTime;
set => SetProperty(ref beginTime, value);
}
/// <summary>
/// 计划列表DataGrid
/// </summary>
private ObservableCollection<ExecutePlanInfo> planInfoDataGrid;
public ObservableCollection<ExecutePlanInfo> PlanInfoDataGrid
{
get => planInfoDataGrid;
set => SetProperty(ref planInfoDataGrid, value);
}
/// <summary>
/// 日产量柱状图
/// </summary>
private SeriesCollection achievement = new SeriesCollection();
public SeriesCollection Achievement
{
get { return achievement; }
set { achievement = value; }
}
/// <summary>
/// 型号统计柱状图
/// </summary>
private SeriesCollection modelStatistics = new SeriesCollection();
public SeriesCollection ModelStatistics
{
get { return modelStatistics; }
set { modelStatistics = value; }
}
/// <summary>
/// 日产量柱状图X轴日期
/// </summary>
private List<string> productionHourList;
public List<string> ProductionHourList
{
get { return productionHourList; }
set { productionHourList = value; }
}
/// <summary>
/// 型号统计柱状图x轴物料类型
/// </summary>
private List<string> materialNameList;
public List<string> MaterialNameList
{
get { return materialNameList; }
set { materialNameList = value; }
}
/// <summary>
/// 计划数量
/// </summary>
private int planAmount = 0;
public int PlanAmount
{
get => planAmount;
set => SetProperty(ref planAmount, value);
}
/// <summary>
/// 完成数量
/// </summary>
public int complateAmout;
public int ComplateAmout
{
get => complateAmout;
set => SetProperty(ref complateAmout, value);
}
/// <summary>
/// 差异值
/// </summary>
public int diffAmount;
public int DiffAmount
{
get => diffAmount;
set => SetProperty(ref diffAmount, value);
}
/// <summary>
/// 完成率
/// </summary>
public string compleRoution;
public string CompleRoution
{
get => compleRoution;
set => SetProperty(ref compleRoution, value);
}
#endregion
#region 事件定义
/// <summary>
/// 上移事件
/// </summary>
public RelayCommand<object> MoveUpCommand { get; set; }
/// <summary>
/// 下移事件
/// </summary>
public RelayCommand<object> MoveDownCommand { get; set; }
/// <summary>
/// 删除计划
/// </summary>
public RelayCommand<object> DeletePlanCommand { get; set; }
/// <summary>
/// 下传事件
/// </summary>
public RelayCommand<object> NextPassCommand { get; set; }
public RelayCommand PlanInfoEditCommand { get; set; }
public RelayCommand MaterialStatisticsCommand { get; set; }
#endregion
/// <summary>
/// 计划上移
/// </summary>
/// <param name="obj"></param>
public void MoveUp(object obj)
{
string info = obj as string;
assemblyPlanBusiness.ExecutePlanInfo_MoveUp(planInfoDataGrid.ToList(), info);
}
/// <summary>
/// 计划下移
/// </summary>
/// <param name="obj"></param>
public void MoveDown(object obj)
{
string info = obj as string;
assemblyPlanBusiness.ExecutePlanInfo_MoveDown(planInfoDataGrid.ToList(), info);
}
/// <summary>
/// 删除计划
/// </summary>
/// <param name="obj"></param>
public async void DeletePlan(object obj)
{
string info = obj as string;
bool result =await 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);
}
}
/// <summary>
/// 下传计划
/// </summary>
/// <param name="obj"></param>
public async 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 =await 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<ExecutePlanInfo> executePlanInfos)
{
PlanInfoDataGrid = new ObservableCollection<ExecutePlanInfo>();
if (executePlanInfos != null)
{
executePlanInfos.ForEach(
arg =>
{
PlanInfoDataGrid.Add(arg);
});
}
RefreshStatisticsGauge(executePlanInfos);
}
/// <summary>
/// 出库完成刷新计划列表
/// </summary>
private void RefreshPlanInfoDataGrid()
{
RefreshExecutePlan();
App.Current.Dispatcher.Invoke((Action)(() =>
{
OrderCode = string.Empty;
PlanCode = string.Empty;
ProductModel = string.Empty;
}));
}
/// <summary>
/// 柱状体加载
/// </summary>
public void Init()
{
RefreshExecutePlan();
}
/// <summary>
/// 计划维护
/// </summary>
private void PlanInfoEdit()
{
PlanInfoEditWindow planInfoEditWindow = new PlanInfoEditWindow();
planInfoEditWindow.ShowDialog();
}
private void MaterialStatistics()
{
MaterialStatisticsWindow materialStatisticsWindow = new MaterialStatisticsWindow();
materialStatisticsWindow.ShowDialog();
}
/// <summary>
/// 刷新计划执行
/// </summary>
/// <param name="planInfo"></param>
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");
}
/// <summary>
/// 加载正在执行的计划
/// </summary>
private async void RefreshExecutePlan()
{
var planInfos =await 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);
}
}
}
/// <summary>
/// 刷新统计图表
/// </summary>
/// <param name="planInfos"></param>
private void RefreshStatisticsGauge(List<ExecutePlanInfo> planInfos)
{
if (planInfos != null)
{
if (planInfos.Count > 0)
{
PlanAmount = planInfos.Sum(x => x.PlanAmount).ObjToInt();
ComplateAmout = planInfos.Sum(x => x.CompleteAmount).ObjToInt();
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();
}
/// <summary>
/// 刷新物料型号统计
/// </summary>
private void RefreshMaterialStats()
{
App.Current.Dispatcher.Invoke((Action)(async () =>
{
//获取物料型号统计
List<dynamic> materialStats =await assemblyPlanBusiness.GetMaterialStats();
if (materialStats != null)
{
ModelStatistics.Clear();
MaterialNameList = new List<string>();
ChartValues<double> materialAchievement = new ChartValues<double>();
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;
ModelStatistics.Add(materialColumn);
}
}));
}
/// <summary>
/// 刷新小时产量统计
/// </summary>
private void RefreshHourAmount()
{
App.Current.Dispatcher.Invoke((Action)(async () =>
{
//获取小时产量
List<dynamic> hourAmount =await assemblyPlanBusiness.GetHourAmount();
if (hourAmount != null)
{
Achievement.Clear();
ProductionHourList = new List<string>();
ChartValues<double> hourAchievement = new ChartValues<double>();
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;
Achievement.Add(houeColumn);
}
}));
}
}
}