excel 日月報完成

This commit is contained in:
b110212000 2021-07-29 18:04:55 +08:00
parent 2a48cc71c7
commit 75753f21e7
5 changed files with 546 additions and 21 deletions

View File

@ -3,21 +3,24 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using SolarPower.Models;
using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SolarPower.Controllers
{
public class StationReportController : MyBaseController<StationReportController>
{
private readonly IPowerStationRepository powerStationRepository;
private readonly IStationReportRepository stationReportRepository;
@ -165,7 +168,7 @@ namespace SolarPower.Controllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
foreach(var powerstationid in postObject.PowerStation)
foreach (var powerstationid in postObject.PowerStation)
{
var sheet = workbook.CreateSheet(powerstationid.Name);
Select_table select_Table = new Select_table
@ -175,13 +178,412 @@ namespace SolarPower.Controllers
Time = postObject.Time,
PowerStation = Convert.ToInt32(powerstationid.Value)
};
var Formbody = GetForm(select_Table);
var Formhead = GetTableHead(select_Table);
var Formbody = GetForm(select_Table);//取body
var Formhead = GetTableHead(select_Table);//取head
int RowPosition = 0;
switch (postObject.FormType)
{
case 0: //日報表
#region head //RowPosition = 0
IRow row = sheet.CreateRow(RowPosition);
ICell cell = row.CreateCell(0);
int index = 0;
cell.SetCellValue("Date");
cell.CellStyle = styleLine12;
sheet.SetColumnWidth(index, 4 * 160 * 8);
index++;
foreach (var head in Formhead.Result.Data)
{
cell = row.CreateCell(index);
cell.SetCellValue(head);
cell.CellStyle = styleLine12;
sheet.SetColumnWidth(index, 4 * 160 * 6);
index++;
}
string[] lasthead =
{
"小時發電量(kWh)",
"小時發電量百分比(%)",
"小時平均日照度(W/㎡)",
"小時平均模組溫度(°C)",
"小時售電金額(NTD)",
};
foreach (var head in lasthead)
{
cell = row.CreateCell(index);
cell.SetCellValue(head);
cell.CellStyle = styleLine12;
sheet.SetColumnWidth(index, 4 * 160 * 12);
index++;
}
#endregion
#region body //RowPosition = 1
RowPosition++;
var thour = ""; var tpr = ""; var tkwh = ""; var kWhkwp = ""; var ntd = ""; var ntdone = "";
foreach (dynamic body in Formbody.Result.Data)
{
index = 0;
var dbody = body as IDictionary<string, object>;
thour = Checknull(Math.Round(Convert.ToDouble(dbody["tothour"]), 2).ToString());
tpr = Checknull(Math.Round(Convert.ToDouble(dbody["pr"]), 2).ToString());
tkwh = Checknull(Math.Round(Convert.ToDouble(dbody["totKWH"]), 2).ToString());
kWhkwp = Checknull(Math.Round(Convert.ToDouble(dbody["totKWHKWP"]), 2).ToString());
ntd = Checknull(Math.Round(Convert.ToDouble(dbody["totmoney"]), 2).ToString());
double onemoney = Convert.ToDouble(ntd) / Convert.ToDouble(tkwh);
if (double.IsNaN(onemoney))
{
onemoney = 0;
}
ntdone = Checknull(Math.Round(onemoney, 2).ToString());
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
var b = dbody["report_date"].ToString();
cell.SetCellValue(b);
cell.CellStyle = styleLine12;
index++;
foreach (var head in Formhead.Result.Data)
{
cell = row.CreateCell(index);
cell.SetCellValue(dbody[head] == null ? "0" : dbody[head].ToString());
cell.CellStyle = styleLine12;
index++;
}
string[] bodynames = {
"hourKWH",
"hourKWHp",
"irradiance",
"temperature",
"hourmoney",
};
foreach (var bodyname in bodynames)
{
cell = row.CreateCell(index);
if (dbody[bodyname] == null)
{
cell.SetCellValue("0");
}
else
{
var c = dbody[bodyname].ToString();
cell.SetCellValue(c);
}
cell.CellStyle = styleLine12;
index++;
}
RowPosition++;
}
#endregion
#region //RowPosition = body + 2
RowPosition += 2;//空兩行
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("日照小時:");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(thour);
cell.CellStyle = styleLine12;
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("kWH/kWP:");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(kWhkwp);
cell.CellStyle = styleLine12;
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("PR%:");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(tpr);
cell.CellStyle = styleLine12;
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("日發電量(kWh):");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(tkwh);
cell.CellStyle = styleLine12;
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("日售電金額(NTD):");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(ntd);
cell.CellStyle = styleLine12;
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("日售電單價(NTD):");
cell.CellStyle = styleLine12;
cell = row.CreateCell(1);
cell.SetCellValue(ntdone);
cell.CellStyle = styleLine12;
#endregion
break;
case 1: // 月報表
#region head //RowPosition = 0
IRow row2 = sheet.CreateRow(RowPosition);
ICell cell2 = row2.CreateCell(0);
int index2 = 0;
cell2.SetCellValue("Date");
cell2.CellStyle = styleLine12;
sheet.SetColumnWidth(index2, 4 * 160 * 8);
index2++;
foreach (var head in Formhead.Result.Data)
{
cell2 = row2.CreateCell(index2);
cell2.SetCellValue(head);
cell2.CellStyle = styleLine12;
sheet.SetColumnWidth(index2, 4 * 160 * 8);
index2++;
}
string[] lasthead2 =
{
"日發電量(kWh)",
"日發電量百分比(%)",
"日照小時(hr)",
"kWH/kWP",
"PR%",
"日平均日照度(W/㎡)",
"日平均模組溫度(°C)",
"日售電金額(NTD)"
};
foreach (var head in lasthead2)
{
cell2 = row2.CreateCell(index2);
cell2.SetCellValue(head);
cell2.CellStyle = styleLine12;
sheet.SetColumnWidth(index2, 4 * 160 * 12);
index2++;
}
#endregion
#region body //RowPosition = 1
RowPosition++;
var avghour = "0"; var avgKWHKWP = "0"; var avgdayKWH = "0"; var monthKWH = "0"; var monthmoney = "0"; var monthmoneyone = "0";
var check_hire = false;
var sitedb = "";
foreach (dynamic body in Formbody.Result.Data)
{
index2 = 0;
var dbody = body as IDictionary<string, object>;
avghour = (Convert.ToDouble(dbody["tothour"].ToString()) + Convert.ToDouble(avghour)).ToString();
avgKWHKWP = (Convert.ToDouble(dbody["KWHKWP"].ToString()) + Convert.ToDouble(avgKWHKWP)).ToString();
avgdayKWH = (Convert.ToDouble(dbody["dayKWH"].ToString()) + Convert.ToDouble(avgdayKWH)).ToString();
monthKWH = dbody["monthKWH"] == null ? "0": dbody["monthKWH"].ToString();
monthmoney = dbody["monthmoney"] == null ? "0" : dbody["monthmoney"].ToString();
monthmoneyone = (Convert.ToDouble(dbody["monthmoneyone"].ToString()) + Convert.ToDouble(monthmoneyone)).ToString();
if (dbody["SolarType"].ToString() == "1")//檢驗是否為租用
{
check_hire = true;
sitedb = dbody["SiteDB"].ToString();
}
row2 = sheet.CreateRow(RowPosition);
cell2 = row2.CreateCell(index2);
var b = dbody["report_date"].ToString();
cell2.SetCellValue(b);
cell2.CellStyle = styleLine12;
index2++;
foreach (var head in Formhead.Result.Data)
{
cell2 = row2.CreateCell(index2);
cell2.SetCellValue(dbody[head] == null ? "0": dbody[head].ToString());
cell2.CellStyle = styleLine12;
index2++;
}
string[] bodynames = {
"dayKWH",
"dayKWHp",
"tothour",
"KWHKWP",
"PR",
"irradiance",
"temperature",
"soldmoney"
};
foreach (var bodyname in bodynames)
{
cell2 = row2.CreateCell(index2);
if (dbody[bodyname] == null)
{
cell2.SetCellValue("0");
}
else
{
var c = dbody[bodyname].ToString();
cell2.SetCellValue(c);
}
cell2.CellStyle = styleLine12;
index2++;
}
RowPosition++;
}
#endregion
#region //RowPosition = body + 2
RowPosition += 2;//空兩行
index = 0;
var Useday = Formbody.Result.Data.Count;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("日日照小時平均");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("日kWH/kWP平均");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("日發電量平均(kWh)");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("月發電量(kWh)");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("月售電金額(NTD)");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("月售電單價(NTD)");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("月售電天數");
cell.CellStyle = styleLine12;
index = 0;
RowPosition++;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(avghour)/Useday,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(avgKWHKWP) / Useday,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(avgdayKWH) / Useday,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(monthKWH);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(monthmoney);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(monthmoneyone) / Useday,2));
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Useday);
cell.CellStyle = styleLine12;
#endregion
#region //RowPosition = 總量 + 2
if (check_hire == true)
{
RowPosition += 2;//空兩行
PsIdAndSiteDB set = new PsIdAndSiteDB()
{
PowerstationId = Convert.ToInt32(powerstationid.Value),
SiteDB = sitedb
};
var info = GetHireInfo(set);
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue("出租人");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("租金比例");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("租金金額(未稅)");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue("租金金額(含稅)");
cell.CellStyle = styleLine12;
foreach(var a in info.Result.Data)
{
RowPosition++;
index = 0;
row = sheet.CreateRow(RowPosition);
cell = row.CreateCell(index);
cell.SetCellValue(a.Landowner);
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(a.LeaseRate + "%");
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(monthmoney) * Convert.ToDouble(a.LeaseRate) /100,2).ToString());
cell.CellStyle = styleLine12;
index++;
cell = row.CreateCell(index);
cell.SetCellValue(Math.Round(Convert.ToDouble(monthmoney) * Convert.ToDouble(a.LeaseRate) / 100 * 1.05, 2).ToString());
cell.CellStyle = styleLine12;
index++;
}
}
#endregion
break;
}
}
var ms = new NpoiMemoryStream
{
@ -193,7 +595,35 @@ namespace SolarPower.Controllers
return File(ms, "application/vnd.ms-excel", "text.xlsx");
}
public string Checknull(string a)
{
if(a == null)
{
return "0";
}else
{
return a;
}
}
public async Task<ApiResult<List<Landinfo>>> GetHireInfo(PsIdAndSiteDB post)
{
ApiResult<List<Landinfo>> apiResult = new ApiResult<List<Landinfo>>();
try
{
var a = await stationReportRepository.GetHire(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;
}
}
}

