修改電站抓取方式 part2

This commit is contained in:
Kai 2021-09-01 10:41:16 +08:00
parent 7c741e8bc6
commit e84c23b616
9 changed files with 161 additions and 66 deletions

View File

@ -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<int> powerStationIds = await powerStationRepository.GetPowerStationIdsByUserRole(myUser);
List<int> 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<int> 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");

View File

@ -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;

View File

@ -93,15 +93,17 @@ namespace SolarPower.Controllers
}
[HttpPost]
public ApiResult<List<MyPowerStationSummary>> GetPowerStationCollapse(string filter)
public ApiResult<List<MyPowerStationGroupByCity>> GetPowerStationCollapse(string filter)
{
ApiResult<List<MyPowerStationSummary>> apiResult = new ApiResult<List<MyPowerStationSummary>>();
ApiResult<List<MyPowerStationGroupByCity>> apiResult = new ApiResult<List<MyPowerStationGroupByCity>>();
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)
{

View File

@ -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, '') */;

View File

@ -51,7 +51,7 @@ namespace SolarPower.Models
public string Email { get; set; }
public MyCompany Company { get; set; } //公司資訊
public MyRole Role { get; set; } //角色資訊
public List<MyPowerStationSummary> PowerStationSummaries { get; set; }
public List<MyPowerStationGroupByCity> myPowerStationGroupByCities { get; set; }
}
/// <summary>
@ -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<MyPowerStation> MyPowerStations { get; set; }
public List<PowerStation.PowerStation> 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; } //累積減碳量
}
}

View File

@ -23,7 +23,7 @@ namespace SolarPower.Repository.Implement
tableName = "power_station";
}
public List<PowerStation> GetMyPowerStationSummary(MyUser myUser, List<string> wheres = null, List<string> orderBy = null)
public List<PowerStation> GetMyPowerStationList(MyUser myUser, List<int> cityIds, List<string> wheres = null, List<string> orderBy = null)
{
var results = new List<PowerStation>();
@ -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<MyPowerStationInfo>(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<MyPowerStation>();
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<PowerStation>(sql, new { CompanyId = myUser.CompanyId, UserId = myUser.Id, CityIds = cityIds }).ToList();
}
catch (Exception exception)
{

View File

@ -17,7 +17,8 @@ namespace SolarPower.Repository.Interface
/// </summary>
/// <param name="myUser"></param>
/// <returns></returns>
List<MyPowerStationSummary> GetMyPowerStationSummary(MyUser myUser, string filter = "");
//List<MyPowerStationSummary> GetMyPowerStationSummary(MyUser myUser, string filter = "");
List<PowerStation> GetMyPowerStationList(MyUser myUser, List<int> cityIds = null, List<string> wheres = null, List<string> orderBy = null);
/// <summary>
/// 查詢縣市列表

View File

@ -17,10 +17,82 @@ namespace SolarPower.Services.Implement
this.powerStationRepository = powerStationRepository;
}
public List<PowerStation> GetMyPowerStations(MyUser myUser, List<int> CityIds = null, List<string> wheres = null, List<OrderBy> orderBy = null)
public List<MyPowerStationGroupByCity> GetMyPowerStations(MyUser myUser, List<int> cityIds = null, List<string> wheres = null, List<string> orderBy = null)
{
List<PowerStation> powerStations = new List<PowerStation>();
return powerStations;
powerStations = powerStationRepository.GetMyPowerStationList(myUser, cityIds, wheres, orderBy);
var myPowerStations_group = powerStations.GroupBy(x => x.CityId);
List<MyPowerStationGroupByCity> myPowerStationGroupByCities = new List<MyPowerStationGroupByCity>();
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<PowerStation> 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;
}
}
}
}

View File

@ -199,23 +199,23 @@
<span class="nav-link-text" data-i18n="nav.category">電站資訊</span>
</a>
<ul>
@foreach (var myPowerStationSummary in ViewBag.myPowerStationSummaries)
@foreach (var MyPowerStationGroupByCity in ViewBag.myPowerStationGroupByCities)
{
<li class="@(ViewData["MainNum"].ToString() == "2" &&
ViewData["SubNum"].ToString() == ViewBag.myPowerStationSummaries.IndexOf(myPowerStationSummary).ToString() ? "active open" : "")">
ViewData["SubNum"].ToString() == ViewBag.myPowerStationGroupByCities.IndexOf(MyPowerStationGroupByCity).ToString() ? "active open" : "")">
<a href="javascript:void(0);" title="Category" data-filter-tags="utilities menu child sublevel item">
<span class="nav-link-text" data-i18n="nav.category">@myPowerStationSummary.CityName</span>
<span class="dl-ref bg-primary-500 hidden-nav-function-minify hidden-nav-function-top">@myPowerStationSummary.Amount</span>
<span class="nav-link-text" data-i18n="nav.category">@MyPowerStationGroupByCity.CityName</span>
<span class="dl-ref bg-primary-500 hidden-nav-function-minify hidden-nav-function-top">@MyPowerStationGroupByCity.Amount</span>
</a>
<ul>
@foreach (var myPowerStation in myPowerStationSummary.MyPowerStations)
@foreach (var myPowerStation in MyPowerStationGroupByCity.MyPowerStations)
{
<li class="@(ViewData["MainNum"].ToString() == "2" &&
ViewData["SubNum"].ToString() == ViewBag.myPowerStationSummaries.IndexOf(myPowerStationSummary).ToString() &&
ViewData["TagNum"].ToString() == myPowerStationSummary.MyPowerStations.IndexOf(myPowerStation).ToString() ? "active" : "")">
<a asp-controller="StationOverview" asp-action="Info" asp-route-stationId="@myPowerStation.PowerStationId" title="Sublevel Item" data-filter-tags="utilities menu child sublevel item">
<span class="nav-link-text" data-i18n="nav.utilities_menu_child_sublevel_item">@myPowerStation.PowerStationName</span>
ViewData["SubNum"].ToString() == ViewBag.myPowerStationGroupByCities.IndexOf(MyPowerStationGroupByCity).ToString() &&
ViewData["TagNum"].ToString() == MyPowerStationGroupByCity.MyPowerStations.IndexOf(myPowerStation).ToString() ? "active" : "")">
<a asp-controller="StationOverview" asp-action="Info" asp-route-stationId="@myPowerStation.Id" title="Sublevel Item" data-filter-tags="utilities menu child sublevel item">
<span class="nav-link-text" data-i18n="nav.utilities_menu_child_sublevel_item">@myPowerStation.Name</span>
</a>
</li>