| | | 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.Json; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Domain; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 12 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 13 | | using WinSolutions.Sveta.Common; |
| | | 14 | | |
| | | 15 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 16 | | { |
| | | 17 | | public class DiscountColorService : SvetaServiceBase, IDiscountColorService |
| | | 18 | | { |
| | | 19 | | private readonly SvetaDbContext _db; |
| | | 20 | | private readonly ILogger<DiscountColorService> _logger; |
| | | 21 | | private readonly IAuthenticationService _authenticationService; |
| | | 22 | | public DiscountColorService(SvetaDbContext db, ILogger<DiscountColorService> logger, IAuthenticationService auth |
| | 112 | 23 | | : base(authenticationService) |
| | 112 | 24 | | { |
| | 112 | 25 | | _authenticationService = authenticationService; |
| | 112 | 26 | | _db = db; |
| | 112 | 27 | | _logger = logger; |
| | 112 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Получить список цветов ярлыков скидки |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="page">пагинация - номер страницы</param> |
| | | 34 | | /// <param name="limit">пагинация - размер страницы</param> |
| | | 35 | | /// <param name="sort">сортировка</param> |
| | | 36 | | /// <param name="filter">фильтр по цвету ярылка, текста, прозрачности и уровню скидки</param> |
| | | 37 | | /// <returns></returns> |
| | | 38 | | public async Task<PaginatedData<List<DiscountColor>>> GetDiscountColors(int page, int limit, string sort, string |
| | 112 | 39 | | { |
| | 112 | 40 | | var colors = PrepareDiscountColors().AsNoTracking(); |
| | 112 | 41 | | int total = await colors?.CountAsync(); |
| | 112 | 42 | | colors = colors.Where(x => string.IsNullOrWhiteSpace(filter) || x.LabelColor.ToUpper().Contains(filter.ToUpp |
| | 112 | 43 | | || x.DiscountLevel.ToString() == filter || x.Transparency.ToString() == filter); |
| | 112 | 44 | | colors = Sort(colors, sort?.ToLower()); |
| | 112 | 45 | | int filtered = await colors?.CountAsync(); |
| | 112 | 46 | | return new PaginatedData<List<DiscountColor>> |
| | 112 | 47 | | { |
| | 112 | 48 | | Result = await colors.Skip(page * limit).Take(limit).ToListAsync(), |
| | 112 | 49 | | TotalCount = total, |
| | 112 | 50 | | TotalFilteredCount = filtered |
| | 112 | 51 | | }; |
| | 112 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// предварительная выборка |
| | | 56 | | /// </summary> |
| | | 57 | | /// <returns></returns> |
| | 112 | 58 | | private IQueryable<DiscountColor> PrepareDiscountColors() => _db.DiscountColors |
| | 112 | 59 | | .Where(e => !e.IsDeleted) |
| | 112 | 60 | | .AsQueryable(); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// получить цвет ярылка |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="id">id записи</param> |
| | | 66 | | /// <returns></returns> |
| | 0 | 67 | | public async Task<DiscountColor> GetDiscountColor(long id) => await PrepareDiscountColors().FirstOrDefaultAsync( |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// создать цвет ярлыка |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="data"> ярлык</param> |
| | | 73 | | /// <returns></returns> |
| | | 74 | | public async Task Create(DiscountColor data) |
| | 0 | 75 | | { |
| | 0 | 76 | | await _db.DiscountColors.AddAsync(data); |
| | 0 | 77 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// проверить существование цвета ярлыка |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="id">id записи</param> |
| | | 84 | | /// <returns></returns> |
| | 0 | 85 | | public async Task<bool> DiscountExists(long id) => await PrepareDiscountColors().AsNoTracking().Where(e => e.Id |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// обновить цвет ярлыка |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="data"> ярлык</param> |
| | | 91 | | /// <returns></returns> |
| | | 92 | | public async Task Update(DiscountColor data) |
| | 0 | 93 | | { |
| | 0 | 94 | | if (!(await DiscountExists(data.Id))) |
| | 0 | 95 | | { |
| | 0 | 96 | | throw new ArgumentException($"Скидка с id={data.Id} не найдена"); |
| | | 97 | | } |
| | 0 | 98 | | _db.DiscountColors.Update(data); |
| | 0 | 99 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Удалить цвет ярлыка |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="id">id записи</param> |
| | | 106 | | /// <returns></returns> |
| | | 107 | | public async Task Delete(long id) |
| | 0 | 108 | | { |
| | 0 | 109 | | var rec = await _db.DiscountColors.FindAsync(id); |
| | 0 | 110 | | if (rec == null) |
| | 0 | 111 | | throw new KeyNotFoundException($"Скидка с id={id} не найдена"); |
| | 0 | 112 | | rec.IsDeleted = true; |
| | 0 | 113 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Возвращает цвет максимальной скидки, которая меньше или равна переданной |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="discountLevel"></param> |
| | | 120 | | /// <returns></returns> |
| | | 121 | | public async Task<DiscountColor> GetColor(int discountLevel) |
| | 0 | 122 | | => await PrepareDiscountColors().AsNoTracking().OrderBy(x => x.DiscountLevel).Where(x => x.DiscountLevel <= |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// сортировка по цвету ярлыка и уровню скидки |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="items">выборка ярлыков</param> |
| | | 128 | | /// <param name="sort">тип сортировки</param> |
| | | 129 | | /// <returns></returns> |
| | 112 | 130 | | private IQueryable<DiscountColor> Sort(IQueryable<DiscountColor> items, string sort = default) => (sort ?? "").T |
| | 112 | 131 | | { |
| | 112 | 132 | | "color" => items.OrderBy(d => d.LabelColor), |
| | 112 | 133 | | "color|desc" => items.OrderByDescending(d => d.LabelColor), |
| | 112 | 134 | | "discount|desc" => items.OrderByDescending(d => d.DiscountLevel), |
| | 112 | 135 | | _ => items.OrderBy(d => d.DiscountLevel) |
| | 112 | 136 | | }; |
| | | 137 | | } |
| | | 138 | | } |