View File

@ -52,5 +52,14 @@ namespace SolarPower.Models
base.Close();
}
}
public class PsIdAndSiteDB
{
public int PowerstationId { get; set; }
public string SiteDB { get; set; }
}
public class Landinfo
{
public string LeaseRate { get; set; }
public string Landowner { get; set; }
}
}

View File

@ -110,7 +110,7 @@ namespace SolarPower.Repository.Implement
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''
c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB
FROM inverter_history_day a left join
( # inv
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH,
@ -133,7 +133,7 @@ namespace SolarPower.Repository.Implement
) 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}
select id, name stationName, powerRate,SolarType,SiteDB 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'')
@ -157,7 +157,7 @@ namespace SolarPower.Repository.Implement
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''
c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB
FROM inverter_history_day a left join
( # inv
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH,
@ -182,7 +182,7 @@ namespace SolarPower.Repository.Implement
) 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}
select id, name stationName, powerRate, SolarType, SiteDB 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'')
@ -194,8 +194,6 @@ namespace SolarPower.Repository.Implement
DEALLOCATE PREPARE stmt;";
}
break;
}
a = await conn.QueryAsync<dynamic>(sql);
@ -211,5 +209,28 @@ namespace SolarPower.Repository.Implement
return a;
}
}
public async Task<List<Landinfo>> GetHire (PsIdAndSiteDB post)
{
List<Landinfo> result = new List<Landinfo>();
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
try
{
string sql = @$"SELECT LeaseRate,Landowner from {post.SiteDB}.land_building WHERE PowerStationId = {post.PowerstationId}";
result = (await conn.QueryAsync<Landinfo>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
}
}

View File

@ -10,5 +10,6 @@ namespace SolarPower.Repository.Interface
{
Task<List<string>> GetInverterId(string DBname, Select_table post);
Task<dynamic> Gettablebody(Select_table post);
Task<List<Landinfo>> GetHire(PsIdAndSiteDB post);
}
}

