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]
public async Task<ActionResult<ResponseObject>> PreviewReceiceProfile([FromForm] ImportFileRequest req, Guid insigniaNoteId)
{
var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_INSIGNIA_RECORD");
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)
try
{
if (item.CitizanId == null) continue;
var _profile = await _userProfileRepository.GetOfficerProfileByCitizenId(item.CitizanId, AccessToken);
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.ProfileId == _profile.Id);
if (profile == null)
var getPermission = await _permission.GetPermissionAPIAsync("CREATE", "SYS_INSIGNIA_RECORD");
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 (_profile == null)
continue;
var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
if (_insignia == null)
continue;
profile = new InsigniaNoteProfile
if (item.CitizanId == null) continue;
var _profile = await _userProfileRepository.GetOfficerProfileByCitizenId(item.CitizanId, AccessToken);
var profile = insigniaNote.InsigniaNoteProfiles.FirstOrDefault(x => x.ProfileId == _profile.Id);
if (profile == null)
{
RequestDate = DateTime.Now,
Salary = _profile.Amount,
IsApprove = true,
Status = "DONE",
Number = item.Number,
RequestInsignia = _insignia,
DateReceive = item.DateReceive,
Date = item.Date,
VolumeNo = item.VolumeNo,
Section = item.Section,
Page = item.Page,
No = item.No,
ProfileId = _profile.Id,
InsigniaNote = insigniaNote,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
Prefix = _profile.Prefix,
Position = _profile.Position,
CitizenId = _profile.CitizenId,
ProfileType = _profile.ProfileType,
FirstName = _profile.FirstName,
LastName = _profile.LastName,
BirthDate = _profile.BirthDate,
DateAppoint = _profile.DateAppoint,
Gender = _profile.Gender,
PosTypeName = _profile.PosType,
PosLevelName = _profile.PosLevel,
PosNo = _profile.PosNo,
Amount = _profile.Amount,
PositionSalaryAmount = _profile.PositionSalaryAmount,
Root = _profile.Root,
RootId = _profile.RootId,
RootDnaId = _profile.RootDnaId,
Child1 = _profile.Child1,
Child1Id = _profile.Child1Id,
Child1DnaId = _profile.Child1DnaId,
Child2 = _profile.Child2,
Child2Id = _profile.Child2Id,
Child2DnaId = _profile.Child2DnaId,
Child3 = _profile.Child3,
Child3Id = _profile.Child3Id,
Child3DnaId = _profile.Child3DnaId,
Child4 = _profile.Child4,
Child4Id = _profile.Child4Id,
Child4DnaId = _profile.Child4DnaId,
};
}
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,
if (_profile == null)
continue;
var _insignia = await _context.Insignias.FirstOrDefaultAsync(x => x.Name == item.RequestInsignia);
if (_insignia == null)
continue;
profile = new InsigniaNoteProfile
{
RequestDate = DateTime.Now,
Salary = _profile.Amount,
IsApprove = true,
Status = "DONE",
Number = item.Number,
RequestInsignia = _insignia,
DateReceive = item.DateReceive,
Date = item.Date,
VolumeNo = item.VolumeNo,
Section = item.Section,
Page = item.Page,
No = item.No,
ProfileId = _profile.Id,
InsigniaNote = insigniaNote,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
Prefix = _profile.Prefix,
Position = _profile.Position,
CitizenId = _profile.CitizenId,
ProfileType = _profile.ProfileType,
FirstName = _profile.FirstName,
LastName = _profile.LastName,
BirthDate = _profile.BirthDate,
DateAppoint = _profile.DateAppoint,
Gender = _profile.Gender,
PosTypeName = _profile.PosType,
PosLevelName = _profile.PosLevel,
PosNo = _profile.PosNo,
Amount = _profile.Amount,
PositionSalaryAmount = _profile.PositionSalaryAmount,
Root = _profile.Root,
RootId = _profile.RootId,
RootDnaId = _profile.RootDnaId,
Child1 = _profile.Child1,
Child1Id = _profile.Child1Id,
Child1DnaId = _profile.Child1DnaId,
Child2 = _profile.Child2,
Child2Id = _profile.Child2Id,
Child2DnaId = _profile.Child2DnaId,
Child3 = _profile.Child3,
Child3Id = _profile.Child3Id,
Child3DnaId = _profile.Child3DnaId,
Child4 = _profile.Child4,
Child4Id = _profile.Child4Id,
Child4DnaId = _profile.Child4DnaId,
};
}
);
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>