| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Linq.Expressions; |
| | | 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.Services.Interfaces; |
| | | 11 | | using WinSolutions.Sveta.Common; |
| | | 12 | | |
| | | 13 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 14 | | { |
| | | 15 | | public class TaxSystemService : SvetaServiceBase, ITaxSystemService |
| | | 16 | | { |
| | | 17 | | private readonly SvetaDbContext _db; |
| | | 18 | | private readonly ILogger<TaxSystemService> _logger; |
| | 0 | 19 | | public TaxSystemService(SvetaDbContext db, ILogger<TaxSystemService> logger, IAuthenticationService authenticati |
| | 0 | 20 | | { |
| | 0 | 21 | | _db = db; |
| | 0 | 22 | | _logger = logger; |
| | 0 | 23 | | } |
| | | 24 | | public async Task CreateTaxSystem(TaxSystem taxSystem) |
| | 0 | 25 | | { |
| | 0 | 26 | | if (taxSystem.RecState != null) |
| | 0 | 27 | | _db.Entry(taxSystem.RecState).State = taxSystem.RecState.Id == 0 ? EntityState.Detached : EntityState.Un |
| | 0 | 28 | | await _db.AddAsync(taxSystem); |
| | 0 | 29 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task DeleteTaxSystem(long id) |
| | 0 | 33 | | { |
| | 0 | 34 | | var taxSystem = await _db.TaxSystems |
| | 0 | 35 | | .Where(d => !d.IsDeleted) |
| | 0 | 36 | | .FirstAsync(t=>t.Id == id); |
| | 0 | 37 | | if (taxSystem == null) |
| | 0 | 38 | | throw new KeyNotFoundException($"Запись #{id} не найдена"); |
| | 0 | 39 | | taxSystem.IsDeleted = true; |
| | 0 | 40 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | public async Task<TaxSystem> GetTaxSystem(long id) => await _db.TaxSystems |
| | 0 | 44 | | .Include(d => d.RecState) |
| | 0 | 45 | | .FirstOrDefaultAsync(d => !d.IsDeleted && d.Id == id); |
| | | 46 | | |
| | | 47 | | public async Task<List<TaxSystem>> GetTaxSystems(int page, int limit, string filter, string sort) |
| | 0 | 48 | | { |
| | 0 | 49 | | string[] sortItems = sort?.Split("|"); |
| | 0 | 50 | | string sortColumn = sortItems?.ElementAtOrDefault(0); |
| | 0 | 51 | | string sortOrder = sortItems?.ElementAtOrDefault(1); |
| | | 52 | | |
| | 0 | 53 | | var tax = _db.TaxSystems.AsNoTracking() |
| | 0 | 54 | | .Include(d => d.RecState) |
| | 0 | 55 | | .Where(d => !d.IsDeleted); |
| | 0 | 56 | | if (!string.IsNullOrWhiteSpace(filter)) |
| | 0 | 57 | | { |
| | 0 | 58 | | tax = tax.Where(d => d.Name.ToUpper().Contains(filter.ToUpper()) || d.Code.ToUpper().Contains(filter.ToU |
| | 0 | 59 | | } |
| | 0 | 60 | | Expression<Func<TaxSystem, object>> sortExpression = sortColumn?.ToLower() switch |
| | 0 | 61 | | { |
| | 0 | 62 | | "code" => e => e.Code, |
| | 0 | 63 | | _ => e => e.Name, |
| | 0 | 64 | | }; |
| | 0 | 65 | | if (sortOrder != default && sortOrder.Equals("desc", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 66 | | tax = tax.OrderByDescending(sortExpression); |
| | | 67 | | else |
| | 0 | 68 | | tax = tax.OrderBy(sortExpression); |
| | 0 | 69 | | return await tax.Skip((page < 2 ? 0 : page - 1) * limit).Take(limit).ToListAsync(); |
| | 0 | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | public async Task<bool> ItemsExists(long id) => await _db.TaxSystems |
| | 0 | 73 | | .Where(e => e.Id == id) |
| | 0 | 74 | | .Where(d => !d.IsDeleted) |
| | 0 | 75 | | .AnyAsync(); |
| | | 76 | | |
| | | 77 | | public async Task UpdateTaxSystem(TaxSystem data) |
| | 0 | 78 | | { |
| | 0 | 79 | | if (!(await ItemsExists(data.Id))) |
| | 0 | 80 | | { |
| | 0 | 81 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 82 | | } |
| | 0 | 83 | | _db.TaxSystems.Update(data); |
| | 0 | 84 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |