|
|
using Custom.Utils.Framework;
|
|
|
using ProductionSystem_Log;
|
|
|
using ProductionSystem_Model.DbModel.Para;
|
|
|
using ProductionSystem_Model.DbModel.Protocol;
|
|
|
using ProductionSystem_Model.Enum;
|
|
|
using ProductionSystem_Model.ViewModel.Request.Protocol;
|
|
|
using ProductionSystem_Model.ViewModel.Response.Protocol;
|
|
|
using SqlSugar;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
namespace ProductionSystem_Service
|
|
|
{
|
|
|
public class ProtocolConfigService : DbContext
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 分页查询CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="v"></param>
|
|
|
/// <param name="totalCount"></param>
|
|
|
/// <returns></returns>
|
|
|
public List<ProtocolVM> GetProtocolPageList(QueryProtocolVM queryProtocolVM, out int totalCount)
|
|
|
{
|
|
|
totalCount = 0;
|
|
|
try
|
|
|
{
|
|
|
var list = db.Queryable<T_Protocol_Config>()
|
|
|
.WhereIF(queryProtocolVM != null && !string.IsNullOrWhiteSpace(queryProtocolVM.KeyWord)
|
|
|
, a => a.ProtocolCode.Contains(queryProtocolVM.KeyWord) || a.ProtocolName.Contains(queryProtocolVM.KeyWord))
|
|
|
.Select((a) => new ProtocolVM
|
|
|
{
|
|
|
Id = a.Id,
|
|
|
SerialNumber = SqlFunc.RowNumber(SqlFunc.Desc(a.CreatedTime)),
|
|
|
ProtocolType = a.ProtocolType,
|
|
|
ProtocolCode = a.ProtocolCode,
|
|
|
ProtocolName = a.ProtocolName,
|
|
|
FrameType = a.FrameType,
|
|
|
MsgFormat = a.MsgFormat,
|
|
|
Port = a.Port,
|
|
|
Bps = a.Bps,
|
|
|
ChkBit = a.ChkBit,
|
|
|
DataBit = a.DataBit,
|
|
|
StopBit = a.StopBit,
|
|
|
StationNo = a.StationNo,
|
|
|
SortIndex = a.SortIndex,
|
|
|
IsActive = a.IsActive,
|
|
|
StrIsActive = a.IsActive ? "是" : "否",
|
|
|
Remark = a.Remark,
|
|
|
CreatedTime = a.CreatedTime,
|
|
|
CreatedBy = a.CreatedBy
|
|
|
}).ToPageList(queryProtocolVM.PageIndex, queryProtocolVM.PageSize, ref totalCount);
|
|
|
if (list.Count > 0)
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
|
{
|
|
|
Enum.TryParse(list[i].FrameType, out FrameTypeEnum enumFrameType);
|
|
|
list[i].FrameTypeName = EnumHelper.GetEnumDescription(enumFrameType);
|
|
|
Enum.TryParse(list[i].MsgFormat, out MsgFormatEnum msgFormatEnum);
|
|
|
list[i].MsgFormatName = EnumHelper.GetEnumDescription(msgFormatEnum);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下GetProtocolPageList时异常");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询所有CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public ISugarQueryable<T_Protocol_Config> QueryProtocolConfigs()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Queryable<T_Protocol_Config>();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下QueryProtocolConfigs时异常");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询有效CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public ISugarQueryable<T_Protocol_Config> QueryActiveProtocolConfigs()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Queryable<T_Protocol_Config>().Where(m => m.IsActive);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下QueryActiveProtocolConfigs时异常");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据Id查询CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <returns></returns>
|
|
|
public T_Protocol_Config QueryProtocolConfigById(string id)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Queryable<T_Protocol_Config>().First(m => m.Id == id);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下QueryProtocolConfigById时异常");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据编码查询有效CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="code"></param>
|
|
|
/// <returns></returns>
|
|
|
public T_Protocol_Config QueryActiveProtocolConfigByCode(string code)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Queryable<T_Protocol_Config>().First(m => m.ProtocolCode == code && m.IsActive);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下QueryActiveProtocolConfigByCode时异常");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 查询最大排序索引
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public int GetMaxSortIndex()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Queryable<T_Protocol_Config>().Max(m => m.SortIndex);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下GetMaxSortIndex时异常");
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 新增CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="t_Protocol_Config"></param>
|
|
|
/// <returns></returns>
|
|
|
public int AddProtocolConfig(T_Protocol_Config t_Protocol_Config)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
return db.Insertable(t_Protocol_Config).ExecuteCommand();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogHelper.Error(ex, "执行ProtocolConfigService下AddProtocolConfig时异常");
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 修改CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="t_Protocol_Config"></param>
|
|
|
/// <param name="oldProtocolConfigCode">旧的协议通讯编码</param>
|
|
|
/// <returns></returns>
|
|
|
public int UpdateProtocolConfig(T_Protocol_Config t_Protocol_Config, string oldProtocolConfigCode)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var result = db.UseTran(() =>
|
|
|
{
|
|
|
var details = db.Queryable<T_Protocol_Detail_Config>().Where(m => m.ProtocolConfigCode == oldProtocolConfigCode).ToList();
|
|
|
if (details != null && details.Count > 0)
|
|
|
{
|
|
|
details.ForEach(m => m.ProtocolConfigCode = t_Protocol_Config.ProtocolCode);
|
|
|
db.Updateable(details).ExecuteCommand();
|
|
|
}
|
|
|
var updateRow = db.Updateable(t_Protocol_Config).ExecuteCommand();
|
|
|
if (updateRow > 0)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!result.Data) //返回值为false
|
|
|
{
|
|
|
return -1;
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
var errorMsg = $"执行ProtocolConfigService下UpdateProtocolConfig时异常:{ex.Message}";
|
|
|
LogHelper.Error(ex, errorMsg);
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 根据Id物理删除CAN/LIN协议配置
|
|
|
/// </summary>
|
|
|
/// <param name="id"></param>
|
|
|
/// <param name="protocolConfigCode"></param>
|
|
|
/// <returns></returns>
|
|
|
public (bool isok, string msg) DoDelProtocolConfigById(string id, string protocolConfigCode)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var result = db.UseTran(() =>
|
|
|
{
|
|
|
var delConfigRows = db.Deleteable<T_Protocol_Config>().Where(m => m.Id == id).ExecuteCommand();
|
|
|
var delDetailConfigRows = db.Deleteable<T_Protocol_Detail_Config>().Where(m => m.ProtocolConfigCode == protocolConfigCode).ExecuteCommand();
|
|
|
if (delConfigRows > 0 && delDetailConfigRows >= 0)
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!result.Data) //返回值为false
|
|
|
{
|
|
|
return (false, "操作失败!");
|
|
|
}
|
|
|
return (true, "操作成功!");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
var errorMsg = $"执行ProtocolConfigService下DoDelProtocolConfigById时异常:{ex.Message}";
|
|
|
LogHelper.Error(ex, errorMsg);
|
|
|
return (false, errorMsg);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|