add in import person and logic
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using FamilyTreeAPI.Entities;
|
||||
using FamilyTreeAPI.Interface;
|
||||
using FamilyTreeAPI.Models;
|
||||
using SpreadsheetLight;
|
||||
|
||||
namespace FamilyTreeAPI.Repository;
|
||||
|
||||
public class ImportPersonRepository
|
||||
{
|
||||
enum enumIdx
|
||||
{
|
||||
Id = 1,
|
||||
FirstName,
|
||||
LastName,
|
||||
Email,
|
||||
Phone,
|
||||
Image,
|
||||
Sex,
|
||||
Address,
|
||||
Alive,
|
||||
Dob,
|
||||
FatherId,
|
||||
MotherId,
|
||||
|
||||
}
|
||||
private FamilyTreeDBContext _context;
|
||||
private IConfiguration _config;
|
||||
private IHttpContextAccessor _httpContext;
|
||||
private IRelationShipd _relationship;
|
||||
public ImportPersonRepository(IConfiguration config, FamilyTreeDBContext context,
|
||||
IRelationShipd relationship,
|
||||
IHttpContextAccessor httpContext)
|
||||
{
|
||||
_context = context;
|
||||
_relationship = relationship;
|
||||
_config = config;
|
||||
_httpContext = httpContext;
|
||||
}
|
||||
|
||||
public async Task<List<CodeDto<string>>> ImportPerson(FileStream fileStream, string sheetName)
|
||||
{
|
||||
int id;
|
||||
List<CodeDto<string>> output = new();
|
||||
Dictionary<int,MappingFatherMother> updateperson = new();
|
||||
CodeDto<string> colums;
|
||||
List<int> ids = new();
|
||||
Person person;
|
||||
Person? uperson;
|
||||
MappingFatherMother mitem,faitem,moitem;
|
||||
int fid, mid;
|
||||
MemoryStream msFirstPass = new MemoryStream();
|
||||
SLDocument sl = new SLDocument(fileStream, sheetName);
|
||||
// There is no way that I can see to get the Rows
|
||||
SLWorksheetStatistics stats = sl.GetWorksheetStatistics();
|
||||
for (int row = 1; row <= stats.NumberOfRows; row++)
|
||||
{
|
||||
person = new Person();
|
||||
mitem = new();
|
||||
id = sl.GetCellValueAsInt32(row, (int) enumIdx.Id);
|
||||
mitem.IId = id;
|
||||
person.FirstName = sl.GetCellValueAsString(row, (int) enumIdx.FirstName);
|
||||
person.LastName = sl.GetCellValueAsString(row, (int) enumIdx.LastName);
|
||||
person.Email = sl.GetCellValueAsString(row, (int) enumIdx.Email);
|
||||
person.Phone = sl.GetCellValueAsString(row, (int) enumIdx.Phone);
|
||||
person.Image = sl.GetCellValueAsString(row, (int) enumIdx.Image);
|
||||
person.Alive = sl.GetCellValueAsBoolean(row, (int) enumIdx.Alive);
|
||||
person.dob = sl.GetCellValueAsDateTime(row, (int) enumIdx.Dob);
|
||||
mitem.IFatherId = sl.GetCellValueAsInt32(row, (int) enumIdx.FatherId);
|
||||
mitem.IMotherId = sl.GetCellValueAsInt32(row, (int) enumIdx.MotherId);
|
||||
_context.Persons.Add(person);
|
||||
_context.SaveChanges();
|
||||
mitem.TId = person.Id;
|
||||
ids.Add(id);
|
||||
updateperson.Add(mitem.IId, mitem);
|
||||
colums = new();
|
||||
colums.Code = $"{mitem.IId} {mitem.IFatherId} {mitem.IMotherId}";
|
||||
colums.Description = $"{mitem.TId} {mitem.TFatherId} {mitem.TMotherId}";
|
||||
output.Add(colums);
|
||||
|
||||
}
|
||||
|
||||
for (int i = 0; i < ids.Count; i++)
|
||||
{
|
||||
id = ids[i];
|
||||
mitem = updateperson[id];
|
||||
fid = mitem.IFatherId;
|
||||
if (updateperson.ContainsKey(fid))
|
||||
{
|
||||
faitem = updateperson[fid];
|
||||
mitem.TFatherId = faitem.TId;
|
||||
}
|
||||
mid = mitem.IMotherId;
|
||||
if (updateperson.ContainsKey(mid))
|
||||
{
|
||||
moitem = updateperson[mid];
|
||||
mitem.TMotherId = moitem.TId;
|
||||
}
|
||||
|
||||
uperson = await _context.Persons.FindAsync(mitem.TId);
|
||||
if (uperson == null) continue;
|
||||
uperson.FatherId = mitem.TFatherId;
|
||||
uperson.MotherId = mitem.TMotherId;
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user