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.
Aucma.Scada/Aucma.Scada.UI/viewModel/AssemblyPlan/AssemblyPlanViewModel.cs

632 lines
21 KiB
C#

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 HighWayIot.Log4net;
using HighWayIot.Repository.service;
using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
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 List<WorkTime> listTime = null;
private RegisterServices registerServices = RegisterServices.Instance;
private readonly IBaseBomInfoService _baseBomInfoServices = null;
public AssemblyPlanViewModel()
{
MoveUpCommand = new RelayCommand<object>(obj => MoveUp(obj));
MoveDownCommand = new RelayCommand<object>(obj => MoveDown(obj));
DeletePlanCommand = new RelayCommand<object>(obj => DeletePlan(obj));
NextPassCommand = new RelayCommand<object>(obj => NextPass(obj));
_baseBomInfoServices = registerServices.GetService<IBaseBomInfoService>();
listTime = _baseBomInfoServices.getWorkTime().Result;
PlanInfoEditCommand = new RelayCommand(PlanInfoEdit);
MaterialStatisticsCommand = new RelayCommand(MaterialStatistics);
AutoSendCommand = new RelayCommand(() =>
{
if (assemblyPlanBusiness.autoFlag)
{
assemblyPlanBusiness.autoFlag = false;
AutoSendContent = "自动下发";
}
else
{
assemblyPlanBusiness.autoFlag = true;
Task.Run(() => { assemblyPlanBusiness.AutomaticDownpass(); });
AutoSendContent = "关闭";
}
});
assemblyPlanBusiness.RefreshExecutePlanInfoEvent += RefreshDataGrid;
outStoreBusiness.RefreshAssemblyPlanInitEvent += RefreshPlanInfoDataGrid;
BindingPageViewModel.RefreshChartsEvent += RefreshCharts;
Init();
AutoSendContent = "自动下发";
}
#region 参数定义
/// <summary>
/// 序号
/// </summary>
private int no;
public int NO
{
get { return no; }
set { no = value; RaisePropertyChanged(nameof(NO)); }
}
/// <summary>
/// 工位名称
/// </summary>
private string stationName = string.Empty;
public string StationName
{
get { return stationName; }
set { stationName = value; RaisePropertyChanged(nameof(StationName)); }
}
/// <summary>
/// 订单编号
/// </summary>
private string orderCode = string.Empty;
public string OrderCode
{
get { return orderCode; }
set { orderCode = value; RaisePropertyChanged(nameof(OrderCode)); }
}
/// <summary>
/// 计划编号
/// </summary>
private string planCode = string.Empty;
public string PlanCode
{
get { return planCode; }
set { planCode = value; RaisePropertyChanged(nameof(PlanCode)); }
}
/// <summary>
/// 产品型号
/// </summary>
private string productModel = string.Empty;
public string ProductModel
{
get { return productModel; }
set { productModel = value; RaisePropertyChanged(nameof(ProductModel)); }
}
/// <summary>
/// 开始时间
/// </summary>
private string beginTime = string.Empty;
public string BeginTime
{
get { return beginTime; }
set { beginTime = value; RaisePropertyChanged(nameof(BeginTime)); }
}
private string autoSendContent = string.Empty;
//
public string AutoSendContent
{
get { return autoSendContent; }
set { autoSendContent = value; RaisePropertyChanged(nameof(AutoSendContent)); }
}
/// <summary>
/// 计划列表DataGrid
/// </summary>
private ObservableCollection<ExecutePlanInfo> planInfoDataGrid;
public ObservableCollection<ExecutePlanInfo> PlanInfoDataGrid
{
get { return planInfoDataGrid; }
set { planInfoDataGrid = value; RaisePropertyChanged(() => PlanInfoDataGrid); }
}
/// <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 = new List<string>();
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 { return planAmount; }
set { planAmount = value; RaisePropertyChanged(nameof(planAmount)); }
}
/// <summary>
/// 完成数量
/// </summary>
public int complateAmout;
public int ComplateAmout
{
get { return complateAmout; }
set { complateAmout = value; RaisePropertyChanged(nameof(ComplateAmout)); }
}
/// <summary>
/// 差异值
/// </summary>
public int diffAmount;
public int DiffAmount
{
get { return diffAmount; }
set { diffAmount = value; RaisePropertyChanged(nameof(DiffAmount)); }
}
/// <summary>
/// 完成率
/// </summary>
public string compleRoution;
public string CompleRoution
{
get { return compleRoution; }
set { compleRoution = value; RaisePropertyChanged(nameof(CompleRoution)); }
}
#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; }
public RelayCommand AutoSendCommand { 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 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);
}
}
/// <summary>
/// 下传计划
/// </summary>
/// <param name="obj"></param>
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<ExecutePlanInfo> executePlanInfos)
{
int count = 0;
PlanInfoDataGrid = new ObservableCollection<ExecutePlanInfo>();
if (executePlanInfos != null)
{
executePlanInfos.ForEach(
arg =>
{
PlanInfoDataGrid.Add(arg);
});
}
RefreshStatisticsGauge(executePlanInfos);
}
/// <summary>
/// 出库完成刷新计划列表
/// </summary>
private void RefreshPlanInfoDataGrid(int isFlag = 0)
{
if (isFlag == 1)
{
AutoSendContent = "关闭";
}
else if (isFlag == 2)
{
AutoSendContent = "自动下发";
}
else
{
//
}
RefreshExecutePlanAsync();
App.Current.Dispatcher.Invoke((Action)(() =>
{
OrderCode = string.Empty;
PlanCode = string.Empty;
ProductModel = string.Empty;
}));
}
/// <summary>
/// 柱状体加载
/// </summary>
public void Init()
{
RefreshExecutePlanAsync();
}
/// <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 Task RefreshExecutePlanAsync()
{
await Task.Run(() =>
{
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);
}
}
});
}
/// <summary>
/// 刷新统计图表
/// </summary>
/// <param name="planInfos"></param>
private async void RefreshStatisticsGauge(List<ExecutePlanInfo> 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;
}
}
}
/// <summary>
/// 刷新柱状图
/// </summary>
public async void RefreshCharts()
{
await RefreshMaterialStatsAsync();
await RefreshHourAmountAsync();
}
/// <summary>
/// 刷新物料型号统计
/// </summary>
private async Task RefreshMaterialStatsAsync()
{
try
{
await Task.Run(() =>
{
//获取物料型号统计
List<dynamic> materialStats = assemblyPlanBusiness.GetMaterialStats();
App.Current.Dispatcher.Invoke((Action)(() =>
{
if (materialStats != null)
{
ModelStatistics.Clear();
MaterialNameList = new List<string>();
ChartValues<double> materialAchievement = new ChartValues<double>();
foreach (var item in materialStats)
{
MaterialNameList.Add(GetTextBetweenCommas(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);
}
}));
});
}
catch (Exception ex)
{
LogHelper.Instance.Error(ex.Message.ToString());
}
}
/// <summary>
/// 截取字符串第一个逗号和第二个逗号之间的字符串
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
static string GetTextBetweenCommas(string input)
{
string pattern = @",([^,]+),";
Regex regex = new Regex(pattern);
Match match = regex.Match(input);
if (match.Success)
{
return match.Groups[1].Value;
}
else
{
return string.Empty;
}
}
/// <summary>
/// 刷新小时产量统计
/// </summary>
private async Task RefreshHourAmountAsync()
{
await Task.Run(() =>
{
listTime = _baseBomInfoServices.getWorkTime().Result;
if (listTime == null) return;
//获取小时产量
List<dynamic> hourAmount = assemblyPlanBusiness.GetHourAmount();
if (hourAmount != null)
{
App.Current.Dispatcher.Invoke((Action)(() =>
{
ProductionHourList.Clear();
Achievement.Clear();
ChartValues<ObservablePoint> achievement = new ChartValues<ObservablePoint>();
// x轴
foreach (WorkTime workTime in listTime)
{
ProductionHourList.Add(workTime.startTime.Hour.ToString());
}
// y轴
int i = 0;
foreach (string item in ProductionHourList)
{
// achievement.Add(new ObservablePoint(i, 2));
var target = hourAmount.Where(x => int.Parse(x.PRODUCT_HOUR) == int.Parse(item)).FirstOrDefault();
if (target != null)
{
achievement.Add(new ObservablePoint(i, Convert.ToDouble(target.PRODUCT_AMOUNT)));
}
else
{
achievement.Add(new ObservablePoint(i, 0));
}
i++;
}
var houeColumn = new ColumnSeries();
houeColumn.DataLabels = true;
houeColumn.Title = "小时产量";
houeColumn.Values = achievement;
houeColumn.Foreground = Brushes.White;
Achievement.Add(houeColumn);
}));
}
//App.Current.Dispatcher.Invoke((Action)(() =>
//{
// 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;
// houeColumn.Foreground = Brushes.White;
// Achievement.Add(houeColumn);
// }
//}));
});
}
}
}