115 lines
3.8 KiB
C#
115 lines
3.8 KiB
C#
using DatabaseCore;
|
|
using DatabaseCore.Models.Department;
|
|
using DepartmentBusinessLogic.BindingModels;
|
|
using DepartmentBusinessLogic.Interfaces;
|
|
using DepartmentBusinessLogic.ViewModels;
|
|
using ModuleTools.BusinessLogics;
|
|
using ModuleTools.Enums;
|
|
using ModuleTools.Models;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DepartmentDatabaseImplementation.Implementations
|
|
{
|
|
/// <summary>
|
|
/// Реализация IOrderSyncHistoryService
|
|
/// </summary>
|
|
public class OrderSyncHistoryService : IOrderSyncHistoryService
|
|
{
|
|
public async Task<OperationResultModel> CreateAsync(OrderSyncHistorySetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var entity = Mapper.MapToClass<OrderSyncHistorySetBindingModel, OrderSyncHistory>(model, true);
|
|
await context.OrderSyncHistories.AddAsync(entity);
|
|
await context.SaveChangesAsync();
|
|
return OperationResultModel.Success(Mapper.MapToClass<OrderSyncHistory, OrderSyncHistoryViewModel>(entity, true));
|
|
}
|
|
|
|
public async Task<OperationResultModel> DeleteAsync(OrderSyncHistoryGetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
using var transaction = context.Database.BeginTransaction();
|
|
try
|
|
{
|
|
var entity = context.OrderSyncHistories.FirstOrDefault(x => x.Id == model.Id);
|
|
if (entity == null)
|
|
{
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
}
|
|
|
|
context.OrderSyncHistories.Remove(entity);
|
|
await context.SaveChangesAsync();
|
|
|
|
var records = context.OrderSyncHistoryRecords.Where(x => x.OrderSyncHistoryId == model.Id);
|
|
context.OrderSyncHistoryRecords.RemoveRange(records);
|
|
await context.SaveChangesAsync();
|
|
|
|
await transaction.CommitAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await transaction.RollbackAsync();
|
|
throw;
|
|
}
|
|
|
|
return OperationResultModel.Success(true);
|
|
}
|
|
|
|
public async Task<OperationResultModel> ReadAsync(OrderSyncHistoryGetBindingModel model)
|
|
{
|
|
int countPages = 0;
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
// для одной записи
|
|
if (model.Id.HasValue)
|
|
{
|
|
var entity = context.OrderSyncHistories.FirstOrDefault(x => x.Id == model.Id.Value);
|
|
if (entity == null)
|
|
{
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
}
|
|
return OperationResultModel.Success(Mapper.MapToClass<OrderSyncHistory, OrderSyncHistoryViewModel>(entity, model.HaveRight));
|
|
}
|
|
|
|
var query = context.OrderSyncHistories.AsQueryable();
|
|
|
|
query = query.OrderByDescending(x => x.SyncDate);
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
if (model.PageNumber.HasValue && model.PageSize.HasValue)
|
|
{
|
|
countPages = (int)Math.Ceiling((double)query.Count() / model.PageSize.Value);
|
|
query = query
|
|
.Skip(model.PageSize.Value * model.PageNumber.Value)
|
|
.Take(model.PageSize.Value);
|
|
}
|
|
var result = new OrderSyncHistoryListViewModel
|
|
{
|
|
MaxCount = countPages,
|
|
List = query.Select(x => Mapper.MapToClass<OrderSyncHistory, OrderSyncHistoryViewModel>(x, model.HaveRight)).ToList()
|
|
};
|
|
|
|
return OperationResultModel.Success(result);
|
|
});
|
|
}
|
|
|
|
public async Task<OperationResultModel> UpdateAsync(OrderSyncHistorySetBindingModel model)
|
|
{
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
var entity = context.OrderSyncHistories.FirstOrDefault(x => x.Id == model.Id);
|
|
if (entity == null)
|
|
{
|
|
return OperationResultModel.Error("Error:", "Элемент не найден", ResultServiceStatusCode.NotFound);
|
|
}
|
|
entity = Mapper.MapToClass(model, entity, true);
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
return OperationResultModel.Success(Mapper.MapToClass<OrderSyncHistory, OrderSyncHistoryViewModel>(entity, true));
|
|
}
|
|
}
|
|
} |