diff --git a/.gitignore b/.gitignore index 2af6ce7..bf47f01 100644 --- a/.gitignore +++ b/.gitignore @@ -342,3 +342,4 @@ ASALocalRun/ healthchecksdb +/SolarPower/wwwroot/upload/operation_recode/1 diff --git a/SolarPower/Controllers/StationReportController.cs b/SolarPower/Controllers/StationReportController.cs index 223788c..2e4a635 100644 --- a/SolarPower/Controllers/StationReportController.cs +++ b/SolarPower/Controllers/StationReportController.cs @@ -1,10 +1,15 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using SolarPower.Models; using SolarPower.Models.PowerStation; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; @@ -115,6 +120,80 @@ namespace SolarPower.Controllers return apiResult; } + public FileResult ExportExcel(string post) + { + var postObject = JsonConvert.DeserializeObject(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"); + } + + } } diff --git a/SolarPower/Models/StationReport.cs b/SolarPower/Models/StationReport.cs index 5328d4c..31a061e 100644 --- a/SolarPower/Models/StationReport.cs +++ b/SolarPower/Models/StationReport.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; @@ -24,6 +25,32 @@ namespace SolarPower.Models public int PowerStation { 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 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(); + } + } } diff --git a/SolarPower/Repository/Implement/StationReportRepository.cs b/SolarPower/Repository/Implement/StationReportRepository.cs index c944240..f89070f 100644 --- a/SolarPower/Repository/Implement/StationReportRepository.cs +++ b/SolarPower/Repository/Implement/StationReportRepository.cs @@ -146,19 +146,59 @@ namespace SolarPower.Repository.Implement } 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; } - - - - a = await conn.QueryAsync(sql); - } catch (Exception exception) { diff --git a/SolarPower/SolarPower.csproj b/SolarPower/SolarPower.csproj index a72716f..d47a005 100644 --- a/SolarPower/SolarPower.csproj +++ b/SolarPower/SolarPower.csproj @@ -28,6 +28,7 @@ + diff --git a/SolarPower/Views/StationReport/Index.cshtml b/SolarPower/Views/StationReport/Index.cshtml index 54e3970..db54ba1 100644 --- a/SolarPower/Views/StationReport/Index.cshtml +++ b/SolarPower/Views/StationReport/Index.cshtml @@ -19,7 +19,7 @@
- +
@@ -239,7 +239,6 @@
- @@ -259,10 +258,15 @@ var datepicker; var timerange;//選取時間 var selecterd_invert = []; - var nowpowerstation;//選擇電站 + var nowpowerstation = null;//選擇電站 var haveinvertName = []; + var nowform; + $(function () { //#region 預設初始值 + var a = $('#collapse').trigger("click"); + //document.getElementById("collapse").click(); + $('.overflow-auto').hide(); $('#DateGet').val(new Date().toISOString().substring(0, 10)); document.getElementById("DateGettextdiv").style.display = "none";//隱藏 $('#DateGet').attr('style', 'width:205px'); @@ -441,15 +445,16 @@ if (inverter.value == nowpowerstation) { stri += ''; - } else { stri += ''; - } }) $('#selectOneStation').append(stri); + if (nowpowerstation == null && selecterd_invert.length>0) { + document.getElementById(selecterd_invert[0].value).onclick(); + } } //#endregion @@ -489,22 +494,18 @@ if (this.checked) { selecterd_invert.push(getstation); } else { - //var ss = $.inArray(getstation, selecterd_invert); - //var a = 0; var a = selecterd_invert.filter(function (n, i) { if (n.name === getstation.name && n.value === getstation.value) { if (nowpowerstation == getstation.value) { nowpowerstation = null; } - console.log(n); selecterd_invert.splice(i, 1); } }); } AddButtonWithStation(); - console.log(selecterd_invert); }); @@ -549,14 +550,37 @@ '
' + '
    '; $.each(inverterCollapse[key], function (index, inverter) { - str += '
  • ' + - '
    ' + - '

    ' + inverter.powerStationName + '

    ' + - '
    ' + - '' + - '
    ' + - '
    ' + - '
  • '; + var getstation = + { + name: inverter.powerStationName, + value: String(inverter.powerStationId) + } + var on = false; + var a = selecterd_invert.find(function (n, i) { + if (n.name === getstation.name && n.value === getstation.value) { + on = true; + } + }); + if (on == true) { + str += '
  • ' + + '
    ' + + '

    ' + inverter.powerStationName + '

    ' + + '
    ' + + '' + + '
    ' + + '
    ' + + '
  • '; + } + else { + str += '
  • ' + + '
    ' + + '

    ' + inverter.powerStationName + '

    ' + + '
    ' + + '' + + '
    ' + + '
    ' + + '
  • '; + } }); str += '
'; @@ -574,7 +598,8 @@ } }); $("#js_list_accordion .collapse").collapse('show'); - + + }, 'json'); } //#endregion @@ -629,12 +654,12 @@ }, 'json'); } - function Dateform(form) { + tablehand(form); + nowform = form; } - function tablebody(form) { var send_data = @@ -670,14 +695,14 @@ if (inverter[i] == null) { sta += "
"; } else { - sta += ""; + sta += ""; } }); - sta += ""; - sta += ""; - sta += ""; - sta += ""; - sta += ""; + sta += ""; + sta += ""; + sta += ""; + sta += ""; + sta += ""; sta += ""; thour = inverter.tothour ? inverter.tothour.toFixed(2) : 0; tpr = inverter.pr ? inverter.pr.toFixed(2) : 0; @@ -786,11 +811,23 @@ $('#tothead').append(stc); haveinvertName = []; } - + $('.overflow-auto').show(); }, 'json'); } + + function ExportExcel() { + var send_data = + { + SearchType: searchType, + Time: timerange, + FormType: nowform, + PowerStation: selecterd_invert + } + window.location = "/StationReport/ExportExcel?post=" + JSON.stringify(send_data); + } + } \ No newline at end of file
" + 0 + "" + inverter[i] + "" + Number(inverter[i]).toFixed(2) + "" + inverter.hourKWH + "" + inverter.hourKWHp + "" + inverter.irradiance + "" + inverter.temperature + "" + inverter.hourmoney + "" + Number(inverter.hourKWH) + "" + Number(inverter.hourKWHp) + "" + Number(inverter.irradiance) + "" + Number(inverter.temperature) + "" + Number(inverter.hourmoney) + "