This commit is contained in:
parent
6ad83ecc53
commit
9bde6a9895
2 changed files with 250 additions and 41 deletions
|
|
@ -99,6 +99,7 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
|
||||
var data = await _repository.GetCoinReport(req.roundId, type, req.node, req.nodeId);
|
||||
var yearInsignalPeriod = await _repository.GetYearInsigniaPeriod(req.roundId);
|
||||
var yearLastTwoDigits = yearInsignalPeriod.Substring(yearInsignalPeriod.Length - 2);
|
||||
|
||||
var dataResult = new List<dynamic>();
|
||||
var dataList = new List<dynamic>(data);
|
||||
|
|
@ -117,6 +118,16 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
InsigniaName = dataList[i].GetType().GetProperty("InsigniaName").GetValue(dataList[i]),
|
||||
FullName = dataList[i].GetType().GetProperty("FullName").GetValue(dataList[i]),
|
||||
InsigniaInitial = dataList[i].GetType().GetProperty("InsigniaInitial").GetValue(dataList[i]),
|
||||
Root = dataList[i].GetType().GetProperty("Root").GetValue(dataList[i]),
|
||||
RootId = dataList[i].GetType().GetProperty("RootId").GetValue(dataList[i]),
|
||||
Child1 = dataList[i].GetType().GetProperty("Child1").GetValue(dataList[i]),
|
||||
Child1Id = dataList[i].GetType().GetProperty("Child1Id").GetValue(dataList[i]),
|
||||
Child2 = dataList[i].GetType().GetProperty("Child2").GetValue(dataList[i]),
|
||||
Child2Id = dataList[i].GetType().GetProperty("Child2Id").GetValue(dataList[i]),
|
||||
Child3 = dataList[i].GetType().GetProperty("Child3").GetValue(dataList[i]),
|
||||
Child3Id = dataList[i].GetType().GetProperty("Child3Id").GetValue(dataList[i]),
|
||||
Child4 = dataList[i].GetType().GetProperty("Child4").GetValue(dataList[i]),
|
||||
Child4Id = dataList[i].GetType().GetProperty("Child4Id").GetValue(dataList[i]),
|
||||
Male = gender == "ชาย" ? 1 : 0,
|
||||
Female = gender == "หญิง" ? 1 : 0,
|
||||
});
|
||||
|
|
@ -145,54 +156,221 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
var stop = 0;
|
||||
var colLeft = 0;
|
||||
var colRight = 0;
|
||||
for (int i = 0; i < detailList.Count / 2; i++)
|
||||
{
|
||||
if (InsigniaName != (string)left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i)))
|
||||
{
|
||||
InsigniaName = (string)left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i));
|
||||
male = left.Count(x => x.Male == 1 && x.InsigniaName == InsigniaName) + right.Count(x => x.Male == 1 && x.InsigniaName == InsigniaName);
|
||||
female = left.Count(x => x.Female == 1 && x.InsigniaName == InsigniaName) + right.Count(x => x.Female == 1 && x.InsigniaName == InsigniaName);
|
||||
var countGroup = detailList.Count(x => x.InsigniaName == InsigniaName);
|
||||
var countGroupTemp = detailList.Count(x => x.InsigniaName == InsigniaName && x.FullName == "");
|
||||
start = i == 0 ? (mergeList.Count + 1) : (stop + 1);
|
||||
stop = i == 0 ? (mergeList.Count) + (countGroup - countGroupTemp) : (countGroup - countGroupTemp) == 1 ? start : start + (countGroup - countGroupTemp);
|
||||
range = countGroup - countGroupTemp != 1 ? $"{start} - {stop}" : $"{start}";
|
||||
colLeft = start;
|
||||
colRight = start + 25;
|
||||
}
|
||||
// ดึงข้อมูล NameLeft และ NameRight
|
||||
var nameLeft = left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i));
|
||||
var nameRight = right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i));
|
||||
var maxRow = Math.Max(left.Count(), right.Count());
|
||||
|
||||
for (int i = 0; i < maxRow; i++)
|
||||
{
|
||||
var leftItem = i < left.Count() ? left.ElementAt(i) : null;
|
||||
var rightItem = i < right.Count() ? right.ElementAt(i) : null;
|
||||
|
||||
// ข้ามถ้าไม่มีข้อมูลทั้งสองฝั่ง
|
||||
if (leftItem == null && rightItem == null)
|
||||
continue;
|
||||
|
||||
var insigniaName = leftItem != null
|
||||
? leftItem.GetType().GetProperty("InsigniaName").GetValue(leftItem)
|
||||
: rightItem.GetType().GetProperty("InsigniaName").GetValue(rightItem);
|
||||
|
||||
male = detailList.Count(x => x.Male == 1 && x.InsigniaName == insigniaName);
|
||||
female = detailList.Count(x => x.Female == 1 && x.InsigniaName == insigniaName);
|
||||
var countGroup = detailList.Count(x => x.InsigniaName == insigniaName);
|
||||
var countGroupTemp = detailList.Count(x => x.InsigniaName == insigniaName && x.FullName == "");
|
||||
|
||||
start = mergeList.Count + 1;
|
||||
stop = start + (countGroup - countGroupTemp) - 1;
|
||||
range = (countGroup - countGroupTemp) != 1 ? $"{start} - {stop}" : $"{start}";
|
||||
|
||||
// ถ้าไม่มีทั้ง NameLeft และ NameRight ให้ข้ามไป
|
||||
if (string.IsNullOrEmpty((string)nameLeft) && string.IsNullOrEmpty((string)nameRight))
|
||||
{
|
||||
continue; // ข้ามการทำงานของลูปในรอบนี้
|
||||
}
|
||||
mergeList.Add(new
|
||||
{
|
||||
row = mergeList.Count + 1,
|
||||
ColLeft = colLeft.ToString().ToThaiNumber(),
|
||||
NameLeft = left.ElementAt(i).GetType().GetProperty("FullName").GetValue(left.ElementAt(i)),
|
||||
ColRight = colRight.ToString().ToThaiNumber(),
|
||||
NameRight = right.ElementAt(i).GetType().GetProperty("FullName").GetValue(right.ElementAt(i)),
|
||||
InsigniaInitial = left.ElementAt(i).GetType().GetProperty("InsigniaInitial").GetValue(left.ElementAt(i)),
|
||||
InsigniaName = left.ElementAt(i).GetType().GetProperty("InsigniaName").GetValue(left.ElementAt(i)),
|
||||
ColLeft = colLeft > 0 ? colLeft.ToString().ToThaiNumber() : "",
|
||||
NameLeft = leftItem?.GetType().GetProperty("FullName").GetValue(leftItem),
|
||||
RootLeft = leftItem?.GetType().GetProperty("Root").GetValue(leftItem),
|
||||
Child1Left = leftItem?.GetType().GetProperty("Child1").GetValue(leftItem),
|
||||
Child2Left = leftItem?.GetType().GetProperty("Child2").GetValue(leftItem),
|
||||
Child3Left = leftItem?.GetType().GetProperty("Child3").GetValue(leftItem),
|
||||
Child4Left = leftItem?.GetType().GetProperty("Child4").GetValue(leftItem),
|
||||
|
||||
ColRight = colRight > 0 ? colRight.ToString().ToThaiNumber() : "",
|
||||
NameRight = rightItem?.GetType().GetProperty("FullName").GetValue(rightItem),
|
||||
RootRight = rightItem?.GetType().GetProperty("Root").GetValue(rightItem),
|
||||
Child1Right = rightItem?.GetType().GetProperty("Child1").GetValue(rightItem),
|
||||
Child2Right = rightItem?.GetType().GetProperty("Child2").GetValue(rightItem),
|
||||
Child3Right = rightItem?.GetType().GetProperty("Child3").GetValue(rightItem),
|
||||
Child4Right = rightItem?.GetType().GetProperty("Child4").GetValue(rightItem),
|
||||
|
||||
InsigniaInitial = leftItem?.GetType().GetProperty("InsigniaInitial").GetValue(leftItem)
|
||||
?? rightItem?.GetType().GetProperty("InsigniaInitial").GetValue(rightItem),
|
||||
InsigniaName = insigniaName,
|
||||
Range = range.ToThaiNumber(),
|
||||
Male = male.ToString().ToThaiNumber(),
|
||||
Female = female.ToString().ToThaiNumber(),
|
||||
Female = female.ToString().ToThaiNumber()
|
||||
});
|
||||
if (mergeList.Count % 25 == 0)
|
||||
{
|
||||
colLeft = colRight;
|
||||
colRight = colLeft + 25;
|
||||
}
|
||||
colLeft++;
|
||||
colRight++;
|
||||
}
|
||||
dataResult = mergeList;
|
||||
|
||||
dataResult = mergeList;
|
||||
}
|
||||
|
||||
var groupedByOrg = detailList
|
||||
.GroupBy(x =>
|
||||
{
|
||||
string child4Id = x.Child4Id?.ToString() ?? "";
|
||||
string child3Id = x.Child3Id?.ToString() ?? "";
|
||||
string child2Id = x.Child2Id?.ToString() ?? "";
|
||||
string child1Id = x.Child1Id?.ToString() ?? "";
|
||||
string rootId = x.RootId?.ToString() ?? "";
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(child4Id)) return child4Id;
|
||||
if (!string.IsNullOrEmpty(child3Id)) return child3Id;
|
||||
if (!string.IsNullOrEmpty(child2Id)) return child2Id;
|
||||
if (!string.IsNullOrEmpty(child1Id)) return child1Id;
|
||||
return rootId;
|
||||
})
|
||||
.Where(g => !string.IsNullOrWhiteSpace(g.Key))
|
||||
.Select(orgGroup =>
|
||||
{
|
||||
var first = orgGroup.FirstOrDefault();
|
||||
|
||||
var org = !string.IsNullOrWhiteSpace(first?.Child4) ? first.Child4.Trim() :
|
||||
!string.IsNullOrWhiteSpace(first?.Child3) ? first.Child3.Trim() :
|
||||
!string.IsNullOrWhiteSpace(first?.Child2) ? first.Child2.Trim() :
|
||||
!string.IsNullOrWhiteSpace(first?.Child1) ? first.Child1.Trim() :
|
||||
first.Root.Trim();
|
||||
|
||||
// Group by InsigniaName
|
||||
var groupedByInsignia = orgGroup
|
||||
.GroupBy(x => x.InsigniaName)
|
||||
.Select(insigniaGroup =>
|
||||
{
|
||||
if (!insigniaGroup.Any(x => !string.IsNullOrWhiteSpace(x.FullName)))
|
||||
return null;
|
||||
|
||||
var insigniaName = insigniaGroup.Key;
|
||||
var left = insigniaGroup.Where(x => x.status == false).ToList();
|
||||
var right = insigniaGroup.Where(x => x.status == true).ToList();
|
||||
|
||||
var male = insigniaGroup.Count(x => x.Male == 1);
|
||||
var female = insigniaGroup.Count(x => x.Female == 1);
|
||||
var countGroup = insigniaGroup.Count();
|
||||
var countGroupTemp = insigniaGroup.Count(x => x.FullName == "");
|
||||
|
||||
var start = 1;
|
||||
var stop = countGroup - countGroupTemp;
|
||||
var range = stop == 1 ? $"{start}" : $"{start} - {stop}";
|
||||
range = range.ToThaiNumber();
|
||||
|
||||
var people = new List<object>();
|
||||
|
||||
int colLeft = 1;
|
||||
int colRight = 26;
|
||||
int rowCount = Math.Max(left.Count, right.Count);
|
||||
|
||||
var allPeople = left.Concat(right).ToList();
|
||||
|
||||
int colLeftStart = 1;
|
||||
int colRightStart = 26;
|
||||
|
||||
for (int i = 0; i < allPeople.Count; i++)
|
||||
{
|
||||
var person = allPeople[i];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(person?.FullName))
|
||||
continue;
|
||||
|
||||
if (i < 25)
|
||||
{
|
||||
people.Add(new
|
||||
{
|
||||
ColLeft = colLeftStart.ToString().ToThaiNumber(),
|
||||
NameLeft = person.FullName,
|
||||
RootLeft = person.Root,
|
||||
Child1Left = person.Child1,
|
||||
Child2Left = person.Child2,
|
||||
Child3Left = person.Child3,
|
||||
Child4Left = person.Child4,
|
||||
|
||||
ColRight = "",
|
||||
NameRight = "",
|
||||
RootRight = "",
|
||||
Child1Right = "",
|
||||
Child2Right = "",
|
||||
Child3Right = "",
|
||||
Child4Right = "",
|
||||
|
||||
InsigniaInitial = person.InsigniaInitial,
|
||||
InsigniaName = insigniaName,
|
||||
Range = range,
|
||||
Male = male.ToString().ToThaiNumber(),
|
||||
Female = female.ToString().ToThaiNumber()
|
||||
});
|
||||
|
||||
colLeftStart++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// คำนวณแถวที่จะเอาข้อมูลขวาไปใส่
|
||||
int indexToUpdate = i - 25; // ถ้า i=25 จะไปแถว 0, i=26 ไปแถว 1 เป็นต้น
|
||||
|
||||
// ดึงแถวที่ต้องการมาแก้ไข
|
||||
var row = (dynamic)people[indexToUpdate];
|
||||
|
||||
// สร้าง object ใหม่โดยคัดลอกข้อมูลซ้ายเดิมและเพิ่มข้อมูลขวา
|
||||
var updatedRow = new
|
||||
{
|
||||
ColLeft = row.ColLeft,
|
||||
NameLeft = row.NameLeft,
|
||||
RootLeft = row.RootLeft,
|
||||
Child1Left = row.Child1Left,
|
||||
Child2Left = row.Child2Left,
|
||||
Child3Left = row.Child3Left,
|
||||
Child4Left = row.Child4Left,
|
||||
|
||||
ColRight = colRightStart.ToString().ToThaiNumber(),
|
||||
NameRight = person.FullName,
|
||||
RootRight = person.Root,
|
||||
Child1Right = person.Child1,
|
||||
Child2Right = person.Child2,
|
||||
Child3Right = person.Child3,
|
||||
Child4Right = person.Child4,
|
||||
|
||||
InsigniaInitial = person.InsigniaInitial,
|
||||
InsigniaName = insigniaName,
|
||||
Range = range,
|
||||
Male = male.ToString().ToThaiNumber(),
|
||||
Female = female.ToString().ToThaiNumber()
|
||||
};
|
||||
|
||||
// แทนที่แถวเดิมด้วยแถวที่อัพเดต
|
||||
people[indexToUpdate] = updatedRow;
|
||||
|
||||
colRightStart++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return new
|
||||
{
|
||||
name = $"(ชั้นตรา) {insigniaName}",
|
||||
shotName = left.FirstOrDefault()?.InsigniaInitial ?? right.FirstOrDefault()?.InsigniaInitial,
|
||||
range = range,
|
||||
male = male.ToString().ToThaiNumber(),
|
||||
female = female.ToString().ToThaiNumber(),
|
||||
people = people
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
return new
|
||||
{
|
||||
org = org,
|
||||
insignia = groupedByInsignia
|
||||
};
|
||||
})
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x.org) && x.insignia.Any())
|
||||
.ToList();
|
||||
|
||||
|
||||
|
||||
var result = new
|
||||
{
|
||||
template = "reportInsignia1",
|
||||
|
|
@ -200,7 +378,8 @@ namespace BMA.EHR.Insignia.Service.Controllers
|
|||
data = new
|
||||
{
|
||||
yearInsignalPeriod = yearInsignalPeriod,
|
||||
data = dataResult,
|
||||
yearLastTwoDigits = yearLastTwoDigits,
|
||||
data = groupedByOrg,
|
||||
}
|
||||
};
|
||||
return Success(result);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue