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.
ProductionSystem/ProductionSystem_Service/ParaConfigService.cs

348 lines
16 KiB
C#

12 months ago
using ProductionSystem_Log;
11 months ago
12 months ago
using ProductionSystem_Model.DbModel.Para;
using ProductionSystem_Model.DbModel.Protocol;
using ProductionSystem_Model.Enum;
12 months ago
using ProductionSystem_Model.ViewModel.Response.Para;
11 months ago
12 months ago
using SqlSugar;
11 months ago
12 months ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
12 months ago
namespace ProductionSystem_Service
{
public class ParaConfigService : DbContext
{
ProtocolDetailConfigService _protocolDetailConfigService = new ProtocolDetailConfigService();
12 months ago
/// <summary>
/// 查询所有参数配置项
/// </summary>
/// <returns></returns>
public ISugarQueryable<T_ParaConfig> QueryParaConfigs()
{
try
{
return db.Queryable<T_ParaConfig>();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryParaConfigs时异常");
return null;
}
}
/// <summary>
/// 查询有效参数配置项
/// </summary>
/// <returns></returns>
public ISugarQueryable<T_ParaConfig> QueryActiveParaConfigs()
{
try
{
return db.Queryable<T_ParaConfig>().Where(m => m.IsActive);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryActiveParaConfigs时异常");
return null;
}
}
/// <summary>
/// 根据参数类别查询有效参数配置项
/// </summary>
/// <param name="paraCategory"></param>
/// <returns></returns>
public List<ParaConfigVM> QueryActiveParaConfigsByParaCategory(string paraCategory)
{
try
{
return db.Queryable<T_ParaConfig>()
11 months ago
.InnerJoin<T_ParaColumnConfig>((a, b) => a.ProductType == b.ProductType && a.ParaCode == b.ParaCode && a.ParaCategory == b.ParaCategory && a.IsActive && b.IsActive)
.WhereIF(!string.IsNullOrEmpty(paraCategory), a => a.ParaCategory == paraCategory)
12 months ago
.Select((a, b) => new ParaConfigVM
{
Id = a.Id,
ParaCode = a.ParaCode,
ParaName = b.ParaName
}).ToList();
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryActiveParaConfigsByParaCategory时异常");
return null;
}
}
12 months ago
/// <summary>
/// 根据产品型号查询有效参数配置项
/// </summary>
/// <param name="productType"></param>
/// <returns></returns>
public ISugarQueryable<T_ParaConfig> QueryActiveParaConfigsByProductType(string productType)
{
try
{
//var temp = db.Queryable<T_ParaConfig>().Where(m => m.ProductType == productType && m.IsActive);
12 months ago
return db.Queryable<T_ParaConfig>().Where(m => m.ProductType == productType && m.IsActive);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryActiveParaConfigsByProductType时异常");
return null;
}
}
11 months ago
private ISugarQueryable<T_ParaConfig> QueryTargetValWithParaCodeByParaCategery(string paraCategory,string productType)
{
try
{
var temp1 = db.Queryable<T_ParaConfig>().Where(m => m.ParaCategory == paraCategory && m.ProductType == productType && m.IsActive);
return temp1;
11 months ago
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryTargetValWithParaCodeByParaCategery时异常");
return null;
}
}
11 months ago
private T_ParaColumnConfig QueryLinByParaCategeryAndParaCode(string paraCategory, string paraCode)
{
11 months ago
T_ParaColumnConfig temp1 = db.Queryable<T_ParaColumnConfig>().
Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive).
First();
if (temp1 == null)
{
throw new Exception("未维护数据!请先维护!");
}
else
{
var temp2 = db.Queryable<T_ParaColumnConfig>().
Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive).
First().ProtocolType;
if (temp2 == null)
{
temp2 = "未配置通讯方式";
}
return db.Queryable<T_ParaColumnConfig>().Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive).First();
11 months ago
}
}
/// <summary>
/// 串口
/// </summary>
/// <param name="lin"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private T_Protocol_Config QueryComByLin(string lin)
{
T_Protocol_Config temp1 = db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First();
if (temp1 == null)
{
throw new Exception("未维护数据!请先维护!");
}
string temp3 = db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First().ProtocolType;
if (temp3 == null)
{
temp3 = "未配置串口";
}
return db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First();
}
/// <summary>
/// 波特率
/// </summary>
/// <param name="lin"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private T_Protocol_Config QueryBpsByCode(string lin)
{
T_Protocol_Config temp1 = db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First();
if (temp1 == null)
{
throw new Exception("未维护数据!请先维护!");
}
string temp3 = db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First().ProtocolType;
if (temp3 == null)
{
temp3 = "未配置波特率";
}
return db.Queryable<T_Protocol_Config>().Where(m => m.ProtocolCode == lin && m.IsActive).First();
}
/// <summary>
/// 报文
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
private List<T_Protocol_Detail_Config> QueryProtocolConfigsByCode(string code)
{
return db.Queryable<T_Protocol_Detail_Config>().Where(m => m.ProtocolConfigCode == code && m.IsActive).ToList();
}
/// <summary>
/// 根据参数种类查询有效参数配置项,例如传“BZ1_DB”、“BZ2_DB”
/// </summary>
/// <param name="paraCategory"></param>
/// <returns></returns>
public EditonType GetParaTargetVal(ModeTypeEnum modeTypeEnum,string productType)
{
EditonType editonType = new EditonType();
var paraConfig = QueryTargetValWithParaCodeByParaCategery(modeTypeEnum.ToString(),productType).ToList();
for (int i = 0; i < paraConfig.Count; i++)
{
11 months ago
if (paraConfig[i].ParaCode == "FKSTF1ZT_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.STF1.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.STF1.TargetVal = "空值"; }
editonType.STF1.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.STF1.Com = QueryComByLin(editonType.STF1.Lin).Port;
editonType.STF1.Bps = QueryBpsByCode(editonType.STF1.Lin).Bps.ToString();
editonType.STF1.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("STF", "Send");
editonType.STF1.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("STF", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "FKSTF2ZT_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.STF2.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.STF2.TargetVal = "空值"; }
editonType.STF2.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.STF2.Com = QueryComByLin(editonType.STF2.Lin).Port;
editonType.STF2.Bps = QueryBpsByCode(editonType.STF2.Lin).Bps.ToString();
editonType.STF2.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("STF", "Send");
editonType.STF2.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("STF", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "DZPZF1_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.PZF1.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.PZF1.TargetVal = "空值"; }
11 months ago
editonType.PZF1.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.PZF1.Com = QueryComByLin(editonType.PZF1.Lin).Port;
editonType.PZF1.Bps = QueryBpsByCode(editonType.PZF1.Lin).Bps.ToString();
editonType.PZF1.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("EXV", "Send");
editonType.PZF1.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("EXV", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "DZPZF2_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.PZF2.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.PZF2.TargetVal = "空值"; }
editonType.PZF2.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.PZF2.Com = QueryComByLin(editonType.PZF2.Lin).Port;
editonType.PZF2.Bps = QueryBpsByCode(editonType.PZF2.Lin).Bps.ToString();
editonType.PZF2.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("EXV", "Send");
editonType.PZF2.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("EXV", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "SB1ZS_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.SB1.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.SB1.TargetVal = "空值"; }
editonType.SB1.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.SB1.Com = QueryComByLin(editonType.SB1.Lin).Port;
editonType.SB1.Bps = QueryBpsByCode(editonType.SB1.Lin).Bps.ToString();
editonType.SB1.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("SBUX", "Send");
editonType.SB1.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("SBUX", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "SB2ZS_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.SB2.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.SB2.TargetVal = "空值"; }
editonType.SB2.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.SB2.Com = QueryComByLin(editonType.SB2.Lin).Port;
editonType.SB2.Bps = QueryBpsByCode(editonType.SB2.Lin).Bps.ToString();
editonType.SB2.SendMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("SBUY", "Send");
editonType.SB2.CollectMsgList = _protocolDetailConfigService.QueryActiveProtocolDetailConfigByProtocolConfigCodeAndMsgType("SBUY", "Receive");
continue;
}
else if (paraConfig[i].ParaCode == "SB3ZS_DB")
{
if (paraConfig[i].TargetVal != null) { editonType.SB3.TargetVal = paraConfig[i].TargetVal.ToString(); }
else { editonType.SB3.TargetVal = "空值"; }
editonType.SB3.Lin = QueryLinByParaCategeryAndParaCode(modeTypeEnum.ToString(), paraConfig[i].ParaCode).ProtocolType;
editonType.SB3.Com = QueryComByLin(editonType.SB3.Lin).Port;
editonType.SB3.Bps = QueryBpsByCode(editonType.SB3.Lin).Bps.ToString();
continue;
11 months ago
}
}
return editonType;
}
12 months ago
/// <summary>
/// 根据产品型号查询参数配置项
/// </summary>
/// <param name="productType"></param>
/// <returns></returns>
public ISugarQueryable<T_ParaConfig> QueryParaConfigsByProductType(string productType)
{
try
{
return db.Queryable<T_ParaConfig>().Where(m => m.ProductType == productType);
}
catch (Exception ex)
{
LogHelper.Error(ex, "执行ParaConfigService下QueryParaConfigsByProductType时异常");
return null;
}
}
/// <summary>
/// 新增参数配置项
/// </summary>
/// <param name="t_ParaConfig"></param>
/// <returns></returns>
public (bool isOk, string msg) AddParaConfig(T_ParaConfig t_ParaConfig)
{
try
{
var row = db.Insertable(t_ParaConfig).ExecuteCommand();
return (row > 0, row > 0 ? "操作成功!" : "操作失败!");
}
catch (Exception ex)
{
var errorMsg = $"执行ParaConfigService下AddParaConfig时异常{ex.Message}";
LogHelper.Error(ex, errorMsg);
return (false, errorMsg);
}
}
/// <summary>
/// 批量保存参数配置项
/// </summary>
/// <param name="productType">产品型号</param>
/// <param name="t_ParaConfigs"></param>
/// <returns></returns>
public (bool isOk, string msg) SaveParaConfigs(string productType, List<T_ParaConfig> t_ParaConfigs)
{
var result = db.UseTran(() =>
{
var delRows = db.Deleteable<T_ParaConfig>().Where(m => m.ProductType == productType).ExecuteCommand();
var rows = db.Insertable(t_ParaConfigs).ExecuteCommand();
if (rows > 0 && rows == t_ParaConfigs.Count)
{
return true;
}
else
{
return false;
}
});
if (!result.Data) //返回值为false
{
return (false, "批量保存参数配置项时,操作失败!");
}
return (true, "操作成功!");
}
}
}