日區間

This commit is contained in:
b110212000 2021-07-28 19:23:12 +08:00
parent 4a3775f1b6
commit 9bb9b6502d
6 changed files with 221 additions and 36 deletions

1
.gitignore vendored
View File

@ -342,3 +342,4 @@ ASALocalRun/
healthchecksdb healthchecksdb
/SolarPower/wwwroot/upload/operation_recode/1

View File

@ -1,10 +1,15 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SolarPower.Models; using SolarPower.Models;
using SolarPower.Models.PowerStation; using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface; using SolarPower.Repository.Interface;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -115,6 +120,80 @@ namespace SolarPower.Controllers
return apiResult; return apiResult;
} }
public FileResult ExportExcel(string post)
{
var postObject = JsonConvert.DeserializeObject<Excel>(post);
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
ICellStyle style12 = workbook.CreateCellStyle();
style12.SetFont(font12);
style12.Alignment = HorizontalAlignment.Center;
style12.VerticalAlignment = VerticalAlignment.Center;
IFont font12Times = workbook.CreateFont();
font12Times.FontName = "Times New Roman";
font12Times.FontHeightInPoints = 12;
IFont font18 = workbook.CreateFont();
font18.FontName = "新細明體";
font18.FontHeightInPoints = 18;
font18.IsBold = true;
ICellStyle styleTitle18 = workbook.CreateCellStyle();
styleTitle18.SetFont(font18);
styleTitle18.Alignment = HorizontalAlignment.Center;
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLeft12 = workbook.CreateCellStyle();
styleLeft12.SetFont(font12);
styleLeft12.Alignment = HorizontalAlignment.Left;
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLine12 = workbook.CreateCellStyle();
styleLine12.SetFont(font12);
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleLine12.VerticalAlignment = VerticalAlignment.Center;
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
ICellStyle stylein12 = workbook.CreateCellStyle();
stylein12.SetFont(font12Times);
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stylein12.VerticalAlignment = VerticalAlignment.Center;
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
foreach(var powerstationid in postObject.PowerStation)
{
var sheet = workbook.CreateSheet(powerstationid.Name);
Select_table select_Table = new Select_table
{
FormType = postObject.FormType,
SearchType = postObject.SearchType,
Time = postObject.Time,
PowerStation = Convert.ToInt32(powerstationid.Value)
};
var Formbody = GetForm(select_Table);
var Formhead = GetTableHead(select_Table);
int RowPosition = 0;
IRow row = sheet.CreateRow(RowPosition);
ICell cell = row.CreateCell(0);
}
var ms = new NpoiMemoryStream
{
AllowClose = false
};
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "text.xlsx");
}
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -24,6 +25,32 @@ namespace SolarPower.Models
public int PowerStation { get; set; } public int PowerStation { get; set; }
public int FormType { get; set; } public int FormType { get; set; }
} }
public class Excel
{
public int SearchType { get; set; }
public string Time { get; set; }
public int FormType { get; set; }
public List<Excelpowerstation> PowerStation { get; set; }
}
public class Excelpowerstation
{
public string Name { get; set; }
public string Value { get; set; }
}
public class NpoiMemoryStream : MemoryStream
{
public NpoiMemoryStream()
{
AllowClose = true;
}
public bool AllowClose { get; set; }
public override void Close()
{
if (AllowClose)
base.Close();
}
}
} }

View File

