Files
Backend-Api/ServiceHost/Areas/AdminNew/Pages/Company/Task/Index.cshtml.cs
2024-10-05 19:27:04 +03:30

830 lines
22 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.Assign;
using AccountManagement.Application.Contracts.Position;
using AccountManagement.Application.Contracts.Task;
using AccountManagement.Application.Contracts.Ticket;
using AccountManagement.Domain.AssignAgg;
using AccountManagement.Domain.MediaAgg;
using CompanyManagment.App.Contracts.Checkout;
using CompanyManagment.App.Contracts.Contract;
using CompanyManagment.App.Contracts.Leave;
using CompanyManagment.App.Contracts.Workshop;
using CompanyManagment.App.Contracts.YearlySalary;
using CompanyManagment.Application;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json.Linq;
using NuGet.Repositories;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace ServiceHost.Areas.AdminNew.Pages.Company.Task
{
[Authorize]
public class IndexModel : PageModel
{
private readonly ITaskApplication _taskApplication;
private readonly IAuthHelper _authHelper;
private readonly IAccountApplication _accountApplication;
private readonly IPositionApplication _positionApplication;
private readonly IWebHostEnvironment _environment;
private readonly IYearlySalaryApplication _yearlySalaryApplication;
private readonly ITicketApplication _ticketApplication;
public IndexModel(ITaskApplication taskApplication, IAuthHelper authHelper,
IAccountApplication accountApplication, IPositionApplication positionApplication,
IWebHostEnvironment environment, IYearlySalaryApplication yearlySalaryApplication, ITicketApplication ticketApplication)
{
_taskApplication = taskApplication;
_authHelper = authHelper;
_accountApplication = accountApplication;
_positionApplication = positionApplication;
_environment = environment;
_yearlySalaryApplication = yearlySalaryApplication;
_ticketApplication = ticketApplication;
}
public List<TaskViewModel> TaskViewModels { get; set; }
public TaskSearchModel SearchModel { get; set; }
public int RequestCount { get; set; }
public int LastPositionValue { get; set; }
public int UserPositionValue { get; set; }
public List<string> YearlyList { get; set; }
public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
bool hasAccessToTask = _positionApplication.HasPositionValue(_authHelper.CurrentAccountId());
if (!hasAccessToTask)
{
context.Result = Forbid();
}
base.OnPageHandlerExecuting(context);
}
public IActionResult OnGet(TaskSearchModel searchModel)
{
if (_authHelper.GetPermissions().Any(x => x == 901))
{
//var serach = new TaskSearchModel();
searchModel.PageIndex = 0;
SearchModel = searchModel;
RequestCount = _taskApplication.GetRequestedTasksCount();
LastPositionValue = _positionApplication.GetLastPositionValue();
UserPositionValue = (int)_authHelper.CurrentAccountInfo().PositionValue;
YearlyList = _yearlySalaryApplication.GetYears();
return Page();
}
else
{
return Forbid();
}
}
public IActionResult OnGetTaskPagination(int pageIndex, string startDate, string endDate, long accountId,
string isDone,
string isCanceled, string isCancelRequest, string isTimeRequest, string timeRequestAccepted, string type, string isDoneRequest, string generalSearch)
{
var searchModel = new TaskSearchModel()
{
IsDone = isDone,
IsDoneRequest = isDoneRequest,
IsCancelRequest = isCancelRequest,
AccountId = accountId,
EndDate = endDate,
IsCanceled = isCanceled,
IsTimeRequest = isTimeRequest,
PageIndex = pageIndex,
StartDate = startDate,
TimeRequestAccepted = timeRequestAccepted,
TypeOfTask = type,
GeneralSearch = generalSearch
};
var lastPosition = _positionApplication.GetLastPositionValue();
var userPositionValue = _authHelper.CurrentAccountInfo().PositionValue;
var taskList = new List<TaskViewModel>();
if (userPositionValue == 1)
{
if (searchModel.TypeOfTask == "request")
{
taskList = _taskApplication.GetRequestedTasks(searchModel);
}
else if (searchModel.TypeOfTask == "selfTask")
{
taskList = _taskApplication.GetSelfTasks(searchModel);
}
else if (searchModel.TypeOfTask == "sent")
{
taskList = _taskApplication.GetSentTasks(searchModel);
}
else if (searchModel.TypeOfTask == "AllTask")
{
taskList = _taskApplication.GetAllTasks(searchModel);
}
else if (searchModel.TypeOfTask == "allRequested")
{
taskList = _taskApplication.AllRequestedTasks(searchModel);
}
else
{
return BadRequest();
}
}
else
{
if (searchModel.TypeOfTask == "request")
{
taskList = _taskApplication.GetRequestedTasks(searchModel);
}
else if (searchModel.TypeOfTask == "received")
{
taskList = _taskApplication.GetReceivedTasks(searchModel);
}
else if (searchModel.TypeOfTask == "sent")
{
taskList = _taskApplication.GetSentTasks(searchModel);
}
else
{
return BadRequest();
}
}
_taskApplication.GetTaskMessages(2);
return new JsonResult(new
{
pageIndex = taskList.Count,
taskList = taskList,
positions = _positionApplication.GetLowerPosition()
});
}
//public IActionResult OnGetRequestCount()
//{
// var requestCount = _taskApplication.GetRequestedTasksCount();
// return new JsonResult(new
// {
// isSuccess = true,
// result = requestCount
// });
//}
// برای ارجاع دادن
public IActionResult OnPostCreateAssign(OperationModalViewModel command)
{
if (_authHelper.GetPermissions().Any(x => x == 90110))
{
var assign = new CreateAssign()
{
AssignerPositionValue = (int)_authHelper.CurrentAccountInfo().PositionValue,
AssignedId = command.CreateAssign.AssignedId,
AssignerId = _authHelper.CurrentAccountId(),
TaskId = command.TaskId,
EndTaskDateFa = command.CreateAssign.EndTaskDateFa
};
var res = _taskApplication.CreateAssign(assign);
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
else
{
return Forbid();
}
}
// برای مهلت زمانی
public IActionResult OnPostCreateTimeRequest(OperationModalViewModel command)
{
if (_authHelper.GetPermissions().Any(x => x == 90111))
{
var requestTime = new CreateTaskTimeRequest()
{
Description = command.CreateTaskTimeRequest.Description?.Replace("\n", "<br>"),
RequestTime = command.CreateTaskTimeRequest.RequestTime,
TaskId = command.TaskId
};
var res = _taskApplication.CreateRequestTime(requestTime);
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
else
{
return Forbid();
}
}
// برای تایید درخواست مهلت
public IActionResult OnPostAcceptTimeRequest(long taskId, long assignedId, string message)
{
var res = _taskApplication.AcceptRequestDatetime(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای لغو درخواست مهلت
public IActionResult OnPostRejectTimeRequest(long taskId, long assignedId, string message)
{
var res = _taskApplication.RejectTimeRequest(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای درخواست انصراف
public IActionResult OnPostCreateCancelRequest(OperationModalViewModel command)
{
var requestCancel = new CreateTaskCancel()
{
Description = command.CreateTaskCancel.Description?.Replace("\n", "<br>"),
TaskId = command.TaskId,
};
var res = _taskApplication.CreateCancelRequest(requestCancel);
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای تایید انصراف
public IActionResult OnPostAcceptCancel(long taskId, long assignedId, string message)
{
var res = _taskApplication.AcceptCancelRequest(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای لغو انصراف
public IActionResult OnPostRejectCancel(long taskId, long assignedId, string message)
{
var res = _taskApplication.RejectCancelRequest(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای انجام تسک
public IActionResult OnPostComplete(OperationModalViewModel command)
{
var completeTask = new CompleteTaskViewModel()
{
Description = command.CompleteTaskViewModel.Description?.Replace("\n", "<br>"),
TaskId = command.TaskId
};
var res = _taskApplication.CreateCompleteTaskRequest(completeTask);
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
public IActionResult OnPostAcceptComplete(long taskId, long assignedId, string message)
{
var res = _taskApplication.AcceptCompleteRequest(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
public IActionResult OnPostRejectComplete(long taskId, long assignedId, string message)
{
var res = _taskApplication.RejectCompleteRequest(taskId, assignedId, message?.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
// برای باز کردن مودال عملیات تسک
public IActionResult OnGetAssignTo(long taskId, string type)
{
if (_authHelper.GetPermissions().Any(x => x == 90110))
{
var model = new OperationModalViewModel()
{
TaskId = taskId,
Type = type,
Accounts = _accountApplication.AccountsForAssign(taskId)
};
return Partial("OperationModal", model);
}
else
{
return Forbid();
}
}
// برای باز کردن مودال عملیات درخواست تسک
public IActionResult OnGetOperationRequestTask(long taskId, string type)
{
var accounts = _accountApplication.AccountsForAssign(taskId);
var taskDetails = _taskApplication.GetRequestDetails(taskId);
var model = new OperationModalViewModel()
{
//ModalTaskRequest = _taskApplication.GetRequestDetails(taskId),
TaskId = taskId,
Type = type,
Accounts = accounts,
TaskDetails = taskDetails,
};
return Partial("OperationRequestModal", model);
}
public IActionResult OnGetOperationRequestTaskDetailsAjax(long taskId)
{
var taskDetails = _taskApplication.GetRequestDetails(taskId);
return new JsonResult(new
{
data = taskDetails
});
}
// برای باز کردن مودال جزئیات تسک مثل عنوان، تاریخ، توضیحات و ...
public IActionResult OnGetDetailsTask(long taskId)
{
if (_authHelper.GetPermissions().Any(x => x == 90113))
{
var model = _taskApplication.GetDetails(taskId);
model.AssignsLists = _accountApplication.AccountsForAssign(taskId);
if (model.TicketViewModel != null)
return Partial("DetailsTaskTicketsModal", model);
return Partial("DetailsModal", model);
}
else
{
return Forbid();
}
}
#region Show and Download Files
public IActionResult OnGetGetFile(string filePath, long id)
{
if (string.IsNullOrEmpty(filePath))
{
return NotFound();
}
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
var contentType = GetContentTypeImage(Path.GetExtension(filePath));
if (string.IsNullOrWhiteSpace(contentType))
{
byte[] fileContent = System.IO.File.ReadAllBytes(filePath);
var extension = GetContentType(Path.GetExtension(filePath));
return File(fileContent, extension, $"Task_{id}{Path.GetExtension(filePath)}");
}
else
{
return PhysicalFile(filePath, contentType);
}
}
public IActionResult OnGetShowVoice(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return NotFound();
}
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
byte[] voiceContent = System.IO.File.ReadAllBytes(filePath);
return File(voiceContent, "audio/ogg", $"Task_voice.ogg");
}
public IActionResult OnGetShowPicture(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return NotFound();
}
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
var contentType = GetContentTypeImage(Path.GetExtension(filePath));
return PhysicalFile(filePath, contentType);
}
private string GetContentTypeImage(string extension)
{
if (!extension.StartsWith("."))
{
extension = "." + extension;
}
return extension.ToLower() switch
{
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".png" => "image/png",
".gif" => "image/gif",
".bmp" => "image/bmp",
".svg" => "image/svg+xml",
".ico" => "image/x-icon",
_ => "",
};
}
public static string GetContentType(string extension)
{
// Ensure the extension starts with a dot
if (!extension.StartsWith("."))
{
extension = "." + extension;
}
// Use switch expression to return the appropriate content type
return extension.ToLower()
switch
{
".txt" => "text/plain",
".htm" => "text/html",
".html" => "text/html",
".css" => "text/css",
".js" => "application/javascript",
".json" => "application/json",
".xml" => "application/xml",
".pdf" => "application/pdf",
".zip" => "application/zip",
".rar" => "application/x-rar-compressed",
".tar" => "application/x-tar",
".mp3" => "audio/mpeg",
".wav" => "audio/wav",
".mp4" => "video/mp4",
".avi" => "video/x-msvideo",
".doc" => "application/msword",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xls" => "application/vnd.ms-excel",
".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".ppt" => "application/vnd.ms-powerpoint",
".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
_ => "application/octet-stream",
};
}
#endregion
public IActionResult OnPostRemoveTask(long taskId)
{
if (_authHelper.GetPermissions().Any(x => x == 90115))
{
var res = _taskApplication.RemoveTask(taskId);
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
else
{
return Forbid();
}
}
// برای تغییر تاریخ درخواست تسک
public IActionResult OnPostChangeTime(string time, long taskId, long assignedId, string message)
{
var res = _taskApplication.ChangeRequestTimeAndAccept(time, taskId, assignedId, message.Replace("\n", "<br>"));
return new JsonResult(new
{
isSuccess = res.IsSuccedded,
message = res.Message,
});
}
public IActionResult OnGetEditTask(long taskId)
{
if (_authHelper.GetPermissions().Any(x => x == 90114))
{
var model = new EditTaskPartialViewModel()
{
Id = _authHelper.CurrentAccountId(),
AccountList = _accountApplication.GetAccountEqualToLowerPositionValue().Select(x => new AccountViewModel()
{
Id = x.Id,
Fullname = x.Fullname
}).ToList(),
EditTask = _taskApplication.GetDetails(taskId)
};
return Partial("Edit", model);
}
else
{
return Forbid();
}
//var index = 1;
//var medias = model.EditTask.medias.Where(x => x.Category == "فایل").ToList();
//foreach (var media in medias)
//{
// switch (index)
// {
// case 1:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document1 = new FormFile(stream, 0, stream.Length, $"{taskId}_1",
// Path.GetFileName(stream.Name));
// }
// break;
// case 2:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document2 = new FormFile(stream, 0, stream.Length, $"{taskId}_2",
// Path.GetFileName(stream.Name));
// }
// break;
// case 3:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document3 = new FormFile(stream, 0, stream.Length, $"{taskId}_3",
// Path.GetFileName(stream.Name));
// }
// break;
// case 4:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document4 = new FormFile(stream, 0, stream.Length, $"{taskId}_4",
// Path.GetFileName(stream.Name));
// }
// break;
// case 5:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document5 = new FormFile(stream, 0, stream.Length, $"{taskId}_5",
// Path.GetFileName(stream.Name));
// }
// break;
// case 6:
// using (var stream = System.IO.File.OpenRead(media.Path))
// {
// model.EditTask.Document6 = new FormFile(stream, 0, stream.Length, $"{taskId}_6",
// Path.GetFileName(stream.Name));
// }
// break;
// }
// index++;
//}
//var voice = model.EditTask.medias.FirstOrDefault(x => x.Category == "صوت");
//if (voice != null)
//{
// using (var stream = System.IO.File.OpenRead(voice.Path))
// {
// model.EditTask.Voice = new FormFile(stream, 0, stream.Length, $"{taskId}_v",
// Path.GetFileName(stream.Name));
// }
//}
}
public IActionResult OnPostEditSaveTask(EditTaskPartialViewModel command)
{
var description = command.EditTask.Description?.Replace("\n", "<br>");
var editCommand = new EditTask()
{
CompleteDescription = command.EditTask.CompleteDescription,
ContractingPartyName = command.EditTask.ContractingPartyName,
Description = description,
UploadedVoiceMedia = command.EditTask.UploadedVoiceMedia,
UploadedMedia = command.EditTask.UploadedMedia,
EndTaskDate = command.EditTask.EndTaskDate,
EndTaskTime = command.EditTask.EndTaskTime,
Id = command.EditTask.Id,
ReceiverId = command.EditTask.ReceiverId,
IsDone = command.EditTask.IsDone,
PositionId = command.EditTask.PositionId,
SenderId = command.EditTask.SenderId,
Title = command.EditTask.Title,
medias = command.EditTask.medias,
DeletedFileIds = command.EditTask.DeletedFileIds,
};
var result = _taskApplication.Edit(editCommand);
var res = result.IsSuccedded;
return new JsonResult(new
{
isSuccess = res,
message = result.Message,
});
}
public IActionResult OnGetEmployeeList()
{
var result = _accountApplication.GetAccountEqualToLowerPositionValue();
return new JsonResult(new { result = result });
}
public SelectList AccountsList { get; set; }
public List<PositionViewModel> PositionViewModels { get; set; }
public long Id { get; set; }
public string DateFa;
// برای باز کردن مودال ایجاد تسک
public IActionResult OnGetCreateTaskModal()
{
var positionValue = _authHelper.CurrentAccountInfo().PositionValue == null
? 0
: _authHelper.CurrentAccountInfo().PositionValue;
if (positionValue > 0)
{
if (_authHelper.GetPermissions().Any(x => x == 903))
{
var model = new CreateTaskModal()
{
AccountsList = _accountApplication.GetAccountEqualToLowerPositionValue(),
PositionViewModels = _positionApplication.GetLowerPosition(),
DateFa = DateTime.Now.ToFarsi(),
Id = _authHelper.CurrentAccountId()
};
return Partial("CreateModal", model);
}
else
{
return Forbid();
}
}
else
{
return Forbid();
}
}
public IActionResult OnGetChatMessageDetailsTask(long assignId, long taskId)
{
var result = _taskApplication.GetTaskMessages(assignId);
var taskDetails = taskId > 0 ? _taskApplication.GetRequestDetails(taskId).AssignViewModels.First(x => x.Id == assignId) : null;
return new JsonResult(new
{
data = result,
taskDetailsData = taskDetails
});
}
public IActionResult OnGetDiagramDetailsTask(long taskId)
{
var model = _taskApplication.GetDetails(taskId);
model.AssignViewModels = _taskApplication.GetAssignsByTaskId(taskId);
return Partial("DiagramTaskModal", model);
}
#region Tickets
public IActionResult OnPostSaveAdminResponseTicket(ResponseTicket command)
{
command.AdminId = _authHelper.CurrentAccountId();
command.Response = command.Response?.Replace("\n", "<br>");
var result = _ticketApplication.AdminResponseTicket(command);
return new JsonResult(new
{
isSuccedd = result.IsSuccedded,
message = result.Message
});
}
public IActionResult OnGetShowDetailTicketMessagesByAdminAjax(long ticketID)
{
var result = _ticketApplication.GetDetails(ticketID);
return new JsonResult(new
{
ticketDetail = result
});
}
public IActionResult OnPostUploadFile(IFormFile media)
{
var accountId = _authHelper.CurrentAccountId();
var operation = _ticketApplication.UploadMedia(media, accountId);
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
id = operation.SendId,
});
}
public IActionResult OnPostDeleteFile(long mediaId)
{
var operation = _ticketApplication.RemoveMedia(mediaId);
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
});
}
public IActionResult OnPostRemoveAllTempFiles()
{
var accId = _authHelper.CurrentAccountId();
var operation = new OperationResult();
_ticketApplication.RemoveTempUploadedFiles(accId);
operation.Succcedded();
return new JsonResult(new
{
IsSuccedded = operation.IsSuccedded,
message = operation.Message,
});
}
#endregion
}
}