| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 11 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 12 | | using WinSolutions.Sveta.Common; |
| | | 13 | | |
| | | 14 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 15 | | { |
| | | 16 | | public class MovementStatusJournalService : SvetaServiceBase, IMovementStatusJournalService |
| | | 17 | | { |
| | | 18 | | private readonly ILogger<MovementStatusJournalService> _logger; |
| | | 19 | | private readonly SvetaDbContext _db; |
| | | 20 | | |
| | | 21 | | public MovementStatusJournalService(ILogger<MovementStatusJournalService> logger, |
| | | 22 | | SvetaDbContext db, |
| | 112 | 23 | | IAuthenticationService authenticationService):base(authenticationService) |
| | 112 | 24 | | { |
| | 112 | 25 | | _db = db; |
| | 112 | 26 | | _logger = logger; |
| | 112 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<List<MovementStatusJournal>> GetMovementStatusJournals(int page, int limit, string filter) => |
| | 0 | 30 | | await PrepareQuery(filter) |
| | 0 | 31 | | .Skip((page < 2 ? 0 : page - 1) * limit ) |
| | 0 | 32 | | .Take(limit) |
| | 0 | 33 | | .ToListAsync(); |
| | | 34 | | |
| | | 35 | | public async Task<List<MovementStatusJournal>> GetMovementStatusJournal(DateTime startDate, bool activeOnly = fa |
| | 0 | 36 | | await PrepareQuery(activeOnly: activeOnly) |
| | 0 | 37 | | .Where(d => d.CreationDateTime >= startDate) |
| | 0 | 38 | | .ToListAsync(); |
| | | 39 | | |
| | 0 | 40 | | public async Task<List<MovementStatusJournal>> GetAllActive() => await PrepareQuery(activeOnly: true).ToListAsyn |
| | | 41 | | |
| | 0 | 42 | | public async Task<int> GetMovementStatusJournalCount(string filter) => await PrepareQuery(filter).CountAsync(); |
| | | 43 | | |
| | 49 | 44 | | public async Task<List<MovementStatusJournal>> GetMovementStatusJournal(long movementId) => await PrepareQuery() |
| | 49 | 45 | | .Where(d => d.Movement.Id == movementId) |
| | 49 | 46 | | .ToListAsync(); |
| | 0 | 47 | | public async Task<MovementStatusJournal> GetMovementStatusJournal(long movementId, MovementStatus status) => aw |
| | 0 | 48 | | .FirstOrDefaultAsync(d => d.Movement.Id == movementId && d.StatusCurrent.Id == status.Id); |
| | | 49 | | public async Task Create(MovementStatusJournal journal) |
| | 348 | 50 | | { |
| | 348 | 51 | | _db.Entry(journal.Movement).State = EntityState.Unchanged; |
| | 348 | 52 | | _db.Entry(journal.StatusCurrent).State = EntityState.Unchanged; |
| | 348 | 53 | | journal.RecState = await _db.refRecordsState.FindAsync((long) RecordState.Active); |
| | 348 | 54 | | var oldJournal = await _db.MovementStatusJournals.Where(d => d.Movement.Id == journal.Movement.Id).ToListAsy |
| | 348 | 55 | | if (oldJournal?.Count > 0) |
| | 246 | 56 | | { |
| | 246 | 57 | | var incative = await _db.refRecordsState.FindAsync((long)RecordState.Inactive); |
| | 1880 | 58 | | foreach (var old in oldJournal) |
| | 571 | 59 | | { |
| | 571 | 60 | | old.RecState = incative; |
| | 571 | 61 | | } |
| | 246 | 62 | | } |
| | 348 | 63 | | await _db.MovementStatusJournals.AddAsync(journal); |
| | 348 | 64 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 348 | 65 | | } |
| | | 66 | | |
| | | 67 | | public async Task SetRecordInactive(MovementStatusJournal item) |
| | 0 | 68 | | { |
| | 0 | 69 | | item.RecState = await _db.refRecordsState.FirstOrDefaultAsync(d => d.Id == (long) RecordState.Inactive); |
| | 0 | 70 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 71 | | } |
| | | 72 | | |
| | 49 | 73 | | private IQueryable<MovementStatusJournal> PrepareQuery(string filter = default, bool activeOnly = false) => _db. |
| | 49 | 74 | | .Include(d => d.Movement) |
| | 49 | 75 | | .ThenInclude(Movement => Movement.RecState) |
| | 49 | 76 | | .Include(d => d.Movement) |
| | 49 | 77 | | .ThenInclude(Movement => Movement.MovementType) |
| | 49 | 78 | | .Include(d => d.Movement) |
| | 49 | 79 | | .ThenInclude(Movement => Movement.Customer) |
| | 49 | 80 | | .Include(d => d.Movement) |
| | 49 | 81 | | .ThenInclude(Movement => Movement.Supplier) |
| | 49 | 82 | | .Include(d => d.Movement) |
| | 49 | 83 | | .ThenInclude(Movement => Movement.Parent) |
| | 49 | 84 | | .ThenInclude(Parent => Parent.MovementStatus) |
| | 49 | 85 | | .Include(d => d.Movement) |
| | 49 | 86 | | .ThenInclude(Movement => Movement.Notes) |
| | 49 | 87 | | .Include(d => d.Movement).ThenInclude(Movement => Movement.Sender) |
| | 49 | 88 | | .Include(d => d.StatusCurrent) |
| | 49 | 89 | | .Include(d => d.RecState) |
| | 49 | 90 | | .Where(d => !d.IsDeleted && !d.Movement.IsDeleted) |
| | 49 | 91 | | .Where(d => !activeOnly || d.RecState.Id == (long)RecordState.Active) |
| | 49 | 92 | | .Where(d => string.IsNullOrWhiteSpace(filter) |
| | 49 | 93 | | || d.StatusCurrent.Name.ToUpper().Contains(filter.ToUpper()) |
| | 49 | 94 | | || d.Movement.MovementStatus.Name.ToUpper().Contains(filter.ToUpper()) |
| | 49 | 95 | | || d.Movement.MovementType.Name.ToUpper().Contains(filter.ToUpper())); |
| | | 96 | | } |
| | | 97 | | } |