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.

127 lines
5.5 KiB
C#

2 years ago
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.18;uid=sa;pwd=klltdb@3391123;database=XJIoTEMSRefactor",
2 years ago
DbType = SqlSugar.DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
public List<T_Led_Info> getLedInfo(string titleName)
{
try
{
var sqlStr = "select * from T_Led_Info where ledId = @titleName";
var info = db.Ado.SqlQuery<T_Led_Info>(sqlStr,
new List<SugarParameter>(){
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 CONVERT(varchar,max(t.进水温度)) as tempreture,\r\n CONVERT(varchar,MAX(t.出水温度)) as humidity,\r\n CONVERT(varchar,ROUND((MAX(t.出水温度) - MAX(t.进水温度)), 2)) as 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 ROUND(b.tempreture+a.repair_value,2) as 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,t2.repair_value,\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.repair_value,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";
2 years ago
//LogHelper.Info("语句:"+sqlStr);
var info = db.Ado.SqlQuery<T_Led_Data>(sqlStr,
new List<SugarParameter>(){
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<T> ConvertDataTableToList<T>(DataTable dataTable) where T : new()
{
List<T> list = new List<T>();
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;
}
}
}