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/InStoreInfo/InStoreInfoViewModel.cs

656 lines
23 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Aucma.Scada.Business;
using Aucma.Scada.Model.domain;
using Aucma.Scada.Model.dto;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HighWayIot.Config;
using HighWayIot.Log4net;
using LiveCharts;
using LiveCharts.Wpf;
using MySqlX.XDevAPI.Common;
using MySqlX.XDevAPI.Relational;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace Aucma.Scada.UI.viewModel.InStoreInfo
{
public class InStoreInfoViewModel : ViewModelBase
{
[DllImport("user32.dll")]
public static extern int MessageBoxTimeoutA(IntPtr hWnd, string msg, string Caps, int type, int Id, int time);
private LogHelper logHelper = LogHelper.Instance;
private ObservableCollection<dynamic> listItems = new ObservableCollection<dynamic>();
private ObservableCollection<RealTaskInfo> taskItems = new ObservableCollection<RealTaskInfo>();
private InStoreBusiness inStoreBusiness = InStoreBusiness.Instance;
private OutStoreBusiness outStoreBusiness = OutStoreBusiness.Instance;
private InStoreTaskHandle inStoreTaskHandle = InStoreTaskHandle.Instance;
private AppConfig appConfig = AppConfig.Instance;
public InStoreInfoViewModel()
{
inStoreBusiness.RefreshInStoreTaskEvent += RefreshInStoreTask;
inStoreBusiness.RefreshScanMateriaCodeEvent += RefreshScanInfo;
inStoreBusiness.RefreshLogMessageEvent += PrintMessageToListBox1;
// inStoreTaskHandle.RefreshInStoreStockEvent += Init;
outStoreBusiness.RefreshStoreStockEvent += Init;
inStoreTaskHandle.SpaceCapacityAlarmEvent += async (storeCode, spaceName) =>
{
AlarmMessage = $"货道预警:{spaceName}库存即将放满请尽快清理库存";
Task.Delay(1000 * 10).Wait();
AlarmMessage = string.Empty;
};
QueryCommand = new RelayCommand(Query);
ResetCommand = new RelayCommand(Reset);
DeleteTaskInfoCommand = new RelayCommand<object>(obj => DeleteTaskInfo(obj));
materialTypeCombox = "所有";
Init();
}
#region 参数定义
/// <summary>
/// 箱壳物料条码
/// </summary>
private string shellMaterialCode = string.Empty;
public string ShellMaterialCode
{
get { return shellMaterialCode; }
set { shellMaterialCode = value; RaisePropertyChanged(nameof(ShellMaterialCode)); }
}
/// <summary>
/// 箱壳物料名称
/// </summary>
private string shellMaterialName = string.Empty;
public string ShellMaterialName
{
get { return shellMaterialName; }
set { shellMaterialName = value; RaisePropertyChanged(nameof(ShellMaterialName)); }
}
/// <summary>
/// 箱壳入库货道
/// </summary>
private string shellSpaceName = string.Empty;
public string ShellSpaceName
{
get { return shellSpaceName; }
set { shellSpaceName = value; RaisePropertyChanged(nameof(ShellSpaceName)); }
}
/// <summary>
/// 内胆物料条码
/// </summary>
private string linerMaterialCode = string.Empty;
public string LinerMaterialCode
{
get { return linerMaterialCode; }
set { linerMaterialCode = value; RaisePropertyChanged(nameof(LinerMaterialCode)); }
}
/// <summary>
/// 内胆物料名称
/// </summary>
private string linerMaterialName = string.Empty;
public string LinerMaterialName
{
get { return linerMaterialName; }
set { linerMaterialName = value; RaisePropertyChanged(nameof(LinerMaterialName)); }
}
/// <summary>
/// 内胆入库货道
/// </summary>
private string linerSpaceName = string.Empty;
public string LinerSpaceName
{
get { return linerSpaceName; }
set { linerSpaceName = value; RaisePropertyChanged(nameof(LinerSpaceName)); }
}
private string alarmMessage = string.Empty;
public string AlarmMessage
{
get { return alarmMessage; }
set { alarmMessage = value; RaisePropertyChanged(nameof(AlarmMessage)); }
}
/// <summary>
/// LisBox数据模板
/// </summary>
private IEnumerable logInfoListBox;
public IEnumerable LogInfoListBox
{
get { return logInfoListBox; }
set { logInfoListBox = value; RaisePropertyChanged(() => LogInfoListBox); }
}
/// <summary>
/// 箱壳入库任务DataGrid
/// </summary>
private IEnumerable instoreTask;
public IEnumerable InstoreTask
{
get { return instoreTask; }
set { instoreTask = value; RaisePropertyChanged(() => InstoreTask); }
}
/// <summary>
/// 内胆入库任务DataGrid
/// </summary>
private ObservableCollection<SysUserInfo> linerInstoreTask;
public ObservableCollection<SysUserInfo> LinerInstoreTask
{
get { return linerInstoreTask; }
set { linerInstoreTask = value; RaisePropertyChanged(() => LinerInstoreTask); }
}
/// <summary>
/// 任务列表搜索条件
/// </summary>
private String search = String.Empty;
public String Search
{
get { return search; }
set { search = value; RaisePropertyChanged(() => Search); }
}
/// <summary>
/// 下拉框
/// </summary>
public string materialTypeCombox;
public string MaterialTypeCombox
{
get { return materialTypeCombox; }
set
{
if (materialTypeCombox != value)
{
materialTypeCombox = value;
RaisePropertyChanged(() => MaterialTypeCombox);
}
}
}
/// <summary>
/// 库存统计柱状图
/// </summary>
private SeriesCollection achievement = new SeriesCollection();
public SeriesCollection Achievement
{
get { return achievement; }
set { achievement = value; }
}
/// <summary>
/// 库存统计柱状图X轴物料信息
/// </summary>
private List<string> materialNameList = new List<string>();
public List<string> MaterialNameList
{
get { return materialNameList; }
set { materialNameList = value; }
}
#endregion
#region 事件定义
/// <summary>
/// 查询事件
/// </summary>
public RelayCommand QueryCommand { get; set; }
/// <summary>
/// 重置
/// </summary>
public RelayCommand ResetCommand { get; set; }
/// <summary>
/// 删除
/// </summary>
public RelayCommand<object> DeleteTaskInfoCommand { get; set; }
#endregion
private async void PrintMessageToListBox1(string message)
{
await Task.Run(() =>
{
try
{
string formattedMessage = $"{DateTime.Now.ToString("HH:mm:ss.ss")} ==> {message}";
lock (listItems)
{
listItems.Add(formattedMessage);
while (listItems.Count > 120)
{
listItems.RemoveAt(0);
}
var orderedList = listItems.OrderByDescending(x => x).ToList(); // 排序后转为 List
Application.Current.Dispatcher.Invoke(() =>
{
LogInfoListBox = orderedList; // 更新 UI
});
}
}
catch (Exception ex)
{
logHelper.Error("日志数据绑定异常", ex);
}
});
}
/// <summary>
/// listBox绑定日志
/// </summary>
/// <param name="message"></param>
private void PrintMessageToListBox(string message)
{
try
{
listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}");
while (listItems.Count > 120)
{
listItems.RemoveAt(0);
}
LogInfoListBox = listItems.OrderByDescending(x => x);
}
catch (Exception ex)
{
logHelper.Error("日志数据绑定异常", ex);
}
}
/// <summary>
/// 箱壳入库任务列表查询
/// </summary>
public async void Query()
{
QueryAndRefresh();
}
public async void QueryAndRefresh()
{
await Task.Run(() =>
{
lock (string.Empty)
{
try
{
taskItems = new ObservableCollection<RealTaskInfo>();
#region 通过数据库获取数据进行刷新
var info = inStoreBusiness.GetInStoreTask();
if (info != null)
{
if (materialTypeCombox == "箱壳")
{
materialTypeCombox = appConfig.shellStoreCode;
}
else if (materialTypeCombox == "内胆")
{
materialTypeCombox = appConfig.linerStoreCode;
}
else if (materialTypeCombox == "所有")
{
materialTypeCombox = string.Empty;
}
//info.ForEach(x => RefreshInStoreTask(x));
try
{
info = info.Where(x => !string.IsNullOrEmpty(search) ? x.materialCode == search : 1 == 1 && !string.IsNullOrEmpty(materialTypeCombox) ? x.storeCode == materialTypeCombox : 1 == 1).ToList();
App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
taskItems.Clear();
foreach (var item in info)
{
item.materialType = inStoreBusiness.GetMaterialName(item.materialType);
taskItems.Add(item);
}
InstoreTask = taskItems;
}));
}
catch (Exception ex)
{
logHelper.Error("入库任务列表刷新异常", ex);
}
}
#endregion
}
catch (Exception ex)
{
logHelper.Error("入库任务加载异常", ex);
}
}
});
}
/// <summary>
/// 重置
/// </summary>
public void Reset()
{
Search = string.Empty;
MaterialTypeCombox = string.Empty;
this.Query();
}
/// <summary>
/// 柱状体加载
/// </summary>
public async Task Init()
{
await Task.Run(() =>
{
try
{
Query();
ChartValues<int> shellAchievement = new ChartValues<int>();
ChartValues<int> linerAchievement = new ChartValues<int>();
// List<dynamic> info = inStoreBusiness.GetMaterialStock();
List<BaseSpaceInfo> list = inStoreBusiness.GetStock();
List<BaseSpaceInfo> ShellList = list.Where(x=>x.storeCode==appConfig.shellStoreCode && x.spaceStock>0).ToList();
List<BaseSpaceInfo> LinerList = list.Where(x => x.storeCode == appConfig.linerStoreCode && x.spaceStock > 0).ToList();
var chartList = from m in ShellList
group m by SubStringByName(m.typeNameA) into g
select new
{
typeNameA = g.Key,
Count = g.Sum(m => m.spaceStock),
StoreCode = g.First().storeCode
};
var chartList2 = from m in LinerList
group m by SubStringByName(m.typeNameA) into g
select new
{
typeNameA = g.Key,
Count = g.Sum(m => m.spaceStock),
StoreCode = g.First().storeCode
};
//var chartList = ShellList.GroupBy(x => SubStringByName(x.typeNameA))
//.Select(g => new
//{
// typeNameA = g.Key,
// // typeNameA = SubStringByName(g.Key),
// Count = g.Sum(x => x.spaceStock),
// StoreCode = g.Select(x=>x.storeCode)
//}) ;
// var chartList2 = LinerList.GroupBy(x => SubStringByName(x.typeNameA))
//.Select(g => new
//{
// typeNameA = g.Key,
// // typeNameA = SubStringByName(g.Key),
// Count = g.Sum(x => x.spaceStock),
// StoreCode = g.Select(x => x.storeCode)
//});
App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
if (chartList != null && chartList.Count() > 0)
{
Achievement.Clear();
MaterialNameList.Clear();
foreach (var item in chartList)
{
if (string.IsNullOrEmpty(item.typeNameA)) continue;
MaterialNameList.Add((item.typeNameA));
shellAchievement.Add(item.Count);
if(chartList2.FirstOrDefault(x=>x.typeNameA == item.typeNameA) != null)
{
linerAchievement.Add(chartList2.FirstOrDefault(x => x.typeNameA == item.typeNameA).Count);
}
else
{
linerAchievement.Add(0);
}
}
foreach(var item in chartList2)
{
if (string.IsNullOrEmpty(item.typeNameA)) continue;
if (MaterialNameList.FirstOrDefault(x => x == item.typeNameA) == null)
{
MaterialNameList.Add((item.typeNameA));
shellAchievement.Add(0);
linerAchievement.Add(item.Count);
}
}
}
//if (chartList2 != null && chartList.Count() > 0)
//{
// foreach (var item in chartList2)
// {
// MaterialNameList.Add((item.typeNameA));
// linerAchievement.Add(item.Count);
// // linerAchievement.Add(0);
// }
//}
var shellColumn = new ColumnSeries();
shellColumn.DataLabels = true;
shellColumn.Title = "箱壳";
shellColumn.Values = shellAchievement;
shellColumn.Foreground = Brushes.White;
var linerColumn = new ColumnSeries();
linerColumn.DataLabels = true;
linerColumn.Title = "内胆";
linerColumn.Values = linerAchievement;
linerColumn.Foreground = Brushes.White;
linerColumn.Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226));
Achievement.Add(shellColumn);
Achievement.Add(linerColumn);
}));
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
}
});
}
////public async Task Init()
////{
//// await Task.Run(() =>
//// {
//// try
//// {
//// Query();
//// ChartValues<int> shellAchievement = new ChartValues<int>();
//// ChartValues<int> linerAchievement = new ChartValues<int>();
//// MaterialNameList = new List<string>();
//// // List<dynamic> info = inStoreBusiness.GetMaterialStock();
//// List<BaseSpaceInfo> list = inStoreBusiness.GetStock();
//// App.Current.Dispatcher.BeginInvoke((Action)(() =>
//// {
//// if (list != null && list.Count > 0)
//// {
//// Achievement.Clear();
//// foreach (var item in list)
//// {
//// MaterialNameList.Add(SubStringByName(item.typeNameA));
//// if (item.storeCode == appConfig.shellStoreCode)
//// {
//// shellAchievement.Add(item.spaceStock);
//// linerAchievement.Add(0);
//// }
//// else if (item.storeCode == appConfig.linerStoreCode)
//// {
//// linerAchievement.Add(item.spaceStock);
//// shellAchievement.Add(0);
//// }
//// }
//// }
//// var shellColumn = new ColumnSeries();
//// shellColumn.DataLabels = true;
//// shellColumn.Title = "箱壳";
//// shellColumn.Values = shellAchievement;
//// shellColumn.Foreground = Brushes.White;
//// var linerColumn = new ColumnSeries();
//// linerColumn.DataLabels = true;
//// linerColumn.Title = "内胆";
//// linerColumn.Values = linerAchievement;
//// linerColumn.Foreground = Brushes.White;
//// linerColumn.Fill = new SolidColorBrush(Color.FromRgb(15, 209, 226));
//// Achievement.Add(shellColumn);
//// Achievement.Add(linerColumn);
//// }));
//// }
//// catch (Exception ex)
//// {
//// logHelper.Error(ex.Message.ToString());
//// }
//// });
////}
/// <summary>
/// 手动删除任务
/// </summary>
/// <param name="obj"></param>
private void DeleteTaskInfo(object obj)
{
string taskCode = obj as string;
if (!string.IsNullOrEmpty(taskCode))
{
bool result = inStoreBusiness.DeleteTaskInfoByTaskCode(taskCode, true);
if (result)
{
logHelper.Info($"手动删除任务:{taskCode};成功");
MessageBoxTimeoutA((IntPtr)0, $"手动删除任务:{taskCode}成功3秒后关闭提示", "提示", 0, 0, 3000);
}
else
{
logHelper.Info($"手动删除任务:{taskCode};失败");
MessageBoxTimeoutA((IntPtr)0, $"手动删除任务:{taskCode}失败3秒后关闭提示", "提示", 0, 0, 3000);
}
Query();
}
}
/// <summary>
/// 截取逗号之间的名称
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private string SubStringByName(string name)
{
if (string.IsNullOrEmpty(name)) return string.Empty;
string result = string.Empty;
try
{
int firstCommaIndex = name.IndexOf(',');
int secondCommaIndex = name.IndexOf(',', firstCommaIndex + 1);
if (firstCommaIndex != -1 && secondCommaIndex != -1)
{
result = name.Substring(firstCommaIndex + 1, secondCommaIndex - firstCommaIndex - 1);
}
}
catch (Exception ex)
{
logHelper.Error(ex.Message.ToString());
}
return result;
}
/// <summary>
/// 刷新入库任务列表
/// </summary>
/// <param name="taskInfo"></param>
private void RefreshInStoreTask(RealTaskInfo taskInfo, bool isFinsih = false)
{
if (isFinsih)
{
Init();
}
else
{
Query();
}
}
/// <summary>
/// 刷新扫码信息
/// </summary>
/// <param name="materialCode"></param>
/// <param name="materialName"></param>
/// <param name="spaceName"></param>
/// <param name="materialType"></param>
private void RefreshScanInfo(string materialCode, string materialName, string spaceName, string materialType)
{
if (materialType == appConfig.shellStoreCode)
{
ShellMaterialCode = materialCode;
ShellMaterialName = materialName;
ShellSpaceName = spaceName;
}
else if (materialType == appConfig.linerStoreCode)
{
LinerMaterialCode = materialCode;
LinerMaterialName = materialName;
LinerSpaceName = spaceName;
}
}
}
}