add try catch

This commit is contained in:
Suphonchai Phoonsawat 2025-05-07 16:42:05 +07:00
parent eeb82dcbe1
commit e351adb5fa

View file

@ -2097,136 +2097,143 @@ namespace BMA.EHR.Insignia.Service.Controllers
[HttpPut("preview/receice/{insigniaNoteId:length(36)}"), DisableRequestSizeLimit] [HttpPut("preview/receice/{insigniaNoteId:length(36)}"), DisableRequestSizeLimit]
public async Task<ActionResult<ResponseObject>> PreviewReceiceProfile([FromForm] ImportFileRequest req, Guid insigniaNoteId) public async Task<ActionResult<ResponseObject>> PreviewReceiceProfile([FromForm] ImportFileRequest req, Guid insigniaNoteId)
{ {
var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_INSIGNIA_RECORD"); try
var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
if (jsonData["status"]?.ToString() != "200")
return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
var insigniaNote = await _context.InsigniaNotes
.Include(x => x.InsigniaNoteProfiles)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.InsigniaNoteProfiles)
.Include(x => x.InsigniaNoteProfiles)
.FirstOrDefaultAsync(x => x.Id == insigniaNoteId);
if (insigniaNote == null)
return Error(GlobalMessages.InsigniaRequestNotFound);
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
return Error(GlobalMessages.NoFileToUpload);
var file = Request.Form.Files[0];
if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
return Error("นามสกุลไฟล์ต้องเป็น .xlsx!");
var items = await ReadExcelImportReceive(file);
var _insigniaNoteProfiles = new List<dynamic>();
foreach (var item in items)
{ {
if (item.CitizanId == null) continue; var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_INSIGNIA_RECORD");
var _profile = await _userProfileRepository.GetOfficerProfileByCitizenId(item.CitizanId, AccessToken); var jsonData = JsonConvert.DeserializeObject<JObject>(getPermission);
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.ProfileId == _profile.Id); if (jsonData["status"]?.ToString() != "200")
if (profile == null) return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden);
var insigniaNote = await _context.InsigniaNotes
.Include(x => x.InsigniaNoteProfiles)
.Include(x => x.InsigniaNoteProfiles)
.ThenInclude(x => x.RequestInsignia)
.ThenInclude(x => x.InsigniaType)
.Include(x => x.InsigniaNoteProfiles)
.Include(x => x.InsigniaNoteProfiles)
.FirstOrDefaultAsync(x => x.Id == insigniaNoteId);
if (insigniaNote == null)
return Error(GlobalMessages.InsigniaRequestNotFound);
if (Request.Form.Files == null || Request.Form.Files.Count == 0)
return Error(GlobalMessages.NoFileToUpload);
var file = Request.Form.Files[0];
if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
return Error("นามสกุลไฟล์ต้องเป็น .xlsx!");
var items = await ReadExcelImportReceive(file);
var _insigniaNoteProfiles = new List<dynamic>();
foreach (var item in items)
{ {
if (_profile == null) if (item.CitizanId == null) continue;
continue; var _profile = await _userProfileRepository.GetOfficerProfileByCitizenId(item.CitizanId, AccessToken);
var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia); var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.ProfileId == _profile.Id);
if (_insignia == null) if (profile == null)
continue;
profile = new InsigniaNoteProfile
{ {
RequestDate = DateTime.Now, if (_profile == null)
Salary = _profile.Amount, continue;
IsApprove = true, var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
Status = "DONE", if (_insignia == null)
Number = item.Number, continue;
RequestInsignia = _insignia, profile = new InsigniaNoteProfile
DateReceive = item.DateReceive, {
Date = item.Date, RequestDate = DateTime.Now,
VolumeNo = item.VolumeNo, Salary = _profile.Amount,
Section = item.Section, IsApprove = true,
Page = item.Page, Status = "DONE",
No = item.No, Number = item.Number,
ProfileId = _profile.Id, RequestInsignia = _insignia,
InsigniaNote = insigniaNote, DateReceive = item.DateReceive,
LastUpdateFullName = FullName ?? "System Administrator", Date = item.Date,
LastUpdateUserId = UserId ?? "", VolumeNo = item.VolumeNo,
LastUpdatedAt = DateTime.Now, Section = item.Section,
Prefix = _profile.Prefix, Page = item.Page,
Position = _profile.Position, No = item.No,
CitizenId = _profile.CitizenId, ProfileId = _profile.Id,
ProfileType = _profile.ProfileType, InsigniaNote = insigniaNote,
FirstName = _profile.FirstName, LastUpdateFullName = FullName ?? "System Administrator",
LastName = _profile.LastName, LastUpdateUserId = UserId ?? "",
BirthDate = _profile.BirthDate, LastUpdatedAt = DateTime.Now,
DateAppoint = _profile.DateAppoint, Prefix = _profile.Prefix,
Gender = _profile.Gender, Position = _profile.Position,
PosTypeName = _profile.PosType, CitizenId = _profile.CitizenId,
PosLevelName = _profile.PosLevel, ProfileType = _profile.ProfileType,
PosNo = _profile.PosNo, FirstName = _profile.FirstName,
Amount = _profile.Amount, LastName = _profile.LastName,
PositionSalaryAmount = _profile.PositionSalaryAmount, BirthDate = _profile.BirthDate,
Root = _profile.Root, DateAppoint = _profile.DateAppoint,
RootId = _profile.RootId, Gender = _profile.Gender,
RootDnaId = _profile.RootDnaId, PosTypeName = _profile.PosType,
Child1 = _profile.Child1, PosLevelName = _profile.PosLevel,
Child1Id = _profile.Child1Id, PosNo = _profile.PosNo,
Child1DnaId = _profile.Child1DnaId, Amount = _profile.Amount,
Child2 = _profile.Child2, PositionSalaryAmount = _profile.PositionSalaryAmount,
Child2Id = _profile.Child2Id, Root = _profile.Root,
Child2DnaId = _profile.Child2DnaId, RootId = _profile.RootId,
Child3 = _profile.Child3, RootDnaId = _profile.RootDnaId,
Child3Id = _profile.Child3Id, Child1 = _profile.Child1,
Child3DnaId = _profile.Child3DnaId, Child1Id = _profile.Child1Id,
Child4 = _profile.Child4, Child1DnaId = _profile.Child1DnaId,
Child4Id = _profile.Child4Id, Child2 = _profile.Child2,
Child4DnaId = _profile.Child4DnaId, Child2Id = _profile.Child2Id,
}; Child2DnaId = _profile.Child2DnaId,
} Child3 = _profile.Child3,
else Child3Id = _profile.Child3Id,
{ Child3DnaId = _profile.Child3DnaId,
profile.Status = "DONE"; Child4 = _profile.Child4,
profile.Number = item.Number; Child4Id = _profile.Child4Id,
profile.RequestInsignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia) == null ? profile.RequestInsignia : await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia); Child4DnaId = _profile.Child4DnaId,
profile.DateReceive = item.DateReceive; };
profile.Date = item.Date;
profile.VolumeNo = item.VolumeNo;
profile.Section = item.Section;
profile.Page = item.Page;
profile.No = item.No;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
_insigniaNoteProfiles.Add(
new
{
profile.Id,
Prefix = _profile.Prefix,
Position = _profile.Position,
CitizenId = _profile.CitizenId,
ProfileType = _profile.ProfileType,
FullName = $"{_profile.Prefix}{_profile.FirstName} {_profile.LastName}",
RequestInsignia = profile.RequestInsignia == null ? null : profile.RequestInsignia.Name,
RequestInsigniaId = profile.RequestInsignia == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : profile.RequestInsignia.Id,
RequestInsigniaShortName = profile.RequestInsignia == null ? null : profile.RequestInsignia.ShortName,
profile.DateReceive,
profile.OrganizationOrganizationSend,
profile.OrganizationOrganizationReceive,
profile.Status,
profile.Issue,
profile.Date,
profile.VolumeNo,
profile.Section,
profile.Page,
profile.No,
profile.DatePayment,
profile.TypePayment,
profile.Address,
profile.Number,
profile.Salary,
} }
); else
{
profile.Status = "DONE";
profile.Number = item.Number;
profile.RequestInsignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia) == null ? profile.RequestInsignia : await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
profile.DateReceive = item.DateReceive;
profile.Date = item.Date;
profile.VolumeNo = item.VolumeNo;
profile.Section = item.Section;
profile.Page = item.Page;
profile.No = item.No;
profile.LastUpdateFullName = FullName ?? "System Administrator";
profile.LastUpdateUserId = UserId ?? "";
profile.LastUpdatedAt = DateTime.Now;
}
_insigniaNoteProfiles.Add(
new
{
profile.Id,
Prefix = _profile.Prefix,
Position = _profile.Position,
CitizenId = _profile.CitizenId,
ProfileType = _profile.ProfileType,
FullName = $"{_profile.Prefix}{_profile.FirstName} {_profile.LastName}",
RequestInsignia = profile.RequestInsignia == null ? null : profile.RequestInsignia.Name,
RequestInsigniaId = profile.RequestInsignia == null ? Guid.Parse("00000000-0000-0000-0000-000000000000") : profile.RequestInsignia.Id,
RequestInsigniaShortName = profile.RequestInsignia == null ? null : profile.RequestInsignia.ShortName,
profile.DateReceive,
profile.OrganizationOrganizationSend,
profile.OrganizationOrganizationReceive,
profile.Status,
profile.Issue,
profile.Date,
profile.VolumeNo,
profile.Section,
profile.Page,
profile.No,
profile.DatePayment,
profile.TypePayment,
profile.Address,
profile.Number,
profile.Salary,
}
);
}
return Success(_insigniaNoteProfiles);
}
catch(Exception ex)
{
return Error(ex);
} }
return Success(_insigniaNoteProfiles);
} }
/// <summary> /// <summary>