添加项目文件。
This commit is contained in:
50
WPFWCSDemo/Utilities/CargoTableInitialize.cs
Normal file
50
WPFWCSDemo/Utilities/CargoTableInitialize.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using WPFWCSDemo.StateCode;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化货位信息表
|
||||
/// </summary>
|
||||
public class CargoTableInitialize
|
||||
{
|
||||
/// <summary>
|
||||
/// 向库表中添加货位
|
||||
/// </summary>
|
||||
/// <param name="row">排</param>
|
||||
/// <param name="column">列</param>
|
||||
/// <param name="floor">层</param>
|
||||
public static void CargoSpaceInit(int row, int column, int floor)
|
||||
{
|
||||
string s = "00";
|
||||
|
||||
if (MessageBox.Show("此操作会删除货位信息表中所有数据(可能会导致数据丢失),是否继续?","警告",MessageBoxButton.OKCancel)==MessageBoxResult.OK)
|
||||
{
|
||||
//删除
|
||||
SqlHelper.ExecuteNonQuery("TRUNCATE TABLE CARGOSPACE");
|
||||
|
||||
//生成
|
||||
for (int i = 1; i < row; i++)
|
||||
{
|
||||
for (int j = 1; j < column; j++)
|
||||
{
|
||||
for (int k = 1; k < floor; k++)
|
||||
{
|
||||
SqlHelper.ExecuteNonQuery($"INSERT INTO CARGOSPACE VALUES(" +
|
||||
$"'{i.ToString(s)}{j.ToString(s)}{k.ToString(s)}','0{i}排{j.ToString(s)}列{k.ToString(s)}层',{null},'1','{i}','{j}','{k}',{null},'1',{DateTime.Now.ToString(TimeFormat.stringFormat)},'')");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
56
WPFWCSDemo/Utilities/Checksum.cs
Normal file
56
WPFWCSDemo/Utilities/Checksum.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WPFWCSDemo.Models.Devices.CircleRGVStation;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
public class Checksum
|
||||
{
|
||||
/// <summary>
|
||||
/// 字节数拆分求和
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static short GetChecksum(int number)
|
||||
{
|
||||
byte byte1 = (byte)(number >> 24);
|
||||
byte byte2 = (byte)(number >> 16);
|
||||
byte byte3 = (byte)(number >> 8);
|
||||
byte byte4 = (byte)(number);
|
||||
|
||||
int sum = byte1 + byte2 + byte3 + byte4;
|
||||
|
||||
return (short)sum;
|
||||
}
|
||||
|
||||
public static short GetChecksum(short number)
|
||||
{
|
||||
byte byte1 = (byte)(number >> 8);
|
||||
byte byte2 = (byte)(number);
|
||||
|
||||
int sum = byte1 + byte2;
|
||||
|
||||
return (short)sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取校验码---字节拆分求和
|
||||
/// </summary>
|
||||
/// <param name="circleRGVStationCommand"></param>
|
||||
/// <returns></returns>
|
||||
public static short GetChecksum(CircleRGVStationCommand circleRGVStationCommand)
|
||||
{
|
||||
return Convert.ToInt16(GetChecksum((short)circleRGVStationCommand.Write_DeviceNumber) +
|
||||
GetChecksum((short)circleRGVStationCommand.Write_SourceLocation) +
|
||||
GetChecksum((short)circleRGVStationCommand.Write_TargetLocation) +
|
||||
GetChecksum((short)circleRGVStationCommand.Write_TaskType) +
|
||||
GetChecksum(circleRGVStationCommand.Write_TaskNumber) +
|
||||
GetChecksum((short)1));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
WPFWCSDemo/Utilities/DataCheck.cs
Normal file
16
WPFWCSDemo/Utilities/DataCheck.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
public class DataCheck
|
||||
{
|
||||
public static string IsBarcodeIllegal(string barcode)
|
||||
{
|
||||
return "条码格式错误!";
|
||||
}
|
||||
}
|
||||
}
|
||||
308
WPFWCSDemo/Utilities/SqlDatabaseMaintenance.cs
Normal file
308
WPFWCSDemo/Utilities/SqlDatabaseMaintenance.cs
Normal file
@@ -0,0 +1,308 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.AccessControl;
|
||||
using System.Security.Principal;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Xml.Linq;
|
||||
using WPFWCSDemo.Data;
|
||||
using WPFWCSDemo.StateCode;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据维护操作相关
|
||||
/// </summary>
|
||||
public class SqlDatabaseMaintenance
|
||||
{
|
||||
/// <summary>
|
||||
/// 手动备份根文件夹路径
|
||||
/// </summary>
|
||||
static string rootPath = @"E:\SqlDataBackup";
|
||||
/// <summary>
|
||||
/// 自动备份根文件夹路径
|
||||
/// </summary>
|
||||
static string rootPathAuto = @"E:\SqlAutoBackup";
|
||||
/// <summary>
|
||||
/// sql库名
|
||||
/// </summary>
|
||||
static string dbName = "FT479";
|
||||
|
||||
/// <summary>
|
||||
/// 备份当前数据库
|
||||
/// </summary>
|
||||
public static void SQLBackup()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 连接字符串
|
||||
string connectionString = SqlHelper.ConStr;
|
||||
|
||||
// 指定备份文件的路径
|
||||
string backupPath = $@"{rootPath}\{dbName}_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.bak";
|
||||
|
||||
// SQL 查询备份数据库
|
||||
string backupQuery = $"BACKUP DATABASE {dbName} TO DISK = '{backupPath}' WITH COPY_ONLY, STATS = 10;";
|
||||
|
||||
|
||||
GetOrCreateDirectory(backupPath);
|
||||
|
||||
// 打开数据库连接
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// 执行备份查询
|
||||
using (SqlCommand command = new SqlCommand(backupQuery, connection))
|
||||
{
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
MessageBox.Show("数据库备份成功!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"数据库备份失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 自动备份当前数据库
|
||||
/// 包含删除旧数据功能
|
||||
/// </summary>
|
||||
public static void SQLBackupAuto()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 连接字符串
|
||||
string connectionString = SqlHelper.ConStr;
|
||||
|
||||
// 指定备份文件的路径
|
||||
string backupPath = $@"{rootPathAuto}\{DateTime.Now.AddMonths(-4).ToString("yyyy")}\{dbName}_{DateTime.Now.AddMonths(-4).ToString("yyyy-MM")}.bak";
|
||||
|
||||
// SQL 查询备份数据库
|
||||
string backupQuery = $"BACKUP DATABASE {dbName} TO DISK = '{backupPath}' WITH COPY_ONLY, STATS = 10;";
|
||||
|
||||
GetOrCreateDirectory(backupPath);
|
||||
|
||||
Log.Warning("【定期自动备份】自动备份数据中...");
|
||||
|
||||
using var context = new AppDbContext();
|
||||
|
||||
//备份前删除前3个月外的调度任务记录
|
||||
context.TaskHistories
|
||||
.Where(t => t.FinishTime < DateTime.Now.AddMonths(-3))
|
||||
.ExecuteDelete();
|
||||
|
||||
//备份前删除前1年前的报警任务记录
|
||||
context.AlarmHistories
|
||||
.Where(t => t.End_Time < DateTime.Now.AddYears(-1))
|
||||
.ExecuteDelete();
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
// 打开数据库连接
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// 执行备份查询
|
||||
using (SqlCommand command = new SqlCommand(backupQuery, connection))
|
||||
{
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Log.Warning("【定期自动备份】备份完成!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning($"【定期自动备份】备份发生错误:{ex.Message}");
|
||||
}
|
||||
|
||||
SqlHelper.NewEventLog("系统自动备份操作", "SYSTEM");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// SQL备份-调度任务表
|
||||
/// </summary>
|
||||
public static void SQLTaskBackup(string yearOrMonthOrDay,int count,bool delete)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 连接字符串
|
||||
string connectionString = SqlHelper.ConStr;
|
||||
|
||||
// 指定备份文件的路径
|
||||
string backupPath = $"{rootPath}\\{dbName}_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.bak";
|
||||
|
||||
// SQL 查询备份数据库
|
||||
string backupQuery = $"BACKUP DATABASE FT_WCS TO DISK = '{backupPath}' WITH COPY_ONLY, STATS = 10;" +
|
||||
$"SELECT * FROM TASK_HISTORY WHERE CREATETIME >= DATEADD({yearOrMonthOrDay}, {count}, GETDATE())";
|
||||
|
||||
|
||||
GetOrCreateDirectory(backupPath);
|
||||
|
||||
// 打开数据库连接
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// 执行备份查询
|
||||
using (SqlCommand command = new SqlCommand(backupQuery, connection))
|
||||
{
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
//选择是否删除已备份的数据
|
||||
if (delete)
|
||||
{
|
||||
SQLClearByTime("TASK_HISTORY", yearOrMonthOrDay, count);
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("数据库备份成功!");
|
||||
|
||||
Log.Warning($"【手动动备份】备份完成!");
|
||||
}
|
||||
|
||||
SqlHelper.NewEventLog("发生了手动备份操作", "USER");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"数据库备份失败: {ex.Message}");
|
||||
|
||||
Log.Warning($"【手动备份】备份发生错误:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// SQL备份-历史报警表
|
||||
/// </summary>
|
||||
public static void SQLAlarmBackup(string yearOrMonthOrDay, int count,bool delete)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 连接字符串
|
||||
string connectionString = SqlHelper.ConStr;
|
||||
|
||||
// 指定备份文件的路径
|
||||
string backupPath = $"{rootPath}\\{dbName}_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.bak";
|
||||
|
||||
// SQL 查询备份数据库
|
||||
string backupQuery = $"BACKUP DATABASE FT_WCS TO DISK = '{backupPath}' WITH COPY_ONLY, STATS = 10;" +
|
||||
$"SELECT * FROM ALARM_LOG WHERE GEN_TIME >= DATEADD({yearOrMonthOrDay}, {count}, GETDATE())";
|
||||
|
||||
GetOrCreateDirectory(backupPath);
|
||||
|
||||
// 打开数据库连接
|
||||
using (SqlConnection connection = new SqlConnection(connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
|
||||
// 执行备份查询
|
||||
using (SqlCommand command = new SqlCommand(backupQuery, connection))
|
||||
{
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
//选择是否删除已备份的数据
|
||||
if (delete)
|
||||
{
|
||||
SQLClearByTime("ALARM_LOG", yearOrMonthOrDay, count);
|
||||
}
|
||||
}
|
||||
|
||||
MessageBox.Show("数据库备份成功!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"数据库备份失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除SQL数据
|
||||
/// </summary>
|
||||
public static void SQLClearByTime(string tableName, string yearOrMonthOrDay, int count)
|
||||
{
|
||||
SqlHelper.ExecuteNonQuery($"DELETE FROM {tableName} WHERE GEN_TIME >= DATEADD({yearOrMonthOrDay}, {count}, GETDATE()");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查找文件夹(不存在则创建)
|
||||
/// 查找后为文件夹设定权限-完全控制
|
||||
/// </summary>
|
||||
/// <param name="backupPath"></param>
|
||||
private static void GetOrCreateDirectory(string backupPath)
|
||||
{
|
||||
// 检查备份路径是否存在,如果不存在则创建
|
||||
if (!Directory.Exists(Path.GetDirectoryName(backupPath)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(backupPath));
|
||||
|
||||
try
|
||||
{
|
||||
// 定义文件夹路径
|
||||
string folderPath = rootPath;
|
||||
|
||||
// 获取文件夹信息
|
||||
DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
|
||||
|
||||
// 获取当前文件夹的安全性设置
|
||||
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
|
||||
|
||||
// 定义 "Authenticated Users" 用户组
|
||||
NTAccount authenticatedUsers = new NTAccount("Authenticated Users");
|
||||
|
||||
// 定义权限(这里使用读取和执行权限)
|
||||
FileSystemRights fileSystemRights = FileSystemRights.FullControl;
|
||||
|
||||
// 定义授权类型
|
||||
InheritanceFlags inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
|
||||
PropagationFlags propagationFlags = PropagationFlags.None;
|
||||
AccessControlType accessControlType = AccessControlType.Allow;
|
||||
|
||||
// 创建权限规则
|
||||
FileSystemAccessRule rule = new FileSystemAccessRule(authenticatedUsers, fileSystemRights, inheritanceFlags, propagationFlags, accessControlType);
|
||||
|
||||
// 添加权限规则到文件夹的安全性设置
|
||||
directorySecurity.AddAccessRule(rule);
|
||||
|
||||
// 设置新的安全性设置到文件夹
|
||||
directoryInfo.SetAccessControl(directorySecurity);
|
||||
|
||||
//Console.WriteLine("权限已成功添加到文件夹: " + folderPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Console.WriteLine("添加权限失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
323
WPFWCSDemo/Utilities/SqlHelper.cs
Normal file
323
WPFWCSDemo/Utilities/SqlHelper.cs
Normal file
@@ -0,0 +1,323 @@
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Xml;
|
||||
using WPFWCSDemo.Data;
|
||||
using WPFWCSDemo.Models.Tables;
|
||||
using WPFWCSDemo.StateCode;
|
||||
using WPFWCSDemo.ViewModels;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
public class SqlHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// sql连接字符串,引用自【app.config】
|
||||
/// </summary>
|
||||
public static string? ConStr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 检查数据库是否连接正常
|
||||
/// </summary>
|
||||
/// <returns>true-连接正常 false-连接失败</returns>
|
||||
public static bool IsDBConnected()
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
return context.Database.CanConnect();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取数据集
|
||||
/// </summary>
|
||||
/// <param name="cmdText">sql执行语句</param>
|
||||
/// <param name="sqlParameters">可选的sql参数</param>
|
||||
/// <returns>查询目标的数据集</returns>
|
||||
public static DataSet GetDataSet(string cmdText, params SqlParameter[] sqlParameters)
|
||||
{
|
||||
//创建sql连接
|
||||
using SqlConnection conn = new SqlConnection(ConStr);
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using SqlCommand cmd = new SqlCommand(cmdText, conn);
|
||||
cmd.Parameters.AddRange(sqlParameters);
|
||||
using SqlDataAdapter sda = new SqlDataAdapter(cmd);
|
||||
using DataSet ds = new DataSet();
|
||||
sda.Fill(ds);
|
||||
//TableRefresh(cmdText);
|
||||
|
||||
return ds;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取对应SQL查询的数据表
|
||||
/// </summary>
|
||||
/// <param name="cmdText">sql执行语句</param>
|
||||
/// <returns>查询目标的数据表名</returns>
|
||||
public static DataTable? ExecuteTable(string cmdText, params SqlParameter[] sqlParameters)
|
||||
{
|
||||
return GetDataSet(cmdText, sqlParameters).Tables[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取某个表字段的集合
|
||||
/// </summary>
|
||||
/// <param name="cmdText">sql执行语句</param>
|
||||
/// <returns>查询目标表字段/列名的值的集合</returns>
|
||||
public static List<string> GetColumns(string cmdText)
|
||||
{
|
||||
//创建用于存储列名的集合
|
||||
List<string> columns = new List<string>();
|
||||
|
||||
//创建sql连接
|
||||
using (SqlConnection conn = new SqlConnection(ConStr))
|
||||
{
|
||||
try
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
SqlCommand cmd = new SqlCommand(cmdText, conn);
|
||||
SqlDataAdapter sda = new SqlDataAdapter(cmdText, conn);
|
||||
DataSet ds = new DataSet();
|
||||
sda.Fill(ds);
|
||||
|
||||
//将获取到的字段名添加到集合中
|
||||
foreach (DataColumn colName in ds.Tables[0].Columns)
|
||||
{
|
||||
columns.Add(colName.ToString());
|
||||
}
|
||||
|
||||
return columns;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 操作获取受影响的行数
|
||||
/// </summary>
|
||||
/// <param name="cmdText">sql语句</param>
|
||||
/// <param name="sqlParameters">sql字段参数</param>
|
||||
/// <returns></returns>
|
||||
public static int ExecuteNonQuery(string cmdText, params SqlParameter[] sqlParameters)
|
||||
{
|
||||
//创建sql连接
|
||||
using (SqlConnection conn = new SqlConnection(ConStr))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using SqlCommand cmd = new SqlCommand(cmdText, conn);
|
||||
|
||||
//sql参数化设置
|
||||
cmd.Parameters.AddRange(sqlParameters);
|
||||
//MessageBox.Show(cmdText);
|
||||
//返回执行sql受影响的行数
|
||||
int rows = cmd.ExecuteNonQuery();
|
||||
if (rows > -1)
|
||||
{
|
||||
//throw new Exception("获取数据失败!(错误代码:000001)");
|
||||
}
|
||||
|
||||
//TableRefresh(cmdText);
|
||||
|
||||
return rows;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取查询数据的行数
|
||||
/// </summary>
|
||||
/// <param name="cmdText">SQL字符串</param>
|
||||
/// <returns>目标行数</returns>
|
||||
public static int GetRowCount(string cmdText, params SqlParameter[] sqlParameters)
|
||||
{
|
||||
using (DataTable dt = ExecuteTable(cmdText, sqlParameters))
|
||||
{
|
||||
if (dt == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取为当前任务【自动生成】的WCS本地任务号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int GetTaskIdByAutoGeneration()
|
||||
{
|
||||
using DataTable dt = ExecuteTable("SELECT TASKNO FROM TASKNUMBER");
|
||||
|
||||
int taskId = (int)dt.Rows[0][0];
|
||||
|
||||
//此处定义9开头的任务号为手动添加的任务,因此自动生成的任务号小于9000
|
||||
if (taskId > 899999)
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET TASKNO=@TaskNo WHERE TASKNO=@TaskId",
|
||||
new SqlParameter("@taskNo", 100001),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
return 100001;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET TASKNO=@TaskNo WHERE TASKNO=@TaskId",
|
||||
new SqlParameter("@taskNo", taskId + 1),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
}
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取为当前任务【手动添加】的WCS本地任务号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int GetTaskIdByManualGeneration()
|
||||
{
|
||||
using DataTable dt = ExecuteTable("SELECT MAN_TASKNO FROM TASKNUMBER");
|
||||
|
||||
int taskId = (int)dt.Rows[0][0];
|
||||
|
||||
//此处定义9开头的任务号为手动添加的任务
|
||||
if (taskId > 999999)
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET MAN_TASKNO=@Man_TaskNo WHERE MAN_TASKNO=@TaskId",
|
||||
new SqlParameter("@Man_TaskNo", 900000),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
return 900000;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET MAN_TASKNO=@Man_TaskNo WHERE MAN_TASKNO=@TaskId",
|
||||
new SqlParameter("@Man_TaskNo", taskId + 1),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
}
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取为当前事件【手动添加】的编号
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int GetEventIdByAutoGeneration()
|
||||
{
|
||||
using DataTable? dt = ExecuteTable("SELECT EVENT_TASKNO FROM TASKNUMBER");
|
||||
|
||||
int taskId = (int)dt.Rows[0][0];
|
||||
|
||||
//此处定义9开头的任务号为手动添加的任务
|
||||
if (taskId > 9999)
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET EVENT_TASKNO=@Event_TaskNo WHERE EVENT_TASKNO=@TaskId",
|
||||
new SqlParameter("@Event_TaskNo", 1001),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
return 1001;
|
||||
}
|
||||
else
|
||||
{
|
||||
ExecuteNonQuery("UPDATE TASKNUMBER SET EVENT_TASKNO=@Event_TaskNo WHERE EVENT_TASKNO=@TaskId",
|
||||
new SqlParameter("@Event_TaskNo", taskId + 1),
|
||||
new SqlParameter("@taskId", taskId));
|
||||
}
|
||||
|
||||
return taskId;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 记录当前操作事件
|
||||
/// </summary>
|
||||
/// <param name="message">信息</param>
|
||||
/// <param name="userId">标识</param>
|
||||
/// <param name="type">类型(登录、操作等)</param>
|
||||
/// <param name="source">来源(名称、数据集等)</param>
|
||||
/// <param name="level">级别</param>
|
||||
/// <param name="status">状态(是否完成等)</param>
|
||||
public static void NewEventLog(string message,string userId, string type, string source, string level, string status)
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
Event_Log event_Log = new Event_Log()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Message = message,
|
||||
TimeStamp = DateTime.Now.ToString(TimeFormat.stringFormat),
|
||||
UserId = userId,
|
||||
Type = type,
|
||||
Source = source,
|
||||
Level = level,
|
||||
Status = status,
|
||||
};
|
||||
|
||||
context.Event_Log.Add(event_Log);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
Log.Warning($"【事件记录】{JsonConvert.SerializeObject(event_Log)}");
|
||||
}
|
||||
/// <summary>
|
||||
/// 记录当前操作事件
|
||||
/// </summary>
|
||||
/// <param name="message">信息</param>
|
||||
/// <param name="userId">标识</param>
|
||||
/// <param name="type">类型(登录、操作等)</param>
|
||||
/// <param name="source">来源(名称、数据集等)</param>
|
||||
/// <param name="level">级别</param>
|
||||
/// <param name="status">状态(是否完成等)</param>
|
||||
public static void NewEventLog(string message, string userId)
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
Event_Log event_Log = new Event_Log()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Message = message,
|
||||
TimeStamp = DateTime.Now.ToString(TimeFormat.stringFormat),
|
||||
UserId = userId,
|
||||
};
|
||||
|
||||
context.Event_Log.Add(event_Log);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
Log.Warning($"【事件记录】{event_Log.UserId}-{event_Log.Message}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
139
WPFWCSDemo/Utilities/Table/AlarmManager.cs
Normal file
139
WPFWCSDemo/Utilities/Table/AlarmManager.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WPFWCSDemo.StateCode;
|
||||
using WPFWCSDemo.Models.Tables;
|
||||
using WPFWCSDemo.Data;
|
||||
|
||||
namespace WPFWCSDemo.Utilities.Table
|
||||
{
|
||||
public class AlarmManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取【实时报警】表所有行数据集合
|
||||
/// </summary>
|
||||
/// <returns>返回所有行的数据</returns>
|
||||
public static List<Alarm> ListAll()
|
||||
{
|
||||
|
||||
using (DataTable? dt = SqlHelper.ExecuteTable("SELECT * FROM ALARM ORDER BY GEN_TIME DESC"))
|
||||
{
|
||||
if (dt == null) return null;
|
||||
|
||||
List<Alarm> alarms = new List<Alarm>();
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
alarms.Add(ToModels(dr));
|
||||
}
|
||||
#region 反射方法
|
||||
/*
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
tasks.Add(dr.DataRowToModel<ScheduleManager>());
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
return alarms;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将【调度管理】表的单行转换为实体数据对象
|
||||
/// </summary>
|
||||
/// <param name="dr">获取表的某行数据</param>
|
||||
/// <returns>当前行所有字段的集合</returns>
|
||||
public static Alarm ToModels(DataRow dr)
|
||||
{
|
||||
Alarm alarm = new Alarm();
|
||||
alarm.Point_Id = dr["Point_Id"].ToString();
|
||||
alarm.Description = dr["Description"].ToString();
|
||||
alarm.Priority = dr["Priority"].ToString();
|
||||
alarm.Device_Id = dr["Device_Id"].ToString();
|
||||
alarm.Gen_Time = Convert.ToDateTime(dr["Generation_Time"]);
|
||||
|
||||
|
||||
return alarm;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 向【实时报警】表中插入数据
|
||||
/// </summary>
|
||||
/// <param name="alarm"></param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int Insert(Alarm alarm)
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
int count = context.Set<Alarm>()
|
||||
.Where(a => a.Point_Id == alarm.Point_Id)
|
||||
.Count();
|
||||
|
||||
if (count > 0) return 0;
|
||||
|
||||
alarm.Gen_Time = DateTime.Now;
|
||||
context.Set<Alarm>()
|
||||
.Add(alarm);
|
||||
|
||||
if (context.SaveChanges() == 1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从【实时报警】表中移除数据记入历史表
|
||||
/// </summary>
|
||||
/// <param name="alarm">当前已结束的报警</param>
|
||||
/// <returns>执行后受影响的行数</returns>
|
||||
public static int Delete(Alarm alarm)
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
int count = context.Set<Alarm>()
|
||||
.Where(a => a.Point_Id == alarm.Point_Id)
|
||||
.Count();
|
||||
|
||||
if(count == 0) return 0;
|
||||
|
||||
AlarmHistory alarmHistory = new AlarmHistory()
|
||||
{
|
||||
Point_Id = alarm.Point_Id,
|
||||
Description = alarm.Description,
|
||||
Priority = alarm.Priority,
|
||||
Device_Id = alarm.Device_Id,
|
||||
Gen_Time = alarm.Gen_Time,
|
||||
End_Time = DateTime.Now,
|
||||
};
|
||||
|
||||
context.Set<AlarmHistory>()
|
||||
.Add(alarmHistory);
|
||||
|
||||
context.Set<Alarm>()
|
||||
.Remove(alarm);
|
||||
|
||||
return context.SaveChanges();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
82
WPFWCSDemo/Utilities/Table/TaskHistoryManager.cs
Normal file
82
WPFWCSDemo/Utilities/Table/TaskHistoryManager.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WPFWCSDemo.Models.Tables;
|
||||
|
||||
namespace WPFWCSDemo.Utilities.Table
|
||||
{
|
||||
public class TaskHistoryManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取表数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<TaskHistory> ListAll()
|
||||
{
|
||||
|
||||
using (DataTable? dt = SqlHelper.ExecuteTable("SELECT * FROM TASK_HISTORY ORDER BY CREATETIME DESC"))
|
||||
{
|
||||
if (dt == null || dt.Rows.Count == 0) return null;
|
||||
|
||||
List<TaskHistory> scheduleHistoryManagers = new List<TaskHistory>();
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
scheduleHistoryManagers.Add(ToModels(dr));
|
||||
}
|
||||
|
||||
#region 反射方法
|
||||
/*
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
tasks.Add(dr.DataRowToModel<ScheduleManager>());
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
|
||||
return scheduleHistoryManagers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将表行转换成实体数据
|
||||
/// </summary>
|
||||
/// <param name="dr"></param>
|
||||
/// <returns></returns>
|
||||
public static TaskHistory ToModels(DataRow dr)
|
||||
{
|
||||
if (dr == null) return null;
|
||||
|
||||
TaskHistory taskHistory = new TaskHistory();
|
||||
|
||||
taskHistory.JobId = dr["jobId"].ToString();
|
||||
taskHistory.TaskId = dr["TaskId"].ToString();
|
||||
taskHistory.PalletId = dr["PalletId"].ToString();
|
||||
taskHistory.SourceLocation = dr["SourceLocation"].ToString();
|
||||
taskHistory.TargetLocation = dr["TargetLocation"].ToString();
|
||||
taskHistory.TaskType = dr["TaskType"].ToString();
|
||||
taskHistory.TaskState = dr["TaskState"].ToString();
|
||||
//scheduleHistoryManager.ReportState = (int)dr["ReportResult"];
|
||||
taskHistory.ScheduleState = dr["ScheduleState"].ToString();
|
||||
taskHistory.CreateTime = Convert.ToDateTime(dr["ScheduleTime"]);
|
||||
taskHistory.FinishTime = Convert.ToDateTime(dr["FinishTime"]);
|
||||
//try
|
||||
//{
|
||||
// taskHistory.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
|
||||
// taskHistory.FinishTime = Convert.ToDateTime(dr["FinishTime"]);
|
||||
//}
|
||||
//catch (Exception)
|
||||
//{
|
||||
// //MessageBox.Show("查询的时间内容不能为空!(错误代码:3001)");
|
||||
//}
|
||||
|
||||
|
||||
return taskHistory;
|
||||
}
|
||||
}
|
||||
}
|
||||
184
WPFWCSDemo/Utilities/Table/TaskManager.cs
Normal file
184
WPFWCSDemo/Utilities/Table/TaskManager.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WPFWCSDemo.Data;
|
||||
using WPFWCSDemo.Models.Tables;
|
||||
using WPFWCSDemo.StateCode;
|
||||
|
||||
namespace WPFWCSDemo.Utilities.Table
|
||||
{
|
||||
public class TaskManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取【调度管理】表所有行数据集合
|
||||
/// </summary>
|
||||
/// <returns>返回所有行的数据</returns>
|
||||
public static List<TaskRT> ListAll()
|
||||
{
|
||||
|
||||
using (DataTable? dt = SqlHelper.ExecuteTable("SELECT * FROM TASK ORDER BY CREATETIME DESC"))
|
||||
{
|
||||
if (dt == null || dt.Rows.Count == 0) return null;
|
||||
|
||||
List<TaskRT> taskRTs = new List<TaskRT>();
|
||||
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
taskRTs.Add(ToModels(dr));
|
||||
}
|
||||
|
||||
#region 反射方法
|
||||
/*
|
||||
foreach (DataRow dr in dt.Rows)
|
||||
{
|
||||
tasks.Add(dr.DataRowToModel<ScheduleManager>());
|
||||
}
|
||||
*/
|
||||
#endregion
|
||||
|
||||
return taskRTs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将【调度管理】表的单行转换为实体数据对象
|
||||
/// </summary>
|
||||
/// <param name="dr">获取表的某行数据</param>
|
||||
/// <returns>当前行所有字段的集合</returns>
|
||||
public static TaskRT? ToModels(DataRow dr)
|
||||
{
|
||||
TaskRT taskRT = new TaskRT();
|
||||
|
||||
try
|
||||
{
|
||||
taskRT.JobId = dr["jobId"].ToString();
|
||||
taskRT.TaskId = dr["TaskId"].ToString();
|
||||
//taskRT.PalletId = dr["PalletId"].ToString();
|
||||
//taskRT.Priority = (int)dr["Priority"];
|
||||
taskRT.SourceLocation = dr["SourceLocation"].ToString();
|
||||
taskRT.TargetLocation = dr["TargetLocation"].ToString();
|
||||
taskRT.TaskType = (int)dr["TaskType"];
|
||||
taskRT.TaskState = dr["TaskState"].ToString();
|
||||
taskRT.ScheduleState = dr["ScheduleState"].ToString();
|
||||
taskRT.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
|
||||
taskRT.ScheduleTime = Convert.ToDateTime(dr["ScheduleTime"]);
|
||||
//taskRT.ReportState = (int)dr["ReportState"];
|
||||
|
||||
return taskRT;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Warning($"【TaskManager】实体类型转换失败:{e.Message}");
|
||||
Log.Warning($"【TaskManager】{JsonConvert.SerializeObject(taskRT)}");
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 向【调度管理】表中插入数据
|
||||
/// </summary>
|
||||
/// <param name="scheduleManager"></param>
|
||||
/// <returns>返回受影响的行数</returns>
|
||||
public static int Insert(TaskRT taskRT, int type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case 1:
|
||||
return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,PalletId,SourceLocation,TARGET,TASKTYPE)" +
|
||||
"VALUES(@TaskId,@PalletId,@SourceLocation,@Target,@TaskType)",
|
||||
new SqlParameter("@TaskId", taskRT.TaskId),
|
||||
new SqlParameter("@PalletId", taskRT.PalletId),
|
||||
new SqlParameter("@SourceLocation", taskRT.SourceLocation),
|
||||
new SqlParameter("@TargetLocation", taskRT.TargetLocation),
|
||||
new SqlParameter("@TaskType", taskRT.TaskType)
|
||||
);
|
||||
|
||||
case 2:
|
||||
return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,SourceLocation,TARGETLOCATION,TASKTYPE)" +
|
||||
"VALUES(@TaskId,@Origin,@Target,@TaskType)",
|
||||
new SqlParameter("@TaskId", taskRT.TaskId),
|
||||
new SqlParameter("@SourceLocation", taskRT.SourceLocation),
|
||||
new SqlParameter("@TargetLocation", taskRT.TargetLocation),
|
||||
new SqlParameter("@TaskType", taskRT.TaskType)
|
||||
);
|
||||
|
||||
case 3:
|
||||
return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,SourceLocation,TARGETLOCATION,TASKTYPE)" +
|
||||
"VALUES(@TaskId,@Origin,@Target,@TaskType)",
|
||||
new SqlParameter("@TaskId", taskRT.TaskId),
|
||||
new SqlParameter("@SourceLocation", taskRT.SourceLocation),
|
||||
new SqlParameter("@TargetLocation", taskRT.TargetLocation),
|
||||
new SqlParameter("@TaskType", taskRT.TaskType)
|
||||
);
|
||||
default:
|
||||
Log.Error("[Schedule]下发任务错误[Schedule]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从【调度管理】表中移除数据记入历史表
|
||||
/// </summary>
|
||||
/// <param name="scheduleManager">当前已完成的任务</param>
|
||||
/// <returns>执行后受影响的行数</returns>
|
||||
public static int Delete(TaskRT taskRT)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var context = new AppDbContext();
|
||||
|
||||
TaskHistory taskHistory = new TaskHistory()
|
||||
{
|
||||
JobId = taskRT.JobId,
|
||||
TaskId = taskRT.TaskId,
|
||||
//PalletId = taskRT.PalletId,
|
||||
TaskType = taskRT.TaskType.ToString(),
|
||||
TaskState = taskRT.TaskState,
|
||||
ScheduleState = taskRT.ScheduleState,
|
||||
SourceLocation = taskRT.SourceLocation,
|
||||
TargetLocation = taskRT.TargetLocation,
|
||||
CreateTime = taskRT.CreateTime,
|
||||
ScheduleTime = taskRT.ScheduleTime,
|
||||
FinishTime = DateTime.Now,
|
||||
Id = Guid.NewGuid(),
|
||||
};
|
||||
|
||||
//1--------------向【调度历史】表添加数据
|
||||
context.TaskHistories.Add(taskHistory);
|
||||
|
||||
//2--------------移除【调度管理】表数据
|
||||
context.Remove(taskRT);
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
return 1;
|
||||
}
|
||||
catch (DBConcurrencyException e)
|
||||
{
|
||||
Log.Error($"【WCS移除已完成任务<异常>】任务号:{taskRT.TaskId},{e.Message}");
|
||||
Log.Error($"【WCS移除已完成任务<异常>】任务号:{taskRT.TaskId},{e.InnerException}");
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
19
WPFWCSDemo/Utilities/UserAccessControl.cs
Normal file
19
WPFWCSDemo/Utilities/UserAccessControl.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WPFWCSDemo.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户权限管理
|
||||
/// </summary>
|
||||
public class UserAccessControl
|
||||
{
|
||||
public static void CheckAccess()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user