View File

@ -236,6 +236,24 @@
</div>
</div>
</div>
<div class="row mb-5">
<div class="col-xl-12">
<div class="card p-3 w-100 " id="hiretable">
<table class="table m-0">
<thead id="HireTableHead">
<tr>
<th>出租人</th>
<th>租金比例</th>
<th>租金金額(未稅)</th>
<th>租金金額(含稅)</th>
</tr>
</thead>
<tbody id="HireTableBody">
</tbody>
</table>
</div>
</div>
</div>
<div class="row mb-5">
<div class="col-xl-12">
<div class="card p-3 w-100 overflow-auto">
@ -248,7 +266,8 @@
</div>
</div>
</div>
</main>
</main>
</div>
</div>
@ -267,6 +286,7 @@
var a = $('#collapse').trigger("click");
//document.getElementById("collapse").click();
$('.overflow-auto').hide();
$('#hiretable').hide();
$('#DateGet').val(new Date().toISOString().substring(0, 10));
document.getElementById("DateGettextdiv").style.display = "none";//隱藏
$('#DateGet').attr('style', 'width:205px');
@ -364,6 +384,7 @@
else {
timerange = $('#DateGet').val();
}
nowform = null;
}
//#endregion
@ -655,9 +676,8 @@
}
function Dateform(form) {
$('#hiretable').hide();
tablehand(form);
nowform = form;
}
function tablebody(form)
@ -736,7 +756,7 @@
$('#tothead').append(stc);
haveinvertName = [];
}
else {
else {//月報表
var avghour = 0;
var avgKWHKWP = 0;
var avgdayKWH = 0;
@ -744,6 +764,9 @@
var monthmoney = 0;
var monthmoneyone = 0;
var monthday = 0;
var check_hire = false;
var sitedb = "";
$.each(rel.data, function (index, inverter) {
sta += "<tr>";
sta += "<td>" + inverter.report_date + "</td>";
@ -769,6 +792,13 @@
monthKWH = inverter.monthKWH ? inverter.monthKWH : 0;
monthmoney = inverter.monthmoney ? inverter.monthmoney : 0;
monthmoneyone += inverter.monthmoneyone ? inverter.monthmoneyone : 0;
if (inverter.SolarType == 1)
{
check_hire = true;
sitedb = inverter.SiteDB
}
})
monthday = rel.data.length;
@ -793,7 +823,9 @@
stb += "<td>" + 0 + "</td>";
stb += "<td>" + 0 + "</td>";
stb += "</tr>";
} else {
}
else
{
stb += "<td>" + (avghour / monthday).toFixed(2) + "</td>";
stb += "<td>" + (avgKWHKWP / monthday).toFixed(2) + "</td>";
stb += "<td>" + (avgdayKWH / monthday).toFixed(2) + "</td>";
@ -803,17 +835,45 @@
stb += "<td>" + monthday + "</td>";
stb += "</tr>";
}
var std = "";
if (check_hire == true)
{
var dataTosent =
{
Sitedb: sitedb,
PowerstationId: nowpowerstation
}
$('#HireTableBody').empty();
var posturl = "/StationReport/GetHireInfo";
$.post(posturl, dataTosent, function (rel) {
if (rel.code != "0000") {
toast_error(rel.data.msg);
return;
}
$.each(rel.data, function (index, value) {
std += "<tr>";
std += "<td>" + value.landowner + "</td>";
std += "<td>" + value.leaseRate + "%" + "</td>";
std += "<td>" + (monthmoney * value.leaseRate / 100).toFixed(2) + "</td>";
std += "<td>" + (monthmoney * value.leaseRate / 100 * 1.05).toFixed(2) + "</td>";
std += "</tr>";
})
$('#HireTableBody').append(std);
$('#hiretable').show();
})
}
$('#TableBody').append(sta);
$('#totbody').append(stb);
$('#tothead').append(stc);
haveinvertName = [];
}
$('.overflow-auto').show();
nowform = form;
}, 'json');
}
@ -826,7 +886,11 @@
FormType: nowform,
PowerStation: selecterd_invert
}
if (send_data.FormType != null && send_data.PowerStation.length != 0) {
window.location = "/StationReport/ExportExcel?post=" + JSON.stringify(send_data);
} else {
toast_warning("請先選擇電站及報表類型");
}
}
</script>