Biskilog local server manager for the Biskilog POS desktop application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
16 KiB

using Biskilog_Cloud.Shared.CustomModels;
using Biskilog_Cloud.Shared.Interfaces;
using Biskilog_Cloud.Shared.Models;
using System.Net.Http;
using System.Net.Http.Headers;
namespace ServerManager.SyncMethods
{
public class CompanySync
{
private readonly ICompanyInfo m_companyService;
private readonly IUser m_userService;
private readonly ICustomer m_customerService;
private HttpClient m_httpClient;
public CompanySync(ICompanyInfo companyService, IUser userService, ICustomer customerService)
{
m_companyService = companyService;
m_userService = userService;
m_customerService = customerService;
m_httpClient = new HttpClient();
}
/// <summary>
/// Returns a collection of tasks to perform a sync in the ICompanyInterface
/// </summary>
/// <returns></returns>
public IEnumerable<Task> GetCompanySyncTask(HttpClient httpClient)
{
m_httpClient = httpClient;
return new Task[] {
SyncCompanyTable(),
SyncBranchTable(),
SyncCustomer(),
SyncDrivers(),
SyncTruckAssignments(),
SyncTruckDriverMappingSync(),
SyncTruckInventorySync(),
SyncUsersSync(),
SyncTruckSync(),
SyncSystemUserRoles()
};
}
private async Task SyncCompanyTable()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tblcompanydetails");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblCompanyDetail> modifiedCart = await m_companyService.FetchCompanyInfoAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "tblcompanydetails",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblCompanyDetail> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tblcompanydetails", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncBranchTable()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tblbranch");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblBranch> modifiedCart = await m_companyService.FetchBranch(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "tblbranch",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblBranch> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tblbranch", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncCustomer()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tblCustomers");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblCustomer> modifiedCart = await m_customerService.FetchCustomers(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp
{
TableName = "tblCustomers",
Timestamp = DateTime.Now.AddSeconds(-10),
};
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblCustomer> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tblCustomers", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncDrivers()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tbldrivers");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblDriver> modifiedCart = await m_companyService.FetchDriversAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbldrivers", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblDriver> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tbldriver", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncSystemUserRoles()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/systemuserroles");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<SystemUserRole> modifiedCart = await m_companyService.FetchSystemRoles(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "systemuserroles", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<SystemUserRole> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/SystemRoles", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncTruckAssignments()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tbltruckassignments");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblTruckAssignment> modifiedCart = await m_companyService.FetchTruckAssignmentAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbltruckassignments", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblTruckAssignment> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tblTruckAssignment", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncTruckDriverMappingSync()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tbltruck_drivermapping");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblTruckDriverMapping> modifiedCart = await m_companyService.FetchDriverMappingAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbltruck_drivermapping", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblTruckDriverMapping> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tbldrivermappings", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncTruckInventorySync()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tbltruckinventory");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblTruckInventory> modifiedCart = await m_companyService.FetchTruckInventoryAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbltruckinventory", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblTruckInventory> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tbltruckinventory", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncTruckSync()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tbltrucks");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblTruck> modifiedCart = await m_companyService.FetchTruckAsync(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tbltrucks", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblTruck> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tbltrucks", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
private async Task SyncUsersSync()
{
var responseMessage = await m_httpClient.GetAsync("/api/SyncCompanyInfo/lastsyncdate/tblusers");
if (!responseMessage.IsSuccessStatusCode)
{
return;
}
DateTime date = await responseMessage.Content.ReadFromJsonAsync<DateTime>();
IEnumerable<TblUser> modifiedCart = await m_userService.FetchUsers(date, "BRID0");
SyncTimestamp syncTimestamp = new SyncTimestamp { TableName = "tblusers", Timestamp = DateTime.Now.AddSeconds(-10) };
int batchSize = 200;
int totalItems = modifiedCart.Count();
int batches = (totalItems + batchSize - 1) / batchSize; // Calculate total batches
for (int batchIndex = 0; batchIndex < batches; batchIndex++)
{
List<TblUser> batch = modifiedCart.Skip(batchIndex * batchSize).Take(batchSize).ToList();
var response = await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/publish/tblusers", batch);
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Sent batch {batchIndex + 1}, Count: {batch.Count}");
}
}
//Set last sync date
await m_httpClient.PostAsJsonAsync("/api/SyncCompanyInfo/setsyncdate", syncTimestamp);
}
}
}