2024-01-23 09:12:08 +08:00
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 ;
2024-01-23 14:41:38 +08:00
using Newtonsoft.Json.Linq ;
2024-01-23 09:12:08 +08:00
// 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]
2024-01-23 14:41:38 +08:00
//public class TenantBillController
public class TenantBillController : MyBaseApiController < TenantBillController >
2024-01-23 09:12:08 +08:00
{
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 < ApiResult < List < TenantList > > > GetTenantList ( )
{
ApiResult < List < TenantList > > apiResult = new ApiResult < List < TenantList > > ( ) ;
List < TenantList > tenantList = new List < TenantList > ( ) ;
try
{
var sqlString = $"SELECT tenant_guid,tenant_id,tenant_name,bill_perKWH,bill_perRCV " +
$"from {TenantListtable} order by created_at" ;
tenantList = await backendRepository . GetAllAsync < TenantList > ( sqlString ) ;
apiResult . Code = "0000" ;
apiResult . Data = tenantList ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
//Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult ;
}
[HttpPost]
2024-01-23 14:41:38 +08:00
public async Task < ApiResult < string > > AddOneTenantList ( [ FromBody ] TenantList tl )
2024-01-23 09:12:08 +08:00
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
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]
2024-01-23 14:41:38 +08:00
public async Task < ApiResult < string > > UpdateOneTenantList ( [ FromBody ] TenantList tl )
2024-01-23 09:12:08 +08:00
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
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]
2024-01-23 14:41:38 +08:00
public async Task < ApiResult < string > > DelOneTenantList ( [ FromBody ] TenantList tl )
2024-01-23 09:12:08 +08:00
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
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 ;
}
2024-01-23 14:41:38 +08:00
public class MyJsonData
{
public string Name { get ; set ; }
public int Age { get ; set ; }
}
2024-01-23 09:12:08 +08:00
[HttpPost]
2024-01-23 14:41:38 +08:00
public async Task < ApiResult < List < TenantBill > > > GetTenantBill ( [ FromBody ] TenantBill tb )
2024-01-23 09:12:08 +08:00
{
ApiResult < List < TenantBill > > apiResult = new ApiResult < List < TenantBill > > ( ) ;
List < TenantBill > tenantBill = new List < TenantBill > ( ) ;
try
{
2024-01-23 14:41:38 +08:00
string tableType = tb . tableType ;
string building_tag = tb . building_tag ;
2024-01-23 09:12:08 +08:00
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 < TenantBill > ( sqlString ) ;
apiResult . Code = "0000" ;
apiResult . Data = tenantBill ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
//Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult ;
}
[HttpPost]
2024-01-23 14:41:38 +08:00
public async Task < ApiResult < string > > UpdateTenantBill ( [ FromBody ] TenantBill tb )
2024-01-23 09:12:08 +08:00
{
ApiResult < string > apiResult = new ApiResult < string > ( ) ;
try
{
2024-01-23 14:41:38 +08:00
string bill_per = tb . tableType = = "elec" ? "bill_perKWH" : "bill_perRCV" ;
2024-01-23 09:12:08 +08:00
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 < ApiResult < List < OutputBill > > > OutputTenantBill ( )
{
ApiResult < List < OutputBill > > apiResult = new ApiResult < List < OutputBill > > ( ) ;
List < OutputBill > outputBill = new List < OutputBill > ( ) ;
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 < OutputBill > ( sqlString ) ;
apiResult . Code = "0000" ;
apiResult . Data = outputBill ;
}
catch ( Exception exception )
{
apiResult . Code = "9999" ;
apiResult . Msg = "系統內部錯誤,請聯絡管理者。" ;
//Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult ;
}
}
}