1045 lines
39 KiB
C#
1045 lines
39 KiB
C#
using _0_Framework.Application;
|
|
using AccountManagement.Application.Contracts.Assign;
|
|
using AccountManagement.Application.Contracts.Task;
|
|
using AccountManagement.Application.Contracts.TaskMessage;
|
|
using AccountManagement.Domain.AccountAgg;
|
|
using AccountManagement.Domain.AdminResponseAgg;
|
|
using AccountManagement.Domain.AssignAgg;
|
|
using AccountManagement.Domain.MediaAgg;
|
|
using AccountManagement.Domain.PositionAgg;
|
|
using AccountManagement.Domain.TaskAgg;
|
|
using AccountManagement.Domain.TaskMessageAgg;
|
|
using AccountManagement.Domain.TicketAgg;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
|
|
namespace AccountManagement.Application;
|
|
public class TaskApplication : ITaskApplication
|
|
{
|
|
private readonly ITaskRepository _taskRepository;
|
|
private readonly IAccountRepository _accountRepository;
|
|
private readonly IMediaRepository _mediaRepository;
|
|
private readonly IPositionRepository _positionRepository;
|
|
private readonly IAssignRepository _assignRepository;
|
|
private readonly IHttpContextAccessor _contextAccessor;
|
|
private readonly ITaskMessageRepository _taskMessageRepository;
|
|
private readonly ITicketRepository _ticketRepository;
|
|
private readonly IAuthHelper _authHelper;
|
|
|
|
|
|
public TaskApplication(ITaskRepository taskRepository, IAccountRepository accountRepository, IMediaRepository mediaRepository, IAssignRepository assignRepository, IHttpContextAccessor contextAccessor, IPositionRepository positionRepository, IAuthHelper authHelper, ITaskMessageRepository taskMessageRepository, ITicketRepository ticketRepository)
|
|
{
|
|
_taskRepository = taskRepository;
|
|
_accountRepository = accountRepository;
|
|
_mediaRepository = mediaRepository;
|
|
_assignRepository = assignRepository;
|
|
_contextAccessor = contextAccessor;
|
|
_positionRepository = positionRepository;
|
|
_authHelper = authHelper;
|
|
_taskMessageRepository = taskMessageRepository;
|
|
_ticketRepository = ticketRepository;
|
|
}
|
|
//غیرفعال سازی تسک
|
|
public OperationResult DeActiveTask(long TaskId)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == TaskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای برای حذف وجود ندارد!");
|
|
}
|
|
|
|
var task = _taskRepository.Get(TaskId);
|
|
task.DeActive();
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
|
|
}
|
|
//حذف کامل تسک
|
|
public OperationResult RemoveTask(long TaskId)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == TaskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای برای حذف وجود ندارد!");
|
|
}
|
|
|
|
var task = _taskRepository.Get(TaskId);
|
|
var assigns = _assignRepository.GetAssignsByTaskId(TaskId);
|
|
var positionValue = int.Parse(_contextAccessor.HttpContext.User.FindFirst("PositionValue").Value);
|
|
var sender = _accountRepository.GetIncludePositions(task.SenderId);
|
|
if (sender.Position.PositionValue < positionValue)
|
|
{
|
|
return operation.Failed("شما نمیتواند وظیفه ای که سطحی از شما پایین تر دارد را حذف کنید");
|
|
}
|
|
var medias = _mediaRepository.GetMediaByTaskId(TaskId);
|
|
if (assigns.Any(x => x.IsCanceledRequest || x.IsDone || x.IsCancel || x.TimeRequest || x.AcceptedTimeRequest > 0))
|
|
{
|
|
task.DeActive();
|
|
}
|
|
else
|
|
{
|
|
foreach (var item in medias)
|
|
{
|
|
RemoveFile(item.Id);
|
|
}
|
|
_taskRepository.Remove(task.id);
|
|
|
|
}
|
|
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
//حذف فایلی در تسک
|
|
public OperationResult RemoveFile(long MediaId)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (!_mediaRepository.Exists(x => x.id == MediaId))
|
|
{
|
|
operation.Failed("چنین فایلی وجود ندارد");
|
|
}
|
|
var media = _mediaRepository.Get(MediaId);
|
|
File.Delete(media.Path);
|
|
_mediaRepository.Remove(media.id);
|
|
_mediaRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
//ویرایش تسک
|
|
public OperationResult Edit(EditTask command)
|
|
{
|
|
var posValue = int.Parse(_contextAccessor.HttpContext.User.FindFirst("PositionValue").Value);
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrEmpty(command.Title))
|
|
{
|
|
return operation.Failed("لطفا عنوان وظیفه خود ار وارد کنید");
|
|
}
|
|
if (string.IsNullOrEmpty(command.EndTaskDate))
|
|
{
|
|
return operation.Failed("لطفا تاریخ انجام وظیفه را وارد کنید");
|
|
}
|
|
if (command.ReceiverId.Count < 1)
|
|
{
|
|
return operation.Failed("طرف وظیفه خود را وارد کنید");
|
|
}
|
|
if (command.SenderId < 1)
|
|
{
|
|
return operation.Failed("خطای سیستمی!!!");
|
|
}
|
|
var sender = _accountRepository.GetIncludePositions(command.SenderId);
|
|
if (sender.Position.PositionValue < posValue)
|
|
{
|
|
return operation.Failed("شما حق ویرایش این وظیفه را ندارید");
|
|
}
|
|
var receivers = _accountRepository.GetAccountsByIds(command.ReceiverId);
|
|
|
|
if (sender.Position.PositionValue == 1)
|
|
{
|
|
|
|
}
|
|
|
|
else if (receivers.Any(x => sender.Position.PositionValue >= x.Position.PositionValue))
|
|
{
|
|
var checkReceiver = receivers.Where(x => sender.Position.PositionValue >= x.Position.PositionValue).ToList();
|
|
if (checkReceiver.All(x => x.id == sender.id))
|
|
{
|
|
|
|
}
|
|
else if (checkReceiver.Any(x => sender.Position.PositionValue > x.Position.PositionValue))
|
|
{
|
|
return operation.Failed("شما نمیتوانید به سطح بالاتر خود وظیفه ای دهید");
|
|
}
|
|
|
|
}
|
|
else if (receivers.Count == 1 && receivers.Any(x => sender.id == x.id))
|
|
{
|
|
|
|
}
|
|
var task = _taskRepository.Get(command.Id);
|
|
var endTask = command.EndTaskDate.ToGeorgianDateTime();
|
|
var errorDateTime = new DateTime(3000, 12, 20, new PersianCalendar());
|
|
if (endTask == errorDateTime)
|
|
{
|
|
return operation.Failed("لطفا تاریخ را به درستی وارد کنید");
|
|
}
|
|
|
|
task.Edit(command.Title, command.Description, command.SenderId, command.ContractingPartyName);
|
|
_taskRepository.SaveChanges();
|
|
_assignRepository.RemoveRangeAssigns(task.id);
|
|
foreach (var receiver in receivers)
|
|
{
|
|
var assign = new Assign(task.id, task.SenderId, receiver.id, sender.Position.PositionValue,
|
|
receiver.Fullname,
|
|
receiver.Position.PositionValue, endTask);
|
|
_assignRepository.Create(assign);
|
|
}
|
|
_assignRepository.SaveChanges();
|
|
|
|
#region ChangeMedias
|
|
|
|
|
|
#region SaveDocuments
|
|
|
|
if (command.UploadedMedia?.Count > 0)
|
|
{
|
|
foreach (var mediaId in command.UploadedMedia)
|
|
{
|
|
MoveTaskFile(mediaId, task.id);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
if (command.UploadedVoiceMedia > 0)
|
|
{
|
|
MoveTaskFile(command.UploadedVoiceMedia, task.id, "voice");
|
|
|
|
}
|
|
#region DeleteMedias
|
|
|
|
if (command.DeletedFileIds?.Count > 0)
|
|
{
|
|
foreach (var mediaId in command.DeletedFileIds)
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
_mediaRepository.Remove(media.id);
|
|
File.Delete(media.Path);
|
|
}
|
|
}
|
|
|
|
_mediaRepository.SaveChanges();
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
//ساخت یک ارجاع دهنده
|
|
public OperationResult CreateAssign(CreateAssign command)
|
|
{
|
|
var posValue = int.Parse(_contextAccessor.HttpContext.User.FindFirst("PositionValue").Value);
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == command.TaskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
if (!_accountRepository.Exists(x => x.id == command.AssignerId))
|
|
{
|
|
return operation.Failed("خطای سیستمی!!");
|
|
}
|
|
|
|
var assignEntity = _assignRepository.GetAssignByAssignedIdAndTaskId(command.AssignerId, command.TaskId);
|
|
var now = DateTime.Now;
|
|
if (assignEntity.EndTaskDate < now)
|
|
{
|
|
operation.Failed("شما نمیتوانید وظیفه ای که مهلت آن گذشته است را به کسی ارجاع دهید");
|
|
}
|
|
|
|
if (_assignRepository.Exists(x => x.TaskId == command.TaskId && command.AssignedId.Contains(x.AssignedId)))
|
|
{
|
|
var assignedIdsInDataBase = _assignRepository.GetAssignedIdsByTaskId(command.TaskId);
|
|
var similarIds = assignedIdsInDataBase.Intersect(command.AssignedId).ToList();
|
|
var assignedAccounts = _accountRepository.GetAccountsByIds(similarIds);
|
|
string accNames = "";
|
|
foreach (var account in assignedAccounts)
|
|
{
|
|
accNames = accNames + account.Fullname + "،";
|
|
|
|
}
|
|
return operation.Failed($"به اشخاص {accNames} قبلا این تسک ارجاع داده شده است");
|
|
}
|
|
|
|
|
|
foreach (var assignedId in command.AssignedId)
|
|
{
|
|
if (!_accountRepository.Exists(x => x.id == assignedId))
|
|
{
|
|
return operation.Failed("چنین شخصی برای ارجاع وجود ندارد.");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var task = _taskRepository.Get(command.TaskId);
|
|
|
|
var endOfDayDateTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
|
|
|
|
|
var assigner = _accountRepository.GetIncludePositions(command.AssignerId);
|
|
foreach (var assignedId in command.AssignedId)
|
|
{
|
|
var assigned = _accountRepository.GetIncludePositions(assignedId);
|
|
var assign = new Assign(command.TaskId, command.AssignerId, assignedId, assigner.Position.PositionValue,
|
|
assigned.Fullname, assigned.Position.PositionValue, endOfDayDateTime);
|
|
_assignRepository.Create(assign);
|
|
}
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
//ساخت تسک
|
|
public OperationResult CreateTask(CreateTask command)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrEmpty(command.Title))
|
|
{
|
|
return operation.Failed("لطفا عنوان وظیفه خود را وارد کنید");
|
|
}
|
|
|
|
var now = DateTime.Now.Date;
|
|
if (string.IsNullOrEmpty(command.EndTaskDate))
|
|
{
|
|
|
|
return operation.Failed("لطفا تاریخ انجام وظیفه را وارد کنید");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(command.ContractingPartyName))
|
|
{
|
|
return operation.Failed("لطفا طرف حساب خود را وارد کنید");
|
|
}
|
|
|
|
|
|
if (command.SenderId < 1)
|
|
{
|
|
return operation.Failed("خطای سیستمی!!!");
|
|
}
|
|
|
|
var sender = _accountRepository.GetIncludePositions(command.SenderId);
|
|
|
|
if (command.PositionId?.Count > 0 && command.ReceiverId?.Count > 0)
|
|
{
|
|
if (command.PositionId.Any(x => x > 0))
|
|
{
|
|
return operation.Failed("شما نمیتوانید همرمان به صورت انفرادی و هم به صورت گروهی تسک دهید. ");
|
|
}
|
|
}
|
|
else if (command.PositionId?.Count > 0)
|
|
{
|
|
var res = CreateTaskByPosition(command);
|
|
return res;
|
|
}
|
|
if (command.ReceiverId.Count < 1)
|
|
{
|
|
return operation.Failed("طرف وظیفه خود را وارد کنید");
|
|
}
|
|
|
|
var receivers = _accountRepository.GetAccountsByIds(command.ReceiverId);
|
|
|
|
if (sender.Position.PositionValue == 1)
|
|
{
|
|
|
|
}
|
|
|
|
else if (receivers.Any(x => sender.Position.PositionValue >= x.Position.PositionValue))
|
|
{
|
|
var checkReceiver = receivers.Where(x => sender.Position.PositionValue >= x.Position.PositionValue).ToList();
|
|
if (checkReceiver.All(x => x.id == sender.id))
|
|
{
|
|
|
|
}
|
|
else if (checkReceiver.Any(x => sender.Position.PositionValue > x.Position.PositionValue))
|
|
{
|
|
return operation.Failed("شما نمیتوانید به سطح بالاتر خود یا هم سطح خود وظیفه ای دهید");
|
|
}
|
|
|
|
}
|
|
else if (receivers.Count == 1 && receivers.Any(x => sender.id == x.id))
|
|
{
|
|
|
|
}
|
|
var endTask = string.IsNullOrWhiteSpace(command.EndTaskTime) || command.EndTaskTime == "00:00" ? command.EndTaskDate.ToEndDayOfGeorgianDateTime() : command.EndTaskDate.ToGeorgianDateWithTime(command.EndTaskTime);
|
|
|
|
var errorDateTime = new DateTime(3000, 12, 20, new PersianCalendar());
|
|
if (endTask == errorDateTime)
|
|
{
|
|
return operation.Failed("لطفا تاریخ را به درستی وارد کنید");
|
|
}
|
|
|
|
if (endTask.Date < now)
|
|
{
|
|
return operation.Failed("لطفا تاریخ آینده را وارد کنید");
|
|
}
|
|
var task = new Tasks(command.Title, command.Description, command.SenderId, command.ContractingPartyName);
|
|
_taskRepository.Create(task);
|
|
if (command.TaskScheduleId > 0)
|
|
{
|
|
task.SetTaskSchedule(command.TaskScheduleId);
|
|
}
|
|
_assignRepository.SaveChanges();
|
|
foreach (var receiver in receivers)
|
|
{
|
|
var assign = new Assign(task.id, task.SenderId, receiver.id, sender.Position.PositionValue, receiver.Fullname,
|
|
receiver.Position.PositionValue, endTask, true);
|
|
_assignRepository.Create(assign);
|
|
}
|
|
|
|
#region SaveMedias
|
|
|
|
#region SaveDocuments
|
|
|
|
if (command.UploadedMedia?.Count > 0)
|
|
{
|
|
foreach (var mediaId in command.UploadedMedia)
|
|
{
|
|
MoveTaskFile(mediaId, task.id);
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
if (command.UploadedVoiceMedia > 0)
|
|
{
|
|
MoveTaskFile(command.UploadedVoiceMedia, task.id, "voice");
|
|
}
|
|
|
|
#endregion
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
|
|
public OperationResult CreateTaskByPosition(CreateTask command)
|
|
{
|
|
var operation = new OperationResult();
|
|
var sender = _accountRepository.GetIncludePositions(command.SenderId);
|
|
|
|
var receivers = _positionRepository.GetAccountsByPositionIds(command.PositionId);
|
|
|
|
|
|
if (sender.Position.PositionValue == 1)
|
|
{
|
|
|
|
}
|
|
else if (receivers.Any(x => sender.Position.PositionValue > x.Position.PositionValue))
|
|
{
|
|
return operation.Failed("شما نمیتوانید به سطح بالاتر خود وظیفه ای دهید");
|
|
}
|
|
else if (receivers.Count == 1 && receivers.Any(x => sender.id == x.id))
|
|
{
|
|
|
|
}
|
|
DateTime endTask;
|
|
if (string.IsNullOrWhiteSpace(command.EndTaskTime))
|
|
{
|
|
endTask = command.EndTaskDate.ToEndDayOfGeorgianDateTime();
|
|
}
|
|
else
|
|
{
|
|
endTask = command.EndTaskDate.ToGeorgianDateWithTime(command.EndTaskTime);
|
|
}
|
|
|
|
var task = new Tasks(command.Title, command.Description, command.SenderId, command.ContractingPartyName);
|
|
_taskRepository.Create(task);
|
|
_assignRepository.SaveChanges();
|
|
foreach (var receiver in receivers)
|
|
{
|
|
var assign = new Assign(task.id, task.SenderId, receiver.id, sender.Position.PositionValue, receiver.Fullname,
|
|
receiver.Position.PositionValue, endTask);
|
|
_assignRepository.Create(assign);
|
|
}
|
|
|
|
#region SaveMedias
|
|
|
|
#region SaveDocuments
|
|
|
|
if (command.UploadedMedia?.Count > 0)
|
|
{
|
|
foreach (var mediaId in command.UploadedMedia)
|
|
{
|
|
MoveTaskFile(mediaId, task.id);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
if (command.UploadedVoiceMedia > 0)
|
|
{
|
|
MoveTaskFile(command.UploadedVoiceMedia, task.id, "voice");
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
|
|
// جزئیات تسک
|
|
public EditTask GetDetails(long taskId)
|
|
{
|
|
return _taskRepository.GetDetails(taskId);
|
|
}
|
|
|
|
|
|
|
|
public List<TaskViewModel> GetSelfTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetSelfTasks(searchModel);
|
|
}
|
|
|
|
public List<TaskViewModel> GetAllTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetAllTasks(searchModel);
|
|
}
|
|
|
|
//public List<TaskViewModel> GetAllNotSelfIncludedTasks(TaskSearchModel searchModel)
|
|
//{
|
|
// return _taskRepository.GetAllNotSelfIncludedTasks(searchModel);
|
|
//}
|
|
|
|
public List<TaskViewModel> GetReceivedTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetReceivedTasks(searchModel);
|
|
}
|
|
|
|
public List<TaskViewModel> GetSentTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetSentTasks(searchModel);
|
|
}
|
|
|
|
public List<TaskViewModel> GetTasksHaveTicket(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetTasksHaveTicket(searchModel);
|
|
}
|
|
|
|
//ساخت درخواست مهلت
|
|
public OperationResult CreateRequestTime(CreateTaskTimeRequest command)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (command.TaskId == 0)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(command.Description))
|
|
{
|
|
return operation.Failed("توضیحات خود را وارد کنید");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(command.RequestTime))
|
|
{
|
|
return operation.Failed("مهلت درخواستی خود را وارد کنید");
|
|
}
|
|
|
|
|
|
var task = _taskRepository.GetIncludeAssign(command.TaskId);
|
|
var requestTime = command.RequestTime.ToGeorgianDateTime();
|
|
var errorDateTime = new DateTime(3000, 12, 20, new PersianCalendar());
|
|
if (requestTime == errorDateTime)
|
|
{
|
|
return operation.Failed("لطفا تاریخ را به درستی وارد کنید");
|
|
}
|
|
if (task.SenderId == accountId)
|
|
{
|
|
foreach (var assign in task.Assigns)
|
|
{
|
|
assign.ChangeTimeTask(requestTime);
|
|
var message = new TaskMessage(command.Description, "تغییر تاریخ", assign.id);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.AssignedId, message.id);
|
|
}
|
|
_taskMessageRepository.SaveChanges();
|
|
|
|
}
|
|
else
|
|
{
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(accountId, command.TaskId);
|
|
assign.CreateTimeRequest(requestTime, command.Description);
|
|
var message = new TaskMessage(command.Description, "درخواست مهلت", assign.id, command.RequestTime);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.Task.SenderId, message.id);
|
|
|
|
}
|
|
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
|
|
public List<TaskViewModel> GetRequestTaskHasTicket(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetRequestTaskHasTicket(searchModel);
|
|
}
|
|
|
|
//تایید درخواست مهلت
|
|
public OperationResult AcceptRequestDatetime(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.TimeRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای مهلت ندارد");
|
|
}
|
|
|
|
if (assign.RequestDate == null)
|
|
{
|
|
return operation.Failed("تاریخی برای درخواست وظیفه ثبت نشده است");
|
|
}
|
|
assign.AcceptTimeRequest();
|
|
message = string.IsNullOrWhiteSpace(message) ? "درخواست شما مورد تایید قرار گرفت" : message;
|
|
var messageEntity = new TaskMessage(message, "تایید درخواست مهلت", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
//لغو درخواست مهلت تسک
|
|
public OperationResult RejectTimeRequest(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.TimeRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای مهلت ندارد");
|
|
}
|
|
assign.RejectTimeRequest();
|
|
if (assign.EndTaskDate.Date <= DateTime.Now.Date)
|
|
{
|
|
var now = DateTime.Now;
|
|
var endOfDay = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
|
assign.EditTime(endOfDay);
|
|
}
|
|
|
|
var messageEntity = new TaskMessage(message, "رد درخواست مهلت", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
|
|
//ساخت درخواست کنسل
|
|
public OperationResult CreateCancelRequest(CreateTaskCancel command)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Description))
|
|
{
|
|
return operation.Failed("توضیحات خود را وارد کنید");
|
|
}
|
|
|
|
if (!_taskRepository.Exists(x => x.id == command.TaskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var task = _taskRepository.GetIncludeAssign(command.TaskId);
|
|
|
|
|
|
|
|
if (task.SenderId == accountId)
|
|
{
|
|
foreach (var assign in task.Assigns)
|
|
{
|
|
assign.CreateCancelRequest(command.Description);
|
|
assign.AcceptCancelRequest();
|
|
var message = new TaskMessage(command.Description, "لغو", assign.id);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.AssignedId, message.id);
|
|
|
|
}
|
|
_taskMessageRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(accountId, task.id);
|
|
assign.CreateCancelRequest(command.Description);
|
|
var message = new TaskMessage(command.Description, "درخواست کنسل", assign.id);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.Task.SenderId, message.id);
|
|
}
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
//تایید درخواست کنسل
|
|
public OperationResult AcceptCancelRequest(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.IsCanceledRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای انصراف ندارد");
|
|
}
|
|
assign.AcceptCancelRequest();
|
|
message = string.IsNullOrWhiteSpace(message) ? "درخواست شما مورد تایید قرار گرفت" : message;
|
|
var messageEntity = new TaskMessage(message, "تایید درخواست کنسل", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
//لغو درخواست کنسل تسک
|
|
public OperationResult RejectCancelRequest(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.IsCanceledRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای انصراف ندارد");
|
|
}
|
|
assign.RejectCancel();
|
|
if (assign.EndTaskDate.Date <= DateTime.Now.Date)
|
|
{
|
|
var now = DateTime.Now;
|
|
var endOfDay = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
|
assign.EditTime(endOfDay);
|
|
}
|
|
var messageEntity = new TaskMessage(message, "رد درخواست کنسل", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
|
|
//انجام شدن تسک
|
|
public OperationResult CreateCompleteTaskRequest(CompleteTaskViewModel command)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == command.TaskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var task = _taskRepository.GetIncludeAssign(command.TaskId);
|
|
|
|
if (task.SenderId == accountId)
|
|
{
|
|
command.Description = string.IsNullOrWhiteSpace(command.Description) ? "وظیفه محولشده توسط ارسال کننده به پایان رسیده است." : command.Description;
|
|
}
|
|
else
|
|
{
|
|
command.Description = string.IsNullOrWhiteSpace(command.Description) ? "وظیفه محول شده با موفقیت به اتمام رسید." : command.Description;
|
|
}
|
|
|
|
if (task.SenderId == accountId)
|
|
{
|
|
foreach (var assign in task.Assigns)
|
|
{
|
|
assign.CompleteRequest(command.Description);
|
|
assign.Completed();
|
|
var message = new TaskMessage(command.Description, "تکمیل", assign.id);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.AssignedId, message.id);
|
|
|
|
}
|
|
_taskMessageRepository.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(accountId, task.id);
|
|
assign.CompleteRequest(command.Description);
|
|
var message = new TaskMessage(command.Description, "درخواست انجام", assign.id);
|
|
_taskMessageRepository.Create(message);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assign.Task.SenderId, message.id);
|
|
}
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
|
|
public OperationResult AcceptCompleteRequest(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.IsDoneRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای تایید ندارد");
|
|
}
|
|
assign.Completed();
|
|
message = string.IsNullOrWhiteSpace(message) ? "درخواست شما مورد تایید قرار گرفت" : message;
|
|
var messageEntity = new TaskMessage(message, "تایید درخواست انجام", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
|
|
public OperationResult RejectCompleteRequest(long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.IsDoneRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای انصراف ندارد");
|
|
}
|
|
assign.RejectCompleteRequest();
|
|
if (assign.EndTaskDate.Date <= DateTime.Now.Date)
|
|
{
|
|
var now = DateTime.Now;
|
|
var endOfDay = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
|
|
assign.EditTime(endOfDay);
|
|
}
|
|
var messageEntity = new TaskMessage(message, "رد درخواست انجام", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_assignRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
|
|
|
|
|
|
|
|
public OperationResult CreateTaskByPosition(CreateTask command, List<long> positionIds)
|
|
{
|
|
var operation = new OperationResult();
|
|
var positionValue = int.Parse(_contextAccessor.HttpContext.User.FindFirst("PositionValue").Value);
|
|
var positions = new List<Position>();
|
|
foreach (var positionId in positionIds)
|
|
{
|
|
var position = _positionRepository.Get(positionId);
|
|
positions.Add(position);
|
|
}
|
|
|
|
|
|
if (positions.Any(x => x.PositionValue < positionValue))
|
|
{
|
|
return operation.Failed("شما نمیتوانید به سطح بالاتر خود تسکی دهد");
|
|
}
|
|
|
|
var receiverIds = new List<long>();
|
|
foreach (var positionId in positionIds)
|
|
{
|
|
var acc = _accountRepository.GetAccountsByPositionId(positionId).Select(x => x.Id).ToList();
|
|
receiverIds.AddRange(acc);
|
|
}
|
|
|
|
command.ReceiverId = receiverIds;
|
|
operation = CreateTask(command);
|
|
return operation;
|
|
}
|
|
|
|
public List<TaskViewModel> GetRequestedTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.GetRequestedTasks(searchModel);
|
|
}
|
|
|
|
public List<TaskViewModel> AllRequestedTasks(TaskSearchModel searchModel)
|
|
{
|
|
return _taskRepository.AllRequestedTasks(searchModel);
|
|
}
|
|
|
|
public int TasksHaveTicketCounts(long userId)
|
|
{
|
|
return _taskRepository.TasksHaveTicketCounts(userId);
|
|
}
|
|
|
|
public int TasksHaveTicketRequestsCount(long userId)
|
|
{
|
|
return _taskRepository.TasksHaveTicketRequestsCount(userId);
|
|
}
|
|
|
|
|
|
public List<TaskMessageViewModel> GetTaskMessages(long assignId)
|
|
{
|
|
return _taskMessageRepository.GetTaskMessages(assignId);
|
|
}
|
|
|
|
public int GetRequestedTasksCount()
|
|
{
|
|
return _taskRepository.GetRequestedTasksCount();
|
|
}
|
|
|
|
public OperationResult ChangeRequestTimeAndAccept(string time, long taskId, long assignedId, string message)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
if (!_taskRepository.Exists(x => x.id == taskId))
|
|
{
|
|
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
|
}
|
|
|
|
var assign = _assignRepository.GetAssignByAssignedIdAndTaskId(assignedId, taskId);
|
|
if (!assign.TimeRequest)
|
|
{
|
|
return operation.Failed("چنین وظیفه ای درخواستی برای مهلت ندارد");
|
|
}
|
|
|
|
if (assign.RequestDate == null)
|
|
{
|
|
return operation.Failed("تاریخی برای درخواست وظیفه ثبت نشده است");
|
|
}
|
|
|
|
DateTime timeGr = time.ToEndDayOfGeorgianDateTime();
|
|
if (timeGr.IsInvalidDateTime())
|
|
{
|
|
return operation.Failed("لطفا تاریخ خود را به درستی وارد کنید");
|
|
}
|
|
message = string.IsNullOrWhiteSpace(message) ? "درخواست شما مورد تایید قرار گرفت" : message;
|
|
assign.ChangeTimeTask(timeGr);
|
|
var messageEntity = new TaskMessage(message, "تایید و تغییر درخواست کنسل", assign.id);
|
|
_taskMessageRepository.Create(messageEntity);
|
|
_taskMessageRepository.SaveChanges();
|
|
_taskMessageRepository.CreateTaskMessageItems(accountId, assignedId, messageEntity.id);
|
|
_taskRepository.SaveChanges();
|
|
return operation.Succcedded(taskId);
|
|
}
|
|
|
|
public EditTask GetRequestDetails(long id)
|
|
{
|
|
return _taskRepository.GetRequestDetails(id);
|
|
}
|
|
public OperationResult UploadMedia(IFormFile mediaFile, long senderId)
|
|
{
|
|
var operation = new OperationResult();
|
|
if ((mediaFile.Length > 50000000))
|
|
return operation.Failed("حجم فایل نمیتواند از 50 مگابایت بیشتر باشد");
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"temp", $"{senderId}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
string uniqueFileName = $"{Path.GetFileNameWithoutExtension(mediaFile.FileName)}_{DateTime.Now.Ticks}{Path.GetExtension(mediaFile.FileName)}";
|
|
string filepath = Path.Combine(path, uniqueFileName);
|
|
|
|
using (var stream = new FileStream(filepath, FileMode.Create))
|
|
{
|
|
mediaFile.CopyTo(stream);
|
|
|
|
}
|
|
var type = Path.GetExtension(filepath);
|
|
var media = new Media(filepath, type, "فایل", "Task");
|
|
_mediaRepository.Create(media);
|
|
_mediaRepository.SaveChanges();
|
|
return operation.Succcedded(media.id);
|
|
|
|
}
|
|
|
|
public OperationResult RemoveMedia(long mediaId)
|
|
{
|
|
var operation = new OperationResult();
|
|
try
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var path = media.Path;
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
_mediaRepository.Remove(mediaId);
|
|
return operation.Succcedded();
|
|
}
|
|
catch
|
|
{
|
|
return operation.Failed("مشکلی در حذف این فایل وجود دارد");
|
|
}
|
|
|
|
}
|
|
|
|
public void RemoveTempUploadedFiles(long userId)
|
|
{
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"temp", $"{userId}");
|
|
|
|
|
|
if (Directory.Exists(path))
|
|
{
|
|
|
|
Directory.Delete(path, true);
|
|
}
|
|
|
|
}
|
|
|
|
public OperationResult SendTicketResponseInTask(long assignId, string message, long ticketId)
|
|
{
|
|
var accountId = _authHelper.CurrentAccountId();
|
|
var operation = new OperationResult();
|
|
var assign = _assignRepository.GetIncludeTask(assignId);
|
|
if (ticketId != assign.Task.TicketId)
|
|
{
|
|
return operation.Failed("خطای سیستمی!");
|
|
|
|
}
|
|
|
|
var adminRes = new AdminResponse(message, ticketId, accountId);
|
|
_ticketRepository.CreateAdminResponse(adminRes);
|
|
_ticketRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public List<AssignViewModel> GetAssignsByTaskId(long taskId)
|
|
{
|
|
return _assignRepository.GetAssignsByTaskId(taskId);
|
|
}
|
|
|
|
public int RequestedAndOverdueTasksCount(long userId)
|
|
{
|
|
return _taskRepository.RequestedAndOverdueTasksCount(userId);
|
|
|
|
}
|
|
|
|
public int OverdueTasksCount(long userId)
|
|
{
|
|
return _taskRepository.OverdueTasksCount(userId);
|
|
}
|
|
|
|
//public OperationResult MoveDataFRomTaskToAssign()
|
|
//{
|
|
// return _taskRepository.MoveDataFRomTaskToAssign();
|
|
//}
|
|
|
|
public void MoveTaskFile(long mediaId, long taskId, string category = "file")
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Task", $"{taskId}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
string categoryMedia = category == "file" ? media.Category :
|
|
category == "voice" ? "صوت" : throw new InvalidDataException();
|
|
|
|
media.Edit(filepath, media.Type, categoryMedia);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateMediaWithTaskMedia(taskId, media.id);
|
|
}
|
|
} |