| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Data; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Reflection; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using WinSolutions.Sveta.Common; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 13 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 14 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 15 | | |
| | | 16 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 17 | | { |
| | | 18 | | public class MovementRouteService: SvetaServiceBase, IMovementRouteService |
| | | 19 | | { |
| | | 20 | | private readonly ILogger<MovementRouteService> _logger; |
| | | 21 | | private readonly SvetaDbContext _db; |
| | | 22 | | |
| | | 23 | | public MovementRouteService(SvetaDbContext db, ILogger<MovementRouteService> logger, IAuthenticationService auth |
| | 186 | 24 | | : base(authenticationService) |
| | 186 | 25 | | { |
| | 186 | 26 | | _logger = logger; |
| | 186 | 27 | | _db = db; |
| | 186 | 28 | | } |
| | | 29 | | |
| | 193 | 30 | | public async Task<List<MovementStatusRoute>> GetRoutes() => await PrepareQuery().ToListAsync(); |
| | | 31 | | |
| | 284 | 32 | | public async Task<List<MovementStatusRoute>> GetRoutes(long movementStatusId, long warehouseId) => await Prepare |
| | 284 | 33 | | .Where(d => d.StatusCurrent.Id == movementStatusId && d.WarehouseId == warehouseId) |
| | 284 | 34 | | .ToListAsync(); |
| | | 35 | | |
| | | 36 | | public async Task CreateRoute(MovementStatusRoute statusRoute) |
| | 1 | 37 | | { |
| | 1 | 38 | | await _db.MovementStatusRoutes.AddAsync(statusRoute); |
| | 1 | 39 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task CreateRoutes(List<MovementStatusRoute> statusRoutes) |
| | 186 | 43 | | { |
| | 186 | 44 | | await _db.MovementStatusRoutes.AddRangeAsync(statusRoutes); |
| | 186 | 45 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 186 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task UpdateRoute(MovementStatusRoute statusRoute) |
| | 1 | 49 | | { |
| | 1 | 50 | | var routeDb = await _db.MovementStatusRoutes.FindAsync(statusRoute.Id) |
| | 1 | 51 | | ?? throw new ArgumentException($"Запись #{statusRoute.Id} не найдена"); |
| | 1 | 52 | | routeDb.Hour = statusRoute.Hour; |
| | 1 | 53 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task DeleteRoute(long id) |
| | 1 | 57 | | { |
| | 1 | 58 | | var routeDb = await _db.MovementStatusRoutes.FindAsync(id) |
| | 1 | 59 | | ?? throw new ArgumentException($"Запись #{id} не найдена"); |
| | 1 | 60 | | await DeleteRoute(routeDb); |
| | 1 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task DeleteRoute(MovementStatusRoute route) |
| | 1 | 64 | | { |
| | 1 | 65 | | route.IsDeleted = true; |
| | 1 | 66 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 67 | | } |
| | | 68 | | |
| | 477 | 69 | | private IQueryable<MovementStatusRoute> PrepareQuery() => _db.MovementStatusRoutes |
| | 477 | 70 | | .Include(d => d.RecState) |
| | 477 | 71 | | .Include(d => d.StatusCurrent) |
| | 477 | 72 | | .ThenInclude(StatusCurrent => StatusCurrent.StatusOwner) |
| | 477 | 73 | | .Include(d => d.StatusNext) |
| | 477 | 74 | | .ThenInclude(StatusNext => StatusNext.StatusOwner) |
| | 477 | 75 | | .Include(d => d.Warehouse) |
| | 477 | 76 | | .Where(d => !d.IsDeleted) |
| | 477 | 77 | | .AsQueryable(); |
| | | 78 | | |
| | | 79 | | public async Task<List<MovementStatusRoute>> GetRoutesByDepartment(long warehouseId) |
| | 0 | 80 | | { |
| | 0 | 81 | | return await PrepareQuery() |
| | 0 | 82 | | .Where(d => d.WarehouseId == warehouseId) |
| | 0 | 83 | | .ToListAsync(); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public async Task<MovementStatusRoute> GetRoute(long id) |
| | 0 | 87 | | { |
| | 0 | 88 | | return await PrepareQuery() |
| | 0 | 89 | | .Where(d => d.Id == id) |
| | 0 | 90 | | .FirstOrDefaultAsync(); |
| | 0 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |