using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.MessageQueue; using BMA.EHR.Discipline.Service.Requests; using BMA.EHR.Domain.Common; using BMA.EHR.Domain.Models.Discipline; using BMA.EHR.Domain.Shared; using BMA.EHR.Infrastructure.Persistence; // using BMA.EHR.Placement.Service.Requests; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Swashbuckle.AspNetCore.Annotations; using System.Security.Claims; namespace BMA.EHR.DisciplineComplaint.Service.Controllers { [Route("api/v{version:apiVersion}/discipline/complaint")] [ApiVersion("1.0")] [ApiController] [Produces("application/json")] [Authorize] [SwaggerTag("ระบบวินัย")] public class DisciplineComplaintController : BaseController { private readonly DisciplineDbContext _context; private readonly MinIOService _documentService; private readonly IHttpContextAccessor _httpContextAccessor; public DisciplineComplaintController(DisciplineDbContext context, MinIOService documentService, IHttpContextAccessor httpContextAccessor) { // _repository = repository; _context = context; _documentService = documentService; _httpContextAccessor = httpContextAccessor; } #region " Properties " private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; #endregion /// /// list รายการวินัย /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet()] public async Task> GetDiscipline(int page = 1, int pageSize = 25, string keyword = "") { var data = await _context.DisciplineComplaints .Select(x => new { Id = x.Id,//id ข้อมูลเรื่องร้องเรียน Title = x.Title,//ชื่อเรื่อง Description = x.Description,//รายละเอียด //Respondent = x.xxx,//ผู้ถูกร้องเรียน DescMistake = x.OffenseDetails,//ลักษณะความผิด CreatedAt = x.CreatedAt,//วันที่สร้างเรื่องร้องเรียน DevLevel = x.LevelConsideration,//ระดับการพัฒนา ConsiderationDate = x.DateConsideration,//วันที่กำหนดพิจารณา ComplaintStatus = x.Status,//สถานะเรื่องร้องเรียน มีดังนี้ ใหม่ (NEW), ยุติเรื่อง (STOP), มีมูลส่งไปสืบสวนแล้ว (SEND_INVESTIGATE) }) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return Success(new { data, total = data.Count() }); } /// /// get รายการวินัย /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpGet("{id:guid}")] public async Task> GetByDiscipline(Guid id) { var data = await _context.DisciplineComplaints .Select(x => new { Id = x.Id,//id ข้อมูลเรื่องร้องเรียน Title = x.Title,//ชื่อเรื่อง Description = x.Description,//รายละเอียด //Respondent = x.xxx,//ผู้ถูกร้องเรียน DescMistake = x.OffenseDetails,//ลักษณะความผิด CreatedAt = x.CreatedAt,//วันที่สร้างเรื่องร้องเรียน DevLevel = x.LevelConsideration,//ระดับการพัฒนา ConsiderationDate = x.DateConsideration,//วันที่กำหนดพิจารณา ComplaintStatus = x.Status,//สถานะเรื่องร้องเรียน มีดังนี้ ใหม่ (NEW), ยุติเรื่อง (STOP), มีมูลส่งไปสืบสวนแล้ว (SEND_INVESTIGATE) }) .Where(x => x.Id == id) .FirstOrDefaultAsync(); if (data == null) return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound); return Success(data); } /// /// สร้างรายการวินัย /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPost()] public async Task> CreateDiscipline([FromForm] DisciplineComplaintRequest req) { var disciplineComplaint = new Domain.Models.Discipline.DisciplineComplaint { RespondentType = req.respondentType, // xxx = req.xxx, // xxx = req.xxx, // xxx = req.xxx, // xxx = req.xxx, Title = req.title, Description = req.description, DateReceived = req.dateReceived, LevelConsideration = req.levelConsideration, DateConsideration = req.dateConsideration, OffenseDetails = req.offenseDetails, DateNotification = req.dateNotification, ComplaintFrom = req.complaintFrom, Appellant = req.appellant, Status = "NEW", CreatedFullName = FullName ?? "System Administrator", CreatedUserId = UserId ?? "", CreatedAt = DateTime.Now, LastUpdateFullName = FullName ?? "System Administrator", LastUpdateUserId = UserId ?? "", LastUpdatedAt = DateTime.Now, }; // var doc = await _documentService.UploadFileAsync(file, file.FileName); // var _doc = await _context.Documents.AsQueryable() // .FirstOrDefaultAsync(x => x.Id == doc.Id); // disciplineComplaint.Document = _doc; await _context.DisciplineComplaints.AddAsync(disciplineComplaint); await _context.SaveChangesAsync(); return Success(); } /// /// แก้ไขรายการวินัย /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpPut("{id:guid}")] public async Task> UpdateDiscipline(Guid id, [FromForm] DisciplineComplaintRequest req) { var data = await _context.DisciplineComplaints.Where(x => x.Id == id).FirstOrDefaultAsync(); if (data == null) return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound); if (data.Status.Trim().ToUpper() != "NEW") return Error(new Exception("ไม่สามารถแก้ไขข้อมูลนี้ได้"), (int)StatusCodes.Status500InternalServerError); data.RespondentType = req.respondentType; // data.xxx = req.xxx; // data.xxx = req.xxx; // data.xxx = req.xxx; // data.xxx = req.xxx; data.Title = req.title; data.Description = req.description; data.DateReceived = req.dateReceived; data.LevelConsideration = req.levelConsideration; data.DateConsideration = req.dateConsideration; data.OffenseDetails = req.offenseDetails; data.DateNotification = req.dateNotification; data.ComplaintFrom = req.complaintFrom; data.Appellant = req.appellant; data.LastUpdateFullName = FullName ?? "System Administrator"; data.LastUpdateUserId = UserId ?? ""; data.LastUpdatedAt = DateTime.Now; await _context.SaveChangesAsync(); // if (Request.Form.Files != null && Request.Form.Files.Count != 0) // { // var doc = await _documentService.UploadFileAsync(file, file.FileName); // var _doc = await _context.Documents.AsQueryable() // .FirstOrDefaultAsync(x => x.Id == doc.Id); // disciplineComplaint.Document = _doc; // var _docId = profileDoc.Document.Id; // await _documentService.DeleteFileAsync(_docId); // await _context.SaveChangesAsync(); // } return Success(data); } /// /// ลบรายการวินัย /// /// /// /// ค่าตัวแปรที่ส่งมาไม่ถูกต้อง /// ไม่ได้ Login เข้าระบบ /// เมื่อเกิดข้อผิดพลาดในการทำงาน [HttpDelete("{id:guid}")] public async Task> DeleteDiscipline(Guid id) { var data = await _context.DisciplineComplaints // .Include(x=>x.Document) .Where(x => x.Id == id) .FirstOrDefaultAsync(); if (data == null) return Error(new Exception(GlobalMessages.DataNotFound), (int)StatusCodes.Status404NotFound); _context.DisciplineComplaints.Remove(data); // var _docId = data.Document.Id; // await _context.SaveChangesAsync(); // await _documentService.DeleteFileAsync(_docId); await _context.SaveChangesAsync(); return Success(); } } }