diff --git a/DataServer/BLL/electricity_data.cs b/DataServer/BLL/electricity_data.cs index b4c3a65..e77d376 100644 --- a/DataServer/BLL/electricity_data.cs +++ b/DataServer/BLL/electricity_data.cs @@ -224,15 +224,33 @@ namespace DataServer.BLL } /// - /// 获取数据 - /// - /// 数据来源 - /// 条件 - /// 求和字段 - /// - public decimal GetSum(string source, string strWhere, string sum_name) + /// 获得数据列表 + /// + public List GetSum(string source, string strWhere, string sum_name) { - return dal.GetSum(source, strWhere, sum_name); + DataSet ds = dal.GetSum(source, strWhere, sum_name); + return DataTableToSumList(ds.Tables[0]); + } + /// + /// 获得数据列表 + /// + public List DataTableToSumList(DataTable dt) + { + List modelList = new List(); + int rowsCount = dt.Rows.Count; + if (rowsCount > 0) + { + DataServer.other.service_rating_data model; + for (int n = 0; n < rowsCount; n++) + { + model = dal.DataRowToSumModel(dt.Rows[n]); + if (model != null) + { + modelList.Add(model); + } + } + } + return modelList; } #endregion ExtensionMethod } diff --git a/DataServer/DAL/electricity_data.cs b/DataServer/DAL/electricity_data.cs index d3f6c3c..6df5a2e 100644 --- a/DataServer/DAL/electricity_data.cs +++ b/DataServer/DAL/electricity_data.cs @@ -583,23 +583,36 @@ namespace DataServer.DAL /// 条件 /// 求和字段 /// - public decimal GetSum(string source, string strWhere, string sum_name) + public DataSet GetSum(string source, string strWhere, string sum_name) { StringBuilder strSql = new StringBuilder(); - strSql.Append("SELECT sum(" + sum_name + ") FROM (" + source + ") as T "); + strSql.Append("SELECT sum(" + sum_name + ") as total,DeviceId FROM (" + source + ") as T "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } - object obj = DbHelperSQL.GetSingle(strSql.ToString(), 300); - if (obj == null) + strSql.Append(" group by DeviceId "); + return DbHelperMySQL.Query(strSql.ToString(), 300); + } + + /// + /// 得到一个对象实体 + /// + public DataServer.other.service_rating_data DataRowToSumModel(DataRow row) + { + DataServer.other.service_rating_data model = new DataServer.other.service_rating_data(); + if (row != null) { - return 0; - } - else - { - return Convert.ToDecimal(obj); + if (row["DeviceId"] != null) + { + model.DeviceId = row["DeviceId"].ToString(); + } + if (row["total"] != null && row["total"].ToString() != "") + { + model.total = decimal.Parse(row["total"].ToString()); + } } + return model; } #endregion ExtensionMethod } diff --git a/DataServer/DataServer.csproj b/DataServer/DataServer.csproj index 34dc0eb..1eed312 100644 --- a/DataServer/DataServer.csproj +++ b/DataServer/DataServer.csproj @@ -145,6 +145,7 @@ + diff --git a/DataServer/other/service_rating_data.cs b/DataServer/other/service_rating_data.cs new file mode 100644 index 0000000..89b4253 --- /dev/null +++ b/DataServer/other/service_rating_data.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataServer.other +{ + public class service_rating_data + { + public decimal total { get; set; } + + public string DeviceId { get; set; } + } +} diff --git a/DongYingAPI/Controllers/api/EnergyEfficiency/GetClassifiedEnergyController.cs b/DongYingAPI/Controllers/api/EnergyEfficiency/GetClassifiedEnergyController.cs index 29fd29b..3bb40f1 100644 --- a/DongYingAPI/Controllers/api/EnergyEfficiency/GetClassifiedEnergyController.cs +++ b/DongYingAPI/Controllers/api/EnergyEfficiency/GetClassifiedEnergyController.cs @@ -61,15 +61,22 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var time = upper_start_time.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { - if (time == upper_start_time.ToString("yyyyMM") || time == modern_end_time.ToString("yyyyMM")) + if (time == upper_start_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, upper_start_time, modern_end_time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_start_time); } - else + else if (time == upper_end_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_end_time); + } + else if (time == modern_start_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_start_time); + } + else if (time == modern_end_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_end_time); } - source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) @@ -78,7 +85,7 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency list = bll.GetList(source, "", ""); } } - if (type == "月") + if (type == "月") { modern_start_time = DateTime.Parse(now.ToString("yyyy-MM") + "-01 00:00:00"); modern_end_time = DateTime.Parse(now.ToString("yyyy-MM-dd HH") + ":00:00"); @@ -91,15 +98,22 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var time = upper_start_time.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { - if (time == upper_start_time.ToString("yyyyMM") || time == modern_end_time.ToString("yyyyMM")) + if (time == upper_start_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, upper_start_time, modern_end_time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_start_time); } - else + else if (time == upper_end_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_end_time); + } + else if (time == modern_start_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_start_time); + } + else if (time == modern_end_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_end_time); } - source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) @@ -108,7 +122,7 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency list = bll.GetList(source, "", ""); } } - if (type == "年") + if (type == "年") { modern_start_time = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00"); modern_end_time = DateTime.Parse(now.ToString("yyyy-MM-dd HH") + ":00:00"); @@ -121,15 +135,22 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var time = upper_start_time.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { - if (time == upper_start_time.ToString("yyyyMM") || time == modern_end_time.ToString("yyyyMM")) + if (time == upper_start_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, upper_start_time, modern_end_time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_start_time); } - else + else if (time == upper_end_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, upper_end_time); + } + else if (time == modern_start_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_start_time); + } + else if (time == modern_end_time.ToString("yyyyMM")) + { + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, modern_end_time); } - source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) @@ -195,10 +216,14 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency //电 var device_list = device_bll.GetModelList(""); + var modern_start_list = list.Where(a => a.EntireTime.Value == modern_start_time).ToList(); + var modern_end_list = list.Where(a => a.EntireTime.Value == modern_end_time).ToList(); + var upper_start_list = list.Where(a => a.EntireTime.Value == upper_start_time).ToList(); + var upper_end_list = list.Where(a => a.EntireTime.Value == upper_end_time).ToList(); foreach (var item in device_list) { - var modern_start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == modern_start_time).FirstOrDefault(); - var modern_end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == modern_end_time).FirstOrDefault(); + var modern_start_data = modern_start_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); + var modern_end_data = modern_end_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); if (modern_start_data != null && modern_end_data != null) { if (modern_start_data.EH != null && modern_end_data.EH != null) @@ -208,8 +233,8 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency } } - var upper_start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == upper_start_time).FirstOrDefault(); - var upper_end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == upper_end_time).FirstOrDefault(); + var upper_start_data = upper_start_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); + var upper_end_data = upper_end_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); if (upper_start_data != null && upper_end_data != null) { if (upper_start_data.EH != null && upper_end_data.EH != null) diff --git a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyCalendarController.cs b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyCalendarController.cs index 0e58bfd..c2fac33 100644 --- a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyCalendarController.cs +++ b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyCalendarController.cs @@ -106,15 +106,14 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var time = start_time.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { - if (time == start_time.ToString("yyyyMM") || time == end_time.ToString("yyyyMM")) + if (time == start_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, start_time, end_time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, start_time); } - else + else if (time == end_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, end_time); } - source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) @@ -128,10 +127,12 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var start = start_time.AddHours(i); var end = start.AddHours(1); decimal electricity = 0; + var start_list = list.Where(a => a.EntireTime.Value == start).ToList(); + var end_list = list.Where(a => a.EntireTime.Value == end).ToList(); foreach (var item in device_list) { - var start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == start).FirstOrDefault(); - var end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == end).FirstOrDefault(); + var start_data = start_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); + var end_data = end_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); if (start_data != null && end_data != null) { if (start_data.EH != null && end_data.EH != null) diff --git a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyConsumptionController.cs b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyConsumptionController.cs index c787ea0..23859ef 100644 --- a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyConsumptionController.cs +++ b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyConsumptionController.cs @@ -31,7 +31,7 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency { var now = DateTime.Now; var device_list = device_bll.GetModelList(""); - var list = new List(); + var list = new List(); //判断表是否存在,不存在就创建 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); var start_date = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00"); @@ -69,7 +69,7 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency if (!string.IsNullOrEmpty(source)) { source = source.Substring(0, source.Length - 11); - list = bll.GetList(source, "", ""); + list = bll.GetSum(source, "", "ServiceRating"); } var data = new List(); @@ -77,7 +77,7 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency { var model = new energy_consumption(); model.DeviceName = item.DeviceName; - model.OperatingPower = list.Where(a => a.DeviceId == item.DeviceId).Sum(a => a.ServiceRating).Value; + model.OperatingPower = list.Where(a => a.DeviceId == item.DeviceId).Select(a => a.total).FirstOrDefault(); data.Add(model); } diff --git a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyTrendController.cs b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyTrendController.cs index f32598f..423688c 100644 --- a/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyTrendController.cs +++ b/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyTrendController.cs @@ -52,15 +52,14 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency var time = start_time.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { - if (time == start_time.ToString("yyyyMM") || time == end_time.ToString("yyyyMM")) + if (time == start_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, start_time, end_time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, start_time); } - else + else if (time == end_time.ToString("yyyyMM")) { - source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); + source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime='{1}') UNION all ", time, end_time); } - source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) @@ -70,7 +69,6 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency } var device_list = device_bll.GetModelList(""); - for (int i = 0; i <= time_count; i++) { var month = start_time.AddMonths(i).Month; @@ -82,10 +80,12 @@ namespace DongYingAPI.Controllers.api.EnergyEfficiency } var plan_model = plan_list.Where(a => a.Month == month).FirstOrDefault(); decimal value = 0; + var start_list = list.Where(a => a.EntireTime.Value == time).ToList(); + var end_list = list.Where(a => a.EntireTime.Value == end).ToList(); foreach (var item in device_list) { - var start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == time).FirstOrDefault(); - var end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == end).FirstOrDefault(); + var start_data = start_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); + var end_data = end_list.Where(a => a.DeviceId == item.DeviceId).FirstOrDefault(); if (start_data != null && end_data != null) { if (start_data.EH != null && end_data.EH != null) diff --git a/DongYingAPI/DongYingAPI.csproj b/DongYingAPI/DongYingAPI.csproj index af766e9..e8e59d2 100644 --- a/DongYingAPI/DongYingAPI.csproj +++ b/DongYingAPI/DongYingAPI.csproj @@ -53,7 +53,7 @@ ..\packages\HslCommunication.10.1.2\lib\net451\HslCommunication.dll - ..\..\DongYingAPI\packages\log4net.2.0.8\lib\net45-full\log4net.dll + ..\packages\log4net.2.0.8\lib\net45-full\log4net.dll diff --git a/DongYingAPI/DongYingAPI.csproj.user b/DongYingAPI/DongYingAPI.csproj.user index 8b4fb8b..70f6263 100644 --- a/DongYingAPI/DongYingAPI.csproj.user +++ b/DongYingAPI/DongYingAPI.csproj.user @@ -18,6 +18,7 @@ True False + ProjectFiles