Files
FT479/WPFWCSDemo/ViewModels/MonitorViewModel.cs
2026-06-29 14:46:32 +08:00

139 lines
4.0 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using WPFWCSDemo.Models;
using WPFWCSDemo.Utilities;
namespace WPFWCSDemo.ViewModels
{
public partial class MonitorViewModel : ObservableObject
{
public MonitorViewModel()
{
WarehouseLayout = new ObservableCollection<WareHouseInfo>();
CargoState = new ObservableCollection<Cargo>();
InitializeWarehouseLayout(LayersSelectedIndex);
}
/// <summary>
/// 货位信息
/// </summary>
[ObservableProperty]
ObservableCollection<WareHouseInfo> _warehouseLayout;
/// <summary>
/// 货位状态
/// </summary>
[ObservableProperty]
ObservableCollection<Cargo> _cargoState;
/// <summary>
/// 层数选择
/// </summary>
[ObservableProperty]
int _layersSelectedIndex;
/// <summary>
/// 货位状态绘图初始化
/// </summary>
private void InitializeWarehouseLayout(int layer)
{
WarehouseLayout.Clear();
CargoState.Clear();
double cellWidth = 36;
double cellHeight = 25;
DataTable dt = SqlHelper.ExecuteTable("SELECT STATUS FROM CARGOSPACE WHERE FLOOR=@Floor ORDER BY ROW ASC,[COLUMN] ASC ",
new System.Data.SqlClient.SqlParameter("@Floor", layer + 1));
foreach (DataRow item in dt.Rows)
{
Cargo cargo = new Cargo();
cargo.Status = int.Parse(item["Status"].ToString());
CargoState.Add(cargo);
}
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 22; col++)
{
WareHouseInfo wareHouseInfo = new WareHouseInfo();
wareHouseInfo.Tooltip = $"排: {row + 1}, 列: {col + 1}, 层: {layer + 1}";
if (CargoState.First().Status == 1)
{
//仓位空
wareHouseInfo.FillColor = Brushes.Silver;
}
else if (CargoState.First().Status == 2)
{
//仓位占用
wareHouseInfo.FillColor = Brushes.LawnGreen;
}
else if (CargoState.First().Status == 3)
{
//仓位盘点
wareHouseInfo.FillColor = Brushes.Orange;
}
else
{
//仓位异常
wareHouseInfo.FillColor = Brushes.Red;
}
CargoState.RemoveAt(0);
//设备排列图形间隔
wareHouseInfo.X = col * cellWidth + 1 + cellWidth;
//wareHouseInfo.Y = row * cellHeight;
//设置奇偶数排图形间隔
if (row % 2 == 0)
{
wareHouseInfo.Y = (row + (row * 1)) * cellHeight + 40;
}
else
{
wareHouseInfo.Y = (row + (row - 1) * 1) * cellHeight + 50;
}
//设置仓位图形缩放中心
wareHouseInfo.RenderCenter = new System.Drawing.Point((int)wareHouseInfo.X + (int)(cellWidth / 2 - 3), (int)wareHouseInfo.Y + (int)(cellWidth / 2 - 3));
WarehouseLayout.Add(wareHouseInfo);
}
}
}
/// <summary>
/// 点击层数切换库存显示
/// </summary>
[RelayCommand]
void LayersSelect()
{
InitializeWarehouseLayout(LayersSelectedIndex);
}
}
}