新增水電報表Controller
This commit is contained in:
parent
4fbc4b8db0
commit
718f2768f7
300
FrontendWebApi/ApiControllers/TenantBillController.cs
Normal file
300
FrontendWebApi/ApiControllers/TenantBillController.cs
Normal file
@ -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<TenantBillController>
|
||||
{
|
||||
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]
|
||||
public async Task<ApiResult<string>> AddOneTenantList(TenantList tl)
|
||||
{
|
||||
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]
|
||||
public async Task<ApiResult<string>> UpdateOneTenantList(TenantList tl)
|
||||
{
|
||||
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]
|
||||
public async Task<ApiResult<string>> DelOneTenantList(TenantList tl)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ApiResult<List<TenantBill>>> GetTenantBill(string tableType, string building_tag)
|
||||
{
|
||||
ApiResult<List<TenantBill>> apiResult = new ApiResult<List<TenantBill>>();
|
||||
List<TenantBill> tenantBill = new List<TenantBill>();
|
||||
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<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]
|
||||
public async Task<ApiResult<string>> UpdateTenantBill(TenantBill tb, string tableType)
|
||||
{
|
||||
ApiResult<string> apiResult = new ApiResult<string>();
|
||||
|
||||
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<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;
|
||||
}
|
||||
}
|
||||
}
|
55
FrontendWebApi/Models/Bill.cs
Normal file
55
FrontendWebApi/Models/Bill.cs
Normal file
@ -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; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user