@ -146,19 +146,59 @@ namespace SolarPower.Repository.Implement
} }
else else
{ {
sql = ""; var times = post.Time.Replace('-', 'a').Replace('/', '-').Replace(" ", "").Split('a');
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-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = b.report_date
left join
( # month
SELECT powerStationid, ROUND(AVG(TODAYKWH),2) AS monthKWH,
ROUND(AVG(KWHKWP),2) AS KWHKWP,
ROUND(AVG(PR),2) AS PR,
ROUND(SUM(MONEY),2) AS money,
ROUND(SUM(SOLARHOUR),2) AS SOLARHOUR
FROM power_station_history_day where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
) c on a.powerStationid = c.powerStationid
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-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
) 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(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
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;";
} }
break; break;
} }
a = await conn.QueryAsync<dynamic>(sql); a = await conn.QueryAsync<dynamic>(sql);
} }
catch (Exception exception) catch (Exception exception)
{ {

View File

@ -28,6 +28,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.17" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.17" />
<PackageReference Include="MySql.Data" Version="8.0.24" /> <PackageReference Include="MySql.Data" Version="8.0.24" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NPOI" Version="2.5.3" />
<PackageReference Include="Quartz" Version="3.3.2" /> <PackageReference Include="Quartz" Version="3.3.2" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" /> <PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />

View File

@ -19,7 +19,7 @@
<div id="js_list_accordion" class="accordion accordion-hover accordion-clean js-list-filter"> <div id="js_list_accordion" class="accordion accordion-hover accordion-clean js-list-filter">
<div class="card border-top-left-radius-0 border-top-right-radius-0"> <div class="card border-top-left-radius-0 border-top-right-radius-0">
<div class="card-header"> <div class="card-header">
<a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-a" aria-expanded="false" data-filter-tags="settings"> <a href="javascript:void(0);" class="card-title collapsed" data-toggle="collapse" data-target="#js_list_accordion-a" aria-expanded="true" data-filter-tags="settings" >
<i class="fal fa-globe width-2 fs-xl"></i> <i class="fal fa-globe width-2 fs-xl"></i>
新北市 新北市
<span class="ml-auto"> <span class="ml-auto">
@ -166,7 +166,7 @@
</div> </div>
</div> </div>
<div class="col-auto px-0"> <div class="col-auto px-0">
<a href="javascript:;" data-target=".sidebar" data-toggle="collapse" class="btn btn-default btn-xs btn-icon waves-effect waves-themed" style="border-radius: 0;"><i onclick="myfunc(this)" class="fal fa-angle-right fa-lg py-3"></i></a> <a href="javascript:;" data-target=".sidebar" data-toggle="collapse" class="btn btn-default btn-xs btn-icon waves-effect waves-themed" style="border-radius: 0;"><i onclick="myfunc(this)" class="fal fa-angle-right fa-lg py-3" id="collapse"></i></a>
</div> </div>
<main class="col px-5 pl-md-2 main"> <main class="col px-5 pl-md-2 main">
@ -211,7 +211,7 @@
</div> </div>
<div class="mb-3 d-flex justify-content-start"> <div class="mb-3 d-flex justify-content-start">
<div class="pr-3"> <div class="pr-3">
<button type="button" class="btn btn-primary waves-effect waves-themed"><span class="fal fa-file-excel mr-1"></span> 匯出</button> <button type="button" class="btn btn-primary waves-effect waves-themed" onclick="ExportExcel()"><span class="fal fa-file-excel mr-1"></span> 匯出</button>
</div> </div>
<div class="row"> <div class="row">
<div class="pr-3" id="selectOneStation"> <div class="pr-3" id="selectOneStation">
@ -239,7 +239,6 @@
<div class="row mb-5"> <div class="row mb-5">
<div class="col-xl-12"> <div class="col-xl-12">
<div class="card p-3 w-100 overflow-auto"> <div class="card p-3 w-100 overflow-auto">
<table class="table m-0"> <table class="table m-0">
<thead id="TableHead"> <thead id="TableHead">
</thead> </thead>
@ -259,10 +258,15 @@
var datepicker; var datepicker;
var timerange;//選取時間 var timerange;//選取時間
var selecterd_invert = []; var selecterd_invert = [];
var nowpowerstation;//選擇電站 var nowpowerstation = null;//選擇電站
var haveinvertName = []; var haveinvertName = [];
var nowform;
$(function () { $(function () {
//#region 預設初始值 //#region 預設初始值
var a = $('#collapse').trigger("click");
//document.getElementById("collapse").click();
$('.overflow-auto').hide();
$('#DateGet').val(new Date().toISOString().substring(0, 10)); $('#DateGet').val(new Date().toISOString().substring(0, 10));
document.getElementById("DateGettextdiv").style.display = "none";//隱藏 document.getElementById("DateGettextdiv").style.display = "none";//隱藏
$('#DateGet').attr('style', 'width:205px'); $('#DateGet').attr('style', 'width:205px');
@ -441,15 +445,16 @@
if (inverter.value == nowpowerstation) if (inverter.value == nowpowerstation)
{ {
stri += '<button type="button" class="btn btn-success waves-effect waves-themed ml-2 mb-2 btn-station" id="' + inverter.value + '" onclick="selectPowerStation(' + inverter.value + ',this) ">' + inverter.name + '</button>'; stri += '<button type="button" class="btn btn-success waves-effect waves-themed ml-2 mb-2 btn-station" id="' + inverter.value + '" onclick="selectPowerStation(' + inverter.value + ',this) ">' + inverter.name + '</button>';
} }
else else
{ {
stri += '<button type="button" class="btn btn-outline-success waves-effect waves-themed ml-2 mb-2 btn-station" id="' + inverter.value + '" onclick="selectPowerStation(' + inverter.value + ',this) ">' + inverter.name + '</button>'; stri += '<button type="button" class="btn btn-outline-success waves-effect waves-themed ml-2 mb-2 btn-station" id="' + inverter.value + '" onclick="selectPowerStation(' + inverter.value + ',this) ">' + inverter.name + '</button>';
} }
}) })
$('#selectOneStation').append(stri); $('#selectOneStation').append(stri);
if (nowpowerstation == null && selecterd_invert.length>0) {
document.getElementById(selecterd_invert[0].value).onclick();
}
} }
//#endregion //#endregion
@ -489,22 +494,18 @@
if (this.checked) { if (this.checked) {
selecterd_invert.push(getstation); selecterd_invert.push(getstation);
} else { } else {
//var ss = $.inArray(getstation, selecterd_invert);
//var a = 0;
var a = selecterd_invert.filter(function (n, i) { var a = selecterd_invert.filter(function (n, i) {
if (n.name === getstation.name && n.value === getstation.value) { if (n.name === getstation.name && n.value === getstation.value) {
if (nowpowerstation == getstation.value) { if (nowpowerstation == getstation.value) {
nowpowerstation = null; nowpowerstation = null;
} }
console.log(n);
selecterd_invert.splice(i, 1); selecterd_invert.splice(i, 1);
} }
}); });
} }
AddButtonWithStation(); AddButtonWithStation();
console.log(selecterd_invert);
}); });
@ -549,14 +550,37 @@
'<div class="card-body">' + '<div class="card-body">' +
'<ul class="list-group list-group-flush">'; '<ul class="list-group list-group-flush">';
$.each(inverterCollapse[key], function (index, inverter) { $.each(inverterCollapse[key], function (index, inverter) {
str += '<li class="list-group-item">' + var getstation =
'<div class="d-flex justify-content-between">' + {
'<h4 class="font-weight-bold"><i class="fal fa-charging-station"></i> ' + inverter.powerStationName + '</h4>' + name: inverter.powerStationName,
'<div class="">' + value: String(inverter.powerStationId)
'<input type="checkbox" class="" name="selectedInverterLayer2[]" value="' + inverter.powerStationId + '" valueName ="' + inverter.powerStationName+'">' + }
'</div>' + var on = false;
'</div>' + var a = selecterd_invert.find(function (n, i) {
'</li>'; if (n.name === getstation.name && n.value === getstation.value) {
on = true;
}
});
if (on == true) {
str += '<li class="list-group-item">' +
'<div class="d-flex justify-content-between">' +
'<h4 class="font-weight-bold"><i class="fal fa-charging-station"></i> ' + inverter.powerStationName + '</h4>' +
'<div class="">' +
'<input type="checkbox" class="" name="selectedInverterLayer2[]" value="' + inverter.powerStationId + '" valueName ="' + inverter.powerStationName + '" checked>' +
'</div>' +
'</div>' +
'</li>';
}
else {
str += '<li class="list-group-item">' +
'<div class="d-flex justify-content-between">' +
'<h4 class="font-weight-bold"><i class="fal fa-charging-station"></i> ' + inverter.powerStationName + '</h4>' +
'<div class="">' +
'<input type="checkbox" class="" name="selectedInverterLayer2[]" value="' + inverter.powerStationId + '" valueName ="' + inverter.powerStationName + '">' +
'</div>' +
'</div>' +
'</li>';
}
}); });
str += '</ul>'; str += '</ul>';
@ -574,7 +598,8 @@
} }
}); });
$("#js_list_accordion .collapse").collapse('show'); $("#js_list_accordion .collapse").collapse('show');
}, 'json'); }, 'json');
} }
//#endregion //#endregion
@ -629,12 +654,12 @@
}, 'json'); }, 'json');
} }
function Dateform(form) { function Dateform(form) {
tablehand(form); tablehand(form);
nowform = form;
} }
function tablebody(form) function tablebody(form)
{ {
var send_data = var send_data =
@ -670,14 +695,14 @@
if (inverter[i] == null) { if (inverter[i] == null) {
sta += "<td>" + 0 + "</td>"; sta += "<td>" + 0 + "</td>";
} else { } else {
sta += "<td>" + inverter[i] + "</td>"; sta += "<td>" + Number(inverter[i]).toFixed(2) + "</td>";
} }
}); });
sta += "<td>" + inverter.hourKWH + "</td>"; sta += "<td>" + Number(inverter.hourKWH) + "</td>";
sta += "<td>" + inverter.hourKWHp + "</td>"; sta += "<td>" + Number(inverter.hourKWHp) + "</td>";
sta += "<td>" + inverter.irradiance + "</td>"; sta += "<td>" + Number(inverter.irradiance) + "</td>";
sta += "<td>" + inverter.temperature + "</td>"; sta += "<td>" + Number(inverter.temperature) + "</td>";
sta += "<td>" + inverter.hourmoney + "</td>"; sta += "<td>" + Number(inverter.hourmoney) + "</td>";
sta += "</tr>"; sta += "</tr>";
thour = inverter.tothour ? inverter.tothour.toFixed(2) : 0; thour = inverter.tothour ? inverter.tothour.toFixed(2) : 0;
tpr = inverter.pr ? inverter.pr.toFixed(2) : 0; tpr = inverter.pr ? inverter.pr.toFixed(2) : 0;
@ -786,11 +811,23 @@
$('#tothead').append(stc); $('#tothead').append(stc);
haveinvertName = []; haveinvertName = [];
} }
$('.overflow-auto').show();
}, 'json'); }, 'json');
} }
function ExportExcel() {
var send_data =
{
SearchType: searchType,
Time: timerange,
FormType: nowform,
PowerStation: selecterd_invert
}
window.location = "/StationReport/ExportExcel?post=" + JSON.stringify(send_data);
}
</script> </script>
} }