Taylohtio/IDP/parsers/parser-management-fee/unesCsv/Program.cs

250 lines
8.3 KiB
C#

using Microsoft.Azure.CosmosDB.Table;
using Microsoft.Azure.Storage;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using taloyhtio.idp.parser.common.model;
using unesCsv.model;
namespace unesCsv
{
class Program
{
private static CloudTableClient tableClient;
private static List<UserData> users;
private static List<FlatData> flats;
private static Dictionary<string, Guid> flatsAzure;
private static CondoMapping condo;
private const string CONDONAME = "itapuisto7";
static void Main(string[] args)
{
InitClient();
CollectUsers(args[0] + "/heti.csv");
CollectFlats(args[0] + "/huos.csv");
CollectLinks(args[0] + "/vuko.csv");
CollectFees(args[0] + "/vuri.csv");
condo = GetCondo(CONDONAME)[0];
if (condo == null)
{
return;
}
flatsAzure = GetFlats(condo.TaloyhtioPMCId, CONDONAME).ToDictionary(x => x.FlatTitle, x => x.Id);
SaveFees();
}
private static void InitClient()
{
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[Constants.KEY_AZURE_STORAGE].ConnectionString);
tableClient = storageAccount.CreateCloudTableClient(
new TableConnectionPolicy()
{
UseDirectMode = false
});
}
private static List<CondoMapping> GetCondo(string condoName)
{
var table = tableClient.GetTableReference("mdbCondoMappings");
table.CreateIfNotExists();
var allCondos = table.CreateQuery<CondoMapping>().ToList();
if (condoName != null)
{
return allCondos.Where(x => condoName.Contains(x.PMSCondoName)).ToList();
}
return allCondos;
}
private static List<Flat> GetFlats(Guid taloyhtioPMCId, string pmsCondoName)
{
if (Guid.Empty.Equals(taloyhtioPMCId) || string.IsNullOrEmpty(pmsCondoName))
{
return null;
}
var table = tableClient.GetTableReference("mdbFlats");
table.CreateIfNotExists();
var query = table.CreateQuery<Flat>()
.Where(d => d.PMCTaloyhtioId == taloyhtioPMCId && d.CondoPMS == pmsCondoName)
.ToArray()
.OrderBy(o => o.FlatTitle);
return query.ToList();
}
private static void SaveFees()
{
var table = tableClient.GetTableReference("mdbMaintenanceFees");
table.CreateIfNotExists();
flats.Where(flat => flatsAzure.ContainsKey(flat.Title)).ToList().ForEach(flat =>
{
flat.Users.ForEach(user =>
{
user.Fees.ForEach(feeRec =>
{
var fee = new MaintenanceFee(Guid.NewGuid())
{
Payer = user.Name,
FlatTitle = flat.Title,
FlatId = flatsAzure[flat.Title],
PaymentType = feeRec.Name,
PMSCondoName = condo.PMSCondoName,
PMCTaloyhtioId = condo.TaloyhtioPMCId,
PeriodOfTime = "201911",
Fee = Convert.ToDouble(feeRec.Amount),
PartitionKey = user.Name,
};
var result = table.Execute(TableOperation.InsertOrReplace(fee));
Console.WriteLine(result.HttpStatusCode);
});
});
});
}
private static void CollectFlats(string dataUrl)
{
flats = new List<FlatData>();
using (TextReader reader = new StreamReader(dataUrl, Encoding.UTF8))
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.Delimiters = new string[] { "," };
bool firstLine = true;
while (true)
{
var parts = parser.ReadFields();
if (parts == null)
{
break;
}
if (firstLine)
{
firstLine = false;
continue;
}
var user = users.Find(u => u.Id.Equals(parts[1]));
var existing = flats.Find(f => f.Title.Equals(parts[2]));
if (existing != null)
{
if (user != null)
{
existing.Users.Add(user);
}
}
else
{
var flat = new FlatData { Id = parts[0], Title = parts[2] };
if (user != null)
{
flat.Users.Add(user);
}
flats.Add(flat);
}
}
//Console.WriteLine($"total: {flats.Count}");
//Console.ReadLine();
}
}
private static void CollectUsers(string dataUrl)
{
users = new List<UserData>();
using (TextReader reader = new StreamReader(dataUrl, Encoding.UTF8))
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.Delimiters = new string[] { "," };
bool firstLine = true;
while (true)
{
var parts = parser.ReadFields();
if (parts == null)
{
break;
}
if (firstLine)
{
firstLine = false;
continue;
}
var user = new UserData { Id = parts[0], Name = parts[1], Pin = parts[7] };
users.Add(user);
}
}
}
private static void CollectLinks(string dataUrl)
{
using (TextReader reader = new StreamReader(dataUrl, Encoding.UTF8))
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.Delimiters = new string[] { "," };
bool firstLine = true;
while (true)
{
var parts = parser.ReadFields();
if (parts == null)
{
break;
}
if (firstLine)
{
firstLine = false;
continue;
}
var user = users.Find(u => u.Id.Equals(parts[1]));
if (user == null)
{
continue;
}
user.FeeId = parts[0];
}
}
}
private static void CollectFees(string dataUrl)
{
using (TextReader reader = new StreamReader(dataUrl, Encoding.UTF8))
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.Delimiters = new string[] { "," };
bool firstLine = true;
while (true)
{
var parts = parser.ReadFields();
if (parts == null)
{
break;
}
if (firstLine)
{
firstLine = false;
continue;
}
var user = users.Find(u => parts[0].Equals(u.FeeId));
if (user == null)
{
continue;
}
var fee = new FeeData { Name = parts[7], Amount = parts[5] };
user.Fees.Add(fee);
}
}
}
}
}