diff --git a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs index b57183d2..ded9e370 100644 --- a/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs +++ b/BMA.EHR.Application/Repositories/Leaves/TimeAttendants/LeaveProcessJobStatusRepository.cs @@ -80,6 +80,19 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants return data; } + /// + /// ดึงข้อมูล Job Status จาก UserId + /// + public async Task> GetByUserIdAsync(Guid userId) + { + var data = await _dbContext.Set() + .Where(x => x.CreatedUserId == userId.ToString("D")) + .OrderByDescending(x => x.CreatedDate) + .ToListAsync(); + + return data; + } + /// /// ดึงข้อมูล Job Status ที่ยัง pending หรือ processing /// @@ -225,6 +238,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants // check วันลาของแต่ละคน var leaveReq = await _leaveRequestRepository.GetLeavePeriodAsync(keycloakUserId, dd.date); var remarkStr = string.Empty; + var status = string.Empty; + var stampType = string.Empty; + var stampAmount = 0.0; if (leaveReq != null) { @@ -260,6 +276,9 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants remarkStr += leaveReq.Type.Name; break; } + status = "LEAVE"; + stampType = leaveReq.LeaveRange ?? ""; + stampAmount = leaveReq.LeaveRange == "MORNING" || leaveReq.LeaveRangeEnd == "MORNING" ? 0.5 : 1; } else { @@ -268,13 +287,18 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants if (dd.date <= DateTime.Now.Date) { remarkStr = "ขาดราชการ"; + status = "ABSENT"; + stampType = "FULL_DAY"; + stampAmount = 1; if (dd.isHoliday == true) { remarkStr = $"วันหยุด ({dd.dateRemark})"; + status = "HOLIDAY"; } else if (dd.isWeekEnd) { remarkStr = dd.dateRemark; + status = "WEEKEND"; } } else remarkStr = ""; @@ -285,12 +309,25 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants if (timeStamps.CheckOut != null) { if (timeStamps.CheckOutStatus == "ABSENT") + { remarkStr = "ขาดราชการ" + (!timeStamps.IsLocationCheckOut ? $" (นอกสถานที่:{timeStamps.CheckOutLocationName})".Trim() : ""); + status = "ABSENT"; + stampType = "FULL_DAY"; + stampAmount = 1; + } else if (timeStamps.CheckInStatus == "ABSENT") + { remarkStr = "ขาดราชการ" + (!timeStamps.IsLocationCheckIn ? $" (นอกสถานที่:{timeStamps.CheckInLocationName})".Trim() : ""); + status = "ABSENT"; + stampType = "FULL_DAY"; + stampAmount = 1; + } else if (timeStamps.CheckInStatus == "LATE") { remarkStr = "สาย" + (!timeStamps.IsLocationCheckIn ? $" (นอกสถานที่:{timeStamps.CheckInLocationName})".Trim() : ""); + status = "LATE"; + stampType = "FULL_DAY"; + stampAmount = 1; //lateTotal += 1; } else @@ -299,9 +336,17 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants else { if (timeStamps.CheckInStatus == "ABSENT") + { + status = "ABSENT"; + stampType = "FULL_DAY"; + stampAmount = 1; remarkStr = "ขาดราชการ" + (!timeStamps.IsLocationCheckIn ? $" (นอกสถานที่:{timeStamps.CheckInLocationName})".Trim() : ""); + } else if (timeStamps.CheckInStatus == "LATE") { + status = "LATE"; + stampType = "FULL_DAY"; + stampAmount = 1; remarkStr = "สาย" + (!timeStamps.IsLocationCheckIn ? $" (นอกสถานที่:{timeStamps.CheckInLocationName})".Trim() : ""); //lateTotal += 1; } @@ -313,24 +358,13 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants var emp = new DateResultReport { - no = count, - fullName = fullName, - dutyTimeName = $"{duty.StartTimeMorning} - {duty.EndTimeAfternoon} น.", - checkInLocation = timeStamps == null ? "" : timeStamps.CheckInPOI, - checkInTime = timeStamps == null ? "" : $"{timeStamps.CheckIn.ToString("HH:mm")} น.", - checkOutLocation = timeStamps == null ? "" : timeStamps.CheckOutPOI ?? "", - checkOutTime = timeStamps == null ? "" : - timeStamps.CheckOut != null ? - $"{timeStamps.CheckOut.Value.ToString("HH:mm")} น." : - "", + profileId = p.Id.ToString(), + stampDate = dd.date, + stampType = stampType, + stampAmount = stampAmount, remark = remarkStr, - checkInDate = timeStamps == null ? dd.date.Date.ToThaiFullDate2() : timeStamps.CheckIn.Date.ToThaiFullDate2(), - checkedOutDate = timeStamps == null ? dd.date.Date.ToThaiFullDate2() : - timeStamps.CheckOut != null ? - timeStamps.CheckOut.Value.ToThaiFullDate2() : - "", - checkInTimeRaw = timeStamps == null ? dd.date.Date : timeStamps?.CheckIn, - checkOutTimeRaw = timeStamps == null ? dd.date.Date : timeStamps?.CheckOut != null ? timeStamps?.CheckOut : null, + status = status + }; employees.Add(emp); @@ -403,18 +437,12 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants class DateResultReport { - public int no { get; set; } - public string fullName { get; set; } - public string dutyTimeName { get; set; } - public string checkInLocation { get; set; } - public string checkInTime { get; set; } - public string checkOutLocation { get; set; } - public string checkOutTime { get; set; } + public string? profileId { get; set; } + public DateTime stampDate { get; set; } + public string stampType { get; set; } + public double stampAmount { get; set; } public string remark { get; set; } - public string checkInDate { get; set; } - public string checkedOutDate { get; set; } - public DateTime? checkInTimeRaw { get; set; } - public DateTime? checkOutTimeRaw { get; set; } + public string status { get; set; } } } \ No newline at end of file diff --git a/BMA.EHR.Leave/Controllers/LeaveController.cs b/BMA.EHR.Leave/Controllers/LeaveController.cs index 930c4043..799f3558 100644 --- a/BMA.EHR.Leave/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveController.cs @@ -4200,6 +4200,139 @@ namespace BMA.EHR.Leave.Service.Controllers return Success(); } + /// + /// แสดงรายการ Task สำหรับ Process ข้อมูลวันลาและขาดราชการ (ADMIN) + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("admin/leave-task/process")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetProcessTaskAsync() + { + var userId = UserId == null ? Guid.Empty : Guid.Parse(UserId); + + var tasks = await _leaveProcessJobStatusRepository.GetByUserIdAsync(userId); + + var result = tasks.Select(t => new + { + t.Id, + t.CreatedFullName, + t.CreatedAt, + t.Status, + t.StartDate, + t.EndDate, + t.ProcessingDate, + t.CompletedDate, + t.ErrorMessage + }); + + return Success(result); + } + + + /// + /// แสดงรายการ Task สำหรับ Process ข้อมูลวันลาและขาดราชการ (ADMIN) + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("admin/leave-task/process/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetProcessTaskByIdAsync(Guid id) + { + var task = await _leaveProcessJobStatusRepository.GetByTaskIdAsync(id); + + if (task == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + var result = new + { + task.Id, + task.CreatedFullName, + task.CreatedAt, + task.Status, + task.StartDate, + task.EndDate, + task.ProcessingDate, + task.CompletedDate, + task.ErrorMessage + }; + + return Success(result); + } + + /// + /// ลบ Task สำหรับ Process ข้อมูลวันลาและขาดราชการ (ADMIN) + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpDelete("admin/leave-task/process/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> DeleteProcessTaskByIdAsync(Guid id) + { + var task = await _leaveProcessJobStatusRepository.GetByTaskIdAsync(id); + + if (task == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + await _leaveProcessJobStatusRepository.DeleteAsync(task); + + return Success(); + } + + /// + /// อัปเดต Task สำหรับ Process ข้อมูลวันลาและขาดราชการ (ADMIN) + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPut("admin/leave-task/process/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> UpdateProcessTaskByIdAsync(Guid id,[FromBody] CreateLeaveProcessJobDto req) + { + var task = await _leaveProcessJobStatusRepository.GetByTaskIdAsync(id); + + if (task == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + if(task.Status != "PENDING") + { + return Error("ไม่สามารถแก้ไขได้เนื่องจาก Task อยู่ในสถานะกำลังดำเนินการหรือดำเนินการเสร็จสิ้นแล้ว"); + } + + task.StartDate = req.StartDate; + task.EndDate = req.EndDate; + + await _leaveProcessJobStatusRepository.UpdateAsync(task); + + return Success(); + } + + #endregion #endregion