using Microsoft.EntityFrameworkCore; using System.Data; using SystemX.Core.DB; using SystemX.Core.Services; using WebApi.Library.Enums; using WebApi.Project.UniqueKeyApi.Models; namespace WebApi.Project.UniqueKeyApi.Services { public class CPMetaService { private readonly IServiceScopeFactory _scopeFactory; private readonly ConfigService? _configService; public CPMetaService(IServiceProvider serviceProvider, IServiceScopeFactory scopeFactory, ConfigService configSerice) { _scopeFactory = scopeFactory; _configService = configSerice; } public async Task SetWbmsMeta(Request_SetWbmsMeta request, string guid = "") { Response_SetWbmsMeta response = new Response_SetWbmsMeta(); if (request != null) { response.ProductID = request.ProductID; bool transactionResult = true; using (var scope = _scopeFactory.CreateScope()) { int shardId = request.ShardID; if (shardId <= 0) shardId = 1; var provider = scope.ServiceProvider.GetRequiredService(); using (var context = GetCPMetaDBContext(provider, shardId)) { if (context is not null) { var data = await context.tWbms.AsNoTracking().Where(x => x.cProductID == request.ProductID)?.FirstOrDefaultAsync(); if (data is not null) //update { var selected = await context.tWbms.FirstOrDefaultAsync(x => x.cProductID == request.ProductID); if (selected != null) { selected.cMacAddress1 = request.MacAddress1; selected.cMacAddress2 = request.MacAddress2; selected.cType = request.Type; selected.cProductNo = request.ProductNo; selected.cSpareValue = request.SpareValue; selected.cDateTime = DateTime.Now; using (var transaction = await context.CreateTransactionAsync()) { context.Update(selected); transactionResult = await context.CloseTransactionAsync(transaction); } } LogXnet.WriteLine($"Update::{guid}", LogXLabel.DB); response.Result = WebApiResult.Update.ToString(); } else //insert { var row = new tWbms { cProductID = request.ProductID, cMacAddress1 = request.MacAddress1, cMacAddress2 = request.MacAddress2, cType = request.Type, cProductNo = request.ProductNo, cSpareValue = request.SpareValue, cDateTime = DateTime.Now }; using (var transaction = await context.CreateTransactionAsync()) { await context.AddAsync(row); transactionResult = await context.CloseTransactionAsync(transaction); } LogXnet.WriteLine($"Insert::{guid}", LogXLabel.DB); response.Result = WebApiResult.Success.ToString(); } //db error if (transactionResult == false) { LogXnet.WriteLine($"Transaction Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Duplicate Mac Address"; } } else //invalid shard id { LogXnet.WriteLine($"ShardID Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Invalid shard id"; } } } } return response; } public async Task GetWbmsMeta(Request_GetWbmsMeta request, string guid = "") { Response_GetWbms response = new Response_GetWbms(); response.Wbms = new List(); if (request != null) { using (var scope = _scopeFactory.CreateScope()) { int shardId = request.ShardID; if (shardId <= 0) shardId = 1; var provider = scope.ServiceProvider.GetRequiredService(); using (var context = GetCPMetaDBContext(provider, shardId)) { if (context is not null) { try { using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted)) { var data = await context.tWbms.AsNoTracking().Where(x => ((DateOnly)request.StartDateTime) <= DateOnly.FromDateTime(x.cDateTime) && DateOnly.FromDateTime(x.cDateTime) <= ((DateOnly)request.EndDateTime)).ToListAsync(); await context.CloseTransactionAsync(transaction); if (data != null) { if (response.Wbms is not null) { response.Wbms.AddRange(data); } response.Result = WebApiResult.Success.ToString(); } else { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Get CPMeta Failed"; } } } catch (Exception e) { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Get CPMeta Failed"; LogXnet.WriteLine($"GetWbmsMeta By ProductID Transaction Error::{guid}", LogXLabel.Error); LogXnet.WriteLine(e); } } else //invalid shard id { LogXnet.WriteLine($"ShardID Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Invalid shard id"; } } } } return response; } public async Task GetWbmsMeta(Request_GetWbmsMetaByProductID request, string guid = "") { Response_GetWbms response = new Response_GetWbms(); response.Wbms = new List(); if (request != null) { using (var scope = _scopeFactory.CreateScope()) { int shardId = request.ShardID; if (shardId <= 0) shardId = 1; var provider = scope.ServiceProvider.GetRequiredService(); using (var context = GetCPMetaDBContext(provider, shardId)) { if (context is not null) { try { using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted)) { var data = await context.tWbms.AsNoTracking().FirstOrDefaultAsync(x => x.cProductID.ToLower() == request.ProductID.ToLower()); await context.CloseTransactionAsync(transaction); if (data != null) { if(response.Wbms is not null) { response.Wbms.Add(data); } response.Result = WebApiResult.Success.ToString(); } else { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Not exist productID: {request.ProductID}"; } } } catch (Exception e) { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Not exist productID: {request.ProductID}"; LogXnet.WriteLine($"GetWbmsMeta By ProductID Transaction Error::{guid}", LogXLabel.Error); LogXnet.WriteLine(e); } } else //invalid shard id { LogXnet.WriteLine($"ShardID Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Invalid shard id"; } } } } return response; } public async Task GetWbmsMeta(Request_GetWbmsMetaByMacAddress request, string guid = "") { Response_GetWbms response = new Response_GetWbms(); response.Wbms = new List(); if (request != null) { using (var scope = _scopeFactory.CreateScope()) { int shardId = request.ShardID; if (shardId <= 0) shardId = 1; var provider = scope.ServiceProvider.GetRequiredService(); using (var context = GetCPMetaDBContext(provider, shardId)) { if (context is not null) { try { using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted)) { var mac1 = await context.tWbms.AsNoTracking().FirstOrDefaultAsync(x => x.cMacAddress1.ToLower() == request.MacAddress.ToLower()); var mac2 = await context.tWbms.AsNoTracking().FirstOrDefaultAsync(x => x.cMacAddress2.ToLower() == request.MacAddress.ToLower()); await context.CloseTransactionAsync(transaction); if (mac1 is not null) { if(response.Wbms is not null) { response.Wbms.Add(mac1); } response.Result = WebApiResult.Success.ToString(); } if(mac2 is not null) { if (response.Wbms is not null) { response.Wbms.Add(mac2); } response.Result = WebApiResult.Success.ToString(); } if(response.Wbms?.Count <= 0) { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Not exist MacAddress: {request.MacAddress}"; } } } catch (Exception e) { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); response.Message = $"Not exist MacAddrss: {request.MacAddress}"; LogXnet.WriteLine($"GetWbmsMeta By MacAddress Transaction Error::{guid}", LogXLabel.Error); LogXnet.WriteLine(e); } } else //invalid shard id { LogXnet.WriteLine($"ShardID Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Invalid shard id"; } } } } return response; } public async Task GetWbmsLatest(Request_GetWbmsLatest request, string guid = "") { Response_GetWbms response = new Response_GetWbms(); response.Wbms = new List(); if (request != null) { using (var scope = _scopeFactory.CreateScope()) { int shardId = request.ShardID; if (shardId <= 0) shardId = 1; var provider = scope.ServiceProvider.GetRequiredService(); using (var context = GetCPMetaDBContext(provider, shardId)) { if (context is not null) { try { using (var transaction = await context.CreateTransactionAsync(IsolationLevel.ReadUncommitted)) { response.Wbms = await context.tWbms.AsNoTracking().OrderByDescending(x => x.cDateTime).Take(request.Count).ToListAsync(); await context.CloseTransactionAsync(transaction); response.Result = WebApiResult.Success.ToString(); } } catch (Exception e) { response.Wbms = null; response.Result = WebApiResult.Failed.ToString(); LogXnet.WriteLine($"GetWbmsMeta By Latest({request.Count}) Transaction Error::{guid}", LogXLabel.Error); LogXnet.WriteLine(e); } } else //invalid shard id { LogXnet.WriteLine($"ShardID Error::{guid}", LogXLabel.Error); response.Result = WebApiResult.Failed.ToString(); response.Message = "Invalid shard id"; } } } } return response; } private CPMetaContext? GetCPMetaDBContext(DbContextProvider provider, int dbID) { var connectionString = _configService?.GetConfig()?.DataBase?.Find(x => x.DBID == dbID); if(connectionString is not null) return provider?.GetDBContext((int)connectionString?.DBID); return null; } } }