hrms-api-backend/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs
2023-08-31 12:34:26 +07:00

2375 lines
110 KiB
C#

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.Models.Retirement;
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
{
public class CommandRepository : GenericRepository<Guid, Command>
{
#region " Fields "
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly OrganizationCommonRepository _organizationCommonRepository;
private readonly UserProfileRepository _userProfileRepository;
#endregion
#region " Constructor and Destuctor "
public CommandRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor,
OrganizationCommonRepository organizationCommonRepository,
UserProfileRepository userProfileRepository) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
_organizationCommonRepository = organizationCommonRepository;
_userProfileRepository = userProfileRepository;
}
#endregion
#region " Properties "
protected Guid UserOrganizationId
{
get
{
if (UserId != null || UserId != "")
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!));
else
return Guid.Empty;
}
}
#endregion
#region " Methods "
#region " Private "
#region " List Receiver "
public async Task<List<CommandReceiver>> GetReceiverForByCommndTypeAsync(Command command)
{
var result = new List<CommandReceiver>();
switch (command.CommandType.CommandCode.Trim().ToUpper())
{
case "C-PM-01":
result = await GetReceiver01Async(command);
break;
case "C-PM-02":
result = await GetReceiver02Async(command);
break;
case "C-PM-03":
result = await GetReceiver03Async(command);
break;
case "C-PM-04":
result = await GetReceiver04Async(command);
break;
case "C-PM-05":
result = await GetReceiver05Async(command);
break;
case "C-PM-06":
result = await GetReceiver06Async(command);
break;
case "C-PM-07":
result = await GetReceiver07Async(command);
break;
case "C-PM-08":
result = await GetReceiver08Async(command);
break;
case "C-PM-09":
result = await GetReceiver09Async(command);
break;
case "C-PM-13":
result = await GetReceiver13Async(command);
break;
case "C-PM-14":
result = await GetReceiver14Async(command);
break;
case "C-PM-15":
result = await GetReceiver15Async(command);
break;
case "C-PM-16":
result = await GetReceiver16Async(command);
break;
case "C-PM-17":
result = await GetReceiver17Async(command);
break;
case "C-PM-18":
result = await GetReceiver18Async(command);
break;
case "C-PM-19":
result = await GetReceiver19Async(command);
break;
case "C-PM-20":
result = await GetReceiver20Async(command);
break;
default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement);
}
return result;
}
/// <summary>
/// C-PM-01 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้สอบแข่งขันได้
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver01Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper().StartsWith("C-PM-01"))
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementProfile>()
.Include(x => x.Placement)
.ThenInclude(x => x.PlacementType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN")
.Where(x => x.Draft! == true)
.Where(x => x.Placement!.PlacementType!.Name == "สอบแข่งขัน")
.OrderBy(x => x.ExamNumber)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
if (!(await CheckIsActiveOfficerAsync(item.CitizenId!)))
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-02 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้ได้รับคัดเลือก
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver02Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-02")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementProfile>()
.Include(x => x.Placement)
.ThenInclude(x => x.PlacementType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN")
.Where(x => x.Draft! == true)
.Where(x => x.Placement!.PlacementType!.Name != "สอบแข่งขัน")
.OrderBy(x => x.ExamNumber)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
if (!(await CheckIsActiveOfficerAsync(item.CitizenId!)))
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-03 - คำสั่งแต่งตั้ง : สำหรับข้าราชการ กทม. เดิม
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver03Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-03")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementProfile>()
.Include(x => x.Placement)
.ThenInclude(x => x.PlacementType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN")
.Where(x => x.Draft! == true)
.OrderBy(x => x.ExamNumber)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var profile_oc = await GetProfileOrganizationAsync(item.CitizenId!);
if (await CheckIsActiveOfficerAsync(item.CitizenId!))
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-04 - คำสั่งย้าย : สำหรับข้าราชการ กทม. เดิม
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver04Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-04")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementProfile>()
.Include(x => x.Placement)
.ThenInclude(x => x.PlacementType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.PlacementStatus.Trim().ToUpper() == "PREPARE-CONTAIN")
.Where(x => x.Draft! == true)
.OrderBy(x => x.ExamNumber)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var profile_oc = await GetProfileOrganizationAsync(item.CitizenId!);
if (await CheckIsActiveOfficerAsync(item.CitizenId!))
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-05 - คำสั่งแต่งตั้ง
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver05Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-05")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementAppointment>()
.Include(x => x.CommandType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.Where(x => x.CommandType!.Id == command.CommandType!.Id)
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-06 - คำสั่งเลื่อน
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver06Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-06")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementAppointment>()
.Include(x => x.CommandType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.Where(x => x.CommandType!.Id == command.CommandType!.Id)
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-07 - คำสั่งย้าย
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver07Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-07")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementAppointment>()
.Include(x => x.CommandType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.Where(x => x.CommandType!.Id == command.CommandType!.Id)
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-08 - คำสั่งบรรจุและแต่งตั้งข้าราชการฯ กลับเข้ารับราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver08Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-08")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementAppointment>()
.Include(x => x.CommandType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.Where(x => x.CommandType!.Id == command.CommandType!.Id)
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-09 - คำสั่งบรรจุและแต่งตั้งผู้ออกไปรับราชการทหารกลับเข้ารับราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver09Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-09")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementAppointment>()
.Include(x => x.CommandType)
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.Where(x => x.CommandType!.Id == command.CommandType!.Id)
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-13 - คำสั่งให้โอนข้าราชการกรุงเทพมหานครสามัญ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver13Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-13")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementTransfer>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-14 - คำสั่งรับโอนข้าราชการกรุงเทพมหานครสามัญ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver14Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-14")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementReceive>()
.Include(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix!.Name,
FirstName = item.Firstname!,
LastName = item.Lastname!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-15 - คำสั่งให้ช่วยราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver15Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-15")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementOfficer>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-16 - คำสั่งส่งตัวกลับ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver16Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-16")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<PlacementRepatriation>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-17 - คำสั่งอนุญาตให้ข้าราชการลาออกจากราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver17Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-17")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<RetirementResign>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-18 - คำสั่งให้ออกจากราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver18Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-18")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<RetirementOut>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-19 - คำสั่งปลดออกจากราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver19Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-19")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<RetirementDischarge>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
/// <summary>
/// C-PM-20 - คำสั่งไล่ออกจากราชการ
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task<List<CommandReceiver>> GetReceiver20Async(Command command)
{
try
{
var result = new List<CommandReceiver>();
// TODO : ต้องมา list คนตามประเภทอีกครั้งนึง
// 1. หารายชื่อที่ถูกเลือกไปแล้ว ในประเภทเดียวกัน
var otherCommandReceivers = await _dbContext.Set<CommandReceiver>()
.Include(x => x.Command)
.ThenInclude(x => x.CommandType)
.Where(x => x.Command.CommandType.CommandCode.Trim().ToUpper() == "C-PM-20")
.Where(x => x.Command.Id != command.Id)
.Select(x => x.CitizenId)
.ToListAsync();
// 2. Query
var appointPeople = await _dbContext.Set<RetirementExpulsion>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
//.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.Where(x => !otherCommandReceivers.Contains(x.Profile!.CitizenId!))
.Where(x => x.Status.Trim().ToUpper() == "REPORT")
.OrderBy(x => x.Profile!.CitizenId)
.ToListAsync();
// 3. Create new Record
var seq = 1;
foreach (var item in appointPeople)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.Profile!.CitizenId!,
Prefix = item.Profile!.Prefix!.Name,
FirstName = item.Profile!.FirstName!,
LastName = item.Profile!.LastName!,
RefPlacementProfileId = item.Id
};
seq++;
result.Add(receiver);
}
return result;
}
catch
{
throw;
}
}
#endregion
#region " Execute and Deploy "
private async Task ExecuteCommandByTypeAsync(Command command)
{
switch (command.CommandType.CommandCode.Trim().ToUpper())
{
case "C-PM-01":
case "C-PM-02":
await ExecuteCommand01_02Async(command);
break;
default: throw new Exception(GlobalMessages.MethodForCommandTypeNotImplement);
}
}
/// <summary>
/// C-PM-01 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้สอบแข่งขันได้,
/// C-PM-02 - คำสั่งบรรจุและแต่งตั้ง: สำหรับผู้ได้รับคัดเลือก
/// </summary>
/// <param name="command">object ของรายการคำสั่ง</param>
/// <returns></returns>
private async Task ExecuteCommand01_02Async(Command command)
{
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)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x.Organization)
.ThenInclude(x => x.OrganizationOrganization)
.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,
DateAppoint = placementProfile.RecruitDate == null ? null : placementProfile.RecruitDate.Value,
DateStart = placementProfile.RecruitDate == null ? null : placementProfile.RecruitDate.Value,
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,
//PositionEmployeeLevelId = placementProfile.PositionLevel!.Id,
PositionLineId = placementProfile.PositionLine == null ? Guid.Empty : placementProfile.PositionLine.Id,
PositionLine = placementProfile.PositionLine == null ? "" : placementProfile.PositionLine.Name,
PosNo = placementProfile.OrganizationPosition!.PositionNumber,
IsVerified = true,
IsProbation = true,
Physical = "",
Ability = "",
AvatarRef = "",
GovAgeAbsent = 0,
GovAgePlus = 0,
EntryStatus = "",
IsTransfer = false,
IsActive = true,
IsLeave = false,
VerifiedUser = "",
FirstNameOld = "",
LastNameOld = "",
CoupleLive = false,
FatherLive = false,
MotherLive = false,
CreatedUser = "",
Educations = new List<ProfileEducation>(),
Certificates = new List<ProfileCertificate>(),
Salaries = new List<ProfileSalary>(),
CreatedAt = DateTime.Now,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
// organization
OrganizationShortNameId = placementProfile.OrganizationPosition!.Organization!.OrganizationShortName!.Id,
OrganizationShortName = placementProfile.OrganizationPosition!.Organization!.OrganizationShortName!.Name,
OrganizationOrganizationId = placementProfile.OrganizationPosition!.Organization!.OrganizationOrganization!.Id,
OcId = placementProfile.OrganizationPosition!.Organization!.Id,
Oc = _organizationCommonRepository.GetOrganizationNameFullPath(placementProfile.OrganizationPosition!.Organization!.Id, false, false, "/"),
};
// 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,
IsActive = true,
CreatedAt = DateTime.Now,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
});
}
// 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,
IsActive = true,
CreatedAt = DateTime.Now,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
});
}
// 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 == null ? 0 : placementProfile.Amount,
PositionSalaryAmount = placementProfile.PositionSalaryAmount == null ? 0 : placementProfile.PositionSalaryAmount,
MouthSalaryAmount = placementProfile.MouthSalaryAmount == null ? 0 : placementProfile.MouthSalaryAmount,
SalaryClass = "",
SalaryRef = "บรรจุและแต่งตั้งผู้สอบแข่งขันได้วุฒิ คำสั่ง" + command.IssuerOrganizationName + "ที่ " + $"{command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate!.Value.ToThaiFullDate3()}",
OcId = oc == null ? Guid.Empty : oc.Id,
PositionLevel = placementProfile.PositionLevel,
PositionLineId = placementProfile.PositionLine!.Id,
PositionTypeId = placementProfile.PositionType!.Id,
OrganizationShortNameId = placementProfile.OrganizationPosition!.Organization!.OrganizationShortName!.Id,
PosNoId = positionNumber!.Id,
CommandNo = $"{command.CommandNo}/{command.CommandYear}",
CommandTypeName = command.CommandType.Name,
PositionEmployeeGroupId = null,
PositionEmployeeLevelId = null,
PositionEmployeePositionId = null,
PositionEmployeePositionSideId = null,
PosNoEmployee = "",
PositionPathSideId = placementProfile.PositionPathSide! == null ? null : placementProfile.PositionPathSide!.Id,
PositionExecutiveId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutive! == null ? null : placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutive!.Id,
PositionExecutiveSideId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutiveSide! == null ? null : placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutiveSide!.Id,
IsActive = true,
CreatedAt = DateTime.Now,
CreatedFullName = FullName ?? "System Administrator",
CreatedUserId = UserId ?? "",
LastUpdatedAt = DateTime.Now,
LastUpdateFullName = FullName ?? "System Administrator",
LastUpdateUserId = UserId ?? "",
};
//if (placementProfile.PositionPathSide! != null)
//{
// salary.PositionPathSideId = placementProfile.PositionPathSide!.Id;
//}
//else
//if (placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutive! != null)
//{
// salary.PositionExecutiveId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutive!.Id;
//}
//if (placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutiveSide! != null)
//{
// salary.PositionExecutiveSideId = placementProfile.OrganizationPosition!.PositionMaster!.PositionExecutiveSide!.Id;
//}
// add profile position
var profilePosition = new ProfilePosition
{
Profile = profile,
OrganizationPosition = placementProfile.OrganizationPosition,
IsActive = true,
IsPublished = true,
};
_dbContext.Set<ProfilePosition>().Add(profilePosition);
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,
Payload = "",
};
_dbContext.Set<Inbox>().Add(inbox);
var noti = new Notification
{
Body = $"คุณได้รับบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ ตามคำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = profile.Id,
Type = "",
Payload = "",
};
_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,
Payload = "",
};
_dbContext.Set<Inbox>().Add(inbox);
var noti = new Notification
{
Body = $"คำสั่งบบรรจุเป็นข้าราชการกรุงเทพมหานครสามัญ คำสั่งเลขที่ {command.CommandNo}/{command.CommandYear} ลงวันที่ {command.CommandExcecuteDate.Value.ToThaiFullDate3()}",
ReceiverUserId = pf.Id,
Type = "",
Payload = "",
};
_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
{
throw;
}
}
#endregion
#endregion
#region " Override "
public override async Task<Command?> GetByIdAsync(Guid id)
{
return await _dbContext.Set<Command>()
.Include(x => x.Placement)
.Include(x => x.CommandType)
.Include(x => x.Documents)
.Include(x => x.Receivers)
.Include(x => x.CommandStatus)
.FirstOrDefaultAsync(x => x.Id == id);
}
public override async Task<IReadOnlyList<Command>> GetAllAsync()
{
return await _dbContext.Set<Command>()
.Include(x => x.Placement)
.Include(x => x.CommandType)
.Include(x => x.CommandStatus)
.ToListAsync();
}
public override async Task<Command> AddAsync(Command command)
{
var status = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == 1);
command.CommandStatus = status!;
_dbContext.Attatch(status!);
return await base.AddAsync(command);
}
public override async Task<Command> UpdateAsync(Command entity)
{
// attatch
_dbContext.Attatch(entity.CommandStatus);
_dbContext.Attatch(entity.CommandType);
if (entity.Placement != null)
_dbContext.Attatch(entity.Placement);
return await base.UpdateAsync(entity);
}
#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)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Receivers)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
var ap = (await GetReceiverForByCommndTypeAsync(command)).Where(x => selected.Contains(x.RefPlacementProfileId!.Value));
//var appointPeople = await _dbContext.Set<PlacementProfile>()
// .Include(x => x.Prefix)
// .Include(x => x.OrganizationPosition)
// .ThenInclude(x => x!.Organization)
// //.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
// .Where(x => selected.Contains(x.Id))
// .OrderBy(x => x.ExamNumber)
// .ToListAsync();
_dbContext.Set<CommandReceiver>().RemoveRange(command.Receivers);
await _dbContext.SaveChangesAsync();
var seq = 1;
foreach (var item in ap)
{
var receiver = new CommandReceiver
{
Sequence = seq,
CitizenId = item.CitizenId!,
Prefix = item.Prefix,
FirstName = item.FirstName!,
LastName = item.LastName!,
RefPlacementProfileId = item.RefPlacementProfileId
};
seq++;
command.Receivers.Add(receiver);
}
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<List<CommandReceiver>> GetReceiverForCommandAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Receivers)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
return await GetReceiverForByCommndTypeAsync(command);
}
catch
{
throw;
}
}
public async Task<List<CommandReceiver>> GetReceiverByCommmandIdAsync(Guid Id)
{
try
{
// ปรับใหม่ให้อ่านจาก database ล้วนๆ
var command = await _dbContext.Set<Command>()
.Include(x => x.Receivers)
.Include(x => x.CommandType)
.FirstOrDefaultAsync(x => x.Id == Id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
if (command.Receivers != null && command!.Receivers!.Count > 0)
{
return command.Receivers;
}
else
{
// returrn empty list
return new List<CommandReceiver>();
}
}
}
catch
{
throw;
}
}
public async Task<CommandReceiver> DeleteCommandReceiverAsync(Guid personalId)
{
try
{
var deleted = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (deleted == null)
throw new Exception(GlobalMessages.DataNotFound);
_dbContext.Set<CommandReceiver>().Remove(deleted);
await _dbContext.SaveChangesAsync();
return deleted;
}
catch
{
throw;
}
}
public async Task<CommandReceiver?> GetCommandReceiverAsync(Guid personalId)
{
try
{
var receiver = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (receiver == null)
throw new Exception(GlobalMessages.DataNotFound);
return receiver;
}
catch
{
throw;
}
}
public async Task SwapReceiverOrderAsync(Guid personalId, string direction)
{
try
{
var current = await _dbContext.Set<CommandReceiver>()
.Include(c => c.Command)
.FirstOrDefaultAsync(x => x.Id == personalId);
if (current == null)
throw new Exception(GlobalMessages.DataNotFound);
var currentSeq = current.Sequence;
var commandID = current.Command!.Id;
switch (direction.Trim().ToLower())
{
case "up":
{
// get prev record
var prev = await _dbContext.Set<CommandReceiver>()
.Include(c => c.Command)
.Where(x => x.Command.Id == commandID)
.Where(x => x.Sequence < currentSeq)
.OrderByDescending(x => x.Sequence)
.Take(1)
.FirstOrDefaultAsync();
if (prev != null)
{
var prevSeq = prev.Sequence;
current.Sequence = prevSeq;
prev.Sequence = currentSeq;
await _dbContext.SaveChangesAsync();
}
break;
}
case "down":
{
// get next record
var next = await _dbContext.Set<CommandReceiver>()
.Include(c => c.Command)
.Where(x => x.Command.Id == commandID)
.Where(x => x.Sequence > currentSeq)
.OrderBy(x => x.Sequence)
.Take(1)
.FirstOrDefaultAsync();
if (next != null)
{
var nextSeq = next.Sequence;
current.Sequence = nextSeq;
next.Sequence = currentSeq;
await _dbContext.SaveChangesAsync();
}
break;
}
default: throw new Exception("Invalid swap direction!");
}
}
catch
{
throw;
}
}
#endregion
public async Task<List<CommandDeployment>> GetDeploymentByCommandIdAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Deployments)
.FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
if (command.Deployments != null || command!.Deployments!.Count > 0)
{
return command.Deployments;
}
else
{
var orgPos = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x!.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.ThenInclude(x => x!.OrganizationOrganization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Where(x => x.OrganizationPosition!.IsDirector! == true)
.Where(x => x.OrganizationPosition!.Organization!.Id == command.OwnerGovId)
.FirstOrDefaultAsync();
if (orgPos != null)
{
if (orgPos.Profile != null)
{
var dp = new CommandDeployment
{
Sequence = 1,
ReceiveUserId = orgPos!.Profile!.Id!.ToString("D"),
CitizenId = orgPos!.Profile!.CitizenId!,
Prefix = orgPos!.Profile!.Prefix!.Name,
FirstName = orgPos!.Profile!.FirstName!,
LastName = orgPos!.Profile!.LastName!,
IsSendInbox = true,
IsSendMail = true,
IsSendNotification = true,
OrganizationName = orgPos!.OrganizationPosition!.Organization!.OrganizationOrganization!.Name,
PositionName = orgPos!.OrganizationPosition!.PositionMaster!.PositionPath!.Name
};
command.Deployments.Add(dp);
}
await _dbContext.SaveChangesAsync();
}
// query for new list
return command.Deployments;
}
}
}
catch
{
throw;
}
}
public async Task<List<ProfilePosition>> GetProfileByOrganizationIdAsync(Guid orgId)
{
try
{
var orgProfiles = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x!.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.ThenInclude(x => x!.OrganizationOrganization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Where(x => x.OrganizationPosition!.Organization!.Id == orgId)
.ToListAsync();
return orgProfiles;
}
catch
{
throw;
}
}
public async Task CreateCommandDeploymentAsync(Guid commandId, List<CommandDeployment> deploy)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Deployments)
.FirstOrDefaultAsync(x => x.Id == commandId);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
else
{
var lastSeq = 0;
var dep = command.Deployments.OrderByDescending(x => x.Sequence).FirstOrDefault();
if (dep == null) lastSeq = 1;
else lastSeq = dep.Sequence;
foreach (var dep2 in deploy)
{
dep2.Sequence = lastSeq;
lastSeq++;
}
command.Deployments.AddRange(deploy);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task UpdatCommandDeploymentAsync(List<CommandDeployment> deploys)
{
try
{
foreach (var dp in deploys)
{
var updated = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == dp.Id);
if (updated != null)
{
updated.IsSendMail = dp.IsSendMail;
updated.IsSendInbox = dp.IsSendInbox;
}
}
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task DeleteCommandDeploymentAsync(Guid id)
{
try
{
var deleted = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == id);
if (deleted == null)
throw new Exception(GlobalMessages.DataNotFound);
_dbContext.Set<CommandDeployment>().Remove(deleted);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<CommandDeployment?> GetCommandDeploymentById(Guid id)
{
try
{
var data = await _dbContext.Set<CommandDeployment>().FirstOrDefaultAsync(x => x.Id == id);
return data;
}
catch
{
throw;
}
}
#region " Documents "
public async Task<List<CommandDocument>> GetExistDocument(Guid id, string category)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Documents)
.ThenInclude(x => x.Document)
.FirstOrDefaultAsync(c => c.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
// insert new record to comand Document
var exist = command.Documents.Where(x => x.Category == category).ToList();
return exist;
}
catch
{
throw;
}
}
public async Task UploadDocument(Guid id, string category, CommandDocument document)
{
try
{
var command = await _dbContext.Set<Command>()
.Include(x => x.Documents)
//.ThenInclude(x =>x.Document)
.FirstOrDefaultAsync(c => c.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
// insert new record to comand Document
//var exist = command.Documents.Where(x => x.Category == category).ToList();
//if (exist.Any())
//{
// _dbContext.Set<CommandDocument>().RemoveRange(exist);
// await _dbContext.SaveChangesAsync();
//}
// insert new Record
command.Documents.Add(document);
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<List<CommandDocument>> GetCommandDocumentAsync(Guid id)
{
try
{
var docs = await _dbContext.Set<CommandDocument>()
.Include(x => x.Command)
.Include(x => x.Document)
.Where(x => x.Command.Id == id)
.ToListAsync();
return docs;
}
catch
{
throw;
}
}
#endregion
#region " Change Command Status "
public async Task GotoNextStateAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
var notProcess = new int[] { 4, 5 };
if (!notProcess.Contains(command.CommandStatus.Sequence))
{
var nextStatus = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence + 1);
command.CommandStatus = nextStatus!;
_dbContext.Attatch(nextStatus!);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
public async Task GotoPrevStateAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>().Include(c => c.CommandStatus).FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
var notProcess = new int[] { 1, 5 };
if (!notProcess.Contains(command.CommandStatus.Sequence))
{
var nextStatus = await _dbContext.Set<CommandStatus>().FirstOrDefaultAsync(c => c.Sequence == command.CommandStatus.Sequence - 1);
command.CommandStatus = nextStatus!;
_dbContext.Attatch(nextStatus!);
await _dbContext.SaveChangesAsync();
}
}
catch
{
throw;
}
}
#endregion
#region " Change Detail Sequence "
#endregion
#region " Placement "
public async Task<PlacementSalaryResponse> GetPlacementSalaryAsync(Guid placementProfileId)
{
try
{
var placementProfile = await _dbContext.Set<PlacementProfile>()
.FirstOrDefaultAsync(p => p.Id == placementProfileId);
if (placementProfile == null)
throw new Exception($"Invalid placement profile: {placementProfileId}");
return new PlacementSalaryResponse
{
SalaryAmount = placementProfile.Amount ?? 0,
PositionSalaryAmount = placementProfile.PositionSalaryAmount ?? 0,
MonthSalaryAmount = placementProfile.MouthSalaryAmount ?? 0
};
}
catch
{
throw;
}
}
public async Task UpdatePlacementSalaryAsync(Guid personalId, UpdatePlacementSalaryRequest req)
{
try
{
var current = await _dbContext.Set<CommandReceiver>()
.FirstOrDefaultAsync(x => x.Id == personalId);
if (current == null)
throw new Exception(GlobalMessages.DataNotFound);
var placementProfile = await _dbContext.Set<PlacementProfile>()
.FirstOrDefaultAsync(p => p.Id == current.RefPlacementProfileId);
if (placementProfile == null)
throw new Exception($"Invalid placement profile: {current.RefPlacementProfileId}");
placementProfile.Amount = req.SalaryAmount;
placementProfile.PositionSalaryAmount = req.PositionSalaryAmount;
placementProfile.MouthSalaryAmount = req.MonthSalaryAmount;
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
public async Task<List<PositionPath>> GetPlacementPositionPath(Guid id)
{
try
{
var data = await _dbContext.Set<PlacementProfile>()
.Include(x => x.PositionPath)
.Include(x => x.Placement)
.Where(x => x.Placement!.Id == id)
.Where(x => x.PositionPath != null)
.Select(x => x.PositionPath)
.Distinct()
.ToListAsync();
return data!;
}
catch
{
throw;
}
}
#endregion
#region " Command Info "
public async Task<CommandInfoResponse> GetCommandInfoAsync(Guid id)
{
try
{
var command = await _dbContext.Set<Command>().FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
return new CommandInfoResponse
{
SignDate = command.CommandExcecuteDate.Value,
OrderNo = command.CommandNo,
OrderYear = command.CommandYear,
};
}
catch
{
throw;
}
}
public async Task UpdateCommandInfoAsync(Guid id, string orderNo, string orderYear, DateTime signDate)
{
try
{
var command = await _dbContext.Set<Command>().FirstOrDefaultAsync(x => x.Id == id);
if (command == null)
throw new Exception(GlobalMessages.CommandNotFound);
command.CommandExcecuteDate = signDate;
command.CommandNo = orderNo;
command.CommandYear = orderYear;
await _dbContext.SaveChangesAsync();
}
catch
{
throw;
}
}
#endregion
#region " Organization and Approver "
public async Task<Guid> GetRootOcIdAsync(Guid ocId)
{
try
{
var data = await _dbContext.Set<OrganizationEntity>().AsQueryable()
.FirstOrDefaultAsync(o => o.Id == ocId);
if (data == null)
throw new Exception(GlobalMessages.OrganizationNotFound);
return data.OrganizationAgencyId == null ? ocId : data.OrganizationAgencyId!.Value;
}
catch
{
throw;
}
}
public async Task<List<KeyValueItemResponse>> GetCommandOrgAsync()
{
try
{
var ret = new List<KeyValueItemResponse>();
ret.Add(new KeyValueItemResponse
{
Id = Guid.Empty,
Name = "กรุงเทพมหานคร"
});
var rootOcId = await GetRootOcIdAsync(UserOrganizationId);
var oc = await _dbContext.Set<OrganizationEntity>()
.Include(x => x.OrganizationOrganization)
.Select(x => new KeyValueItemResponse
{
Id = x.Id,
Name = x.OrganizationOrganization!.Name
})
.FirstOrDefaultAsync(x => x.Id == rootOcId);
if (oc != null)
ret.Add(oc);
return ret;
}
catch
{
throw;
}
}
public async Task<List<OrganizationApproverResponse>> GetOrgApproverAsync(Guid ocId)
{
try
{
if (ocId == Guid.Empty)
return new List<OrganizationApproverResponse>() { new OrganizationApproverResponse { Id = Guid.Empty, Name = "", PositionName = "ปลัดกรุงเทพมหานคร" } };
else
{
//var ret = new List<KeyValueItemResponse>();
var oc = await _dbContext.Set<OrganizationEntity>().Include(x => x.Parent).FirstOrDefaultAsync(x => x.Id == ocId);
var profilePosition = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionExecutive)
.Where(x => x.OrganizationPosition!.Organization!.Id == ocId &&
x.OrganizationPosition!.PositionMaster!.IsDirector == true)
.Select(x => new OrganizationApproverResponse
{
Id = x.Profile!.Id,
Name = $"{x.Profile!.Prefix!.Name}{x.Profile!.FirstName} {x.Profile!.LastName}",
PositionName = x.OrganizationPosition!.PositionMaster!.PositionExecutive != null ?
x.OrganizationPosition!.PositionMaster!.PositionExecutive!.Name
:
x.OrganizationPosition!.PositionMaster!.PositionPath == null ? "" : x.OrganizationPosition!.PositionMaster!.PositionPath!.Name
})
.ToListAsync();
if (profilePosition.Count > 0)
{
return profilePosition;
}
else
{
if (oc != null && oc.Parent != null)
{
var parentProfilePosition = await _dbContext.Set<ProfilePosition>()
.Include(x => x.Profile)
.ThenInclude(x => x.Prefix)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.Organization)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionPath)
.Include(x => x.OrganizationPosition)
.ThenInclude(x => x!.PositionMaster)
.ThenInclude(x => x!.PositionExecutive)
.Where(x => x.OrganizationPosition!.Organization!.Id == oc.Parent.Id &&
x.OrganizationPosition!.PositionMaster!.IsDirector == true)
.Select(x => new OrganizationApproverResponse
{
Id = x.Profile!.Id,
Name = $"{x.Profile!.Prefix!.Name}{x.Profile!.FirstName} {x.Profile!.LastName}",
PositionName = x.OrganizationPosition!.PositionMaster!.PositionExecutive != null ?
x.OrganizationPosition!.PositionMaster!.PositionExecutive!.Name
:
x.OrganizationPosition!.PositionMaster!.PositionPath == null ? "" : x.OrganizationPosition!.PositionMaster!.PositionPath!.Name
})
.ToListAsync();
return parentProfilePosition;
}
else
return new List<OrganizationApproverResponse>();
}
}
}
catch
{
throw;
}
}
#endregion
#endregion
}
}