using Commom.Utility;
using Common;
using Model;
using Model.WcsModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace BLL.DAL
{
public class DAL_AlarmInfo
{
#region 报警维护
///
/// 根据ID获取报警信息
///
///
public WCSAlarmInfo GetAlarmInfo(string Id)
{
try
{
StringBuilder strSql = new StringBuilder();
strSql.Append($"select * from WCSAlarmInfo where IsDel = 0 and Id = '{Id}' order by createTime;");
IDataReader dt = DataFactory.SqlDataBase().GetDataReaderBySQL(strSql);
var model = ModelConvertHelper.ReaderToModel(dt);
return model;
}
catch
{
throw new NotImplementedException();
}
}
///
/// 获取报警信息数据集合
///
/// 查询条件
/// 分页信息
///
public IList GetAlarmInfoList(AjaxAlarmInfoList Json, ref PageInfo pageInfo)
{
try
{
IList list = new List();
StringBuilder sqlString = new StringBuilder();
List para = new List();
sqlString.Append("select tb1.*,tb2.RealName as CreateUserName ");
sqlString.Append("from WCSAlarmInfo as tb1 left join UserInfo as tb2 on tb1.CreateUser = tb2.ID ");
sqlString.Append("where tb1.IsDel = 0 ");
if (!string.IsNullOrWhiteSpace(Json.AlarmCode))
{
sqlString.Append($"and tb1.AlarmCode like '%{Json.AlarmCode}%' ");
}
if (!string.IsNullOrWhiteSpace(Json.AlarmName))
{
sqlString.Append($"and tb1.AlarmName like '%{Json.AlarmName}%' ");
}
DataTable dt = DataFactory.SqlDataBase().GetPageList(sqlString.ToString(), null, "CreateTime", "DESC", ref pageInfo);
list = ModelConvertHelper.DataTableToModel(dt);
return list;
}
catch
{
throw new NotImplementedException();
}
}
///
/// 删除报警信息
///
/// ID集合
///
public bool DeleteAlarmInfo(string[] ID)
{
bool result = false;
try
{
int dt = DataFactory.SqlDataBase().IsExist("WCSAlarmInfo", "ID", ID);
if (dt >= ID.Length)
{
int i = 0;
while (i < ID.Length)
{
StringBuilder sql = new StringBuilder();
sql.Append("update WCSAlarmInfo set IsDel=1 where Id='" + ID[i] + "'");
int _ret = DataFactory.SqlDataBase().ExecuteBySql(sql);
if (_ret >= ID.Length) result = true;
i++;
}
}
return result;
}
catch
{
return result;
}
}
///
/// 新增编辑报警信息
///
/// 流程字信息
/// true:保存成功 false:保存失败
public bool AddAlarmInfo(AjaxAlarmInfo model, string loginName)
{
bool bl = false;
try
{
int rowCount = 0;
Hashtable ht = new Hashtable();
if (model.Operation == "Add")
{
// add
ht["PlcIP"] = model.PlcIP.AddQuotes();
ht["AlarmCode"] = model.AlarmCode.AddQuotes();
ht["AlarmName"] = model.AlarmName.AddQuotes();
ht["AlarmType"] = model.AlarmType.AddQuotes();
ht["LedIP"] = model.LedIP.AddQuotes();
ht["Type"] = model.Type.AddQuotes();
ht["AlarmTime"] = model.AlarmTime.AddQuotes();
ht["LedStatus"] = 0;
ht["CreateUser"] = model.CreateUser;
rowCount = DataFactory.SqlDataBase().InsertByHashtableNullParam("WCSAlarmInfo", ht);
StringBuilder sql = new StringBuilder();
sql.Append($"");
if (rowCount >= 1)
{
DAL_OperationRecord oper = new DAL_OperationRecord();
var operation = new OperationModel()
{
MenuName = "报警维护",
FkNo = model.AlarmCode + "-" + model.AlarmName,
Msg = "添加报警维护",
Type = "添加",
};
oper.AddOperation(operation, loginName);
bl = true;
}
if (rowCount >= 1)
{
DAL_AlarmLog oper = new DAL_AlarmLog();
var operation = new WCSAlarmLogModel()
{
PlcIP = model.PlcIP,
AlarmCode = model.AlarmCode,
AlarmName = model.AlarmName,
AlarmType = model.AlarmType,
LedIP = model.LedIP,
Type = model.Type,
AlarmTime = model.AlarmTime,
LedStatus = model.LedStatus,
CreateUser = model.CreateUser.ToInt(),
};
oper.AddAlarmLog(operation, loginName);
bl = true;
}
}
else
{
// Edit
ht["PlcIP"] = model.PlcIP.AddQuotes();
ht["AlarmCode"] = model.AlarmCode.AddQuotes();
ht["AlarmName"] = model.AlarmName.AddQuotes();
ht["AlarmType"] = model.AlarmType.AddQuotes();
ht["LedIP"] = model.LedIP.AddQuotes();
ht["Type"] = model.Type.AddQuotes();
ht["LedStatus"] = 0;
//ht["CreateUser"] = "";
rowCount = DataFactory.SqlDataBase().UpdateByHashtable("WCSAlarmInfo", "id", model.Id.ToString(), ht);
if (rowCount >= 1)
{
DAL_OperationRecord oper = new DAL_OperationRecord();
var operation = new OperationModel()
{
MenuName = "报警维护",
FkNo = model.AlarmCode + "-" + model.AlarmName,
Msg = "修改报警维护",
Type = "修改",
};
oper.AddOperation(operation, loginName);
bl = true;
}
if (rowCount >= 1)
{
DAL_AlarmLog oper = new DAL_AlarmLog();
var operation = new WCSAlarmLogModel()
{
PlcIP = model.PlcIP,
AlarmCode = model.AlarmCode,
AlarmName = model.AlarmName,
AlarmType = model.AlarmType,
LedIP = model.LedIP,
Type = model.Type,
AlarmTime = model.AlarmTime,
LedStatus = model.LedStatus,
CreateUser = model.CreateUser.ToInt(),
};
oper.AddAlarmLog(operation, loginName);
bl = true;
}
}
if (rowCount == 1)
{
bl = true;
}
}
catch (Exception)
{
bl = false;
}
return bl;
}
///
/// 获取设备下拉框 (此方法不启用 liudl)
///
/// 工位ID
///
public string GetAlarmInfoHtml(string PlcInfoId = "")
{
try
{
StringBuilder sb = new StringBuilder();
string resMenuTemplate = "";
sb.AppendFormat(resMenuTemplate, "", "", "选择");
StringBuilder sqlString = new StringBuilder();
sqlString.Append("select * from WCSIP where IsDel = 0;");
DataTable dt = DataFactory.SqlDataBase().GetDataTableBySQL(sqlString);
foreach (DataRow row in dt.Rows)
{
sb.AppendFormat(resMenuTemplate, row["Id"].ToString(), row["Id"].ToString() == PlcInfoId ? "selected='selected'" :
string.Empty, row["Type"].ToString() + "-" + row["Text"].ToString());
}
return sb.ToString();
}
catch
{
return "";
}
}
///
/// 验证是否存在重复项
///
/// 查询条件
///
public bool IsExist(string sqlWhere)
{
return DataFactory.SqlDataBase().IsExist("WCSAlarmInfo", sqlWhere);
}
#endregion
#region 报警日志
///
/// 获取报警日志数据集合
///
/// 查询条件
/// 分页信息
///
public IList GetAlarmLogList(AjaxAlarmInfoList Json, ref PageInfo pageInfo)
{
try
{
IList list = new List();
StringBuilder sqlString = new StringBuilder();
List para = new List();
sqlString.Append("select tb1.*,tb2.RealName as CreateUserName ");
sqlString.Append("from WCSAlarmLog as tb1 left join UserInfo as tb2 on tb1.CreateUser = tb2.ID ");
sqlString.Append("where tb1.IsDel = 0 ");
if (!string.IsNullOrWhiteSpace(Json.AlarmCode))
{
sqlString.Append($"and tb1.AlarmCode like '%{Json.AlarmCode}%' ");
}
if (!string.IsNullOrWhiteSpace(Json.AlarmName))
{
sqlString.Append($"and tb1.AlarmName like '%{Json.AlarmName}%' ");
}
if (!string.IsNullOrWhiteSpace(Json.BeAlarmTime))
{
sqlString.Append($"and AlarmTime >= '{Json.BeAlarmTime}' ");
}
if (!string.IsNullOrWhiteSpace(Json.EnAlarmTime))
{
sqlString.Append($"and AlarmTime <= '{Json.EnAlarmTime}' ");
}
DataTable dt = DataFactory.SqlDataBase().GetPageList(sqlString.ToString(), null, "CreateTime", "DESC", ref pageInfo);
list = ModelConvertHelper.DataTableToModel(dt);
return list;
}
catch
{
throw new NotImplementedException();
}
}
#endregion
}
}