using _0_Framework.Application; using CompanyManagement.Infrastructure.Excel.RollCall; using CompanyManagment.App.Contracts.RollCall; using CompanyManagment.App.Contracts.RollCallEmployee; using CompanyManagment.App.Contracts.Workshop; using Microsoft.AspNetCore.Mvc; using ServiceHost.BaseControllers; namespace ServiceHost.Areas.Client.Controllers.RollCall; public class RollCallCaseHistoryController : ClientBaseController { private readonly IRollCallApplication _rollCallApplication; private readonly long _workshopId; private readonly IWorkshopApplication _workshopApplication; private readonly IRollCallEmployeeApplication _rollCallEmployeeApplication; public RollCallCaseHistoryController(IRollCallApplication rollCallApplication, IAuthHelper authHelper, IWorkshopApplication workshopApplication, IRollCallEmployeeApplication rollCallEmployeeApplication) { _rollCallApplication = rollCallApplication; _workshopApplication = workshopApplication; _rollCallEmployeeApplication = rollCallEmployeeApplication; _workshopId = authHelper.GetWorkshopId(); } [HttpGet] public async Task>> GetTitles( RollCallCaseHistorySearchModel searchModel) { return await _rollCallApplication.GetCaseHistoryTitles(_workshopId, searchModel); } [HttpGet("details")] public async Task>> GetDetails(string titleId, RollCallCaseHistorySearchModel searchModel) { return await _rollCallApplication.GetCaseHistoryDetails(_workshopId, titleId, searchModel); } /// /// ایجاد و ویرایش /// /// /// [HttpPost] public ActionResult Upsert(CreateOrEditEmployeeRollCall command) { command.WorkshopId = _workshopId; return _rollCallApplication.ManualEdit(command); } [HttpGet("print")] public async Task>> GetPrintDetails(string titleId, RollCallCaseHistorySearchModel searchModel) { return await _rollCallApplication.GetCaseHistoryDetails(_workshopId, titleId, searchModel); } [HttpGet("total-working")] public ActionResult> OnGetTotalWorking( string startDate, string startTime, string endDate, string endTime) { var op = new OperationResult(); const string emptyValue = "-"; if (!TryParseDateTime(startDate, startTime, out var start) || !TryParseDateTime(endDate, endTime, out var end)) { return op.Succcedded(emptyValue); } if (start >= end) { return op.Succcedded(emptyValue); } var duration = (end - start).ToFarsiHoursAndMinutes(emptyValue); return op.Succcedded(duration); } [HttpGet("excel")] public async Task GetDownload(string titleId, RollCallCaseHistorySearchModel searchModel) { var res =await _rollCallApplication.DownloadCaseHistoryExcel(_workshopId, titleId, searchModel); return File(res.Bytes, res.MimeType, res.FileName); } [HttpGet("edit")] public ActionResult GetEditDetails(string date, long employeeId) { var result = _rollCallApplication.GetWorkshopEmployeeRollCallsForDate(_workshopId, employeeId, date); //var dates = _rollCallApplication.GetEditableDatesForManualEdit(date.ToGeorgianDateTime()); var name = _rollCallEmployeeApplication.GetByEmployeeIdAndWorkshopId(employeeId, _workshopId); var total = new TimeSpan(result.Sum(x => (x.EndDate!.Value.Ticks - x.StartDate!.Value.Ticks))); var res = new EditRollCallDetailsResult() { EmployeeFullName = name.EmployeeFullName, EmployeeId = employeeId, DateFa = date, //EditableDates = dates, Records = result.Select(x=>new EmployeeRollCallRecord() { Date = x.DateGr, EndDate = x.EndDateFa, EndTime = x.EndTimeString, RollCallId = x.Id, StartDate = x.StartDateFa, StartTime = x.StartTimeString }).ToList(), TotalRollCallsDuration = total.ToFarsiHoursAndMinutes("-") }; return res; } [HttpPost("edit")] public ActionResult Edit(CreateOrEditEmployeeRollCall command) { command.WorkshopId = _workshopId; var result = _rollCallApplication.ManualEdit(command); return result; } [HttpDelete("delete")] public IActionResult OnPostRemoveEmployeeRollCallsInDate(RemoveEmployeeRollCallRequest request) { var result = _rollCallApplication.RemoveEmployeeRollCallsInDate(_workshopId, request.EmployeeId, request.Date); return new JsonResult(new { success = result.IsSuccedded, message = result.Message, }); } // [HttpGet("edit")] // public ActionResult<> GetEditDetails(string date,long employeeId) // { // var result = _rollCallApplication.GetWorkshopEmployeeRollCallsForDate(_workshopId, employeeId, date); // //var dates = _rollCallApplication.GetEditableDatesForManualEdit(date.ToGeorgianDateTime()); // var name = _rollCallEmployeeApplication.GetByEmployeeIdAndWorkshopId(employeeId, _workshopId); // // var total = new TimeSpan(result.Sum(x => // (x.EndDate!.Value.Ticks - x.StartDate!.Value.Ticks))); // // var command = new EmployeeRollCallsViewModel() // { // EmployeeFullName = name.EmployeeFullName, // EmployeeId = employeeId, // DateFa = date, // //EditableDates = dates, // RollCalls = result, // TotalRollCallsDuration = total.ToFarsiHoursAndMinutes("-") // }; // } private static bool TryParseDateTime(string date, string time, out DateTime result) { result = default; try { var dateTime = date.ToGeorgianDateTime(); var timeOnly = TimeOnly.Parse(time); result = dateTime.AddTicks(timeOnly.Ticks); return true; } catch { return false; } } } public class EditRollCallDetailsResult { public string EmployeeFullName { get; set; } public long EmployeeId { get; set; } public string DateFa { get; set; } public string TotalRollCallsDuration { get; set; } public List Records { get; set; } } public class RemoveEmployeeRollCallRequest { public long EmployeeId { get; set; } public string Date { get; set; } }