|
@@ -1,23 +1,15 @@
|
|
|
using Dapper;
|
|
|
-using Dapper.Transaction;
|
|
|
using EVCB_OCPP.Domain;
|
|
|
using EVCB_OCPP.TaskScheduler.Helper;
|
|
|
using EVCB_OCPP.TaskScheduler.Models;
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
-using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Polly;
|
|
|
-using Polly.Retry;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
-using System.Configuration;
|
|
|
using System.Data;
|
|
|
-using System.Data.Common;
|
|
|
-using System.Diagnostics;
|
|
|
using System.Linq;
|
|
|
-using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
-using System.Transactions;
|
|
|
using Transaction = EVCB_OCPP.TaskScheduler.Models.Transaction;
|
|
|
|
|
|
namespace EVCB_OCPP.TaskScheduler.Services
|
|
@@ -27,6 +19,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
private readonly ILogger logger;
|
|
|
private readonly SqlConnectionFactory<MainDBContext> mainDbConnectionFactory;
|
|
|
private readonly SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory;
|
|
|
+ private readonly SqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
|
|
|
|
|
|
//private readonly string mainDBConnectString;
|
|
|
//private readonly string onlineDBConnectString;
|
|
@@ -34,11 +27,13 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
public DatabaseService(
|
|
|
ILogger<DatabaseService> logger,
|
|
|
SqlConnectionFactory<MainDBContext> mainDbConnectionFactory,
|
|
|
- SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory)
|
|
|
+ SqlConnectionFactory<OnlineLogDBContext> onlineLogDbConnectionFactory,
|
|
|
+ SqlConnectionFactory<WebDBConetext> webDbConnectionFactory)
|
|
|
{
|
|
|
this.logger = logger;
|
|
|
this.mainDbConnectionFactory = mainDbConnectionFactory;
|
|
|
this.onlineLogDbConnectionFactory = onlineLogDbConnectionFactory;
|
|
|
+ this.webDbConnectionFactory = webDbConnectionFactory;
|
|
|
|
|
|
//mainDBConnectString = configuration.GetConnectionString("MainDBContext");
|
|
|
//onlineDBConnectString = configuration.GetConnectionString("OnlineLogDBContext");
|
|
@@ -90,31 +85,31 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
|
|
|
}
|
|
|
|
|
|
- internal CustomerConnectionDto GetAPIConnectionInfo(Guid partnerId)
|
|
|
+ internal async Task<CustomerConnectionDto> GetAPIConnectionInfo(Guid partnerId)
|
|
|
{
|
|
|
CustomerConnectionDto result = new CustomerConnectionDto();
|
|
|
string key = string.Empty;
|
|
|
var parameters = new DynamicParameters();
|
|
|
parameters.Add("@Id", partnerId, DbType.Guid, ParameterDirection.Input);
|
|
|
- using (SqlConnection conn = mainDbConnectionFactory.Create())
|
|
|
+ using (SqlConnection conn = await mainDbConnectionFactory.CreateAsync())
|
|
|
{
|
|
|
string strSql = "Select ApiKey, ApiUrl from [dbo].[Customer] where Id=@Id; ";
|
|
|
- result = conn.Query<CustomerConnectionDto>(strSql, parameters).FirstOrDefault();
|
|
|
+ result = await conn.QueryFirstOrDefaultAsync<CustomerConnectionDto>(strSql, parameters);
|
|
|
}
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
- internal List<Guid> GetCallParterAPICustomers()
|
|
|
+ internal async Task<List<Guid>> GetCallParterAPICustomers()
|
|
|
{
|
|
|
List<Guid> result = new List<Guid>();
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
{
|
|
|
//dbConn.Open();
|
|
|
- result = dbConn.Query<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1").ToList();
|
|
|
+ result = (await dbConn.QueryAsync<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1")).ToList();
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -126,15 +121,15 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- internal List<Guid> GetNotifyStopTransactionCustomers()
|
|
|
+ internal async Task<List<Guid>> GetNotifyStopTransactionCustomers()
|
|
|
{
|
|
|
List<Guid> result = new List<Guid>();
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
{
|
|
|
//dbConn.Open();
|
|
|
- result = dbConn.Query<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1 and InstantStopTxReport=0").ToList();
|
|
|
+ result = (await dbConn.QueryAsync<Guid>("SELECT Id FROM [dbo].[Customer] where CallPartnerApiOnSchedule=1 and InstantStopTxReport=0")).ToList();
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -188,12 +183,12 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- internal List<MachineOperateRecord> GetNeedReportExecution(Guid customerId, int size)
|
|
|
+ internal async Task<List<MachineOperateRecord>> GetNeedReportExecution(Guid customerId, int size)
|
|
|
{
|
|
|
List<MachineOperateRecord> result = new List<MachineOperateRecord>();
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
{
|
|
|
//dbConn.Open();
|
|
|
var parameters = new DynamicParameters();
|
|
@@ -203,7 +198,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
sqlString = "SELECT Top(" + size + ") Machine.CustomerId, record.Id, record.ChargeBoxId,record.Action,record.SerialNo,record.Status,record.EVSE_Value,record.EVSE_Status FROM[dbo].[MachineOperateRecord] record" +
|
|
|
" left join[dbo].[Machine] on record.ChargeBoxId = Machine.ChargeBoxId " +
|
|
|
" where Machine.CustomerId =@CustomerId and Status!= 0 and RequestType = 1 and ReportedOn = '1991/01/01'";
|
|
|
- result = dbConn.Query<MachineOperateRecord>(sqlString, parameters).ToList();
|
|
|
+ result = (await dbConn.QueryAsync<MachineOperateRecord>(sqlString, parameters)).ToList();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -220,7 +215,7 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
{
|
|
|
foreach (var kv in reportResults)
|
|
|
{
|
|
@@ -247,24 +242,22 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ using (var trans = await dbConn.BeginTransactionAsync())
|
|
|
{
|
|
|
- //await dbConn.OpenAsync();
|
|
|
- using (var trans = await dbConn.BeginTransactionAsync())
|
|
|
- {
|
|
|
- //dbConn.Open();
|
|
|
-
|
|
|
- foreach (var kv in reportResults)
|
|
|
- {
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@StopTransactionReportedOn", kv.Value.StopTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
- parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input, -1);
|
|
|
- await trans.ExecuteAsync("UPDATE [dbo].[TransactionRecord] set StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg where Id=@Id", parameters);
|
|
|
- }
|
|
|
+ //dbConn.Open();
|
|
|
|
|
|
- await trans.CommitAsync();
|
|
|
+ foreach (var kv in reportResults)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StopTransactionReportedOn", kv.Value.StopTransactionReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ErrorMsg", kv.Value.ErrorMsg, DbType.String, ParameterDirection.Input, -1);
|
|
|
+ await dbConn.ExecuteAsync("UPDATE [dbo].[TransactionRecord] set StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg where Id=@Id", parameters, trans);
|
|
|
+ //await trans.ExecuteAsync("UPDATE [dbo].[TransactionRecord] set StopTransactionReportedOn=@StopTransactionReportedOn, ErrorMsg=@ErrorMsg where Id=@Id", parameters);
|
|
|
}
|
|
|
+
|
|
|
+ await trans.CommitAsync();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -300,31 +293,231 @@ namespace EVCB_OCPP.TaskScheduler.Services
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- using (var dbConn = mainDbConnectionFactory.Create())
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ using (var trans = await dbConn.BeginTransactionAsync())
|
|
|
{
|
|
|
- //await dbConn.OpenAsync();
|
|
|
- using (var trans = await dbConn.BeginTransactionAsync())
|
|
|
+ //dbConn.Open();
|
|
|
+
|
|
|
+ foreach (var kv in reportResults)
|
|
|
{
|
|
|
- //dbConn.Open();
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@ReportedOn", kv.Value.ReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
|
|
|
- foreach (var kv in reportResults)
|
|
|
- {
|
|
|
- var parameters = new DynamicParameters();
|
|
|
- parameters.Add("@Id", kv.Key, DbType.Int32, ParameterDirection.Input);
|
|
|
- parameters.Add("@ReportedOn", kv.Value.ReportedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+ await dbConn.ExecuteAsync("UPDATE [dbo].[MachineOperateRecord] set ReportedOn=@ReportedOn where Id=@Id", parameters, trans);
|
|
|
+ }
|
|
|
|
|
|
- await trans.ExecuteAsync("UPDATE [dbo].[MachineOperateRecord] set ReportedOn=@ReportedOn where Id=@Id", parameters);
|
|
|
- }
|
|
|
+ await trans.CommitAsync();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("ReportExecution Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- await trans.CommitAsync();
|
|
|
+ internal async Task<List<CustomerInfoDto>> GetCustomerInfos()
|
|
|
+ {
|
|
|
+ List<CustomerInfoDto> result = new List<CustomerInfoDto>();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ result = (await dbConn.QueryAsync<CustomerInfoDto>("SELECT Id , Name FROM [dbo].[Customer] where IsShow =1 "))
|
|
|
+ .ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("GetCustomerInfos Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //add PHT Electric Avenue
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async Task<List<ChargerAmountDto>> GetChargerAmount(bool connected)
|
|
|
+ {
|
|
|
+ List<ChargerAmountDto> result = new List<ChargerAmountDto>();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ if (connected)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@HeartbeatUpdatedOn", new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DbType.DateTime, ParameterDirection.Input);
|
|
|
+ var quertResult = await dbConn.QueryAsync<ChargerAmountDto>("""
|
|
|
+ SELECT count(ChargeBoxId) as Charger_Count , Customer.Name as CustomerName FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId
|
|
|
+ WHERE IsDelete=0 and HeartbeatUpdatedOn>=@HeartbeatUpdatedOn
|
|
|
+ group by CustomerId,Customer.Name
|
|
|
+ """, parameters);
|
|
|
+ result = quertResult.ToList();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var queryResult = await dbConn.QueryAsync<ChargerAmountDto>("""
|
|
|
+ SELECT count(ChargeBoxId) as Charger_Count , Customer.Name as CustomerName FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId
|
|
|
+ where IsDelete=0
|
|
|
+ group by CustomerId,Customer.Name
|
|
|
+ """);
|
|
|
+ result = queryResult.ToList();
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- logger.LogError("ReportExecution Error " + ex.ToString());
|
|
|
+ logger.LogError("GetChargerAmount Error " + ex.ToString());
|
|
|
}
|
|
|
|
|
|
+ //add PHT Electric Avenue
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async Task<List<ChargerAmountDto>> GetConnectorAmount()
|
|
|
+ {
|
|
|
+ List<ChargerAmountDto> result = new List<ChargerAmountDto>();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@HeartbeatUpdatedOn", new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DbType.DateTime, ParameterDirection.Input);
|
|
|
+ var queryResult = await dbConn.QueryAsync<ChargerAmountDto>("""
|
|
|
+ SELECT sum(GunAmt) as Charger_Count , Customer.Name as CustomerName FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId
|
|
|
+ where IsDelete=0 and HeartbeatUpdatedOn>=@HeartbeatUpdatedOn
|
|
|
+ group by CustomerId,Customer.Name
|
|
|
+ """, parameters);
|
|
|
+ result = queryResult.ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("GetConnectorAmount Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //add PHT Electric Avenue
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async Task<List<ChargerAmountDto>> GetPHTChargerAmount(bool connected, bool inner)
|
|
|
+ {
|
|
|
+ List<ChargerAmountDto> result = new List<ChargerAmountDto>();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ if (connected)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@HeartbeatUpdatedOn", new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DbType.DateTime, ParameterDirection.Input);
|
|
|
+ var requestResult = await dbConn.QueryAsync<ChargerAmountDto>($"""
|
|
|
+ SELECT count(ChargeBoxId) as Charger_Count , Customer.Name as CustomerName
|
|
|
+ FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId
|
|
|
+ where IsDelete=0 and Customer.Name='PHT'
|
|
|
+ {(inner ? "and [ChargePointVendor]!='Electric Avenue'" : "and [ChargePointVendor]='Electric Avenue'")}
|
|
|
+ and HeartbeatUpdatedOn>=@HeartbeatUpdatedOn group by CustomerId,Customer.Name
|
|
|
+ """, parameters);
|
|
|
+ result = requestResult.ToList();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var requestResult = await dbConn.QueryAsync<ChargerAmountDto>($"""
|
|
|
+ SELECT count(ChargeBoxId) as Charger_Count , Customer.Name as CustomerName
|
|
|
+ FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId where IsDelete=0
|
|
|
+ {(inner ? "and [ChargePointVendor]!='Electric Avenue'" : "and [ChargePointVendor]='Electric Avenue'")}
|
|
|
+ and Customer.Name='PHT' group by CustomerId,Customer.Name
|
|
|
+ """);
|
|
|
+ result = requestResult.ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("GetChargerAmount Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //add PHT Electric Avenue
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async Task<List<ChargerAmountDto>> GetPHTConnectorAmount(bool inner)
|
|
|
+ {
|
|
|
+ List<ChargerAmountDto> result = new List<ChargerAmountDto>();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await mainDbConnectionFactory.CreateAsync())
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@HeartbeatUpdatedOn", new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1), DbType.DateTime, ParameterDirection.Input);
|
|
|
+ var queryResult = await dbConn.QueryAsync<ChargerAmountDto>($"""
|
|
|
+ SELECT sum(GunAmt) as Charger_Count , Customer.Name as CustomerName FROM [dbo].[Machine]
|
|
|
+ inner join Customer on Customer.id =[Machine].CustomerId where IsDelete=0 and Customer.Name='PHT'
|
|
|
+ {(inner ? "and [ChargePointVendor]!='Electric Avenue'" : "and [ChargePointVendor]='Electric Avenue'")}
|
|
|
+ and HeartbeatUpdatedOn>=@HeartbeatUpdatedOn group by CustomerId,Customer.Name
|
|
|
+ """, parameters);
|
|
|
+ result = queryResult.ToList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("GetConnectorAmount Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+ //add PHT Electric Avenue
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ internal async Task SaveConnectionStatistics(List<ConnectionStatistics> connectionStatistics)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ using (var dbConn = await webDbConnectionFactory.CreateAsync())
|
|
|
+ using (var trans = await dbConn.BeginTransactionAsync())
|
|
|
+ {
|
|
|
+ foreach (var connection in connectionStatistics)
|
|
|
+ {
|
|
|
+ var parameters = new DynamicParameters();
|
|
|
+ parameters.Add("@CustomerName", connection.CustomerName, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StationName", connection.StationName, DbType.String, ParameterDirection.Input);
|
|
|
+ parameters.Add("@StoragedChargerAmount", connection.StoragedChargerAmount, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@UsedChargerAmount", connection.UsedChargerAmount, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@UsedConnectorAmount", connection.UsedConnectorAmount, DbType.Int32, ParameterDirection.Input);
|
|
|
+ parameters.Add("@CreatedOn", connection.CreatedOn, DbType.DateTime, ParameterDirection.Input);
|
|
|
+
|
|
|
+ await dbConn.ExecuteAsync("""
|
|
|
+ INSERT INTO [dbo].[ConnectionStatistics]
|
|
|
+ ([CustomerName],[StationName],[StoragedChargerAmount],[UsedChargerAmount],[UsedConnectorAmount],[CreatedOn])
|
|
|
+ VALUES(@CustomerName,@StationName,@StoragedChargerAmount,@UsedChargerAmount,@UsedConnectorAmount,@CreatedOn)
|
|
|
+ """, parameters);
|
|
|
+ }
|
|
|
+
|
|
|
+ await trans.CommitAsync();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.LogError("ReportStartTx Error " + ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|