| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 10 | | using WinSolutions.Sveta.Common; |
| | | 11 | | using TidVtbc; |
| | | 12 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 13 | | |
| | | 14 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 15 | | { |
| | | 16 | | public class CategoryService: SvetaServiceBase, ICategoryService |
| | | 17 | | { |
| | | 18 | | private readonly SvetaDbContext _db; |
| | | 19 | | private readonly ILogger<CategoryService> _logger; |
| | | 20 | | |
| | | 21 | | public CategoryService(SvetaDbContext db, ILogger<CategoryService> logger, IAuthenticationService authentication |
| | 189 | 22 | | : base(authenticationService) |
| | 189 | 23 | | { |
| | 189 | 24 | | _db = db; |
| | 189 | 25 | | _logger = logger; |
| | 189 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<Category> GetCategory(long categoryId) => |
| | 2 | 29 | | await _db.Categories |
| | 2 | 30 | | .Include(x => x.Parent) |
| | 2 | 31 | | .Include(d => d.RecState) |
| | 2 | 32 | | .Where(d => !d.IsDeleted) |
| | 2 | 33 | | .FirstOrDefaultAsync(e => e.Id == categoryId); |
| | | 34 | | |
| | | 35 | | public async Task<Category> GetCategory(string name) => |
| | 0 | 36 | | await _db.Categories.Where(d => !d.IsDeleted) |
| | 0 | 37 | | .FirstOrDefaultAsync(d => d.Name.ToLower().Equals(name.NormalizeName().ToLower())); |
| | | 38 | | |
| | | 39 | | public async Task<Category> FindCategory(long categoryId) => |
| | 0 | 40 | | await PrepareCategoriesQuery().FirstOrDefaultAsync(e => e.Id == categoryId); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Проверяем на колизии |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="id"></param> |
| | | 46 | | /// <param name="parentId"></param> |
| | | 47 | | /// <returns></returns> |
| | | 48 | | public async Task<bool> CheckCollision(long id, long parentId) |
| | 0 | 49 | | { |
| | | 50 | | //Проверка прямой колизии |
| | 0 | 51 | | var items = await _db.Categories |
| | 0 | 52 | | .Include(p => p.Parent) |
| | 0 | 53 | | .Where(x => x.Id == parentId && x.Parent.Id == id) |
| | 0 | 54 | | .AnyAsync(); |
| | | 55 | | |
| | 0 | 56 | | return items; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public async Task<List<Category>> FindCategory(string code) |
| | 0 | 60 | | { |
| | 0 | 61 | | var result = PrepareCategoriesQuery(); |
| | 0 | 62 | | if(!string.IsNullOrWhiteSpace(code)) |
| | 0 | 63 | | { |
| | 0 | 64 | | result = result.Where(e => e.Code == code.NormalizeName()); |
| | 0 | 65 | | } |
| | 0 | 66 | | return await result.ToListAsync(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | public async Task<List<long>> CheckParentIds(long[] parentIds) => await _db.Categories |
| | 0 | 70 | | .Include(p=>p.Parent) |
| | 0 | 71 | | .Where(x => parentIds.Contains(x.Parent.Id)) //проверив кто является родителем, отправим обратно список id э |
| | 0 | 72 | | .Select(x=>x.Parent.Id) |
| | 0 | 73 | | .ToListAsync(); |
| | | 74 | | |
| | 0 | 75 | | public async Task<Category> GetNoTrackCategory(long categoryId) => await _db.Categories |
| | 0 | 76 | | .Include(d => d.RecState) |
| | 0 | 77 | | .AsNoTracking() |
| | 0 | 78 | | .Include(x => x.Parent) |
| | 0 | 79 | | .Where(d => !d.IsDeleted) |
| | 0 | 80 | | .FirstOrDefaultAsync(e => e.Id == categoryId); |
| | | 81 | | |
| | 0 | 82 | | public async Task<Category> GetCategoryByGuid(Guid guid) => await _db.Categories |
| | 0 | 83 | | .Where(d => !d.IsDeleted) |
| | 0 | 84 | | .FirstOrDefaultAsync(d => d.GUID == guid); |
| | | 85 | | |
| | 0 | 86 | | public async Task<List<Category>> GetCategories(int page, int limit, string filter, long? parentId = null) => aw |
| | 0 | 87 | | .AsNoTracking() |
| | 0 | 88 | | .Skip((page < 2 ? 0: page -1) * limit) |
| | 0 | 89 | | .Take(limit) |
| | 0 | 90 | | .ToListAsync(); |
| | | 91 | | |
| | 0 | 92 | | public async Task<int> GetCategoriesCount(string filter, long? parentId) => await PrepareCategoriesQuery(filter, |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Метод возвращает полный путь к категории в виде Главная категория > Дочерняя категория > Входная категория |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="categoryId"></param> |
| | | 98 | | /// <returns></returns> |
| | | 99 | | public async Task<string> GetPathToCategoryForFeed(long categoryId) |
| | 0 | 100 | | { |
| | 0 | 101 | | List<string> categories = new List<string>() { }; |
| | 0 | 102 | | var category = await GetCategory(categoryId); |
| | 0 | 103 | | categories.Add(category.Name); |
| | 0 | 104 | | while (category.Parent != null) |
| | 0 | 105 | | { |
| | 0 | 106 | | category = await GetCategory(category.Parent.Id); |
| | 0 | 107 | | categories.Add(category.Name); |
| | 0 | 108 | | } |
| | 0 | 109 | | categories.Reverse(); |
| | 0 | 110 | | return string.Join(" > ", categories); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | IQueryable<Category> PrepareCategoriesQuery(string filter = "", long? parentId = null) |
| | 0 | 114 | | { |
| | 0 | 115 | | var result = _db.Categories |
| | 0 | 116 | | .Include(d => d.Parent) |
| | 0 | 117 | | .Include(d => d.RecState) |
| | 0 | 118 | | .OrderBy(x => x.Name) |
| | 0 | 119 | | .Where(e => !e.IsDeleted); |
| | 0 | 120 | | if (!string.IsNullOrWhiteSpace(filter)) |
| | 0 | 121 | | { |
| | 0 | 122 | | result = result.Where(n => n.Name.ToUpper().Contains(filter.ToUpper()) || n.Code.ToUpper().Contains(filt |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | if (parentId.HasValue) |
| | 0 | 126 | | { |
| | 0 | 127 | | result = parentId.Value switch |
| | 0 | 128 | | { |
| | 0 | 129 | | -1 => result, |
| | 0 | 130 | | 0 => result.Where(x => x.Parent == null), |
| | 0 | 131 | | _ => result.Where(x => x.Parent != null && x.Parent.Id == parentId.Value) |
| | 0 | 132 | | }; |
| | 0 | 133 | | } |
| | 0 | 134 | | return result; |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | public async Task<List<Category>> GetChildCategories(int page, int limit, long? parentId) |
| | 0 | 138 | | { |
| | 0 | 139 | | return await PrepareCategoriesQuery(parentId: parentId) |
| | 0 | 140 | | .AsNoTracking() |
| | 0 | 141 | | .Skip(page * limit) |
| | 0 | 142 | | .Take(limit) |
| | 0 | 143 | | .ToListAsync(); |
| | 0 | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | public async Task<int> GetChildCategoriesCount(long? parentId) => await PrepareCategoriesQuery(parentId: parentI |
| | | 147 | | |
| | | 148 | | public async Task CreateCategory(Category categoryIn) |
| | 0 | 149 | | { |
| | 0 | 150 | | var exists = PrepareCategoriesQuery() |
| | 0 | 151 | | .Where(x => x.Name.ToLower() == categoryIn.Name.ToLower() |
| | 0 | 152 | | || x.Code.ToLower() == categoryIn.Code.ToLower()) |
| | 0 | 153 | | .Any(); |
| | 0 | 154 | | if(exists) |
| | 0 | 155 | | { |
| | 0 | 156 | | throw new ArgumentException("Такая категория уже существует"); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | if (categoryIn.RecState != null) |
| | 0 | 160 | | { |
| | 0 | 161 | | _db.Entry(categoryIn.RecState).State = categoryIn.RecState.Id == 0 ? EntityState.Detached : EntityState. |
| | 0 | 162 | | } |
| | 0 | 163 | | if (categoryIn.Parent != null) |
| | 0 | 164 | | { |
| | 0 | 165 | | _db.Entry(categoryIn.Parent).State = categoryIn.Parent?.Id == 0 ? EntityState.Added : EntityState.Unchan |
| | 0 | 166 | | } |
| | 0 | 167 | | await _db.Categories.AddAsync(categoryIn); |
| | 0 | 168 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | public async Task UpdateCategory(Category data) |
| | 0 | 172 | | { |
| | 0 | 173 | | if (!(await ItemsExists(data.Id))) |
| | 0 | 174 | | { |
| | 0 | 175 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | var exists = PrepareCategoriesQuery() |
| | 0 | 179 | | .Where(x => x.Id != data.Id |
| | 0 | 180 | | && (x.Name.ToLower() == data.Name.NormalizeName().ToLower() |
| | 0 | 181 | | || x.Code.ToLower() == data.Code.NormalizeName().ToLower())) |
| | 0 | 182 | | .Any(); |
| | 0 | 183 | | if (exists) |
| | 0 | 184 | | { |
| | 0 | 185 | | throw new ArgumentException("Такая категория уже существует"); |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | if (data.Parent != null) |
| | 0 | 189 | | { |
| | 0 | 190 | | _db.Entry(data.Parent).State = data.Parent?.Id == 0 ? EntityState.Added : EntityState.Unchanged; |
| | 0 | 191 | | } |
| | 0 | 192 | | _db.Categories.Update(data); |
| | 0 | 193 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | public async Task<bool> ItemsExists(long id) => await _db.Categories |
| | 0 | 197 | | .Where(e => e.Id == id) |
| | 0 | 198 | | .Where(d => !d.IsDeleted) |
| | 0 | 199 | | .AnyAsync(); |
| | | 200 | | |
| | | 201 | | public async Task DeleteCategory(long categoryId) |
| | 0 | 202 | | { |
| | 0 | 203 | | Category rec = await _db.Categories.FindAsync(categoryId); |
| | 0 | 204 | | if (rec == null) |
| | 0 | 205 | | { |
| | 0 | 206 | | throw new KeyNotFoundException($"Record #{categoryId} not found"); |
| | | 207 | | } |
| | 0 | 208 | | rec.IsDeleted = true; |
| | 0 | 209 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |