WebDbService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using Dapper;
  2. using EVCB_OCPP.Domain.ConnectionFactory;
  3. using EVCB_OCPP.WSServer.Dto;
  4. using EVCB_OCPP.WSServer.Helper;
  5. using EVCB_OCPP.WSServer.Service.WsService;
  6. using Microsoft.Data.SqlClient;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace EVCB_OCPP.WSServer.Service.DbService;
  16. public class WebDbService
  17. {
  18. private readonly ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory;
  19. private readonly ILogger<WebDbService> logger;
  20. public WebDbService(ISqlConnectionFactory<WebDBConetext> webDbConnectionFactory, ILogger<WebDbService> logger)
  21. {
  22. this.webDbConnectionFactory = webDbConnectionFactory;
  23. this.logger = logger;
  24. //this.webConnectionString = configuration.GetConnectionString("WebDBContext");
  25. }
  26. //private readonly string webConnectionString;
  27. public async Task<List<string>> GetDenyModelNames(CancellationToken token = default)
  28. {
  29. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  30. string strSql = """
  31. SELECT [Value]
  32. FROM [dbo].[KernelConfig]
  33. where SystemKey = 'DenyModelNames';
  34. """;
  35. var result = await conn.QueryFirstOrDefaultAsync<string>(
  36. new CommandDefinition(strSql, cancellationToken: token)
  37. );
  38. return result.Split(',').ToList();
  39. }
  40. async public Task<string> SetDefaultFee(WsClientData client)
  41. {
  42. string displayPriceText = string.Empty;
  43. string charingPriceText = string.Empty;
  44. if (string.IsNullOrEmpty(client.ChargeBoxId)) return displayPriceText;
  45. try
  46. {
  47. using (SqlConnection conn = await webDbConnectionFactory.CreateAsync())
  48. {
  49. var parameters = new DynamicParameters();
  50. parameters.Add("@MachineId", client.MachineId, DbType.String, ParameterDirection.Input, 36);
  51. string displayPricestrSql = "";
  52. string strSql = "";
  53. if (client.IsAC)
  54. {
  55. displayPricestrSql = """
  56. SELECT [AC_BillingMethod] as BillingMethod,[AC_FeeName] as FeeName,[AC_Fee] as ChargingFeebyHour ,[AC_ParkingFee] as ParkingFee, [Currency]
  57. FROM[StationMachine] left join[dbo].[Station]
  58. on[StationMachine].StationId = Station.[Id]
  59. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  60. """;
  61. strSql = """
  62. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  63. FROM[StationMachine] left join [dbo].[StationFee]
  64. on[StationMachine].StationId = StationFee.StationId
  65. where StationMachine.MachineId =@MachineId and StationFee.IsAC=1;
  66. """;
  67. }
  68. else
  69. {
  70. displayPricestrSql = """
  71. SELECT [DC_BillingMethod] as BillingMethod,[DC_FeeName] as FeeName,[DC_Fee] as ChargingFeebyHour ,[DC_ParkingFee] as ParkingFee, [Currency]
  72. FROM[StationMachine] left join[dbo].[Station]
  73. on[StationMachine].StationId = Station.[Id]
  74. where StationMachine.MachineId=@MachineId and Station.IsBilling=1;
  75. """;
  76. strSql = """
  77. SELECT CAST( [StartTime] as varchar(5)) StartTime,CAST( [EndTime] as varchar(5)) EndTime,[Fee]
  78. FROM[StationMachine] left join [dbo].[StationFee]
  79. on[StationMachine].StationId = StationFee.StationId
  80. where StationMachine.MachineId =@MachineId and StationFee.IsAC=0;
  81. """;
  82. }
  83. var result = await conn.QueryFirstOrDefaultAsync<StationFee>(displayPricestrSql, parameters);
  84. if (result == default)
  85. {
  86. client.IsBilling = false;
  87. return string.Empty;
  88. }
  89. var stationPrice = result;//.First();
  90. if (stationPrice.BillingMethod == 1)
  91. {
  92. var chargingPriceResult = await conn.QueryAsync<ChargingPrice>(strSql, parameters);
  93. client.ChargingPrices = chargingPriceResult.ToList();
  94. if (string.IsNullOrEmpty(client.ChargingPrices[0].StartTime))
  95. {
  96. client.ChargingPrices = new List<ChargingPrice>();
  97. }
  98. }
  99. displayPriceText = stationPrice.FeeName;
  100. client.BillingMethod = stationPrice.BillingMethod;
  101. client.Currency = stationPrice.Currency;
  102. client.ChargingFeebyHour = stationPrice.ChargingFeebyHour;
  103. client.ParkingFee = stationPrice.ParkingFee;
  104. client.IsBilling = true;
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. logger.LogError("SetDefaultFee", ex.ToString());
  110. }
  111. return displayPriceText;
  112. }
  113. internal async Task<Dictionary<string, string>> GetCustomerStationEvseConfig(string chargeBoxId, CancellationToken token = default)
  114. {
  115. string strSql = """
  116. SELECT [ConfigureName], [ConfigureSetting]
  117. FROM [dbo].[StationMachine]
  118. INNER Join [dbo].[StationMachineConfig] on [dbo].[StationMachine].StationId = [dbo].[StationMachineConfig].StationId
  119. WHERE [ChargeBoxId] = @ChargeBoxId
  120. """;
  121. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  122. DynamicParameters parameters = new DynamicParameters();
  123. parameters.Add("@ChargeBoxId", chargeBoxId, DbType.String, ParameterDirection.Input, 50);
  124. var configs = await conn.QueryAsync<StationMachineConfig>(new CommandDefinition(strSql, parameters: parameters, cancellationToken: token));
  125. return configs.ToDictionary(x=>x.ConfigureName, x=>x.ConfigureSetting);
  126. }
  127. internal async Task<Dictionary<string, string>> GetEvseStationConfig(int stationId, CancellationToken token = default)
  128. {
  129. string strSql = """
  130. SELECT [ConfigureName], [ConfigureSetting]
  131. FROM [dbo].[StationMachineConfig]
  132. WHERE [StationId] = @StationId
  133. """;
  134. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  135. DynamicParameters parameters = new DynamicParameters();
  136. parameters.Add("@StationId", stationId, DbType.Int32, ParameterDirection.Input);
  137. var configs = await conn.QueryAsync<StationMachineConfig>(new CommandDefinition(strSql, parameters: parameters, cancellationToken: token));
  138. return configs.ToDictionary(x => x.ConfigureName, x => x.ConfigureSetting);
  139. }
  140. internal async Task<int?> GetEvseStation(string chargeboxId, CancellationToken token = default)
  141. {
  142. string getStationStrSql = """
  143. SELECT [StationId]
  144. FROM [dbo].[StationMachine]
  145. WHERE [ChargeBoxId] = @ChargeBoxIds;
  146. """;
  147. var parameters = new DynamicParameters();
  148. parameters.Add("@ChargeBoxIds", chargeboxId, direction: ParameterDirection.Input, size: 50);
  149. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  150. var stationId = await conn.QueryFirstOrDefaultAsync<int?>(new CommandDefinition(getStationStrSql, parameters, cancellationToken: token));
  151. return stationId;
  152. }
  153. internal async Task<Dictionary<string, int>> GetEvseStationPair(List<string> chargeboxIds)
  154. {
  155. string getStationStrSql = """
  156. SELECT [StationId],[ChargeBoxId]
  157. FROM [dbo].[StationMachine]
  158. WHERE [ChargeBoxId] IN @ChargeBoxIds;
  159. """;
  160. var parameters = new DynamicParameters();
  161. parameters.Add("@ChargeBoxIds", chargeboxIds, direction: ParameterDirection.Input, size: 50);
  162. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  163. var configs = await conn.QueryAsync<StationMachine>(getStationStrSql, parameters);
  164. return configs.ToDictionary(x => x.ChargeBoxId, x => x.StationId);
  165. }
  166. internal async Task<Dictionary<int, Dictionary<string, string>>> GetStationEvseConfigs(List<int> stationIds, CancellationToken token = default)
  167. {
  168. string getSql = """
  169. SELECT [StationId],[ConfigureName],[ConfigureSetting]
  170. FROM [dbo].[StationMachine]
  171. WHERE [StationId] in @StationIds
  172. """;
  173. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  174. var parameters = new DynamicParameters();
  175. parameters.Add("@StationIds", stationIds, direction: ParameterDirection.Input);
  176. var configs = await conn.QueryAsync<StationMachineConfig>(new CommandDefinition(getSql, parameters, cancellationToken: token));
  177. return configs
  178. .GroupBy(x => x.StationId)
  179. .ToDictionary(
  180. x => x.Key,
  181. x => x.ToDictionary(
  182. x => x.ConfigureName,
  183. x => x.ConfigureSetting
  184. ));
  185. }
  186. internal async Task<Dictionary<int, Dictionary<string, string>>> GetStationEvseConfigs(CancellationToken token = default)
  187. {
  188. string getSql = """
  189. SELECT [StationId],[ConfigureName],[ConfigureSetting]
  190. FROM [dbo].[StationMachineConfig]
  191. """;
  192. using SqlConnection conn = await webDbConnectionFactory.CreateAsync();
  193. try
  194. {
  195. var configs = await conn.QueryAsync<StationMachineConfig>(new CommandDefinition(getSql, cancellationToken: token));
  196. return configs
  197. .GroupBy(x => x.StationId)
  198. .ToDictionary(
  199. x => x.Key,
  200. x => x.ToDictionary(
  201. x => x.ConfigureName,
  202. x => x.ConfigureSetting
  203. ));
  204. }
  205. catch(Exception e)
  206. {
  207. logger.LogWarning("StationMachineConfig get failed");
  208. return new Dictionary<int, Dictionary<string, string>>();
  209. }
  210. }
  211. }