add all change in now.

This commit is contained in:
2025-08-29 23:17:58 +10:00
parent be2756d85f
commit 6cea606cc2
22 changed files with 259 additions and 63 deletions
@@ -38,34 +38,39 @@ public class ImportPersonRepository
_httpContext = httpContext;
}
public async Task<List<CodeDto<string>>> ImportPerson(FileStream fileStream, string sheetName)
public async Task<List<CodeDto<string>>> ImportPerson(MemoryStream fileStream, string sheetName)
{
Dictionary<string, ImportRelation> relationDic = new();
int id;
string keypair = ""; //father and mother key
List<CodeDto<string>> output = new();
Dictionary<int,MappingFatherMother> updateperson = new();
CodeDto<string> colums;
List<int> ids = new();
Person person;
Person? uperson;
ImportRelation iRelation;
MappingFatherMother mitem,faitem,moitem;
int fid, mid;
MemoryStream msFirstPass = new MemoryStream();
// 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++)
for (int row = 2; 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);
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);
person.Sex = sl.GetCellValueAsString(row, (int) enumIdx.Sex);
person.Address = sl.GetCellValueAsString(row, (int) enumIdx.Address);
mitem.IFatherId = sl.GetCellValueAsInt32(row, (int) enumIdx.FatherId);
mitem.IMotherId = sl.GetCellValueAsInt32(row, (int) enumIdx.MotherId);
_context.Persons.Add(person);
@@ -101,9 +106,33 @@ public class ImportPersonRepository
if (uperson == null) continue;
uperson.FatherId = mitem.TFatherId;
uperson.MotherId = mitem.TMotherId;
if (mitem.TFatherId > 0 && mitem.TMotherId > 0)
{
keypair = mitem.TFatherId + "," + mitem.TMotherId;
if (!relationDic.ContainsKey(keypair))
{
iRelation = new();
iRelation.FatherId = mitem.TFatherId;
iRelation.MotherId = mitem.TMotherId;
relationDic.Add(keypair, iRelation);
}
}
await _context.SaveChangesAsync();
}
//finally add relation ship table
RelationShip model;
foreach (KeyValuePair<string, ImportRelation> item in relationDic)
{
model = new RelationShip();
model.PersonId = item.Value.FatherId;
model.RelatePersonId = item.Value.MotherId;
_context.RelationShips.Add(model);
_context.SaveChanges();
}
return output;
}