365 lines
12 KiB
C#
365 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Sockets;
|
|
using _0_Framework.Application;
|
|
using AccountManagement.Application.Contracts.Task;
|
|
using AccountManagement.Application.Contracts.Ticket;
|
|
using AccountManagement.Domain.AccountAgg;
|
|
using AccountManagement.Domain.AdminResponseAgg;
|
|
using AccountManagement.Domain.AssignAgg;
|
|
using AccountManagement.Domain.ClientResponseAgg;
|
|
using AccountManagement.Domain.MediaAgg;
|
|
using AccountManagement.Domain.TaskAgg;
|
|
using AccountManagement.Domain.TicketAgg;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
namespace AccountManagement.Application;
|
|
|
|
public class TicketApplication : ITicketApplication
|
|
{
|
|
private readonly ITicketRepository _ticketRepository;
|
|
private readonly IAccountRepository _accountRepository;
|
|
private readonly ITaskRepository _taskRepository;
|
|
private readonly IAssignRepository _assignRepository;
|
|
private readonly IMediaRepository _mediaRepository;
|
|
|
|
public TicketApplication(ITicketRepository ticketRepository, IAccountRepository accountRepository, ITaskRepository taskRepository, IAssignRepository assignRepository, IMediaRepository mediaRepository)
|
|
{
|
|
_ticketRepository = ticketRepository;
|
|
_accountRepository = accountRepository;
|
|
_taskRepository = taskRepository;
|
|
_assignRepository = assignRepository;
|
|
_mediaRepository = mediaRepository;
|
|
}
|
|
|
|
public OperationResult CreateTicket(CreateTicket command)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.ContractingPartyName))
|
|
{
|
|
return operation.Failed("خطای سیستمی");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(command.Title))
|
|
{
|
|
return operation.Failed("لطفا عنوان را وارد کنید");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(command.Description))
|
|
{
|
|
return operation.Failed("لطفا توضیحات خودرا وارد کنید");
|
|
}
|
|
|
|
|
|
|
|
var ticket = new Ticket(command.Title, command.Description, command.SenderId, command.ContractingPartyName,
|
|
command.TicketType);
|
|
_ticketRepository.Create(ticket);
|
|
_ticketRepository.SaveChanges();
|
|
#region SaveDocuments
|
|
|
|
|
|
if (command.UploadedMediaIds?.Count>0)
|
|
{
|
|
foreach (var mediaId in command.UploadedMediaIds)
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Ticket", $"{ticket.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, media.Category);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateTicketMedia(ticket.id, media.id);
|
|
}
|
|
}
|
|
if (command.ScreenShotId > 0)
|
|
{
|
|
var media = _mediaRepository.Get(command.ScreenShotId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Ticket", $"{ticket.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(path));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, media.Category);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateTicketMedia(ticket.id, media.id);
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
#region SaveVoice
|
|
if (command.VoiceId > 0)
|
|
{
|
|
var media = _mediaRepository.Get(command.ScreenShotId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Ticket", $"{ticket.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(path));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, "صوت");
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateTicketMedia(ticket.id, media.id);
|
|
|
|
}
|
|
#endregion
|
|
|
|
_mediaRepository.SaveChanges();
|
|
return operation.Succcedded(ticket.id);
|
|
}
|
|
|
|
public OperationResult AssignTicket(CreateTask command, long ticketId)
|
|
{
|
|
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.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
|
|
{
|
|
return operation.Failed("شما نمیتوانید به سطح بالاتر خود یا هم سطح خود وظیفه ای دهید");
|
|
}
|
|
|
|
}
|
|
else if (receivers.Count == 1 && receivers.Any(x => sender.id == x.id))
|
|
{
|
|
|
|
}
|
|
|
|
|
|
var 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, ticketId);
|
|
_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.UploadedMedia?.Count>0)
|
|
{
|
|
foreach (var mediaId in command.UploadedMedia)
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Task", $"{task.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, media.Category);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
if (command.UploadedVoiceMedia > 0)
|
|
{
|
|
var media = _mediaRepository.Get(command.UploadedVoiceMedia);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"Task", $"{task.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, "صوت");
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateMediaWithTaskMedia(task.id, media.id);
|
|
|
|
}
|
|
#endregion
|
|
|
|
|
|
_taskRepository.SaveChanges();
|
|
|
|
return operation.Succcedded(task.id);
|
|
}
|
|
|
|
public OperationResult CompleteTicket(long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (!_ticketRepository.Exists(x => x.id == id))
|
|
{
|
|
return operation.Failed("چنین تیکتی وجود ندارد");
|
|
}
|
|
var ticket = _ticketRepository.Get(id);
|
|
ticket.Completed();
|
|
_ticketRepository.SaveChanges();
|
|
return operation.Succcedded(ticket.id);
|
|
}
|
|
|
|
public OperationResult AdminResponseTicket(ResponseTicket command)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Response))
|
|
{
|
|
return operation.Failed("لطفا پیغام خود را وارد کنبد");
|
|
}
|
|
|
|
var adminRes = new AdminResponse(command.Response, command.TicketId);
|
|
_ticketRepository.CreateAdminResponse(adminRes);
|
|
_ticketRepository.SaveChanges();
|
|
|
|
#region SaveDocuments
|
|
|
|
if (command.UploadedFileIds?.Count>0)
|
|
{
|
|
foreach (var mediaId in command.UploadedFileIds)
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"TicketAdminResponse", $"{adminRes.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, media.Category);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateAdminResponseMedia(adminRes.id, media.id);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
_mediaRepository.SaveChanges();
|
|
|
|
|
|
return operation.Succcedded(adminRes.id);
|
|
}
|
|
|
|
public OperationResult ClientResponseTicket(ResponseTicket command)
|
|
{
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Response))
|
|
{
|
|
return operation.Failed("لطفا پیغام خود را وارد کنبد");
|
|
}
|
|
|
|
var clientRes = new ClientResponse(command.Response, command.TicketId);
|
|
_ticketRepository.CreateClientResponse(clientRes);
|
|
_ticketRepository.SaveChanges();
|
|
#region SaveDocuments
|
|
|
|
if (command.UploadedFileIds?.Count>0)
|
|
{
|
|
foreach (var mediaId in command.UploadedFileIds)
|
|
{
|
|
var media = _mediaRepository.Get(mediaId);
|
|
var oldPath = media.Path;
|
|
var path = Path.Combine($"{_taskRepository.GetWebEnvironmentPath()}", "Storage",
|
|
"TicketClientResponse", $"{clientRes.id}");
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
string filepath = Path.Combine(path, Path.GetFileName(oldPath));
|
|
File.Move(oldPath, filepath);
|
|
|
|
media.Edit(filepath, media.Type, media.Category);
|
|
|
|
_mediaRepository.SaveChanges();
|
|
_mediaRepository.CreateClientResponseMedia(clientRes.id, media.id);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
_mediaRepository.SaveChanges();
|
|
return operation.Succcedded(clientRes.id);
|
|
}
|
|
|
|
public EditTicket GetDetails(long id)
|
|
{
|
|
return _ticketRepository.GetDetails(id);
|
|
}
|
|
|
|
public List<TicketViewModel> GetAll(TicketSearchModel searchModel)
|
|
{
|
|
return _ticketRepository.GetAll(searchModel);
|
|
}
|
|
|
|
public bool IsExist(long id)
|
|
{
|
|
return _ticketRepository.Exists(x => x.id == id);
|
|
}
|
|
} |