diff --git a/BMA.EHR.Application/Repositories/UserProfileRepository.cs b/BMA.EHR.Application/Repositories/UserProfileRepository.cs index 7c3a9449..b088c52f 100644 --- a/BMA.EHR.Application/Repositories/UserProfileRepository.cs +++ b/BMA.EHR.Application/Repositories/UserProfileRepository.cs @@ -740,7 +740,7 @@ namespace BMA.EHR.Application.Repositories } } - public async Task> SearchProfileEmployee(string? citizenId, string? firstName, string? lastName, string accessToken) + public async Task> SearchProfileEmployee(string? citizenId, string? firstName, string? lastName, string accessToken, string? role, string? nodeId, int? node) { try { @@ -750,7 +750,10 @@ namespace BMA.EHR.Application.Repositories { citizenId = citizenId, firstName = firstName, - lastName = lastName + lastName = lastName, + role = role, + nodeId = nodeId, + node = node, }; var profiles = new List(); diff --git a/BMA.EHR.Leave/Controllers/LeaveController.cs b/BMA.EHR.Leave/Controllers/LeaveController.cs index d4b60e3f..7e6a1105 100644 --- a/BMA.EHR.Leave/Controllers/LeaveController.cs +++ b/BMA.EHR.Leave/Controllers/LeaveController.cs @@ -1851,6 +1851,193 @@ namespace BMA.EHR.Leave.Service.Controllers } + #endregion + + #region " เปลี่ยนรอบการทำงาน ลจ. " + + /// + /// LV1_006 - เช็คเวลาต้องลงเวลาเข้าหรือออกงาน (USER) ลจ. + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("emp/search")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> SearchEmpProfileAsync([FromBody] DTOs.ChangeRound.SearchProfileDto req) + { + var getPermission = await _permission.GetPermissionAPIAsync("LIST", "SYS_WORK_ROUND_EDIT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + string role = jsonData["result"]?.ToString(); + var nodeId = string.Empty; + var profileAdmin = new GetUserOCAllDto(); + profileAdmin = await _userProfileRepository.GetUserOCAll(Guid.Parse(UserId!), AccessToken); + if (role == "NORMAL" || role == "CHILD") + { + nodeId = profileAdmin?.Node == 4 + ? profileAdmin?.Child4DnaId + : profileAdmin?.Node == 3 + ? profileAdmin?.Child3DnaId + : profileAdmin?.Node == 2 + ? profileAdmin?.Child2DnaId + : profileAdmin?.Node == 1 + ? profileAdmin?.Child1DnaId + : profileAdmin?.Node == 0 + ? profileAdmin?.RootDnaId + : ""; + } + else if (role == "ROOT") + { + nodeId = profileAdmin?.RootDnaId; + } + var profile = await _userProfileRepository.SearchProfileEmployee(req.CitizenId, req.FirstName, req.LastName, AccessToken ?? "", role, nodeId, profileAdmin?.Node); + + var pagedProfile = profile.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList(); + + var getDefaultRound = await _dutyTimeRepository.GetDefaultAsync(); + + var resultSet = new List(); + + foreach (var p in pagedProfile) + { + + var effectiveDate = await _userDutyTimeRepository.GetLastEffectRound(p.Id); + var roundId = effectiveDate != null ? effectiveDate.DutyTimeId : Guid.Empty; + var userRound = await _dutyTimeRepository.GetByIdAsync(roundId); + + var duty = userRound ?? getDefaultRound; + + var res = new SearchProfileResultDto + { + ProfileId = p.Id, + CitizenId = p.CitizenId ?? "", + FullName = $"{p.Prefix ?? ""}{p.FirstName ?? ""} {p.LastName ?? ""}", + StartTimeMorning = duty.StartTimeMorning, + LeaveTimeAfterNoon = duty.EndTimeAfternoon, + EffectiveDate = effectiveDate == null ? null : effectiveDate.EffectiveDate.Value.Date + }; + resultSet.Add(res); + } + + return Success(new { data = resultSet, total = profile.Count }); + } + + /// + /// LV1_014 - เปลี่ยนรอบการลงเวลา (ADMIN) Employee + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPost("emp/round")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> CreateChangeEmpRoundAsync([FromBody] CreateChangeRoundDto req) + { + var getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_WORK_ROUND_EDIT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + var currentDate = DateTime.Now.Date; + if (req.EffectiveDate.Date < currentDate) + { + return Error(new Exception($"วันที่มีผลต้องมากกว่าหรือเท่ากับวันที่ปัจจุบัน({currentDate.ToString("yyyy-MM-dd")})"), StatusCodes.Status400BadRequest); + } + + var old = await _userDutyTimeRepository.GetExist(req.ProfileId, req.EffectiveDate); + + var profile = await _userProfileRepository.GetProfileByProfileIdAsync(req.ProfileId, AccessToken); + if (profile == null) + { + return Error(GlobalMessages.DataNotFound, StatusCodes.Status404NotFound); + } + + if (old != null) + { + return Error(new Exception("ไม่สามารถทำรายการได้ เนื่องจากมีการกำหนดรอบการทำงานในวันที่นี้ไว้แล้ว"), StatusCodes.Status400BadRequest); + } + + var data = new UserDutyTime + { + ProfileId = req.ProfileId, + DutyTimeId = req.RoundId, + EffectiveDate = req.EffectiveDate, + Remark = req.Remark, + + RootDnaId = profile.RootDnaId, + Child1DnaId = profile.Child1DnaId, + Child2DnaId = profile.Child2DnaId, + Child3DnaId = profile.Child3DnaId, + Child4DnaId = profile.Child4DnaId, + }; + + await _userDutyTimeRepository.AddAsync(data); + + return Success(); + } + + /// + /// LV1_015 - ประวัติการเปลี่ยนรอบการลงเวลา (ADMIN) Employee + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("emp/round/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetChangeEmpRoundHistoryByProfileIdAsync(Guid id, int page = 1, int pageSize = 10, string keyword = "") + { + var getWorkflow = await _permission.GetPermissionAPIWorkflowAsync(id.ToString(), "SYS_WORK_ROUND_EDIT"); + if (getWorkflow == false) + { + var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_WORK_ROUND_EDIT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + } + var data = await _userDutyTimeRepository.GetListByProfileIdAsync(id); + + var resultSet = new List(); + + if (data != null) + { + resultSet = data + .GroupBy(item => item.ProfileId) + .SelectMany(group => group + .OrderBy(item => item.EffectiveDate) // เรียงลำดับตาม property ที่คุณต้องการ + .Select((item, index) => new ChangeRoundHistoryDto + { + Round = index + 1, + StartTimeMorning = item.DutyTime.StartTimeMorning, + LeaveTimeAfternoon = item.DutyTime.EndTimeAfternoon, + EffectiveDate = item.EffectiveDate.Value, + Remark = item.Remark + })) + .Skip((page - 1) * pageSize) + .Take(pageSize) + .ToList(); + + } + + return Success(new { data = resultSet, total = data.Count }); + } + #endregion #region " Check Checkout Time " @@ -2631,6 +2818,82 @@ namespace BMA.EHR.Leave.Service.Controllers #endregion + #region " ปฏิทินการทำงานของ ลจ. " + + /// + /// LV1_023 - แสดงปฏิทินวันทำงานรายคน (ADMIN) Employee + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("admin/emp/work/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetCalendarEmpByProfileAsync(Guid id) + { + var getWorkflow = await _permission.GetPermissionAPIWorkflowAsync(id.ToString(), "SYS_WORK_ROUND_EDIT"); + if (getWorkflow == false) + { + var getPermission = await _permission.GetPermissionAPIAsync("GET", "SYS_WORK_ROUND_EDIT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + } + var data = await _userCalendarRepository.GetExist(id); + if (data == null) + return Success(new { Work = "NORMAL" }); + else + return Success(new { Work = data.Calendar }); + } + + /// + /// LV1_024 - บันทึกแก้ไขปฏิทินวันทำงาน (ADMIN) + /// + /// + /// + /// เมื่อทำรายการสำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpPut("admin/emp/work/{id:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> UpdateEmpCalendarByProfileAsync(Guid id, [FromBody] UpdateCalendarDto req) + { + var getPermission = await _permission.GetPermissionAPIAsync("UPDATE", "SYS_WORK_ROUND_EDIT"); + var jsonData = JsonConvert.DeserializeObject(getPermission); + if (jsonData["status"]?.ToString() != "200") + { + return Error(jsonData["message"]?.ToString(), StatusCodes.Status403Forbidden); + } + var data = await _userCalendarRepository.GetExist(id); + if (data != null) + { + data.Calendar = req.Work; + await _userCalendarRepository.UpdateAsync(data); + return Success(); + } + else + { + data = new UserCalendar + { + ProfileId = id, + Calendar = req.Work + }; + + await _userCalendarRepository.AddAsync(data); + + return Success(); + } + } + + #endregion + #region " แก้ไขสถานะการลงเวลา " ///