Files
Backend-Api/AccountMangement.Infrastructure.EFCore/Repository/AccountLeftworkRepository.cs
2024-10-05 19:27:04 +03:30

132 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using _0_Framework.Application;
using _0_Framework.InfraStructure;
using AccountManagement.Application.Contracts.Account;
using AccountManagement.Domain.AccountLeftWorkAgg;
using Company.Domain.LeftWorkAgg;
using Company.Domain.WorkingHoursItemsAgg;
using Company.Domain.WorkshopAccountAgg;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.EntityFrameworkCore;
namespace AccountMangement.Infrastructure.EFCore.Repository;
public class AccountLeftworkRepository : RepositoryBase<long, AccountLeftWork>, IAccountLeftworkRepository
{
private readonly AccountContext _accountContext;
private readonly IWorkshopAccountRepository _workshopAccountRepository;
private readonly IWorkshopApplication _workshopApplication;
public AccountLeftworkRepository(AccountContext accountContext, IWorkshopAccountRepository workshopAccountRepository, IWorkshopApplication workshopApplication) : base(accountContext)
{
_accountContext = accountContext;
_workshopAccountRepository = workshopAccountRepository;
_workshopApplication = workshopApplication;
}
public (string StartWorkFa, string LeftWorkFa) GetByAccountId(long accountId)
{
var initial = new DateTime(2150, 1, 1);
string start = "";
string end = "";
var res = _accountContext.AccountLeftWorks.Where(x => x.AccountId == accountId).OrderByDescending(x=>x.StartWorkGr).FirstOrDefault();
if (res != null)
{
start = res.StartWorkGr != initial ? res.StartWorkGr.ToFarsi() : "";
end = res.LeftWorkGr != initial ? res.LeftWorkGr.ToFarsi() : "";
}
return (start, end);
}
public List<WorkshopAccountlistViewModel> WorkshopList(long accountId)
{
var res = _workshopAccountRepository.GetList(accountId);
return res.Select(x => new WorkshopAccountlistViewModel
{
WorkshopId = x.WorkshopId,
WorkshopName = x.WorkshopName,
AccountId = x.AccountId,
ContractAndCheckout = x.ContractAndCheckout,
Insurance = x.Insurance,
Tax = x.Tax,
IsActiveSting = x.IsActiveSting,
}).ToList();
}
public List<WorkshopSelectList> GetAllWorkshops()
{
return this._workshopApplication.GetWorkshopAll().Select(x => new WorkshopSelectList()
{
Id = x.Id,
WorkshopFullName = x.WorkshopFullName
}).ToList();
}
public OperationResult CopyWorkshopToNewAccount(long currentAccountId, long newAccountId)
{
List<WorkshopAccountViewModel> accountViewModelList = new List<WorkshopAccountViewModel>();
OperationResult operationResult = new OperationResult();
List<WorkshopAccountViewModel> list =_workshopAccountRepository.GetList(currentAccountId);
List<long> newPrsonIds =_workshopAccountRepository.GetList(newAccountId).Select(x => x.WorkshopId).ToList();
if (list.Count <= 0)
return operationResult.Failed("پرسنل هیچ کارگاهی برای انتقال ندارد");
List<WorkshopAccountViewModel> source = newPrsonIds.Count <= 0 ? list : list.Where(x => !newPrsonIds.Contains(x.WorkshopId)).ToList();
try
{
var res = source.Select(x => new WorkshopAccountViewModel()
{
AccountId = newAccountId,
WorkshopId = x.WorkshopId,
Insurance = x.Insurance,
Tax = x.Tax,
ContractAndCheckout = x.ContractAndCheckout,
WorkshopName = x.WorkshopName,
IsActiveSting = "true"
}).ToList();
_workshopAccountRepository.CreateNewWorkshopAccountByList(res);
return operationResult.Succcedded();
}
catch (Exception ex)
{
return operationResult.Failed("خطا در انجام عملیات");
}
}
public OperationResult SaveWorkshopAccount(
List<WorkshopAccountlistViewModel> workshopAccountList,
string startDate,
string leftDate,
long accountId)
{
OperationResult operationResult = new OperationResult();
DateTime start = startDate.ToGeorgianDateTime();
DateTime leftWorkGr = new DateTime(2150, 1, 1);
if (!string.IsNullOrWhiteSpace(leftDate) && leftDate.Length == 10)
leftWorkGr = leftDate.ToGeorgianDateTime();
if (start > leftWorkGr)
return operationResult.Failed("تاریخ شروع بکار از تاریخ ترک کار بزرگتر است");
if (start == leftWorkGr)
return operationResult.Failed("تاریخ شروع بکار و ترک کار برابرند");
var oldLefts = _accountContext.AccountLeftWorks.Where(x => x.AccountId == accountId).ToList();
_accountContext.AccountLeftWorks.RemoveRange(oldLefts);
Create(new AccountLeftWork(start, leftWorkGr, accountId));
SaveChanges();
var workshopAccountViewModel = workshopAccountList.Select(x => new WorkshopAccountViewModel()
{
AccountId = x.AccountId,
WorkshopId = x.WorkshopId,
Insurance = x.Insurance,
ContractAndCheckout = x.ContractAndCheckout,
Tax = x.Tax,
IsActiveSting = x.IsActiveSting
}).ToList();
_workshopAccountRepository.SaveWorkshopAccount(workshopAccountViewModel);
return operationResult.Succcedded();
}
}