From 718f2768f75359501da79719e58c4543771631b0 Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Tue, 23 Jan 2024 09:12:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B0=B4=E9=9B=BB=E5=A0=B1?= =?UTF-8?q?=E8=A1=A8Controller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiControllers/TenantBillController.cs | 300 ++++++++++++++++++ FrontendWebApi/Models/Bill.cs | 55 ++++ 2 files changed, 355 insertions(+) create mode 100644 FrontendWebApi/ApiControllers/TenantBillController.cs create mode 100644 FrontendWebApi/Models/Bill.cs diff --git a/FrontendWebApi/ApiControllers/TenantBillController.cs b/FrontendWebApi/ApiControllers/TenantBillController.cs new file mode 100644 index 0000000..d60593a --- /dev/null +++ b/FrontendWebApi/ApiControllers/TenantBillController.cs @@ -0,0 +1,300 @@ +using FrontendWebApi.Models; +using Microsoft.AspNetCore.Mvc; +using Repository.BackendRepository.Interface; +using Repository.FrontendRepository.Interface; +using System.Collections.Generic; +using System.Threading.Tasks; +using System; +using System.IO; +using static FrontendWebApi.Models.Bill; +using Microsoft.Extensions.Logging; +using System.Text.Json; + + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace FrontendWebApi.ApiControllers +{ + //[Route("api/[controller]")] + //[ApiController] + public class TenantBillController + //public class TenantBillController : MyBaseApiController + { + private readonly IBackendRepository backendRepository; + + + const string TenantListtable = "archive_electric_meter_tenant_list"; + const string TenantBilltable = "archive_electric_meter_tenant_bill"; + + + public TenantBillController(IBackendRepository backendRepository, IFrontendRepository frontendRepository) + { + this.backendRepository = backendRepository; + + } + + [HttpPost] + public async Task>> GetTenantList() + { + ApiResult> apiResult = new ApiResult>(); + List tenantList = new List(); + try + { + var sqlString = $"SELECT tenant_guid,tenant_id,tenant_name,bill_perKWH,bill_perRCV " + + $"from {TenantListtable} order by created_at"; + + tenantList = await backendRepository.GetAllAsync(sqlString); + + + apiResult.Code = "0000"; + apiResult.Data = tenantList; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> AddOneTenantList(TenantList tl) + { + ApiResult apiResult = new ApiResult(); + + try + { + var tenant_guid = Guid.NewGuid(); + var tenant_name = tl.tenant_name; + var bill_perKWH = tl.bill_perKWH; + var bill_perRCV = tl.bill_perRCV; + var created_at = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + + var sqlString = $"INSERT INTO {TenantListtable} (tenant_guid,tenant_id,tenant_name, bill_perKWH, bill_perRCV, created_by,created_at) " + + $"VALUES ('{tenant_guid}',{tl.tenant_id},'{tenant_name}', {bill_perKWH}, {bill_perRCV},'{tl.created_by}', '{created_at}')"; + + + await backendRepository.ExecuteSql(sqlString); + apiResult.Code = "0000"; + apiResult.Data = "新增成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。" + exception.Message; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> UpdateOneTenantList(TenantList tl) + { + ApiResult apiResult = new ApiResult(); + + try + { + var updated_at = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + var sqlString = $"UPDATE {TenantListtable} SET " + + $"`tenant_name` = '{tl.tenant_name}', " + + $"`bill_perKWH` = {tl.bill_perKWH}, " + + $"`bill_perRCV` = {tl.bill_perRCV}, " + + $"`updated_by` = {tl.updated_by}, " + + $"`updated_at` = '{updated_at}' " + + $"WHERE `tenant_guid` = '{tl.tenant_guid}'"; + + + await backendRepository.ExecuteSql(sqlString); + apiResult.Code = "0000"; + apiResult.Data = "修改成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> DelOneTenantList(TenantList tl) + { + ApiResult apiResult = new ApiResult(); + + try + { + var sqlString = $"delete from {TenantListtable} WHERE tenant_guid = '{tl.tenant_guid}' "; + + await backendRepository.ExecuteSql(sqlString); + apiResult.Code = "0000"; + apiResult.Data = "刪除成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task>> GetTenantBill(string tableType, string building_tag) + { + ApiResult> apiResult = new ApiResult>(); + List tenantBill = new List(); + try + { + string ElecOrWater = tableType == "elec" ? "E4" : "W1"; + string sqlString = null; + if (building_tag == "ALL") + { + sqlString = + $"SELECT tenant_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name " + + $"from {TenantBilltable} a join device b on a.device_number =b.device_number" + + $"where a.device_name_tag = '{ElecOrWater}' "; + } + else if (building_tag == "D2") + { + sqlString = + $"SELECT tenant_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name " + + $"from {TenantBilltable} a join device b on a.device_number =b.device_number " + + $"where device_building_tag = 'D1' and a.device_name_tag = '{ElecOrWater}' || device_building_tag = 'D2' and a.device_name_tag = '{ElecOrWater}'"; + } + else + { + sqlString = + $"SELECT tenant_id,a.device_number,a.full_name,start_timestamp,end_timestamp,result,bill,tenant_name " + + $"from {TenantBilltable} a join device b on a.device_number =b.device_number " + + $"where device_building_tag = '{building_tag}' and a.device_name_tag = '{ElecOrWater}' "; + } + + + tenantBill = await backendRepository.GetAllAsync(sqlString); + + + apiResult.Code = "0000"; + apiResult.Data = tenantBill; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task> UpdateTenantBill(TenantBill tb, string tableType) + { + ApiResult apiResult = new ApiResult(); + + try + { + string bill_per = tableType == "elec" ? "bill_perKWH" : "bill_perRCV"; + var updated_at = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + var start_timestamp = tb.start_timestamp.ToString("yyyy-MM-dd HH:mm:ss"); + var end_timestamp = tb.end_timestamp.ToString("yyyy-MM-dd HH:mm:ss"); + string sqlString = null; + + string startMonth = tb.start_timestamp.Year.ToString() + tb.start_timestamp.Month.ToString("D2"); + string endMonth = tb.end_timestamp.Year.ToString() + tb.end_timestamp.Month.ToString("D2"); + if (startMonth == endMonth) + { + sqlString = + $"UPDATE {TenantBilltable} " + + $"set tenant_id = {tb.tenant_id} ,tenant_name = '{tb.tenant_name}', start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , " + + $"result= " + + $"(select sum(sub_result) " + + $"from archive_electric_water_meter_day_{startMonth} " + + $"WHERE device_number = '{tb.device_number}' and start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + + $"GROUP BY device_number) , " + + $"bill = " + + $"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " + + $"updated_by = '{tb.updated_by}', " + + $"updated_at = '{updated_at}', " + + $"tenant_guid = '{tb.tenant_guid}' " + + $"WHERE device_number = '{tb.device_number}'"; + } + else + { + sqlString = + $"UPDATE {TenantBilltable} " + + $"set tenant_id = {tb.tenant_id} ,tenant_name = '{tb.tenant_name}', start_timestamp = '{start_timestamp}',end_timestamp = '{end_timestamp}' , " + + $"result= " + + $"(SELECT sum(sub_result) " + + $"FROM ( " + + $" SELECT start_timestamp,device_number, sub_result " + + $" FROM archive_electric_water_meter_day_{startMonth} " + + $" WHERE device_number = '{tb.device_number}' " + + $" UNION ALL " + + $" SELECT start_timestamp,device_number, sub_result " + + $" FROM archive_electric_water_meter_day_{endMonth} " + + $" WHERE device_number = '{tb.device_number}' " + + $") combined_result " + + $"WHERE start_timestamp BETWEEN '{start_timestamp}' and '{end_timestamp}' " + + $"GROUP BY device_number) ," + + $"bill = " + + $"ROUND(result *(SELECT {bill_per} from {TenantListtable} WHERE tenant_guid = '{tb.tenant_guid}') ), " + + $"updated_by = '{tb.updated_by}', " + + $"updated_at = '{updated_at}', " + + $"tenant_guid = '{tb.tenant_guid}' " + + $"WHERE device_number = '{tb.device_number}'"; + + } + + + await backendRepository.ExecuteSql(sqlString); + + + apiResult.Code = "0000"; + apiResult.Data = "修改成功"; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + + [HttpPost] + public async Task>> OutputTenantBill() + { + ApiResult> apiResult = new ApiResult>(); + List outputBill = new List(); + + try + { + string sqlString = + $"SELECT distinct a.tenant_guid,b.tenant_name,start_timestamp,end_timestamp,bill_perKWH,bill_perRCV,sum(result) as name_result,sum(bill) as name_bill " + + $"from archive_electric_meter_tenant_bill a join archive_electric_meter_tenant_list b on a.tenant_guid = b.tenant_guid " + + $"WHERE a.tenant_guid = '37639e92-20ce-4064-956b-b6b0eaa7d7d2' " + + $"group by a.device_name_tag"; + + outputBill = await backendRepository.GetAllAsync(sqlString); + + apiResult.Code = "0000"; + apiResult.Data = outputBill; + } + catch (Exception exception) + { + apiResult.Code = "9999"; + apiResult.Msg = "系統內部錯誤,請聯絡管理者。"; + //Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); + } + + return apiResult; + } + } +} \ No newline at end of file diff --git a/FrontendWebApi/Models/Bill.cs b/FrontendWebApi/Models/Bill.cs new file mode 100644 index 0000000..f4f81fe --- /dev/null +++ b/FrontendWebApi/Models/Bill.cs @@ -0,0 +1,55 @@ +using System; + +namespace FrontendWebApi.Models +{ + public class Bill + { + public class TenantList + { + public string tenant_guid { get; set; } + public int tenant_id { get; set; } + public string tenant_name { get; set; } + public decimal bill_perKWH { get; set; } + public decimal bill_perRCV { get; set; } + public string created_by { get; set; } + public DateTime created_at { get; set; } + public string updated_by { get; set; } + public DateTime? updated_at { get; set; } + + } + + public class TenantBill + { + public int tenant_id { get; set; } + public string tenant_name { get; set; } + public string device_number { get; set; } + public string full_name { get; set; } + public DateTime start_timestamp { get; set; } + public DateTime end_timestamp { get; set; } + public string device_name_tag { get; set; } + public decimal result { get; set; } + public int bill { get; set; } + public string created_by { get; set; } + public DateTime created_at { get; set; } + public string updated_by { get; set; } + public DateTime? updated_at { get; set; } + public string tenant_guid { get; set; } + + } + public class OutputBill + { + public string tenant_name { get; set; } + public DateTime start_timestamp { get; set; } + public DateTime end_timestamp { get; set; } + public decimal kwh_total { get; set; } + public decimal bill_perKWH { get; set; } + public int electric_total { get; set; } + public decimal rcv_total { get; set; } + public decimal bill_perRCV { get; set; } + public int water_total { get; set; } + public int all_total { get; set; } + + } + + } +} \ No newline at end of file