using ProductionSystem_Log; using ProductionSystem_Model.DbModel.Para; using ProductionSystem_Model.DbModel.Protocol; using ProductionSystem_Model.Enum; using ProductionSystem_Model.ViewModel.Response.Para; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; namespace ProductionSystem_Service { public class ParaConfigService : DbContext { ProtocolDetailConfigService _protocolDetailConfigService = new ProtocolDetailConfigService(); /// /// 查询所有参数配置项 /// /// public ISugarQueryable QueryParaConfigs() { try { return db.Queryable(); } catch (Exception ex) { LogHelper.Error(ex, "执行ParaConfigService下QueryParaConfigs时异常"); return null; } } /// /// 查询有效参数配置项 /// /// public ISugarQueryable QueryActiveParaConfigs() { try { return db.Queryable().Where(m => m.IsActive); } catch (Exception ex) { LogHelper.Error(ex, "执行ParaConfigService下QueryActiveParaConfigs时异常"); return null; } } /// /// 根据参数类别查询有效参数配置项 /// /// /// public List QueryActiveParaConfigsByParaCategory(string paraCategory) { try { return db.Queryable() .InnerJoin((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) .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; } } /// /// 根据产品型号查询有效参数配置项 /// /// /// public ISugarQueryable QueryActiveParaConfigsByProductType(string productType) { try { //var temp = db.Queryable().Where(m => m.ProductType == productType && m.IsActive); return db.Queryable().Where(m => m.ProductType == productType && m.IsActive); } catch (Exception ex) { LogHelper.Error(ex, "执行ParaConfigService下QueryActiveParaConfigsByProductType时异常"); return null; } } private ISugarQueryable QueryTargetValWithParaCodeByParaCategery(string paraCategory,string productType) { try { var temp1 = db.Queryable().Where(m => m.ParaCategory == paraCategory && m.ProductType == productType && m.IsActive); return temp1; } catch (Exception ex) { LogHelper.Error(ex, "执行ParaConfigService下QueryTargetValWithParaCodeByParaCategery时异常"); return null; } } private T_ParaColumnConfig QueryLinByParaCategeryAndParaCode(string paraCategory, string paraCode) { T_ParaColumnConfig temp1 = db.Queryable(). Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive). First(); if (temp1 == null) { throw new Exception("未维护数据!请先维护!"); } else { var temp2 = db.Queryable(). Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive). First().ProtocolType; if (temp2 == null) { temp2 = "未配置通讯方式"; } return db.Queryable().Where(m => m.ParaCategory == paraCategory && m.ParaCode == paraCode && m.IsActive).First(); } } /// /// 串口 /// /// /// /// private T_Protocol_Config QueryComByLin(string lin) { T_Protocol_Config temp1 = db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First(); if (temp1 == null) { throw new Exception("未维护数据!请先维护!"); } string temp3 = db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First().ProtocolType; if (temp3 == null) { temp3 = "未配置串口"; } return db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First(); } /// /// 波特率 /// /// /// /// private T_Protocol_Config QueryBpsByCode(string lin) { T_Protocol_Config temp1 = db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First(); if (temp1 == null) { throw new Exception("未维护数据!请先维护!"); } string temp3 = db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First().ProtocolType; if (temp3 == null) { temp3 = "未配置波特率"; } return db.Queryable().Where(m => m.ProtocolCode == lin && m.IsActive).First(); } /// /// 报文 /// /// /// private List QueryProtocolConfigsByCode(string code) { return db.Queryable().Where(m => m.ProtocolConfigCode == code && m.IsActive).ToList(); } /// /// 根据参数种类查询有效参数配置项,例如传“BZ1_DB”、“BZ2_DB” /// /// /// public EditonType GetParaTargetVal(ModeTypeEnum modeTypeEnum,string productType) { EditonType editonType = new EditonType(); var paraConfig = QueryTargetValWithParaCodeByParaCategery(modeTypeEnum.ToString()).ToList(); for (int i = 0; i < paraConfig.Count; i++) { 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 = "空值"; } 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; } } return editonType; } /// /// 根据产品型号查询参数配置项 /// /// /// public ISugarQueryable QueryParaConfigsByProductType(string productType) { try { return db.Queryable().Where(m => m.ProductType == productType); } catch (Exception ex) { LogHelper.Error(ex, "执行ParaConfigService下QueryParaConfigsByProductType时异常"); return null; } } /// /// 新增参数配置项 /// /// /// 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); } } /// /// 批量保存参数配置项 /// /// 产品型号 /// /// public (bool isOk, string msg) SaveParaConfigs(string productType, List t_ParaConfigs) { var result = db.UseTran(() => { var delRows = db.Deleteable().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, "操作成功!"); } } }