173 lines
5.5 KiB
C#
173 lines
5.5 KiB
C#
using Microsoft.Azure.CosmosDB.Table;
|
|
using Microsoft.Azure.Storage;
|
|
using Microsoft.VisualBasic.FileIO;
|
|
using Newtonsoft.Json;
|
|
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 CondoMapping condo;
|
|
|
|
private const string CONDONAME = "itapuisto7";
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
InitClient();
|
|
CollectUsers(args[0] + "/heti.csv");
|
|
CollectFlats(args[0] + "/huos.csv");
|
|
condo = GetCondo(CONDONAME)[0];
|
|
if (condo == null)
|
|
{
|
|
return;
|
|
}
|
|
SaveFlats();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static void SaveFlats()
|
|
{
|
|
var table = tableClient.GetTableReference("mdbFlats");
|
|
table.CreateIfNotExists();
|
|
|
|
flats.ForEach(f =>
|
|
{
|
|
var id = Guid.NewGuid();
|
|
var flat = new Flat
|
|
{
|
|
PartitionKey = condo.TaloyhtioPMCId.ToString(),
|
|
Id = id,
|
|
RowKey = id.ToString(),
|
|
CondoPMS = condo.PMSCondoName,
|
|
FlatTitle = f.Title,
|
|
PMCTaloyhtioId = condo.TaloyhtioPMCId,
|
|
FlatUsers = JsonConvert.SerializeObject(
|
|
f.Users.ConvertAll(p =>
|
|
{
|
|
return new FlatUser
|
|
{
|
|
DisplayName = p.Name,
|
|
PIN = p.Pin
|
|
};
|
|
})
|
|
)
|
|
};
|
|
|
|
var result = table.Execute(TableOperation.InsertOrReplace(flat));
|
|
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);
|
|
}
|
|
|
|
Console.WriteLine($"total: {users.Count}");
|
|
Console.ReadLine();
|
|
}
|
|
}
|
|
}
|
|
}
|