From e84c23b61697e53974ea69eb5fbabbabbf016614 Mon Sep 17 00:00:00 2001 From: Kai Date: Wed, 1 Sep 2021 10:41:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9B=BB=E7=AB=99=E6=8A=93?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F=20part2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/MapOverviewController.cs | 35 +++++---- SolarPower/Controllers/MyBaseController.cs | 15 ++-- .../Controllers/StationOverviewController.cs | 10 ++- SolarPower/DBSchema/solar_power_schema.sql | 13 ++++ SolarPower/Models/MyBaseModel.cs | 22 ++++-- .../Implement/PowerStationRepository.cs | 33 +++----- .../Interface/IPowerStationRepository.cs | 3 +- .../Implement/MyPowerStationService.cs | 78 ++++++++++++++++++- SolarPower/Views/Shared/_Layout.cshtml | 18 ++--- 9 files changed, 161 insertions(+), 66 deletions(-) diff --git a/SolarPower/Controllers/MapOverviewController.cs b/SolarPower/Controllers/MapOverviewController.cs index d69950c..e4bfb98 100644 --- a/SolarPower/Controllers/MapOverviewController.cs +++ b/SolarPower/Controllers/MapOverviewController.cs @@ -4,6 +4,7 @@ using SolarPower.Models; using SolarPower.Models.PowerStation; using SolarPower.Models.Role; using SolarPower.Repository.Interface; +using SolarPower.Services.Implement; using System; using System.Collections.Generic; using System.IO; @@ -41,20 +42,25 @@ namespace SolarPower.Controllers MapOverview mapOverview = new MapOverview(); try { + MyPowerStationService myPowerStationService = new MyPowerStationService(powerStationRepository); + var myPowerStations = myPowerStationService.GetMyPowerStations(myUser); - //List powerStationIds = await powerStationRepository.GetPowerStationIdsByUserRole(myUser); - List powerStationIds = myUser.PowerStationSummaries.SelectMany(x => x.MyPowerStations.Select(y=> y.PowerStationId)).ToList(); - var overview = await overviewRepository.GetOverviewByPowerStationIds(powerStationIds); - mapOverview.Today_kwh = overview.Today_kwh; - mapOverview.Total_kwh = overview.Total_kwh; - mapOverview.Today_irradiance = overview.Today_irradiance; - mapOverview.Avg_irradiance = overview.Avg_irradiance; - mapOverview.Today_PR = overview.Today_PR; - mapOverview.Avg_PR = overview.Avg_PR; - mapOverview.Today_kwhkwp = overview.Today_kwhkwp; - mapOverview.Avg_kwhkwp = overview.Avg_kwhkwp; - mapOverview.Today_carbon = overview.Today_carbon; - mapOverview.Total_carbon = overview.Total_carbon; + List powerStationIds = myPowerStations.SelectMany(x => x.MyPowerStations.Select(y => y.Id)).ToList(); + + var powerStations = myPowerStations.SelectMany(x => x.MyPowerStations).ToList(); + var powerStationSummary = myPowerStationService.GetMyPowerStationsSummary(powerStations); + + //var overview = await overviewRepository.GetOverviewByPowerStationIds(powerStationIds); + mapOverview.Today_kwh = powerStationSummary.Today_kwh; + mapOverview.Total_kwh = powerStationSummary.Total_kwh; + mapOverview.Today_irradiance = powerStationSummary.Today_irradiance; + mapOverview.Avg_irradiance = powerStationSummary.Avg_irradiance; + mapOverview.Today_PR = powerStationSummary.Today_PR; + mapOverview.Avg_PR = powerStationSummary.Avg_PR; + mapOverview.Today_kwhkwp = powerStationSummary.Today_kwhkwp; + mapOverview.Avg_kwhkwp = powerStationSummary.Avg_kwhkwp; + mapOverview.Today_carbon = powerStationSummary.Today_carbon; + mapOverview.Total_carbon = powerStationSummary.Total_carbon; mapOverview.CapacityDataTables = await overviewRepository.GetCapacityDataTableByPowerStationIds(powerStationIds); @@ -70,7 +76,8 @@ namespace SolarPower.Controllers mapOverview.TotalPowerStationCount = totalPowerStationCount; mapOverview.TotalCapacity = totalCapacity / 1000; // kWp -> MWp - mapOverview.PowerStations = await overviewRepository.GetListPowerStationByPowerStationIds(powerStationIds); + //mapOverview.PowerStations = await overviewRepository.GetListPowerStationByPowerStationIds(powerStationIds); + mapOverview.PowerStations = powerStations; mapOverview.UpdatedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index e481f88..69cfbef 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -22,6 +22,7 @@ using Newtonsoft.Json; using SolarPower.Models.Company; using SolarPower.Models.Role; using Microsoft.AspNetCore.Routing; +using SolarPower.Services.Implement; namespace SolarPower.Controllers { @@ -131,9 +132,11 @@ namespace SolarPower.Controllers //取得當前使用者可以查看的電站 - var myPowerStationSummaries = powerStationRepository.GetMyPowerStationSummary(myUser); - myUser.PowerStationSummaries = myPowerStationSummaries; - ViewBag.myPowerStationSummaries = myPowerStationSummaries; + MyPowerStationService myPowerStationService = new MyPowerStationService(powerStationRepository); + var myPowerStations = myPowerStationService.GetMyPowerStations(myUser); + //var myPowerStationSummaries = powerStationRepository.GetMyPowerStationSummary(myUser); + myUser.myPowerStationGroupByCities = myPowerStations; + ViewBag.myPowerStationGroupByCities = myPowerStations; if (controllerName == "PowerStation" && actionName == "Edit") { @@ -146,16 +149,16 @@ namespace SolarPower.Controllers { var hasSubTagNum = false; int i = 0; - foreach(var myPowerStationSummary in myPowerStationSummaries) + foreach (var myPowerStation in myPowerStations) { if (hasSubTagNum) { break; } int j = 0; - foreach(var myPowerStation in myPowerStationSummary.MyPowerStations) + foreach (var station in myPowerStation.MyPowerStations) { - if(myPowerStation.PowerStationId == stationId) + if (station.Id == stationId) { ViewData["SubNum"] = i; ViewData["TagNum"] = j; diff --git a/SolarPower/Controllers/StationOverviewController.cs b/SolarPower/Controllers/StationOverviewController.cs index 414d049..fea1d53 100644 --- a/SolarPower/Controllers/StationOverviewController.cs +++ b/SolarPower/Controllers/StationOverviewController.cs @@ -93,15 +93,17 @@ namespace SolarPower.Controllers } [HttpPost] - public ApiResult> GetPowerStationCollapse(string filter) + public ApiResult> GetPowerStationCollapse(string filter) { - ApiResult> apiResult = new ApiResult>(); + ApiResult> apiResult = new ApiResult>(); try { apiResult.Code = "0000"; - var powerStationSummary = powerStationRepository.GetMyPowerStationSummary(myUser, filter); + //var powerStationSummary = powerStationRepository.GetMyPowerStationSummary(myUser); - apiResult.Data = powerStationSummary; + //TODO + //apiResult.Data = powerStationSummary; + apiResult.Data = null; } catch (Exception exception) { diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index 2657926..fb525eb 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -2178,6 +2178,19 @@ COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB ; +-- 修改電站資料型態 20210901 +ALTER TABLE `power_station` + CHANGE COLUMN `today_kwhkwp` `today_kwhkwp` DECIMAL(10,3) UNSIGNED NOT NULL DEFAULT '0.000' COMMENT '今日kwhkwp' AFTER `Total_kwh`, + CHANGE COLUMN `avg_kwhkwp` `avg_kwhkwp` DECIMAL(10,3) UNSIGNED NOT NULL DEFAULT '0.000' COMMENT '30天平均kwhkwp' AFTER `today_kwhkwp`, + CHANGE COLUMN `today_money` `today_money` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '今日金額' AFTER `avg_kwhkwp`, + CHANGE COLUMN `total_money` `total_money` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '總金額' AFTER `today_money`, + CHANGE COLUMN `today_PR` `today_PR` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '電站Pr值' AFTER `total_money`, + CHANGE COLUMN `avg_PR` `avg_PR` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '平均Pr值' AFTER `today_PR`, + CHANGE COLUMN `today_carbon` `today_carbon` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '今日減碳量' AFTER `avg_PR`, + CHANGE COLUMN `total_carbon` `total_carbon` DECIMAL(10,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '總減碳量' AFTER `today_carbon`, + CHANGE COLUMN `today_irradiance` `today_irradiance` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '今日日照度' AFTER `total_carbon`, + CHANGE COLUMN `avg_irradiance` `avg_irradiance` DECIMAL(5,2) UNSIGNED NOT NULL DEFAULT '0.00' COMMENT '平均日照度' AFTER `today_irradiance`; + /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; diff --git a/SolarPower/Models/MyBaseModel.cs b/SolarPower/Models/MyBaseModel.cs index 1c8c3ba..9789881 100644 --- a/SolarPower/Models/MyBaseModel.cs +++ b/SolarPower/Models/MyBaseModel.cs @@ -51,7 +51,7 @@ namespace SolarPower.Models public string Email { get; set; } public MyCompany Company { get; set; } //公司資訊 public MyRole Role { get; set; } //角色資訊 - public List PowerStationSummaries { get; set; } + public List myPowerStationGroupByCities { get; set; } } /// @@ -83,11 +83,11 @@ namespace SolarPower.Models public string Name { get; set; } } - public class MyPowerStationSummary + public class MyPowerStationGroupByCity { public string CityName { get; set; } public int Amount { get; set; } - public List MyPowerStations { get; set; } + public List MyPowerStations { get; set; } } public class MyPowerStation @@ -108,9 +108,19 @@ namespace SolarPower.Models public string Name { get; set; } } - public class OrderBy + public class PowerStationsSummary { - public string Col { get; set; } - public string Sort { get; set; } + public double Today_kwh { get; set; } //今日總發電量 + public double Total_kwh { get; set; } //累計總發電量 + public double Today_irradiance { get; set; } //即時平均日照度 + public double Avg_irradiance { get; set; } //平均日照度(30天) + public double Today_PR { get; set; } //即時平均 PR 值 + public double Avg_PR { get; set; } //平均 PR 值(30天) + public double Today_kwhkwp { get; set; } //即時平均 kWh / kWp + public double Avg_kwhkwp { get; set; } //平均 kWh / kWp (30天) + public double Today_money { get; set; } //今日金額 + public double Total_money { get; set; } //總金額 + public double Today_carbon { get; set; } //今日減碳量 + public double Total_carbon { get; set; } //累積減碳量 } } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index 3d0ed9c..5eb89f1 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -23,7 +23,7 @@ namespace SolarPower.Repository.Implement tableName = "power_station"; } - public List GetMyPowerStationSummary(MyUser myUser, List wheres = null, List orderBy = null) + public List GetMyPowerStationList(MyUser myUser, List cityIds, List wheres = null, List orderBy = null) { var results = new List(); @@ -49,6 +49,11 @@ namespace SolarPower.Repository.Implement sql += @" WHERE ps.Deleted = 0"; } + if(cityIds != null && cityIds.Count > 0) + { + sql += @" AND c.Id IN @CityIds"; + } + if (wheres != null && wheres.Count > 0) { var temp_where = ""; @@ -62,31 +67,13 @@ namespace SolarPower.Repository.Implement temp_order = string.Join(" , ", orderBy); sql += $" ORDER BY c.Priority, {temp_order}"; } - - - - var myPowerStationInfos = conn.Query(sql, new { CompanyId = myUser.CompanyId, UserId = myUser.Id, Filter = filter }).ToList(); - - var myPowerStationInfos_group = myPowerStationInfos.GroupBy(x => x.CityId); - - foreach (var myPowerStationInfo in myPowerStationInfos_group) + else { - MyPowerStationSummary myPowerStationSummary = new MyPowerStationSummary(); - myPowerStationSummary.CityName = myPowerStationInfo.First().CityName; - myPowerStationSummary.Amount = myPowerStationInfo.Count(); - myPowerStationSummary.MyPowerStations = new List(); - foreach (var info in myPowerStationInfo) - { - MyPowerStation myPowerStation = new MyPowerStation(); - myPowerStation.PowerStationId = info.Id; - myPowerStation.PowerStationName = info.Name; - - myPowerStationSummary.MyPowerStations.Add(myPowerStation); - } - - results.Add(myPowerStationSummary); + sql += $" ORDER BY c.Priority"; } + results = conn.Query(sql, new { CompanyId = myUser.CompanyId, UserId = myUser.Id, CityIds = cityIds }).ToList(); + } catch (Exception exception) { diff --git a/SolarPower/Repository/Interface/IPowerStationRepository.cs b/SolarPower/Repository/Interface/IPowerStationRepository.cs index 757aa6c..26396f7 100644 --- a/SolarPower/Repository/Interface/IPowerStationRepository.cs +++ b/SolarPower/Repository/Interface/IPowerStationRepository.cs @@ -17,7 +17,8 @@ namespace SolarPower.Repository.Interface /// /// /// - List GetMyPowerStationSummary(MyUser myUser, string filter = ""); + //List GetMyPowerStationSummary(MyUser myUser, string filter = ""); + List GetMyPowerStationList(MyUser myUser, List cityIds = null, List wheres = null, List orderBy = null); /// /// 查詢縣市列表 diff --git a/SolarPower/Services/Implement/MyPowerStationService.cs b/SolarPower/Services/Implement/MyPowerStationService.cs index 90b1ce2..848e274 100644 --- a/SolarPower/Services/Implement/MyPowerStationService.cs +++ b/SolarPower/Services/Implement/MyPowerStationService.cs @@ -17,10 +17,82 @@ namespace SolarPower.Services.Implement this.powerStationRepository = powerStationRepository; } - public List GetMyPowerStations(MyUser myUser, List CityIds = null, List wheres = null, List orderBy = null) + public List GetMyPowerStations(MyUser myUser, List cityIds = null, List wheres = null, List orderBy = null) { List powerStations = new List(); - return powerStations; + + powerStations = powerStationRepository.GetMyPowerStationList(myUser, cityIds, wheres, orderBy); + + var myPowerStations_group = powerStations.GroupBy(x => x.CityId); + + List myPowerStationGroupByCities = new List(); + foreach (var powerStations_group_item in myPowerStations_group) + { + MyPowerStationGroupByCity myPowerStationGroupByCity = new MyPowerStationGroupByCity(); + myPowerStationGroupByCity.CityName = powerStations_group_item.First().CityName; + myPowerStationGroupByCity.Amount = powerStations_group_item.Count(); + myPowerStationGroupByCity.MyPowerStations = powerStations_group_item.ToList(); + + myPowerStationGroupByCities.Add(myPowerStationGroupByCity); + } + + return myPowerStationGroupByCities; + } + + public PowerStationsSummary GetMyPowerStationsSummary(List powerStations) + { + PowerStationsSummary powerStationsSummary = new PowerStationsSummary(); + + if(powerStations.Count() <= 0) + { + return powerStationsSummary; + } + + var sum_Today_kwh = 0.0; + var sum_Total_kwh = 0.0; + var sum_Today_irradiance = 0.0; + var sum_Avg_irradiance = 0.0; + var sum_Today_PR = 0.0; + var sum_Avg_PR = 0.0; + var sum_Today_kwhkwp = 0.0; + var sum_Avg_kwhkwp = 0.0; + var sum_Today_money = 0.0; + var sum_Total_money = 0.0; + var sum_Today_carbon = 0.0; + var sum_Total_carbon = 0.0; + + var count_powerStation = powerStations.Count(); + + foreach (var powerStation in powerStations) + { + sum_Today_kwh += powerStation.Today_kWh; + sum_Total_kwh += powerStation.Total_kWh; + sum_Today_irradiance += powerStation.Today_irradiance; + sum_Avg_irradiance += powerStation.Avg_irradiance; + sum_Today_PR += powerStation.Today_PR; + sum_Avg_PR += powerStation.Avg_PR; + sum_Today_kwhkwp += powerStation.Today_kwhkwp; + sum_Avg_kwhkwp += powerStation.Avg_kwhkwp; + sum_Today_money += powerStation.Today_Money; + sum_Total_money += powerStation.Total_Money; + sum_Today_carbon += powerStation.Today_Carbon; + sum_Total_carbon += powerStation.Total_Carbon; + } + + powerStationsSummary.Today_kwh = sum_Today_kwh; + powerStationsSummary.Total_kwh = sum_Total_kwh; + powerStationsSummary.Today_irradiance = sum_Today_irradiance / count_powerStation; + powerStationsSummary.Avg_irradiance = sum_Avg_irradiance / count_powerStation; + powerStationsSummary.Today_PR = sum_Today_PR / count_powerStation; + powerStationsSummary.Avg_PR = sum_Avg_PR / count_powerStation; + powerStationsSummary.Today_kwhkwp = sum_Today_kwhkwp / count_powerStation; + powerStationsSummary.Avg_kwhkwp = sum_Avg_kwhkwp / count_powerStation; + powerStationsSummary.Today_money = sum_Today_money; + powerStationsSummary.Total_money = sum_Total_money; + powerStationsSummary.Today_carbon = sum_Today_carbon; + powerStationsSummary.Total_carbon = sum_Total_carbon; + + return powerStationsSummary; } - } +} } diff --git a/SolarPower/Views/Shared/_Layout.cshtml b/SolarPower/Views/Shared/_Layout.cshtml index c7f2e3d..f039a68 100644 --- a/SolarPower/Views/Shared/_Layout.cshtml +++ b/SolarPower/Views/Shared/_Layout.cshtml @@ -199,23 +199,23 @@ 電站資訊