From 4a3775f1b633893648aafdb241929cd432ce7af7 Mon Sep 17 00:00:00 2001 From: b110212000 Date: Tue, 27 Jul 2021 19:05:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A0=B1=E8=A1=A8=E6=9F=A5=E8=A9=A2(=E6=97=A5?= =?UTF-8?q?=E6=9C=88=E5=A0=B1=E4=B8=8D=E5=90=AB=E7=A7=9F=E7=94=A8=E5=8F=8A?= =?UTF-8?q?=E6=97=A5=E5=8D=80=E9=96=93)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/StationReportController.cs | 104 ++++ SolarPower/Helper/DatabaseHelper.cs | 2 +- SolarPower/Models/ExceptionRecord.cs | 12 - SolarPower/Models/StationReport.cs | 29 + .../Implement/ExceptionRecordRepository.cs | 18 - .../Implement/PowerStationRepository.cs | 50 ++ .../Implement/StationReportRepository.cs | 175 ++++++ .../Interface/IExceptionRecordRepository.cs | 12 - .../Interface/IPowerStationRepository.cs | 2 + .../Interface/IStationReportRepository.cs | 14 + SolarPower/Startup.cs | 2 +- SolarPower/Views/StationReport/Index.cshtml | 586 +++++++++++++++++- 12 files changed, 950 insertions(+), 56 deletions(-) delete mode 100644 SolarPower/Models/ExceptionRecord.cs create mode 100644 SolarPower/Models/StationReport.cs delete mode 100644 SolarPower/Repository/Implement/ExceptionRecordRepository.cs create mode 100644 SolarPower/Repository/Implement/StationReportRepository.cs delete mode 100644 SolarPower/Repository/Interface/IExceptionRecordRepository.cs create mode 100644 SolarPower/Repository/Interface/IStationReportRepository.cs diff --git a/SolarPower/Controllers/StationReportController.cs b/SolarPower/Controllers/StationReportController.cs index 16724b3..223788c 100644 --- a/SolarPower/Controllers/StationReportController.cs +++ b/SolarPower/Controllers/StationReportController.cs @@ -1,4 +1,8 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using SolarPower.Models; +using SolarPower.Models.PowerStation; +using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Linq; @@ -8,9 +12,109 @@ namespace SolarPower.Controllers { public class StationReportController : MyBaseController { + + private readonly IPowerStationRepository powerStationRepository; + private readonly IStationReportRepository stationReportRepository; + + public StationReportController(IPowerStationRepository powerStationRepository, IStationReportRepository stationReportRepository) : base() + { + this.powerStationRepository = powerStationRepository; + this.stationReportRepository = stationReportRepository; + } public IActionResult Index() { return View(); } + + + + [HttpPost] + public async Task>>> GetPowerStationNameList(string filter) + { + ApiResult>> apiResult = new ApiResult>>(); + try + { + var powerStations = new List(); + if (IsPlatformLayer(myUser.Role.Layer)) + { + powerStations = await powerStationRepository.GetPowerStationsAllWithfilter(filter); + } + else + { + powerStations = await powerStationRepository.GetPowerStationsByCompanyIdWithfilter(myUser.CompanyId,filter); + } + + var siteDBNamePowerStationId = new Dictionary>(); + + var powerStation_Group = powerStations.GroupBy(x => x.CityName).ToList(); + foreach (var stations in powerStation_Group) + { + siteDBNamePowerStationId.Add(stations.Key, stations.ToList()); + } + + apiResult.Code = "0000"; + + apiResult.Data = siteDBNamePowerStationId; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】"); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + apiResult.Msg = errorCode.GetString(apiResult.Code); + return apiResult; + } + + public async Task>> GetTableHead(Select_table post) + { + ApiResult> apiResult = new ApiResult>(); + List inverter = new List(); + try + { + var powerStation = await powerStationRepository.GetOneAsync(post.PowerStation); + if(powerStation == null) + { + apiResult.Code = "0001"; + apiResult.Msg = "需加入查詢電站"; + return apiResult; + } + inverter = await stationReportRepository.GetInverterId(powerStation.SiteDB,post); + for (int i = 0;i> GetForm(Select_table post) + { + ApiResult apiResult = new ApiResult(); + try + { + var a = await stationReportRepository.Gettablebody(post); + apiResult.Code = "0000"; + apiResult.Data = a; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + Logger.LogError("【" + controllerName + "/" + actionName + "】"); + Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + return apiResult; + } + + } } diff --git a/SolarPower/Helper/DatabaseHelper.cs b/SolarPower/Helper/DatabaseHelper.cs index bcd499f..0bbdd56 100644 --- a/SolarPower/Helper/DatabaseHelper.cs +++ b/SolarPower/Helper/DatabaseHelper.cs @@ -37,7 +37,7 @@ namespace SolarPower.Helper var rootStr = ed.AESDecrypt(dbConfig.Root); var passwordStr = ed.AESDecrypt(dbConfig.Password); - var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;"; + var connStr = $"server={serverStr};port={portStr};database={databaseStr};user={rootStr};password={passwordStr};charset=utf8;Allow User Variables=True;"; this._connectionString = connStr; } diff --git a/SolarPower/Models/ExceptionRecord.cs b/SolarPower/Models/ExceptionRecord.cs deleted file mode 100644 index 784e4db..0000000 --- a/SolarPower/Models/ExceptionRecord.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SolarPower.Models -{ - public class ExceptionRecord - { - public string asd { get; set; } - } -} diff --git a/SolarPower/Models/StationReport.cs b/SolarPower/Models/StationReport.cs new file mode 100644 index 0000000..5328d4c --- /dev/null +++ b/SolarPower/Models/StationReport.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SolarPower.Models +{ + public class StationReport + { + public string asd { get; set; } + } + + public class PowerStationIdAndCity + { + public int PowerStationId { get; set; } + public string CityName { get; set; } + public string PowerStationName { get; set; } + } + + public class Select_table + { + public int SearchType { get; set; } + public string Time { get; set; } + public int PowerStation { get; set; } + public int FormType { get; set; } + } + + +} diff --git a/SolarPower/Repository/Implement/ExceptionRecordRepository.cs b/SolarPower/Repository/Implement/ExceptionRecordRepository.cs deleted file mode 100644 index 54fe895..0000000 --- a/SolarPower/Repository/Implement/ExceptionRecordRepository.cs +++ /dev/null @@ -1,18 +0,0 @@ -using SolarPower.Helper; -using SolarPower.Models; -using SolarPower.Repository.Interface; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SolarPower.Repository.Implement -{ - public class ExceptionRecordRepository : RepositoryBase, IExceptionRecordRepository - { - public ExceptionRecordRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) - { - tableName = "power_station"; - } - } -} diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 85ceebc..7c5813c 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -3672,5 +3672,55 @@ namespace SolarPower.Repository.Implement } } + + public async Task> GetPowerStationsByCompanyIdWithfilter(int companyId,string filter) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = $@"SELECT ps.Id AS PowerStationId , ps.`Name` AS PowerStationName,city.Name AS CityName FROM {tableName} ps + LEFT JOIN city ON city.Id = ps.CityId WHERE ps.Deleted = 0 AND ps.CompanyId = @CompanyId "; + + + if (!string.IsNullOrEmpty(filter)) + { + sql += @" AND ps.Name LIKE CONCAT('%', @Filter, '%')"; + } + result = (await conn.QueryAsync(sql, new { CompanyId = companyId, Filter = filter })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } + + public async Task> GetPowerStationsAllWithfilter(string filter) + { + List result; + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + try + { + var sql = @$"SELECT ps.Id AS PowerStationId , ps.`Name` AS PowerStationName,city.Name AS CityName FROM {tableName} ps + LEFT JOIN city ON city.Id = ps.CityId WHERE ps.Deleted = 0"; + + + if (!string.IsNullOrEmpty(filter)) + { + sql += @" AND ps.Name LIKE CONCAT('%', @Filter, '%')"; + } + result = (await conn.QueryAsync(sql, new { Filter = filter })).ToList(); + } + catch (Exception exception) + { + throw exception; + } + return result; + } + } } } diff --git a/SolarPower/Repository/Implement/StationReportRepository.cs b/SolarPower/Repository/Implement/StationReportRepository.cs new file mode 100644 index 0000000..c944240 --- /dev/null +++ b/SolarPower/Repository/Implement/StationReportRepository.cs @@ -0,0 +1,175 @@ +using Dapper; +using SolarPower.Helper; +using SolarPower.Models; +using SolarPower.Repository.Interface; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; + +namespace SolarPower.Repository.Implement +{ + public class StationReportRepository : RepositoryBase, IStationReportRepository + { + public StationReportRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) + { + tableName = "power_station"; + } + + + public async Task> GetInverterId(string DBname, Select_table post) + { + List result = new List(); + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + conn.Open(); + try + { + string sql = @$"SELECT InverterId from {DBname}.inverter i left JOIN {DBname}.controller c ON i.ControllerId = c.Id + WHERE i.Deleted != 1 AND c.PowerStationId = {post.PowerStation}"; + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return result; + } + } + + public async Task Gettablebody(Select_table post) + { + using (IDbConnection conn = _databaseHelper.GetConnection()) + { + dynamic a; + conn.Open(); + try + { + string sql = ""; + switch(post.FormType) + { + case 0: + sql = @$" + SET @sql = NULL; + SELECT + GROUP_CONCAT(DISTINCT + CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then a.KWH end) ''inv_', right(INVERTERID, 4), '''') + ) INTO @sql + FROM inverter_history_hour; + SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%m-%d %H'') report_date, ', @sql, + ', b.KWH hourKWH, round((b.KWH / (SELECT MAX(TODAYKWH) FROM power_station_history_hour WHERE DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') = ''{post.Time}'' and powerstationid = {post.PowerStation}))*100,2) ''hourKWHp'', d.irradiance ''irradiance'', d.Temperature ''temperature'', + b.money ''hourmoney'', c.TODAYKWH ''totKWH'', c.KWHKWP ''totKWHKWP'', c.money ''totmoney'', stationName, powerRate daymoney, c.SOLARHOUR tothour,round(b.PR, 2) as pr + FROM inverter_history_hour a left join + ( # 每小時加總 inv + select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d %H:%i'') report_date, siteid, sitetype, round(KWH, 2) KWH, + round(TODAYKWH, 2) TODAYKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, round(money, 2) money + from power_station_history_hour + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') = ''{post.Time}'' + ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d %H:%i'') = b.report_date + left join + ( # day + select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, sitetype, round(TODAYKWH, 2) TODAYKWH,round(KWHKWP, 2) KWHKWP + , round(PR, 2) PR, round(money, 2) money , round(SOLARHOUR, 2) SOLARHOUR + from power_station_history_day + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') = ''{post.Time}'' + ) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = c.report_date + left join + ( + select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d %H:%i'')report_date, irradiance, Temperature + from sensor_history_hour + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') = ''{post.Time}'' + ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d %H:%i'') = d.report_date + join + ( + select id, name stationName, powerRate from power_station where id = {post.PowerStation} + )z on a.powerstationid = z.id + where DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') = ''{post.Time}'' + GROUP BY DATE_FORMAT(TIMESTAMP,''%Y-%m-%d %H:%i'') + order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d %H:%i'') '); + + #select @sql as 'mySelect'; #顯示動態語法 + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt;"; + break; + case 1: + if(post.SearchType == 2) + { + sql = @$" + SET @sql = NULL; + SELECT + GROUP_CONCAT(DISTINCT + CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 4), '''') + ) INTO @sql + FROM inverter_history_hour; + SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%m/%d'') report_date, ', @sql, + ',b.TODAYKWH ''dayKWH'', round((b.TODAYKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR, + d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'', + c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'' + FROM inverter_history_day a left join + ( # 每日加總 inv + select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH, + round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR + from power_station_history_day + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' + ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = b.report_date + left join + ( # month + select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m'') report_date, sitetype, round(monthKWH, 2) monthKWH,round(KWHKWP, 2) KWHKWP + , round(PR, 2) PR, round(money, 2) money , round(SOLARHOUR, 2) SOLARHOUR + from power_station_history_month + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' + ) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = c.report_date + left join + ( + select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'')report_date, irradiance, Temperature + from sensor_history_day + where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' + ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = d.report_date + join + ( + select id, name stationName, powerRate from power_station where id = {post.PowerStation} + )z on a.powerstationid = z.id + where DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = ''{post.Time}'' + GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') + order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') '); + + # select @sql as 'mySelect'; #顯示動態語法 + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt;"; + } + else + { + sql = ""; + } + + + + break; + } + + + + + a = await conn.QueryAsync(sql); + + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + return a; + } + } + } +} diff --git a/SolarPower/Repository/Interface/IExceptionRecordRepository.cs b/SolarPower/Repository/Interface/IExceptionRecordRepository.cs deleted file mode 100644 index add4ae8..0000000 --- a/SolarPower/Repository/Interface/IExceptionRecordRepository.cs +++ /dev/null @@ -1,12 +0,0 @@ -using SolarPower.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SolarPower.Repository.Interface -{ - public interface IExceptionRecordRepository : IRepositoryBase - { - } -} diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index e6a1f3c..bf46b87 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -548,5 +548,7 @@ namespace SolarPower.Repository.Interface Task UpdateSensorAvgHistoryMonthList(List entity); Task> GetPowerStationsByCompanyId(int companyId); Task> GetPowerStationInverter(Dictionary> dic, string filter); + Task> GetPowerStationsByCompanyIdWithfilter(int companyId,string filter); + Task> GetPowerStationsAllWithfilter(string filter); } } diff --git a/SolarPower/Repository/Interface/IStationReportRepository.cs b/SolarPower/Repository/Interface/IStationReportRepository.cs new file mode 100644 index 0000000..9b4b4d6 --- /dev/null +++ b/SolarPower/Repository/Interface/IStationReportRepository.cs @@ -0,0 +1,14 @@ +using SolarPower.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SolarPower.Repository.Interface +{ + public interface IStationReportRepository : IRepositoryBase + { + Task> GetInverterId(string DBname, Select_table post); + Task Gettablebody(Select_table post); + } +} diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index 5eb163f..da60772 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -77,7 +77,7 @@ namespace SolarPower services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); #endregion double loginExpireMinute = this.Configuration.GetValue("LoginExpireMinute"); diff --git a/SolarPower/Views/StationReport/Index.cshtml b/SolarPower/Views/StationReport/Index.cshtml index 7f2c478..54e3970 100644 --- a/SolarPower/Views/StationReport/Index.cshtml +++ b/SolarPower/Views/StationReport/Index.cshtml @@ -204,20 +204,18 @@
- - - + + +
- +
-
- - - - +
+
+
@@ -226,9 +224,573 @@ - - - +
+
+
+ + + + + +
+
+
+
+
+
+
+ + + + + +
+
+
+
+ + +@section Scripts{ + +} \ No newline at end of file