fix bug upload

api ออกคำสั่ง
ส่ง inbox and noti
This commit is contained in:
Suphonchai Phoonsawat 2023-08-08 15:05:53 +07:00
parent 9ff5725be4
commit c99cb5344e
11 changed files with 13843 additions and 4 deletions

View file

@ -1,14 +1,19 @@
using BMA.EHR.Application.Common.Interfaces;
using Amazon;
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Application.Requests.Commands;
using BMA.EHR.Application.Responses;
using BMA.EHR.Domain.Extensions;
using BMA.EHR.Domain.Models.Commands.Core;
using BMA.EHR.Domain.Models.HR;
using BMA.EHR.Domain.Models.MetaData;
using BMA.EHR.Domain.Models.Notifications;
using BMA.EHR.Domain.Models.Organizations;
using BMA.EHR.Domain.Models.Placement;
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Command = BMA.EHR.Domain.Models.Commands.Core.Command;
using Profile = BMA.EHR.Domain.Models.HR.Profile;
namespace BMA.EHR.Application.Repositories.Commands
{
@ -18,16 +23,19 @@ namespace BMA.EHR.Application.Repositories.Commands
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly OrganizationCommonRepository _organizationCommonRepository;
#endregion
#region " Constructor and Destuctor "
public CommandRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
IHttpContextAccessor httpContextAccessor,
OrganizationCommonRepository organizationCommonRepository) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_organizationCommonRepository = organizationCommonRepository;
}
#endregion
@ -137,7 +145,240 @@ namespace BMA.EHR.Application.Repositories.Commands
{
try
{
// create new profile
foreach (var recv in command.Receivers)
{
// query placement Profile
var placementProfile = await _dbContext.Set<PlacementProfile>()
.Include(x => x.Prefix)
.Include(x => x.FatherPrefix)
.Include(x => x.MotherPrefix)
.Include(x => x.MarryPrefix)
.Include(x => x.Gender)
.Include(x => x.Relationship)
.Include(x => x.BloodGroup)
.Include(x => x.Religion)
.Include(x => x.RegistSubDistrict)
.Include(x => x.RegistDistrict)
.Include(x => x.RegistProvince)
.Include(x => x.CurrentSubDistrict)
.Include(x => x.CurrentDistrict)
.Include(x => x.CurrentProvince)
.Include(x => x.PositionPath)
.Include(x => x.PositionPathSide)
.Include(x => x.PositionType)
.Include(x => x.PositionLine)
.Include(x => x.PositionLevel)
.Include(x => x.PositionNumber)
.Include(x => x.PlacementCertificates)
.Include(x => x.PlacementEducations)
.ThenInclude(x => x.EducationLevel)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.Organization)
.ThenInclude(x => x.OrganizationShortName)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.PositionMaster)
.ThenInclude(x => x.PositionExecutive)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.PositionMaster)
.ThenInclude(x => x.PositionExecutiveSide)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.PositionMaster)
.ThenInclude(x => x.PositionLine)
.FirstOrDefaultAsync(x => x.Id == recv.RefPlacementProfileId);
if (placementProfile == null)
throw new Exception("Invalid placement profile: " + recv.RefPlacementProfileId);
// ใส่ฟีลจากข้อมูล
var profile = new Profile
{
ProfileType = "officer",
CitizenId = placementProfile.CitizenId,
Prefix = placementProfile.Prefix,
FirstName = placementProfile.Firstname,
LastName = placementProfile.Lastname,
Gender = placementProfile.Gender,
Nationality = placementProfile.Nationality,
BirthDate = placementProfile.DateOfBirth == null ? DateTime.MinValue : placementProfile.DateOfBirth.Value,
RelationshipId = placementProfile.Relationship == null ? Guid.Empty : placementProfile.Relationship!.Id,
TelephoneNumber = placementProfile.Telephone,
Race = placementProfile.Race,
ReligionId = placementProfile.Religion == null ? Guid.Empty : placementProfile.Religion.Id,
BloodGroupId = placementProfile.BloodGroup == null ? Guid.Empty : placementProfile.BloodGroup.Id,
RegistrationAddress = placementProfile.RegistAddress,
RegistrationSubDistrictId = placementProfile.RegistSubDistrict == null ? Guid.Empty : placementProfile.RegistSubDistrict!.Id,
RegistrationDistrictId = placementProfile.RegistDistrict == null ? Guid.Empty : placementProfile.RegistDistrict!.Id,
RegistrationProvinceId = placementProfile.RegistProvince == null ? Guid.Empty : placementProfile.RegistProvince!.Id,
CurrentAddress = placementProfile.CurrentAddress,
CurrentSubDistrictId = placementProfile.CurrentSubDistrict == null ? Guid.Empty : placementProfile.CurrentSubDistrict!.Id,
CurrentDistrictId = placementProfile.CurrentDistrict == null ? Guid.Empty : placementProfile.CurrentDistrict!.Id,
CurrentProvinceId = placementProfile.CurrentProvince == null ? Guid.Empty : placementProfile.CurrentProvince!.Id,
FatherPrefixId = placementProfile.FatherPrefix == null ? Guid.Empty : placementProfile.FatherPrefix.Id,
FatherFirstName = placementProfile.FatherFirstName,
FatherLastName = placementProfile.FatherLastName,
FatherCareer = placementProfile.FatherOccupation,
MotherPrefixId = placementProfile.MotherPrefix == null ? Guid.Empty : placementProfile.MotherPrefix.Id,
MotherFirstName = placementProfile.MotherFirstName,
MotherLastName = placementProfile.MotherLastName,
MotherCareer = placementProfile.MotherOccupation,
CouplePrefixId = placementProfile.MarryPrefix == null ? Guid.Empty : placementProfile.MarryPrefix.Id,
CoupleFirstName = placementProfile.MarryFirstName,
CoupleLastName = placementProfile.MarryLastName,
CoupleCareer = placementProfile.MarryOccupation,
Position = placementProfile.PositionPath,
PositionPathSideId = placementProfile.PositionPathSide == null ? Guid.Empty : placementProfile.PositionPathSide.Id,
PositionType = placementProfile.PositionType,
PositionLevel = placementProfile.PositionLevel,
PositionLineId = placementProfile.PositionLine == null ? Guid.Empty : placementProfile.PositionLine.Id,
IsVerified = true,
IsProbation = true,
Educations = new List<ProfileEducation>(),
Certificates = new List<ProfileCertificate>(),
Salaries = new List<ProfileSalary>()
};
// add profile education
foreach (var edu in placementProfile.PlacementEducations)
{
profile.Educations.Add(new ProfileEducation
{
Country = edu.Country,
Degree = edu.Degree,
Duration = edu.Duration,
DurationYear = edu.DurationYear == null ? 0 : edu.DurationYear.Value,
EducationLevelId = edu.EducationLevel == null ? Guid.Empty : edu.EducationLevel.Id,
EducationLevel = edu.EducationLevel == null ? "" : edu.EducationLevel.Name,
StartDate = edu.StartDate,
EndDate = edu.EndDate,
Field = edu.Field,
FinishDate = edu.FinishDate,
FundName = edu.FundName,
Gpa = edu.Gpa,
Institute = edu.Institute,
});
}
// add profile certificate
foreach (var cert in placementProfile.PlacementCertificates)
{
profile.Certificates.Add(new ProfileCertificate
{
CertificateNo = cert.CertificateNo,
IssueDate = cert.IssueDate,
Issuer = cert.Issuer,
CertificateType = cert.CertificateType,
ExpireDate = cert.ExpireDate,
});
}
// add profile salary
var oc = _dbContext.Set<OrganizationEntity>()
.FirstOrDefault(x => x.Id == placementProfile.OrganizationPosition!.Organization!.Id);
var position = placementProfile.PositionPath;
var positionNumber = placementProfile.PositionNumber;
var positionType = placementProfile.PositionType;
var positionLevel = placementProfile.PositionLevel;
var salary = new ProfileSalary
{
Order = 1,
Date = command.CommandAffectDate,
Amount = placementProfile.Amount,
PositionSalaryAmount = placementProfile.PositionSalaryAmount,
MouthSalaryAmount = placementProfile.MouthSalaryAmount,
SalaryRef = "บรรจุและแต่งตั้งผู้สอบแข่งขันได้วุฒิ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}",
OcId = oc == null ? Guid.Empty : oc.Id,
PositionExecutiveId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutive!.Id,
PositionExecutiveSideId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutiveSide!.Id,
PositionLevel = placementProfile.PositionLevel,
PositionLineId = placementProfile.PositionLine!.Id,
PositionTypeId = placementProfile.PositionType!.Id,
OrganizationShortNameId = placementProfile.OrganizationPosition!.Organization!.OrganizationShortName!.Id,
CommandNo = $"{command.CommandNo}/{command.CommandYear}",
CommandTypeName = command.CommandType.Name
};
profile.Salaries.Add(salary);
_dbContext.Set<Profile>().Add(profile);
// update placementstatus
placementProfile.PlacementStatus = "CONTAIN";
await _dbContext.SaveChangesAsync();
// Send noti inbox and email
var inbox = new Inbox
{
Subject = $"คุณได้รับคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
Body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = profile.Id,
};
_dbContext.Set<Inbox>().Add(inbox);
var noti = new Notification
{
Body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = profile.Id,
};
_dbContext.Set<Notification>().Add(noti);
await _dbContext.SaveChangesAsync();
}
// send cc noti inbox
foreach (var cc in command.Deployments)
{
var pf = await _dbContext.Set<Profile>().FirstOrDefaultAsync(x => x.CitizenId == cc.CitizenId);
if (pf != null)
{
var inbox = new Inbox
{
Subject = $"คุณได้รับสำเนาคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = pf.Id,
};
_dbContext.Set<Inbox>().Add(inbox);
var noti = new Notification
{
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = pf.Id,
};
_dbContext.Set<Notification>().Add(noti);
}
}
// change command status
var cmdStatus = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(x => x.Sequence == 5);
command.CommandStatusId = cmdStatus!.Id;
await _dbContext.SaveChangesAsync();
}
catch
{
@ -193,6 +434,31 @@ namespace BMA.EHR.Application.Repositories.Commands
#endregion
#region " Execute Command "
public async Task ExecuteCommandAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Receivers)
.Include(x => x.Deployments)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
await ExecuteCommandByTypeAsync(command);
}
catch
{
throw;
}
}
#endregion
#region " Command Receiver "
public async Task SaveSelectedReceiverAsync(Guid id, List<Guid> selected)