Files
Backend-Api/AccountManagement.Application/PositionApplication.cs

201 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using _0_Framework.Application;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Application.Contracts.Position;
using AccountManagement.Domain.AccountAgg;
using AccountManagement.Domain.AssignAgg;
using AccountManagement.Domain.PositionAgg;
namespace TaskManager.Application;
public class PositionApplication : IPositionApplication
{
private readonly IPositionRepository _positionRepository;
private readonly IAccountRepository _accountRepository;
private readonly IAccountApplication _accountApplication;
private readonly IAssignRepository _assignRepository;
public PositionApplication(IPositionRepository positionRepository, IAccountRepository accountRepository, IAccountApplication accountApplication, IAssignRepository assignRepository)
{
_positionRepository = positionRepository;
_accountRepository = accountRepository;
_accountApplication = accountApplication;
_assignRepository = assignRepository;
}
//ساخت پوزیشن
public OperationResult Create(CreatePosition command)
{
var operation = new OperationResult();
if (string.IsNullOrWhiteSpace(command.Name))
{
return operation.Failed("لطفا نام سمت را وارد کنید");
}
if (command.Value == 0)
{
return operation.Failed("لطفا سطح سمت را وارد کنید");
}
if (command.Value > 5)
{
return operation.Failed(" سطح سمت وارد شده معتبر نمیباشد");
}
if (_positionRepository.Exists(x => x.PositionValue == command.Value))
{
return operation.Failed("سطح انتخاب شده قبلا تعریف شده است");
}
var position = new Position(command.Name, command.Value);
_positionRepository.Create(position);
_positionRepository.SaveChanges();
return operation.Succcedded(position.id);
}
//ذخیره لیست پرسنل برای سمت خود
public OperationResult SaveAccountPosition(List<long> accountIds, long positionId)
{
var operation = new OperationResult();
if (!_positionRepository.Exists(x => x.id == positionId))
{
return operation.Failed("چنین سمتی وجود ندارد");
}
if (accountIds.Count < 1)
{
return operation.Failed("لطفا شخصی برای ذخیره انتخاب کنید");
}
foreach (var id in accountIds)
{
var account = _accountRepository.Get(id);
account.SetPosition(positionId);
}
_accountRepository.SaveChanges();
return operation.Succcedded(positionId);
}
public List<AccountViewModel> GetNoPositionAccounts()
{
return _positionRepository.GetNoPositionAccounts();
}
public List<AccountViewModel> GetAllPositionAccounts()
{
return _positionRepository.GetAllPositionAccounts();
}
public List<int> GetUnUsedPositionValues()
{
return _positionRepository.GetUnUsedPositionValues();
}
public OperationResult Remove(long id)
{
var operation = new OperationResult();
if (!_positionRepository.Exists(x => x.id == id))
{
return operation.Failed("گروه مورد نظر یافت نشد");
}
if (_accountApplication.GetAccountsByPositionId(id).Count > 0)
{
return operation.Failed("شما نمیتوانید گروهی که در آن شخصی وجود دارد را حذف کنید.");
}
var deActives = _accountRepository.GetAccountsDeactivePositionValue(id);
if (deActives?.Count > 0)
{
foreach (var acc in deActives)
{
var account = _accountRepository.Get(acc.Id);
account.RemovePosition();
}
}
_positionRepository.Remove(id);
_positionRepository.SaveChanges();
return operation.Succcedded();
}
//ویرایش پوزیشن
public OperationResult Edit(EditPosition command)
{
var operation = new OperationResult();
if (_positionRepository.Exists(x => x.id != command.Id && x.PositionName == command.Name))
{
return operation.Failed("سمتی با این نام وجود دارد");
}
if (_positionRepository.Exists(x => x.id != command.Id && x.PositionValue == command.Value))
{
return operation.Failed("سمتی با این سطح وجود دارد");
}
var position = _positionRepository.Get(command.Id);
position.Edit(command.Name, command.Value);
_positionRepository.SaveChanges();
return operation.Succcedded(command.Id);
}
//گرفتن تمامی پوزیشن ها برای نمایش
public List<PositionViewModel> GetPositions() =>
_positionRepository.GetPositions();
//ذخیره پوزیشن
public OperationResult Save()
{
_positionRepository.SaveChanges();
return new OperationResult().Succcedded();
}
//حذف یک شخص از یک گروه
public OperationResult RemoveAccountFromPosition(long positionId, long AccountId)
{
var operation = new OperationResult();
if (!_accountRepository.Exists(x => x.id == AccountId))
{
return operation.Failed("چنین شخصی وجود ندارد");
}
var account = _accountRepository.GetIncludePositions(AccountId);
if (account.Position.id != positionId)
{
return operation.Failed("چنین شخصی در این گروه وجود ندارد");
}
account.DeActivePosition();
_positionRepository.SaveChanges();
return operation.Succcedded();
}
//حذف یک لیستی از اشخاص از یک گروه
public OperationResult RemoveAccountListFromPosition(List<long> accountIdList, long PositionId)
{
var operation = new OperationResult();
foreach (var account in accountIdList)
{
var acc = _accountRepository.GetIncludePositions(account);
if (_assignRepository.Exists(x => x.AssignerId == acc.id || x.AssignedId == acc.id))
acc.DeActivePosition();
else
acc.RemovePosition();
}
_accountRepository.SaveChanges();
return operation.Succcedded();
}
public List<PositionViewModel> GetLowerPosition()
{
return _positionRepository.GetLowerPosition();
}
//گرفتن بالاترین سطح قابل ذخیره
public int GetLastPositionValue()
{
return _positionRepository.GetLastPositionValue();
}
public bool HasPositionValue(long accountId)
{
return _positionRepository.HasPositionValue(accountId);
}
}