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.
lj_plc/Actions/Default/Mesnac.Action.Default/DefaultAction.cs

409 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Data;
using System.IO;
using ICSharpCode.Core;
using Mesnac.Basic;
using Mesnac.Equips;
using Mesnac.Codd.Session;
using Mesnac.Action.Base;
namespace Mesnac.Action.Default
{
/// <summary>
/// 默认Action
/// </summary>
public class DefaultAction : DatabaseAction
{
#region 操作日志
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="userGUId">用户GUID</param>
/// <param name="userName">用户名</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public void DBLog(int shiftId, string shiftName, int classId, string className, string userGUId, string userName, string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
try
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("获取本地数据连接失败...");
return;
}
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
string sqlstr = @"INSERT INTO SysLog(GUID,ShiftID,ShiftName,ClassID,ClassName,UserGUID,UserName,FormGUID,FormText,FunctionGUID,FunctionText,PerformResult,WorkType,OperDest,Remark,RecordTime,IsUpFlag)
VALUES(@GUID,@ShiftID,@ShiftName,@ClassID,@ClassName,@UserGUID,@UserName,@FormGUID,@FormText,@FunctionGUID,@FunctionText,@PerformResult,@WorkType,@OperDest,@Remark,@RecordTime,@IsUpFlag)";
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@GUID", Guid.NewGuid().ToString().ToUpper());
dbHelper.AddParameter("@ShiftName", shiftName);
dbHelper.AddParameter("@ShiftID", shiftId);
dbHelper.AddParameter("@ClassID", classId);
dbHelper.AddParameter("@ClassName", className);
dbHelper.AddParameter("@UserGUID", userGUId);
dbHelper.AddParameter("@UserName", userName);
dbHelper.AddParameter("@FormGUID", formGUID);
dbHelper.AddParameter("@FormText", formText);
dbHelper.AddParameter("@FunctionGUID", functionGUID);
dbHelper.AddParameter("@FunctionText", functionText);
dbHelper.AddParameter("@PerformResult", performResult);
dbHelper.AddParameter("@WorkType", worktype);
dbHelper.AddParameter("@OperDest", operdest);
dbHelper.AddParameter("@Remark", remark);
dbHelper.AddParameter("@RecordTime", DateTime.Now);
dbHelper.AddParameter("@IsUpFlag", 0);
dbHelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("数据库日志记录异常:" + ex.Message, ex);
}
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public void DBLog(int shiftId, string shiftName, int classId, string className, string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, worktype, operdest, remark);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
public void DBLog(int shiftId, string shiftName, int classId, string className, string formGUID, string formText, string functionGUID, string functionText, string performResult)
{
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, formText, functionText, String.Empty);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public void DBLog(string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
int shiftId = GetSysValue("PlanLog.CurrentShiftID", 0); //获取当前班次
string shiftName = GetSysValue("PlanLog.CurrentShiftName", String.Empty); //获取当前班次名称
int classId = Mesnac.Basic.UserInfo.Instance.ClassID; //获取当前班组
string className = GetSysValue("PlanLog.CurrentClassName", String.Empty); //获取当前班组名称
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, worktype, operdest, remark);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
public void DBLog(string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype)
{
DBLog(formGUID, formText, functionGUID, functionText, performResult, worktype, formText, functionText);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
public void DBLog(string formGUID, string formText, string functionGUID, string functionText)
{
DBLog(formGUID, formText, functionGUID, functionText, formText, functionText);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="operateType">操作类别</param>
/// <param name="caption">日志标题</param>
/// <param name="content">日志内容</param>
public void DBLog(string operateType, string caption, string content)
{
DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, operateType, caption, content);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="content">日志内容</param>
public void DBLog(string content)
{
DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, this.FormText, this.FunctionText, content);
}
#endregion
#region 字典获取
#region 字典判断
/// <summary>
/// 判断SysKeyValue中是否存在对应key的记录
/// </summary>
/// <param name="key">要检索的key</param>
/// <returns>存在返回true否则返回false</returns>
public bool ExistsKey(string key)
{
try
{
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("获取SysKeyValue表中对应键的值失败获取本地数据连接失败");
return false;
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
string strSql = "select count(1) from SysKeyValue where ssKey = @ssKey";
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
object result = localHelper.ToScalar();
if (result == null || result == System.DBNull.Value || Convert.ToInt32(result) <= 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("获取SysKeyValue表中对应键的值失败" + ex.Message, ex);
return false;
}
}
#endregion
#region 获取值
/// <summary>
/// 获取本地库SysKeyValue表中对应键的值
/// </summary>
/// <param name="key">要查找的键</param>
/// <returns>返回对应键的值获取失败返回String.Empty</returns>
public string GetSysValue(string key)
{
try
{
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("获取SysKeyValue表中对应键的值失败获取本地数据连接失败");
return String.Empty;
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
string strSql = "select top 1 ssValue from SysKeyValue where ssKey = @ssKey";
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
object result = localHelper.ToScalar();
if (result == null || result == System.DBNull.Value)
{
return String.Empty;
}
else
{
return result.ToString();
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<DefaultAction>.Error("获取SysKeyValue表中对应键的值失败" + ex.Message, ex);
return String.Empty;
}
}
/// <summary>
/// 字典获取string重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public string GetSysValue(string key, string defaultValue)
{
if (ExistsKey(key))
{
string value = GetSysValue(key);
if (String.IsNullOrEmpty(value))
{
return defaultValue;
}
else
{
return value;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取int重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public int GetSysValue(string key, int defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
int intValue = defaultValue;
if (int.TryParse(strValue, out intValue))
{
return intValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取bool重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public bool GetSysValue(string key, bool defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
bool boolValue = defaultValue;
if (bool.TryParse(strValue, out boolValue))
{
return boolValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取float重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public float GetSysValue(string key, float defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
float floatValue = defaultValue;
if (float.TryParse(strValue, out floatValue))
{
return floatValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取double重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public double GetSysValue(string key, double defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
double doubleValue = defaultValue;
if (double.TryParse(strValue, out doubleValue))
{
return doubleValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
#endregion
#endregion
}
}