แก้บันทึกพ้นราชการ (ยังไม่เสร็จ)

This commit is contained in:
Kittapath 2023-08-03 09:35:54 +07:00
parent e55c0f746b
commit 2e7abdb60a
13 changed files with 12478 additions and 189 deletions

View file

@ -51,47 +51,90 @@ namespace BMA.EHR.Retirement.Service.Controllers
#endregion
#region " จัดลำดับเกษียณ "
private async Task GenOrderByYear(string type, int year)
private class ObjectOrderRetire
{
if (type.Trim().ToUpper().Contains("OFFICER"))
{
var profiles = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod.Year == year)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderBy(x => x.Profile.OrganizationOrganization)
.ThenBy(x => x.Profile.PositionType == null ? null : x.Profile.PositionType.Name)
.ThenBy(x => x.Profile.PositionLevel == null ? null : x.Profile.PositionLevel.Name)
public int Order { get; set; }
public int? Order1 { get; set; }
public int? Order2 { get; set; }
public int? Order3 { get; set; }
public string LastUpdateFullName { get; set; }
public string LastUpdateUserId { get; set; }
public DateTime? LastUpdatedAt { get; set; }
public Guid? Id { get; set; }
}
private async Task GenOrderByYear(Guid id)
{
var Org = await _context.Organizations.FirstOrDefaultAsync(x => x.OrganizationOrder == null);
var ocIdList = await _context.Organizations.Select(x => x.Id).ToListAsync();
if (Org != null)
ocIdList = _documentService.GetAllIdByRoot(Org.Id);
var retire = await _context.RetirementPeriods
.FirstOrDefaultAsync(x => x.Id == id);
if (retire == null)
return;
var _retireProfile = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod == retire)
.ToListAsync();
var order = 1;
foreach (var profile in profiles)
{
profile.Order = order;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
order++;
}
}
if (type.Trim().ToUpper().Contains("EMPLOYEE"))
var profiles = new List<ObjectOrderRetire>();
if (retire.Type.Trim().ToUpper().Contains("OFFICER"))
{
var profiles = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod.Year == year)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderBy(x => x.Profile.OrganizationOrganization)
.ThenBy(x => x.Profile.EmployeeType)
.ThenBy(x => x.Profile.PositionEmployeeLevel)
.ToListAsync();
var order = 1;
foreach (var profile in profiles)
{
profile.Order = order;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
order++;
}
profiles = await (from x in _context.RetirementProfiles
where x.RetirementPeriod == retire
select new ObjectOrderRetire
{
Id = x.Id,
Order = x.Order,
Order1 = x.Profile == null ? 999999999 : ocIdList.IndexOf((Guid)(x.Profile.OcId)),
Order2 = x.Profile == null || x.Profile.PositionType == null ? 999999999 : x.Profile.PositionType.Order,
Order3 = x.Profile == null || x.Profile.PositionLevel == null ? 999999999 : x.Profile.PositionLevel.Level,
LastUpdateFullName = x.LastUpdateFullName,
LastUpdateUserId = x.LastUpdateUserId,
LastUpdatedAt = x.LastUpdatedAt,
}).ToListAsync();
}
_context.SaveChanges();
if (retire.Type.Trim().ToUpper().Contains("EMPLOYEE"))
{
profiles = await (from x in _context.RetirementProfiles
where x.RetirementPeriod == retire
// x.Profile.PositionEmployeeLevelId == null ? null : x.Profile.PositionEmployeeLevelId.Order
select new ObjectOrderRetire
{
Id = x.Id,
Order = x.Order,
Order1 = x.Profile == null ? null : ocIdList.IndexOf((Guid)(x.Profile.OcId)),
LastUpdateFullName = x.LastUpdateFullName,
LastUpdateUserId = x.LastUpdateUserId,
LastUpdatedAt = x.LastUpdatedAt,
}).ToListAsync();
}
var _profiles = profiles.AsQueryable().OrderBy(x => x.Order1).ThenBy(x => x.Order2).ThenBy(x => x.Order3).ToList();
var order = 1;
foreach (var profile in _profiles)
{
var retireProfile = _retireProfile.Find(x => x.Id == profile.Id);
retireProfile.Order = order;
retireProfile.LastUpdateFullName = FullName ?? "System Administrator";
retireProfile.LastUpdateUserId = UserId ?? "";
retireProfile.LastUpdatedAt = DateTime.Now;
order++;
}
await _context.SaveChangesAsync();
}
private int SortOrg(Guid? ocId)
{
if (ocId == null)
return 999999999;
var Org = _context.Organizations.Include(x => x.OrganizationOrganization).FirstOrDefault(x => x.OrganizationOrder == null);
if (Org != null && Org.OrganizationOrganization != null && Org.OrganizationOrganization.Name.Contains("ปลัด"))
return -1;
var ocIdList = _context.Organizations.Select(x => x.Id).ToList();
if (Org != null)
ocIdList = _documentService.GetAllIdByRoot(Org.Id);
int index = ocIdList.IndexOf((Guid)ocId);
if (index == -1)
return 999999999;
return index;
}
#endregion
@ -110,29 +153,20 @@ namespace BMA.EHR.Retirement.Service.Controllers
{
if (type.Trim().ToUpper().Contains("OFFICER") || type.Trim().ToUpper().Contains("EMPLOYEE"))
{
var retire_old = await _context.RetirementPeriods
.Include(x => x.RetirementProfiles)
var retire_old = await _context.RetirementPeriodHistorys
.Where(x => x.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderByDescending(x => x.Round)
.FirstOrDefaultAsync();
if (retire_old != null)
{
var data = await _context.RetirementPeriods
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.Where(x => year > 0 ? (x.Year == year) : (x.Year > 0))
.Select(x => new
{
Id = x.Id,
CreatedAt = x.CreatedAt,
Year = x.Year,
Round = x.Round,
Total = retire_old.Id == x.Id ? retire_old.RetirementProfiles.Count() : retire_old.RetirementProfiles.Count() - x.RetirementProfiles.Where(x => x.Remove != "pending").Count(),
}).OrderByDescending(x => x.CreatedAt)
.ToListAsync();
return Success(data);
}
.Select(x => new
{
Id = x.Id,
CreatedAt = x.CreatedAt,
Year = x.Year,
Round = x.Round,
Total = x.Total,
})
.ToListAsync();
return Success(retire_old);
}
return Success();
}
@ -147,61 +181,40 @@ namespace BMA.EHR.Retirement.Service.Controllers
/// <response code="400">ค่าตัวแปรที่ส่งมาไม่ถูกต้อง</response>
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
[HttpGet("profile/{type}/{year}")]
public async Task<ActionResult<ResponseObject>> CreateProfileRetirement(string type, int year)
[HttpGet("profile/{retire_history_id}/{option}/{type}/{year}")]
public async Task<ActionResult<ResponseObject>> CreateProfileRetirement(Guid retire_history_id, string option, string type, int year)
{
var round = 1;
var retire_old = await _context.RetirementPeriods
.Include(x => x.RetirementProfiles)
.Where(x => x.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.OrderByDescending(x => x.Round)
.FirstOrDefaultAsync();
if (retire_old != null)
round = retire_old.Round + 1;
if (!type.Trim().ToUpper().Contains("EMPLOYEE") && !type.Trim().ToUpper().Contains("OFFICER"))
return Error("ประเภทพ้นราชการไม่ถูกต้อง");
var retire = new RetirementPeriod
var round = 1;
var retire = await _context.RetirementPeriods
.Include(x => x.RetirementPeriodHistorys)
.Include(x => x.RetirementProfiles)
.ThenInclude(x => x.Profile)
.Where(x => x.Year == year)
.Where(x => x.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.FirstOrDefaultAsync();
if (retire == null)
{
Round = round,
Type = type.Trim().ToUpper(),
Year = year,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementPeriods.AddAsync(retire);
if (retire_old != null)
{
var profiles = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod == retire_old)
.Where(x => x.Remove == "pending")
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(type.Trim().ToUpper()))
.ToListAsync();
foreach (var profile in profiles)
retire = new RetirementPeriod
{
profile.RetirementPeriod = retire;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
}
else
{
Round = round,
TypeReport = null,
Type = type.Trim().ToUpper(),
Year = year,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementPeriods.AddAsync(retire);
var profiles = await _context.Profiles.AsQueryable()
// .Where(x => x.BirthDate.CalculateRetireDate().Year == year)
.Where(x => x.ProfileType.Trim().ToUpper().Contains(type.Trim().ToUpper()))
// .Where(x => x.CitizenId == "0000000000001")
.ToListAsync();
// var profiles = await (from p in _context.Profiles
// where p.BirthDate.CalculateRetireDate().Year == year
// select p)
// .ToListAsync();
profiles = profiles.Where(x => x.BirthDate.CalculateRetireDate().Year == year).ToList();
var order = 1;
foreach (var profile in profiles)
@ -222,12 +235,52 @@ namespace BMA.EHR.Retirement.Service.Controllers
await _context.RetirementProfiles.AddAsync(data);
order++;
}
await _context.SaveChangesAsync();
await GenOrderByYear(retire.Id);
}
else
{
var file_name = DateTime.Now.ToString();
await _documentService.GenerateJsonFile("xxx", "/retire", file_name);
var history = new RetirementPeriodHistory
{
RetirementPeriod = retire,
Round = retire.Round,
TypeReport = retire.TypeReport,
Year = retire.Year,
Type = retire.Type,
Total = retire.RetirementProfiles.Count(),
ProfileFile = file_name,
CreatedUserId = FullName ?? "",
CreatedFullName = UserId ?? "System Administrator",
CreatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
await _context.RetirementPeriodHistorys.AddAsync(history);
await _context.SaveChangesAsync();
retire.Round = retire.Round + 1;
retire.TypeReport = option.Trim().ToUpper();
retire.LastUpdateFullName = FullName ?? "System Administrator";
retire.LastUpdateUserId = UserId ?? "";
retire.LastUpdatedAt = DateTime.Now;
// retire_history_id
////ดึงไฟล์json
// foreach (var retire_profile in retire.RetirementProfiles)
// {
// retire_profile.Remove = retire_profile.Remove.Trim().ToUpper().Contains("CHANGE") ? "PENDING" : retire_profile.Remove;
// retire_profile.LastUpdateFullName = FullName ?? "System Administrator";
// retire_profile.LastUpdateUserId = UserId ?? "";
// retire_profile.LastUpdatedAt = DateTime.Now;
// }
await _context.SaveChangesAsync();
}
_context.SaveChanges();
await _context.SaveChangesAsync();
var profile_new = await _context.RetirementProfiles
.Where(x => x.RetirementPeriod == retire)
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.Select(x => new
{
Order = x.Order,
@ -265,44 +318,12 @@ namespace BMA.EHR.Retirement.Service.Controllers
[HttpGet("{retireId:length(36)}")]
public async Task<ActionResult<ResponseObject>> GetProfileRetirement(Guid retireId)
{
var retire = await _context.RetirementPeriods
var retire = await _context.RetirementPeriodHistorys
.FirstOrDefaultAsync(x => x.Id == retireId);
if (retire == null)
return Error(GlobalMessages.InvalidRetirementRequest, 404);
var retire_old = await _context.RetirementPeriods
.Where(x => x.CreatedAt < retire.CreatedAt)
.Where(x => x.Year == retire.Year)
.Where(x => x.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.ToListAsync();
var profile = await _context.RetirementProfiles
.Where(x => !retire_old.Contains(x.RetirementPeriod))
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.Select(x => new
{
Order = x.Order,
Id = x.Id,
Reason = x.Reason,
Remove = x.Remove,
ProfileId = x.Profile.Id,
CitizenId = x.Profile.CitizenId,
Prefix = x.Profile.Prefix == null ? null : x.Profile.Prefix.Name,
FullName = $"{x.Profile.FirstName} {x.Profile.LastName}",
OrganizationOrganization = x.Profile.OrganizationOrganization,
Oc = x.Profile.Oc,
Position = x.Profile.Position == null ? null : x.Profile.Position.Name,
PositionType = x.Profile.PositionType == null ? null : x.Profile.PositionType.Name,
PositionExecutive = x.Profile.PositionExecutive,
PosNo = x.Profile.PosNo == null ? null : x.Profile.PosNo.Name,
PositionEmployeePosition = x.Profile.PositionEmployeePosition,
PositionEmployeeLevel = x.Profile.PositionEmployeeLevel,
PositionEmployeeGroup = x.Profile.PositionEmployeeGroup,
PosNoEmployee = x.Profile.PosNoEmployee,
})
.ToListAsync();
return Success(profile);
return Success(retire);
}
/// <summary>
@ -322,10 +343,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
profile.Remove = "delete";
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
_context.RetirementProfiles.Remove(profile);
_context.SaveChanges();
return Success();
@ -350,30 +368,26 @@ namespace BMA.EHR.Retirement.Service.Controllers
return Error(GlobalMessages.DataNotFound, 404);
var retire = await _context.RetirementPeriods
.Include(x => x.RetirementPeriodHistorys)
.Include(x => x.RetirementProfiles)
.ThenInclude(x => x.Profile)
.FirstOrDefaultAsync(x => x.Id == retireId);
if (retire == null)
return Error(GlobalMessages.InvalidRetirementRequest, 404);
var order = 1;
var retire_old = await _context.RetirementPeriods
.Include(x => x.RetirementProfiles)
.ThenInclude(x => x.Profile)
.Where(x => x.Year == retire.Year)
.Where(x => x.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.ToListAsync();
if (retire_old.Where(x => x.RetirementProfiles.Where(y => y.Profile == profile).Count() > 0).Count() > 0)
if (retire.RetirementProfiles.Where(x => x.Profile == profile).Count() > 0)
return Error("บุคคลนี้ได้ทำการเลือกไว้อยู่แล้ว");
foreach (var retire_profile in retire.RetirementProfiles)
{
retire_profile.Order++;
retire_profile.LastUpdateFullName = FullName ?? "System Administrator";
retire_profile.LastUpdateUserId = UserId ?? "";
retire_profile.LastUpdatedAt = DateTime.Now;
}
var last_order = await _context.RetirementProfiles
.Where(x => retire_old.Contains(x.RetirementPeriod))
.Where(x => x.RetirementPeriod.Type.Trim().ToUpper().Contains(retire.Type.Trim().ToUpper()))
.OrderByDescending(x => x.Order)
.FirstOrDefaultAsync();
if (last_order != null)
order = last_order.Order + 1;
var data = new RetirementProfile
{
Order = order,
Remove = "pending",
Order = 1,
Remove = "PENDING",
RetirementPeriod = retire,
Profile = profile,
CreatedUserId = FullName ?? "",
@ -383,15 +397,18 @@ namespace BMA.EHR.Retirement.Service.Controllers
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
};
_context.RetirementProfiles.Add(data);
_context.SaveChanges();
await _context.SaveChangesAsync();
// if (retire.RetirementPeriodHistorys.Count() <= 1)
// {
// await GenOrderByYear(retire.Id);
// }
return Success();
}
/// <summary>
/// ใส่เหตุผลไม่เกษียณ
/// แก้ไขข้อมูลบุคคล
/// </summary>
/// <param name="retireProfileId">Id ผู้ใช้งานในประกาศ</param>
/// <param name="reason">เหตุผล</param>
@ -408,7 +425,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
if (profile == null)
return Error(GlobalMessages.DataNotFound, 404);
profile.Remove = "change";
profile.Remove = "EDIT";
profile.Reason = req.Reason;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
@ -419,7 +436,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
}
/// <summary>
/// View เหตุผลไม่เกษียณ
/// View เหตุผลแก้ไขข้อมูลบุคคล
/// </summary>
/// <param name="retireProfileId">Id ผู้ใช้งานในประกาศ</param>
/// <returns></returns>
@ -444,7 +461,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
}
/// <summary>
/// View รายชื่อผู้เกษียณอายุราชการในประกาศ
/// จัดอันดับเกษียณ
/// </summary>
/// <param name="retireId">Id ประกาศ</param>
/// <returns></returns>
@ -460,7 +477,7 @@ namespace BMA.EHR.Retirement.Service.Controllers
if (retire == null)
return Error(GlobalMessages.InvalidRetirementRequest, 404);
await GenOrderByYear(retire.Type.Trim().ToUpper(), retire.Year);
await GenOrderByYear(retire.Id);
return Success();
}