[前台][水電報表] 比較日期 api 程序建置

This commit is contained in:
dev01 2023-06-12 16:02:18 +08:00
parent 985aaed080
commit cca132a876
3 changed files with 104 additions and 20 deletions

View File

@ -510,7 +510,7 @@ namespace Backend.Controllers
var buildingGuid = ""; var buildingGuid = "";
if (post.SelectedBuild != "0") if (post.SelectedBuild != "0")
{ {
buildingGuid = $" AND ap.building_guid = '{post.SelectedBuild}'"; buildingGuid = $" AND ap.building_tag = '{post.SelectedBuild}'";
} }
var sqlString = @$" SELECT ap.AuthCode, ap.MainName, ap.SubName FROM auth_page ap var sqlString = @$" SELECT ap.AuthCode, ap.MainName, ap.SubName FROM auth_page ap
WHERE ap.AuthType='{post.SelectedAuthType}' WHERE ap.AuthType='{post.SelectedAuthType}'

View File

@ -440,7 +440,7 @@ namespace FrontendWebApi.ApiControllers
int i = 0; int i = 0;
ICell cell = row.CreateCell(i++); ICell cell = row.CreateCell(i++);
cell.SetCellValue("別"); cell.SetCellValue("別");
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue("樓層"); cell.SetCellValue("樓層");
@ -529,12 +529,13 @@ namespace FrontendWebApi.ApiControllers
} }
[HttpPost] [HttpPost]
[Route("api/ExportWaterList")] [Route("api/ExportElectricCompareList")]
public FileResult OpeExportExcelWater([FromBody] HydroMeterInput input) public FileResult OpeExportCompareExcelElec([FromBody] List<HydroMeterInput> input)
{ {
var result = new List<HydroMeterOutput>();
var result = this.WaterList(input).Result.Value.Data.ToList(); var result1 = this.ElectricList(input[0]).Result.Value.Data.ToList();
var result2 = this.ElectricList(input[1]).Result.Value.Data.ToList();
Dictionary<string, List<string>> compareDict = new Dictionary<string, List<string>>();
var workbook = new XSSFWorkbook(); var workbook = new XSSFWorkbook();
#region excel設定 #region excel設定
IFont font12 = workbook.CreateFont(); IFont font12 = workbook.CreateFont();
@ -577,11 +578,46 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true; stylein12.WrapText = true;
#endregion #endregion
var rawDataTitle = new List<string>();
if (input[0].tableType == "month")
{
for (var i = 1; i <= 12; i++)
{
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
else if (input[0].tableType == "day")
{
var days = GetDayInMonth(input[0].startTime);
for (var i = 1 ;i <= days ; i++) {
rawDataTitle.Add(input[0].startTime + "-" + i.ToString().PadLeft(2, '0'));
rawDataTitle.Add(input[1].startTime + "-" + i.ToString().PadLeft(2, '0'));
}
}
var sheet = workbook.CreateSheet("電表報表"); var sheet = workbook.CreateSheet("電表報表");
int RowPosition = 0; int RowPosition = 0;
if (result.Count > 0) if (result1.Count > 0 || result2.Count > 0)
{ {
result = result1;
foreach (var r2 in result2)
{
var target = result.Where(r => r.building_tag == r2.building_tag && r.device_serial_tag == r2.device_serial_tag && r.floor_tag == r2.floor_tag).FirstOrDefault();
compareDict[r2.building_tag + r2.floor_tag + r2.device_serial_tag] = new List<string>() { r2.total, r2.price, r2.total_price };
if (target != null)
{
target.rawData = target.rawData.Concat(r2.rawData).ToList();
}
else {
r2.total = null;
r2.price = null;
r2.total_price= null;
result.Add(r2);
}
}
#region set cell #region set cell
IRow row = sheet.CreateRow(RowPosition); IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12); sheet.SetColumnWidth(0, 4 * 160 * 12);
@ -590,7 +626,7 @@ namespace FrontendWebApi.ApiControllers
int i = 0; int i = 0;
ICell cell = row.CreateCell(i++); ICell cell = row.CreateCell(i++);
cell.SetCellValue("別"); cell.SetCellValue("別");
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue("樓層"); cell.SetCellValue("樓層");
@ -599,26 +635,43 @@ namespace FrontendWebApi.ApiControllers
cell.SetCellValue("設備"); cell.SetCellValue("設備");
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
foreach (var rr in result.FirstOrDefault().rawData) foreach (var rr in rawDataTitle)
{ {
sheet.SetColumnWidth(i, 2 * 160 * 12);
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue(rr.timeStamp); cell.SetCellValue(rr);
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
} }
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue("小計"); cell.SetCellValue(input[0].startTime + "小計");
sheet.SetColumnWidth(i-1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "小計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue("單價"); cell.SetCellValue(input[0].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
cell = row.CreateCell(i++); cell = row.CreateCell(i++);
cell.SetCellValue("金額總計"); cell.SetCellValue(input[1].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12; cell.CellStyle = styleLine12;
#endregion #endregion
foreach (var r in result) foreach (var r in result)
{ {
string compareDictKey = r.building_tag + r.floor_tag + r.device_serial_tag;
RowPosition += 1; RowPosition += 1;
int k = 3; int k = 3;
row = sheet.CreateRow(RowPosition); row = sheet.CreateRow(RowPosition);
@ -640,9 +693,10 @@ namespace FrontendWebApi.ApiControllers
if (j == 3) if (j == 3)
{ {
foreach (var rr in r.rawData) foreach (var rr in rawDataTitle)
{ {
cell.SetCellValue(rr.avg_rawdata.ToString()); var targetRaw = r.rawData.Where(rd => rd.timeStamp == rr).FirstOrDefault();
cell.SetCellValue(targetRaw?.avg_rawdata.ToString() ?? "");
j++; j++;
k++; k++;
cell = row.CreateCell(j); cell = row.CreateCell(j);
@ -655,12 +709,24 @@ namespace FrontendWebApi.ApiControllers
} }
if (j == k + 1) if (j == k + 1)
{ {
cell.SetCellValue(r.price); cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][0] : "");
} }
if (j == k + 2) if (j == k + 2)
{
cell.SetCellValue(r.price);
}
if (j == k + 3)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][1] : "");
}
if (j == k + 4)
{ {
cell.SetCellValue(r.total_price); cell.SetCellValue(r.total_price);
} }
if (j == k + 5)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][2] : "");
}
cell.CellStyle = style12; cell.CellStyle = style12;
} }
@ -674,10 +740,18 @@ namespace FrontendWebApi.ApiControllers
workbook.Write(ms); workbook.Write(ms);
ms.Flush(); ms.Flush();
ms.Seek(0, SeekOrigin.Begin); ms.Seek(0, SeekOrigin.Begin);
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(ms, "application/vnd.ms-excel", "電表報表.xlsx");
}
return File(ms, "application/vnd.ms-excel", "水表報表.xlsx"); public static int GetDayInMonth(string yearMonth)
{
List<string> datesList = new List<string>();
DateTime startOfMonth = DateTime.Parse(yearMonth + "-01");
int daysInMonth = DateTime.DaysInMonth(startOfMonth.Year, startOfMonth.Month);
return daysInMonth;
} }
} }
} }

View File

@ -41,4 +41,14 @@ namespace FrontendWebApi.Models
{ {
public string type { get; set; } public string type { get; set; }
} }
public class HydroMeterCompareOutput
{
}
public class HydroMeterCompareExportInput
{
public List<HydroMeterCompareOutput> exportList { get; set; }
}
} }