998 lines
35 KiB
C#
998 lines
35 KiB
C#
using _0_Framework.Application;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using AccountManagement.Application.Contracts.Assign;
|
||
using AccountManagement.Application.Contracts.Task;
|
||
using AccountManagement.Domain.AccountAgg;
|
||
using AccountManagement.Domain.AssignAgg;
|
||
using AccountManagement.Domain.MediaAgg;
|
||
using AccountManagement.Domain.TaskAgg;
|
||
using IPE.SmsIrClient.Models.Results;
|
||
using Microsoft.AspNetCore.Http;
|
||
using TaskManager.Domain.PositionAgg;
|
||
using System.Reflection;
|
||
|
||
|
||
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;
|
||
|
||
|
||
public TaskApplication(ITaskRepository taskRepository, IAccountRepository accountRepository, IMediaRepository mediaRepository, IAssignRepository assignRepository, IHttpContextAccessor contextAccessor, IPositionRepository positionRepository)
|
||
{
|
||
_taskRepository = taskRepository;
|
||
_accountRepository = accountRepository;
|
||
_mediaRepository = mediaRepository;
|
||
_assignRepository = assignRepository;
|
||
_contextAccessor = contextAccessor;
|
||
_positionRepository = positionRepository;
|
||
}
|
||
//غیرفعال سازی تسک
|
||
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 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 (task.IsCanceledRequest || task.IsDone || task.IsCancel || task.TimeRequest || task.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 task = _taskRepository.Get(command.Id);
|
||
var endTask = command.EndTaskDate.ToGeorgianDateTime();
|
||
task.Edit(command.Title, endTask, command.Description, command.SenderId);
|
||
|
||
|
||
#region ChangeMedias
|
||
|
||
#region SaveDocuments
|
||
|
||
if ((command.Document1?.Length > 2000000) || (command.Document2?.Length > 2000000) || (command.Document3?.Length > 2000000))
|
||
return operation.Failed("حجم فایل نمیتواند از 2 مگابایت بیشتر باشد");
|
||
|
||
if (command.Document1?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document1.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document1.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
if (command.Document2?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document2.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document2.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
if (command.Document3?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document3.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document3.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
if (command.Document4?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document4.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document4.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
if (command.Document5?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document5.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document5.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
if (command.Document6?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document6.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document6.CopyTo(stream);
|
||
|
||
}
|
||
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
if (command.Voice?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Voice.FileName);
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Voice.CopyTo(stream);
|
||
}
|
||
|
||
if (!_mediaRepository.Exists(x => x.Path == filepath))
|
||
{
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "صوت");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#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("خطای سیستمی!!");
|
||
}
|
||
|
||
foreach (var assignedId in command.AssignedId)
|
||
{
|
||
if (!_accountRepository.Exists(x => x.id == assignedId))
|
||
{
|
||
return operation.Failed("چنین شخصی برای ارجاع وجود ندارد.");
|
||
}
|
||
|
||
}
|
||
|
||
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);
|
||
_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("لطفا عنوان وظیفه خود ار وارد کنید");
|
||
}
|
||
|
||
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))
|
||
{
|
||
return operation.Failed("شما نمیتوانید به سطح بالاتر خود وظیفه ای دهید");
|
||
}
|
||
else if (receivers.Count == 1 && receivers.Any(x => sender.id == x.id))
|
||
{
|
||
|
||
}
|
||
else if (receivers.Any(x => sender.Position.PositionValue == x.Position.PositionValue))
|
||
{
|
||
return operation.Failed("شما نمیتوانید به هم سطح خود وظیفه ای دهید");
|
||
}
|
||
|
||
|
||
DateTime endTask;
|
||
endTask = string.IsNullOrWhiteSpace(command.EndTaskTime) ? command.EndTaskDate.ToGeorgianDateTime2() : command.EndTaskDate.ToGeorgianDateWithTime(command.EndTaskTime);
|
||
|
||
|
||
|
||
var task = new Tasks(command.Title, endTask, 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);
|
||
_assignRepository.Create(assign);
|
||
}
|
||
|
||
|
||
|
||
#region SaveMedias
|
||
|
||
#region SaveDocuments
|
||
|
||
if ((command.Document1?.Length > 2000000) || (command.Document2?.Length > 2000000) || (command.Document3?.Length > 2000000))
|
||
return operation.Failed("حجم فایل نمیتواند از 2 مگابایت بیشتر باشد");
|
||
|
||
if (command.Document1?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document1.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document1.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document2?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document2.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document2.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document3?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document3.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document3.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document4?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document4.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document4.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document5?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document5.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document5.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document6?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document6.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document6.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
if (command.Voice?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Voice.FileName);
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Voice.CopyTo(stream);
|
||
}
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "صوت");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
|
||
}
|
||
|
||
|
||
|
||
#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.GetAccountsByIds(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))
|
||
{
|
||
|
||
}
|
||
else if (receivers.Any(x => sender.Position.PositionValue == x.Position.PositionValue))
|
||
{
|
||
return operation.Failed("شما نمیتوانید به هم سطح خود وظیفه ای دهید");
|
||
}
|
||
|
||
|
||
DateTime endTask;
|
||
if (string.IsNullOrWhiteSpace(command.EndTaskTime))
|
||
{
|
||
endTask = command.EndTaskDate.ToGeorgianDateTime2();
|
||
}
|
||
else
|
||
{
|
||
endTask = command.EndTaskDate.ToGeorgianDateWithTime(command.EndTaskTime);
|
||
}
|
||
|
||
|
||
|
||
var task = new Tasks(command.Title, endTask, 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);
|
||
_assignRepository.Create(assign);
|
||
}
|
||
|
||
|
||
|
||
#region SaveMedias
|
||
|
||
#region SaveDocuments
|
||
|
||
if ((command.Document1?.Length > 2000000) || (command.Document2?.Length > 2000000) || (command.Document3?.Length > 2000000))
|
||
return operation.Failed("حجم فایل نمیتواند از 2 مگابایت بیشتر باشد");
|
||
|
||
if (command.Document1?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document1.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document1.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document2?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document2.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document2.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document3?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document3.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document3.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document4?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document4.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document4.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document5?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document5.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document5.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
if (command.Document6?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Document6.FileName);
|
||
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Document6.CopyTo(stream);
|
||
|
||
}
|
||
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "فایل");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
|
||
|
||
if (command.Voice?.Length > 0)
|
||
{
|
||
var path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "ServiceHost", "Storage",
|
||
"Task", $"{task.id}");
|
||
Directory.CreateDirectory(path);
|
||
|
||
string filepath = Path.Combine(path, command.Voice.FileName);
|
||
using (var stream = new FileStream(filepath, FileMode.Create))
|
||
{
|
||
command.Voice.CopyTo(stream);
|
||
}
|
||
var type = Path.GetExtension(filepath);
|
||
var media = new Media(filepath, type, "صوت");
|
||
_mediaRepository.Create(media);
|
||
_mediaRepository.SaveChanges();
|
||
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
||
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
|
||
// ویرایش تسک
|
||
public EditTask GetDetails(long taskId)
|
||
{
|
||
return _taskRepository.GetDetails(taskId);
|
||
}
|
||
|
||
//لیست کامل تسک ها
|
||
public List<TaskViewModel> GetTasks(TaskSearchModel searchModel)
|
||
{
|
||
var test = _taskRepository.GetTasks(searchModel);
|
||
|
||
return test;
|
||
}
|
||
//ساخت درخواست مهلت
|
||
public OperationResult CreateRequestTime(CreateTaskTimeRequest command)
|
||
{
|
||
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.Get(command.TaskId);
|
||
var requestTime = command.RequestTime.ToGeorgianDateTime();
|
||
task.CreateTimeRequest(requestTime, command.Description);
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
//تایید درخواست مهلت
|
||
public OperationResult AcceptRequestDatetime(long taskId)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (!_taskRepository.Exists(x => x.id == taskId))
|
||
{
|
||
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
||
}
|
||
|
||
var task = _taskRepository.Get(taskId);
|
||
if (!task.TimeRequest)
|
||
{
|
||
return operation.Failed("چنین وظیفه ای درخواستی برای مهلت ندارد");
|
||
}
|
||
|
||
if (task.RequestDate == null)
|
||
{
|
||
return operation.Failed("تاریخی برای درخواست وظیفه ثبت نشده است");
|
||
}
|
||
task.AcceptTimeRequest();
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
//ساخت درخواست کنسل
|
||
public OperationResult CreateCancelRequest(CreateTaskCancel command)
|
||
{
|
||
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.Get(command.TaskId);
|
||
task.CreateCancelRequest(command.Description);
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
//تایید درخواست کنسل
|
||
public OperationResult AcceptCancelRequest(long taskId)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (!_taskRepository.Exists(x => x.id == taskId))
|
||
{
|
||
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
||
}
|
||
var task = _taskRepository.Get(taskId);
|
||
if (!task.IsCanceledRequest)
|
||
{
|
||
return operation.Failed("چنین وظیفه ای درخواستی برای انصراف ندارد");
|
||
}
|
||
task.AcceptCancelRequest();
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(taskId);
|
||
}
|
||
//انجام شدن تسک
|
||
public OperationResult CompleteTask(CompleteTaskViewModel command)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (!_taskRepository.Exists(x => x.id == command.Id))
|
||
{
|
||
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
||
}
|
||
var task = _taskRepository.Get(command.Id);
|
||
task.Completed(command.Description);
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
//لغو درخواست کنسل تسک
|
||
public OperationResult RejectCancelRequest(long taskId)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (!_taskRepository.Exists(x => x.id == taskId))
|
||
{
|
||
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
||
}
|
||
var task = _taskRepository.Get(taskId);
|
||
if (!task.IsCanceledRequest)
|
||
{
|
||
return operation.Failed("چنین وظیفه ای درخواستی برای انصراف ندارد");
|
||
}
|
||
task.RejectCancel();
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(taskId);
|
||
}
|
||
//لغو درخواست مهلت تسک
|
||
public OperationResult RejectTimeRequest(long taskId)
|
||
{
|
||
var operation = new OperationResult();
|
||
if (!_taskRepository.Exists(x => x.id == taskId))
|
||
{
|
||
return operation.Failed("چنین وظیفه ای وجود ندارد");
|
||
}
|
||
|
||
var task = _taskRepository.Get(taskId);
|
||
if (!task.TimeRequest)
|
||
{
|
||
return operation.Failed("چنین وظیفه ای درخواستی برای مهلت ندارد");
|
||
}
|
||
task.RejectTimeRequest();
|
||
_taskRepository.SaveChanges();
|
||
return operation.Succcedded(task.id);
|
||
}
|
||
|
||
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> GetAllRequestedTasks(TaskSearchModel searchModel)
|
||
{
|
||
return _taskRepository.GetAllRequestedTasks(searchModel);
|
||
}
|
||
|
||
public int GetRequestedTasksCount()
|
||
{
|
||
return _taskRepository.GetRequestedTasksCount();
|
||
}
|
||
} |