| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading.Tasks; |
| | | 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.Domain; |
| | | 12 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 13 | | using WinSolutions.Sveta.Common; |
| | | 14 | | |
| | | 15 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 16 | | { |
| | | 17 | | public class WalletService : SvetaServiceBase, IWalletService |
| | | 18 | | { |
| | | 19 | | private readonly ILogger<WalletService> _logger; |
| | | 20 | | private readonly SvetaDbContext _db; |
| | | 21 | | public WalletService(SvetaDbContext db, ILogger<WalletService> logger, IAuthenticationService authenticationServ |
| | 86 | 22 | | : base(authenticationService) |
| | 86 | 23 | | { |
| | 86 | 24 | | _db = db; |
| | 86 | 25 | | _logger = logger; |
| | 86 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task CreateWalletTransaction(WalletTransaction transaction) |
| | 0 | 29 | | { |
| | 0 | 30 | | await _db.WalletTransactions.AddAsync(transaction); |
| | 0 | 31 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Возвращает список траназакций |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="movementId">Идентификатор документа</param> |
| | | 38 | | /// <param name="buyerId"></param> |
| | | 39 | | /// <param name="transactionStatus">Идентификатор статуса транзакции</param> |
| | | 40 | | /// <param name="dateFrom">Дата начала отбора</param> |
| | | 41 | | /// <param name="dateTo">Дата окончания отбора</param> |
| | | 42 | | /// <param name="page">Страница отбора</param> |
| | | 43 | | /// <param name="limit">Лимит отбора</param> |
| | | 44 | | /// <param name="sort">сортировка по умолчанию по id - id|desc,created_on,created_on|descstatus,status|desc,cust |
| | | 45 | | /// <returns></returns> |
| | | 46 | | public async Task<PaginatedData<List<WalletTransaction>>> GetTransactions(long movementId = 0, long buyerId = 0, |
| | 13 | 47 | | { |
| | 13 | 48 | | var list = PrepareQuery(); |
| | 13 | 49 | | int totalCount = await FilterQuery(list, movementId, 0, 0, DateTime.MinValue, DateTime.MinValue).CountAsync( |
| | 13 | 50 | | list = FilterQuery(list, movementId, buyerId, transactionStatus, dateFrom, dateTo); |
| | 13 | 51 | | list = Sort(list, sort?.ToLower()); |
| | 13 | 52 | | int totalFilteredCount = await list.CountAsync(); |
| | 13 | 53 | | return new PaginatedData<List<WalletTransaction>> |
| | 13 | 54 | | { |
| | 13 | 55 | | TotalCount = totalCount, |
| | 13 | 56 | | TotalFilteredCount = totalFilteredCount, |
| | 13 | 57 | | Result = await list.Skip((page < 2 ? 0 : page -1 ) * limit).Take(limit).ToListAsync() |
| | 13 | 58 | | }; |
| | 13 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Получение транзакции по id документа и статусу |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="movementId"></param> |
| | | 65 | | /// <param name="transactionStatusId"></param> |
| | | 66 | | /// <returns></returns> |
| | 29 | 67 | | public async Task<WalletTransaction> GetTransaction(long movementId, long transactionStatusId) => await PrepareQ |
| | 29 | 68 | | .FirstOrDefaultAsync(d => d.Movement.Id == movementId && d.Status.Id == transactionStatusId); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Получение статусе по идентификатору |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="id"></param> |
| | | 74 | | /// <returns></returns> |
| | 0 | 75 | | public async Task<WalletTransactionStatus> GetTransactionStatus(long id) => await _db.refWalletTransactionStatus |
| | 0 | 76 | | .FindAsync(id); |
| | | 77 | | |
| | 0 | 78 | | public async Task<List<WalletTransactionStatus>> GetTransactionStatuses() => await _db.refWalletTransactionStatu |
| | | 79 | | |
| | | 80 | | public async Task Update(WalletTransaction walletTransaction) |
| | 0 | 81 | | { |
| | 0 | 82 | | _db.WalletTransactions.Update(walletTransaction); |
| | 0 | 83 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | private IQueryable<WalletTransaction> FilterQuery(IQueryable<WalletTransaction> preparedList, long movementId, l |
| | 26 | 87 | | preparedList.Where(d => movementId == 0 || d.Movement.Id == movementId) |
| | 26 | 88 | | .Where(d => buyerId == 0 || d.Movement.Customer.Id == buyerId) |
| | 26 | 89 | | .Where(d => transactionStatus == 0 || d.Status.Id == transactionStatus) |
| | 26 | 90 | | .Where(d => dateFrom == DateTime.MinValue || d.CreationDateTime >= dateFrom) |
| | 26 | 91 | | .Where(d => dateTo == DateTime.MinValue || d.CreationDateTime < dateTo); |
| | | 92 | | |
| | | 93 | | |
| | 42 | 94 | | private IQueryable<WalletTransaction> PrepareQuery() => _db.WalletTransactions |
| | 42 | 95 | | .Include(d => d.Movement) |
| | 42 | 96 | | .ThenInclude(Movement => Movement.Customer) |
| | 42 | 97 | | .Include(d => d.Movement) |
| | 42 | 98 | | .ThenInclude(Movement => Movement.Supplier) |
| | 42 | 99 | | .Include(d => d.Status) |
| | 42 | 100 | | .Where(d => !d.IsDeleted) |
| | 42 | 101 | | .AsQueryable(); |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Сортирует по полям |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="movements"></param> |
| | | 107 | | /// <param name="sort">сортировка по умолчанию по id - id|desc,created_on,created_on|descstatus,status|desc,cust |
| | | 108 | | /// <returns></returns> |
| | 13 | 109 | | private IQueryable<WalletTransaction> Sort(IQueryable<WalletTransaction> transactions, string sort = default) => |
| | 13 | 110 | | { |
| | 13 | 111 | | "created_on" => transactions.OrderBy(d => d.CreationDateTime), |
| | 13 | 112 | | "created_on|desc" => transactions.OrderByDescending(d => d.CreationDateTime), |
| | 13 | 113 | | "status" => transactions.OrderBy(d => d.Status.Id), |
| | 13 | 114 | | "status|desc" => transactions.OrderByDescending(d => d.Status.Id), |
| | 13 | 115 | | "customer" => transactions.OrderBy(d => d.Movement.Customer), |
| | 13 | 116 | | "customer|desc" => transactions.OrderByDescending(d => d.Movement.Customer), |
| | 13 | 117 | | "supplier" => transactions.OrderBy(d => d.Movement.Supplier), |
| | 13 | 118 | | "supplier|desc" => transactions.OrderByDescending(d => d.Movement.Supplier), |
| | 13 | 119 | | "id|desc" => transactions.OrderByDescending(d => d.Id), |
| | 13 | 120 | | _ => transactions.OrderBy(d => d.Id), |
| | 13 | 121 | | }; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | } |