using IOT.Show.Common; using IOT.Show.Model; using SqlSugar; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; namespace IOT.Show.SqlBase { public class SqlSugarBase { SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=10.113.254.16;uid=sa;pwd=klltdb@3391123;database=XJEMSRefactor", DbType = SqlSugar.DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); public List getLedInfo(string titleName) { try { var sqlStr = "select * from T_Led_Info where ledId = @titleName"; var info = db.Ado.SqlQuery(sqlStr, new List(){ new SugarParameter("@titleName",titleName), //执行sql语句 }); return info; } catch (Exception ex) { LogHelper.Error("获取Led信息失败",ex); return null; } } public DataTable getLedData(string titleName) { try { var sqlStr = "select monitorName,\r\n ip,\r\n title,\r\n max(t.进水温度) as tempreture,\r\n MAX(t.出水温度) as humidity,\r\n (MAX(t.出水温度) - MAX(t.进水温度)) illuminance\r\nfrom (select Left(a.monitorName, len(a.monitorName) - 4) as monitorName,\r\n Right(a.monitorName, 4) as monitorType,\r\n a.ip,\r\n a.title,\r\n b.tempreture,\r\n a.monitorOrder\r\n from (\r\n select t1.title,\r\n t2.ledId,\r\n t1.ip,\r\n t2.monitorId,\r\n t2.monitorOrder,\r\n t3.monitorName,\r\n MAX(t4.collectTime) as collectTime\r\n from T_Led_Info t1\r\n left join T_Led_MonitorUnit t2 on t1.ledId = t2.ledId\r\n left join T_Monitor t3 on t2.monitorId = t3.monitorId\r\n left join T_W_TempertureData t4 on t4.monitorId = t3.monitorId\r\n where t1.title = @titleName \r\n group by t1.title, t2.ledId, t1.ip, t2.monitorId, t3.monitorName, t2.monitorOrder\r\n ) as a\r\n left join T_W_TempertureData b on b.collectTime = a.collectTime and a.monitorId = b.monitorId) as s1\r\n pivot (max(tempreture) for monitorType in (进水温度,出水温度)) t\r\ngroup by monitorName, ip, title, monitorOrder\r\norder by monitorOrder"; //LogHelper.Info("语句:"+sqlStr); var info = db.Ado.SqlQuery(sqlStr, new List(){ new SugarParameter("@titleName",titleName), //执行sql语句 }); var result = ListToTable(info); return result; } catch (Exception ex) { LogHelper.Error("获取Led数据失败",ex); return null; } } public DataTable ListToTable(IList info) { try { DataTable dt = new DataTable(); if (info.Count > 0) { PropertyInfo[] propertys = info[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { dt.Columns.Add(pi.Name); } foreach (object t in info) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { object obj = pi.GetValue(t, null); tempList.Add(obj); } object[] array = tempList.ToArray(); dt.LoadDataRow(array, true); } } return dt; } catch (Exception ex) { Console.WriteLine($"list转为DataTable异常:{ex.Message}"); return null; } } public List ConvertDataTableToList(DataTable dataTable) where T : new() { List list = new List(); foreach (DataRow row in dataTable.Rows) { T obj = new T(); foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties()) { if (dataTable.Columns.Contains(propertyInfo.Name)) { if (row[propertyInfo.Name] != DBNull.Value) { propertyInfo.SetValue(obj, row[propertyInfo.Name], null); } } } list.Add(obj); } return list; } } }