259 lines
12 KiB
C#
259 lines
12 KiB
C#
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<UniqueKeyApiConfig>? _configService;
|
|
|
|
public CPMetaService(IServiceProvider serviceProvider, IServiceScopeFactory scopeFactory, ConfigService<UniqueKeyApiConfig> configSerice)
|
|
{
|
|
_scopeFactory = scopeFactory;
|
|
_configService = configSerice;
|
|
}
|
|
|
|
public async Task<Response_SetWbmsMeta> 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<DbContextProvider>();
|
|
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<Response_GetWbms> GetWbmsMeta(Request_GetWbmsMetaByProductID request, string guid = "")
|
|
{
|
|
Response_GetWbms response = new Response_GetWbms();
|
|
response.Wbms = new List<tWbms>();
|
|
|
|
if (request != null)
|
|
{
|
|
using (var scope = _scopeFactory.CreateScope())
|
|
{
|
|
int shardId = request.ShardID;
|
|
if (shardId <= 0)
|
|
shardId = 1;
|
|
|
|
var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
|
|
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<Response_GetWbms> GetWbmsMeta(Request_GetWbmsMetaByMacAddress request, string guid = "")
|
|
{
|
|
Response_GetWbms response = new Response_GetWbms();
|
|
response.Wbms = new List<tWbms>();
|
|
|
|
if (request != null)
|
|
{
|
|
using (var scope = _scopeFactory.CreateScope())
|
|
{
|
|
int shardId = request.ShardID;
|
|
if (shardId <= 0)
|
|
shardId = 1;
|
|
|
|
var provider = scope.ServiceProvider.GetRequiredService<DbContextProvider>();
|
|
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;
|
|
}
|
|
|
|
|
|
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<CPMetaContext>((int)connectionString?.DBID);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|