using System.Security.Claims; using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Application.Requests; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Insignias; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; namespace BMA.EHR.Insignia.Service.Controllers { [Route("api/v{version:apiVersion}/insignia/receive")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("เครื่องราช")] public class InsigniaReceiveController : BaseController { private readonly ApplicationDBContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; private readonly InsigniaPeriodsRepository _repository; private readonly NotificationRepository _repositoryNoti; public InsigniaReceiveController(ApplicationDBContext context, MinIOService documentService, InsigniaPeriodsRepository repository, NotificationRepository repositoryNoti, IHttpContextAccessor httpContextAccessor) { _context = context; _documentService = documentService; _repository = repository; _repositoryNoti = repositoryNoti; _httpContextAccessor = httpContextAccessor; } [HttpGet("{type}/{ocId:length(36)}")] public async Task> GetInsigniaList(string type, Guid id, Guid ocId) { var result = _repository.GetInsigniaRequest(id, ocId); if (result == null) { return NotFound(); } else { // var request = _context.InsigniaRequestProfiles // .Include(x => x.Request) // .ThenInclude(x => x.Period) // .Include(x => x.RequestInsignia) // .ThenInclude(x => x.InsigniaType) // .Include(x => x.Profile) // .ThenInclude(x => x.AcademicStanding) // .Include(x => x.Profile) // .ThenInclude(x => x.Position) // .Include(x => x.Profile) // .ThenInclude(x => x.Insignias) // .ThenInclude(x => x.Insignia) // .ThenInclude(x => x.InsigniaType) // .Include(x => x.Profile) // .ThenInclude(x => x.PositionNumber) // .Where(x => x.Request.Period.Id == result.PeriodId) // .Where(x => x.Request.RequestStatus == "st5p") // .Where(x => x.IsApprove) // .Select(p => new // { // Profile = p.Profile.Id, // Name = $"{p.Profile.Prefix} {p.Profile.FirstName} {p.Profile.LastName}", // Insignia = p.RequestInsignia, // Type = p.RequestInsignia.InsigniaType, // IsApprove = p.IsApprove, // InsigniaId = p.RequestInsignia.Id, // Year = p.Request.Period.Year, // Special = p.Special, // LastInsignia = p.Profile.Insignias.AsQueryable() // .Include(x => x.Insignia) // .Where(x => x.Insignia.Id == p.RequestInsignia.Id) // .Where(x => x.Year == p.Request.Period.Year) // .FirstOrDefault() // }) // .ToList() // .Select(r => new InsigniaReceiveResponse // { // Profile = r.Profile, // Name = r.Name, // Insignia = r.Insignia.Name, // TypeId = r.Type == null ? null : r.Type.Id, // TypeName = r.Type == null ? "" : r.Type.Description, // IsApprove = r.IsApprove, // InsigniaId = r.InsigniaId, // InsigniaPage = r.LastInsignia == null ? "" : r.LastInsignia.Page, // InsigniaNo = r.LastInsignia == null ? "" : r.LastInsignia.No, // InsigniaIssue = r.LastInsignia == null ? "" : r.LastInsignia.Issue, // InsigniaVolumeno = r.LastInsignia == null ? "" : r.LastInsignia.VolumeNo, // InsigniaVolume = r.LastInsignia == null ? "" : r.LastInsignia.Volume, // InsigniaSection = r.LastInsignia == null ? "" : r.LastInsignia.Section, // InsigniaDatereceive = r.LastInsignia == null ? null : r.LastInsignia.DateReceive, // InsigniaDateannounce = r.LastInsignia == null ? null : r.LastInsignia.DateAnnounce, // Special = r.Special // }) // .ToList() // .GroupBy(r => new { r.TypeId, r.TypeName }) // .Select(r => new // { // TypeId = r.Key.TypeId, // InsigniaIssue = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaIssue : "", // InsigniaVolumeno = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaVolumeno : null, // InsigniaVolume = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaVolume : "", // InsigniaSection = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaSection : "", // InsigniaDatereceive = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaDatereceive : null, // InsigniaDateannounce = r.Where(r => r.InsigniaIssue != "").FirstOrDefault() != null ? r.Where(r => r.InsigniaIssue != "").FirstOrDefault().InsigniaDateannounce : null, // TypeName = r.Key.TypeName, // Profile = r.Select(r => new // { // Profile = r.Profile, // Name = r.Name, // Insignia = r.Insignia, // InsigniaId = r.InsigniaId, // InsigniaPage = r.InsigniaPage, // InsigniaNo = r.InsigniaNo, // //Special = bool.Parse(r.Special) // }).ToList(), // Docs = new List() // }); return Success(); // return Success(request); } } [HttpPost("{type}")] public async Task> SaveToProfile([FromBody] SaveToProfileRequest items, string type) { var item = new Kp7Item { InsigniaDatereceive = items.InsigniaDatereceive, InsigniaLevel = items.InsigniaLevel, InsigniaIssue = items.InsigniaIssue, InsigniaVolumeno = items.InsigniaVolumeno, InsigniaVolume = items.InsigniaVolume, InsigniaSection = items.InsigniaSection, InsigniaDateannounce = items.InsigniaDateannounce }; if (items.Profile.Count() != 0) { // foreach (var i in items.Profile) // { // var profile = _context.Profiles.AsQueryable() // .Include(x => x.Insignias) // .ThenInclude(x => x.Insignia) // .Where(x => x.Id == i.FkProfileId) // .FirstOrDefault(); // if (profile != null) // { // var kp7 = profile.Insignias.AsQueryable() // .Where(x => x.Insignia.Id == i.Kp7InsigniaId) // .FirstOrDefault(); // if (kp7 != null) // { // // exit item update to database // kp7.DateReceive = items.InsigniaDatereceive.Value; // kp7.Level = items.InsigniaLevel; // kp7.Issue = items.InsigniaIssue; // kp7.VolumeNo = items.InsigniaVolumeno.Value.ToString(); // kp7.Volume = items.InsigniaVolume; // kp7.Section = items.InsigniaSection; // kp7.DateAnnounce = items.InsigniaDateannounce.Value; // kp7.Page = i.InsigniaPage; // kp7.No = i.InsigniaNo; // } // else // { // // insert new item to kp7 // var insignia_item = _context.Insignias.FirstOrDefault(x => x.Id == i.Kp7InsigniaId); // var result = _repository.GetInsigniaRequest(type, items.OCId); // var period = _context.InsigniaPeriods.FirstOrDefault(x => x.Id == result.PeriodId); // kp7 = new Models.HR.ProfileInsignia // { // Order = profile.Insignias.ToList().Count + 1, // Year = period.Year, // Insignia = insignia_item, // DateReceive = items.InsigniaDatereceive.Value, // DateStamp = DateTime.Now, // Level = items.InsigniaLevel, // Issue = items.InsigniaIssue, // VolumeNo = items.InsigniaVolumeno.Value.ToString(), // Volume = items.InsigniaVolume, // Section = items.InsigniaSection, // DateAnnounce = items.InsigniaDateannounce.Value, // Page = i.InsigniaPage, // No = i.InsigniaNo, // }; // profile.Insignias.Add(kp7); // } // } // else // return NotFound("Profile not found!!!"); // } } _context.SaveChanges(); return Success(); } } }