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; } } }