| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using System.Linq.Expressions; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Microsoft.EntityFrameworkCore.Internal; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 12 | | using WinSolutions.Sveta.Common; |
| | | 13 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 14 | | |
| | | 15 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 16 | | { |
| | | 17 | | public class BrandService : SvetaServiceBase, IBrandService |
| | | 18 | | { |
| | | 19 | | private readonly SvetaDbContext _db; |
| | | 20 | | private readonly ILogger<BrandService> _logger; |
| | | 21 | | |
| | | 22 | | public BrandService(SvetaDbContext db, ILogger<BrandService> logger, IAuthenticationService authenticationServic |
| | 7 | 23 | | : base(authenticationService) |
| | 7 | 24 | | { |
| | 7 | 25 | | _db = db; |
| | 7 | 26 | | _logger = logger; |
| | 7 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public IQueryable<Brand> GetBrands() => _db.Brands.Where(d => !d.IsDeleted).AsQueryable(); |
| | | 30 | | |
| | 3 | 31 | | public async Task<Brand> GetBrand(long brandId) => await _db.Brands.Include(x => x.Parent).Where(d => !d.IsDelet |
| | | 32 | | |
| | 3 | 33 | | public async Task<Brand> GetBrandByName(string name) => await _db.Brands |
| | 3 | 34 | | .Include(x => x.Parent) |
| | 3 | 35 | | .Include(d => d.RecState) |
| | 3 | 36 | | .Where(e => !e.IsDeleted) |
| | 3 | 37 | | .FirstOrDefaultAsync(d => d.Name.Contains(name.NormalizeName())); |
| | | 38 | | |
| | | 39 | | IQueryable<Brand> PrepareBrandsQuery(string filter = null, long? parentId = null) |
| | 73 | 40 | | { |
| | 73 | 41 | | var result = _db.Brands |
| | 73 | 42 | | .Include(x => x.Parent) |
| | 73 | 43 | | .Include(d => d.RecState) |
| | 73 | 44 | | .AsNoTracking() |
| | 73 | 45 | | .Where(e => !e.IsDeleted); |
| | 73 | 46 | | if (!string.IsNullOrWhiteSpace(filter)) |
| | 5 | 47 | | { |
| | 5 | 48 | | result = result.Where(x => x.Name.ToUpper().Contains(filter.ToUpper())); |
| | 5 | 49 | | } |
| | 73 | 50 | | if (parentId.HasValue) |
| | 0 | 51 | | { |
| | 0 | 52 | | result = parentId.Value switch |
| | 0 | 53 | | { |
| | 0 | 54 | | -1 => result, |
| | 0 | 55 | | 0 => result.Where(x => x.Parent == null), |
| | 0 | 56 | | _ => result.Where(x => x.Parent != null && x.Parent.Id == parentId.Value) |
| | 0 | 57 | | }; |
| | 0 | 58 | | } |
| | 73 | 59 | | return result; |
| | 73 | 60 | | } |
| | | 61 | | /// <summary> |
| | | 62 | | /// Проверяем являются ли родителем текущие элементы |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="parentIds">Список проверяемых элементов</param> |
| | | 65 | | /// <returns>Отправляем обратно список всех тех кто является родителем</returns> |
| | 0 | 66 | | public async Task<List<long>> CheckParentIds(long[] parentIds) => await _db.Brands |
| | 0 | 67 | | .Include(p => p.Parent) |
| | 0 | 68 | | .Where(x => parentIds.Contains(x.Parent.Id)) //проверив кто является родителем, отправим обратно список id э |
| | 0 | 69 | | .Select(x => x.Parent.Id) |
| | 0 | 70 | | .ToListAsync(); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Проверяем на колизии |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="id"></param> |
| | | 76 | | /// <param name="parentId"></param> |
| | | 77 | | /// <returns></returns> |
| | | 78 | | public async Task<bool> CheckCollision(long id, long parentId) |
| | 0 | 79 | | { |
| | | 80 | | //Проверка прямой колизии |
| | 0 | 81 | | var items = await _db.Brands |
| | 0 | 82 | | .Include(p => p.Parent) |
| | 0 | 83 | | .Where(x => x.Id == parentId && x.Parent.Id == id) |
| | 0 | 84 | | .AnyAsync(); |
| | | 85 | | |
| | 0 | 86 | | return items; |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | |
| | | 90 | | public async Task<long> GetBrandsCount(string filter, long? parentId) |
| | 4 | 91 | | { |
| | 4 | 92 | | return await PrepareBrandsQuery(filter, parentId).CountAsync(); |
| | 4 | 93 | | } |
| | | 94 | | public async Task<List<Brand>> GetBrands(string sort, string filter, int page, int limit, long? parentId) |
| | 8 | 95 | | { |
| | 8 | 96 | | string[] sortItems = sort.Split('|'); |
| | 8 | 97 | | string sortColumn = string.Empty; |
| | 8 | 98 | | string sortOrder = string.Empty; |
| | 8 | 99 | | if(sortItems.Length == 2) |
| | 7 | 100 | | { |
| | 7 | 101 | | sortColumn = sortItems[0].ToLower(); |
| | 7 | 102 | | sortOrder = sortItems[1].ToLower(); |
| | 7 | 103 | | } |
| | | 104 | | |
| | | 105 | | //filtering |
| | 8 | 106 | | IQueryable<Brand> brands = PrepareBrandsQuery(filter, parentId); |
| | | 107 | | |
| | | 108 | | //sorting |
| | | 109 | | Expression<Func<Brand, object>> sortingExpression; |
| | | 110 | | |
| | 8 | 111 | | switch (sortColumn) |
| | | 112 | | { |
| | | 113 | | case "name": |
| | 7 | 114 | | sortingExpression = e => e.Name; |
| | 7 | 115 | | break; |
| | | 116 | | case "modificationdatetime": |
| | 0 | 117 | | sortingExpression = e => e.ModificationDateTime; |
| | 0 | 118 | | break; |
| | | 119 | | default: |
| | 1 | 120 | | sortingExpression = e => e.Name; |
| | 1 | 121 | | break; |
| | | 122 | | } |
| | | 123 | | |
| | 8 | 124 | | if(sortOrder == "desc") |
| | 4 | 125 | | brands = brands.OrderByDescending(sortingExpression); |
| | | 126 | | else |
| | 4 | 127 | | brands = brands.OrderBy(sortingExpression); |
| | | 128 | | |
| | | 129 | | |
| | 8 | 130 | | return await brands.Skip((page < 2 ? 0 : page-1)*limit).Take(limit).ToListAsync(); |
| | 8 | 131 | | } |
| | | 132 | | public async Task CreateBrand(Brand brandIn) |
| | 60 | 133 | | { |
| | 60 | 134 | | if(PrepareBrandsQuery().Where(x => x.Name.ToLower() == brandIn.Name.NormalizeName().ToLower()).Any()) |
| | 0 | 135 | | { |
| | 0 | 136 | | throw new Exception("Такой бренд уже существует"); |
| | | 137 | | } |
| | | 138 | | |
| | 60 | 139 | | if (brandIn.RecState != null) |
| | 0 | 140 | | _db.Entry(brandIn.RecState).State = brandIn.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchan |
| | 60 | 141 | | await _db.Brands.AddAsync(brandIn); |
| | 60 | 142 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 60 | 143 | | } |
| | | 144 | | |
| | | 145 | | public async Task UpdateBrand(Brand data) |
| | 1 | 146 | | { |
| | 1 | 147 | | if (!(await BrandExists(data.Id))) |
| | 0 | 148 | | { |
| | 0 | 149 | | throw new ArgumentException($"Запись #{data?.Name} не найдена"); |
| | | 150 | | } |
| | | 151 | | |
| | 1 | 152 | | if (PrepareBrandsQuery().Where(x => x.Name.ToLower() == data.Name.NormalizeName().ToLower() && x.Id != data. |
| | 0 | 153 | | { |
| | 0 | 154 | | throw new Exception("Такой бренд уже существует"); |
| | | 155 | | } |
| | | 156 | | |
| | 1 | 157 | | _db.Brands.Update(data); |
| | 1 | 158 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 159 | | } |
| | | 160 | | |
| | | 161 | | public async Task DeleteBrand(long brandId) |
| | 1 | 162 | | { |
| | 1 | 163 | | Brand rec = await _db.Brands.FindAsync(brandId); |
| | 1 | 164 | | if (rec == null) |
| | 0 | 165 | | throw new KeyNotFoundException($"Запись #{brandId} не найдена "); |
| | 1 | 166 | | if (_db.Brands.Include(d => d.Parent).Any(d => !d.IsDeleted && d.Parent.Id == brandId)) |
| | 0 | 167 | | throw new ArgumentException($"Брэнд #{brandId} имеет дочерние брэнды и не может быть удалён"); |
| | 1 | 168 | | if (_db |
| | 1 | 169 | | .Goods.Include(good => good.Brand) |
| | 1 | 170 | | .Any(d => !d.IsDeleted && d.Brand.Id == brandId)) |
| | 0 | 171 | | throw new ArgumentException($"Невозможно удалить бренд с товарами"); |
| | | 172 | | |
| | | 173 | | |
| | 1 | 174 | | rec.IsDeleted = true; |
| | 1 | 175 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 176 | | } |
| | | 177 | | |
| | 1 | 178 | | public async Task<bool> BrandExists(long brandId) => await _db.Brands |
| | 1 | 179 | | .Include(x => x.Parent) |
| | 1 | 180 | | .AsNoTracking() |
| | 1 | 181 | | .Where(d => !d.IsDeleted) |
| | 1 | 182 | | .Where(e => e.Id == brandId).AnyAsync(); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | } |