| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using WinSolutions.Sveta.Server.Domain; |
| | | 3 | | using WinSolutions.Sveta.Common; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 13 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 14 | | using Clave.Expressionify; |
| | | 15 | | using Microsoft.EntityFrameworkCore.Internal; |
| | | 16 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 17 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 18 | | |
| | | 19 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 20 | | { |
| | | 21 | | public class GoodService : SvetaServiceBase, IGoodService |
| | | 22 | | { |
| | | 23 | | private readonly ILogger<GoodService> _logger; |
| | | 24 | | private readonly SvetaDbContext _db; |
| | | 25 | | public GoodService(SvetaDbContext db, ILogger<GoodService> logger, IAuthenticationService authenticationService) |
| | 184 | 26 | | : base(authenticationService) |
| | 184 | 27 | | { |
| | 184 | 28 | | _db = db; |
| | 184 | 29 | | _logger = logger; |
| | 184 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Возвращает товары по фильтру и категории |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="page">пейджинг: номер страницы</param> |
| | | 36 | | /// <param name="limit">пейджинг: размер страницы</param> |
| | | 37 | | /// <param name="filter">фильтр по значимым полям</param> |
| | | 38 | | /// <param name="sort">сортировка</param> |
| | | 39 | | /// <param name="categoryId">код категории, null если возвращать все товары из всех категорий</param> |
| | | 40 | | /// <param name="activeOnly">возвращать только активные товары, иначе активные + неактивные</param> |
| | | 41 | | /// <returns></returns> |
| | | 42 | | public async Task<List<Good>> GetGoods(int page, int limit, string filter, string sort, long? categoryId, bool a |
| | 0 | 43 | | { |
| | 0 | 44 | | var qry = PrepareGetNotDeletedGoodsQuery(filter, categoryId, activeOnly); |
| | 0 | 45 | | switch((sort ?? "").ToLower()) |
| | | 46 | | { |
| | 0 | 47 | | case "name": qry = qry.OrderBy(x => x.Name); break; |
| | 0 | 48 | | case "name|desc": qry = qry.OrderByDescending(x => x.Name); break; |
| | 0 | 49 | | case "brandname": qry = qry.OrderBy(x => x.Brand.Name); break; |
| | 0 | 50 | | case "brandname|desc": qry = qry.OrderByDescending(x => x.Brand.Name); break; |
| | 0 | 51 | | case "id|desc": qry = qry.OrderByDescending(x => x.Id); break; |
| | 0 | 52 | | default: qry = qry.OrderBy(x => x.Id); break; |
| | | 53 | | } |
| | 0 | 54 | | qry = qry.Skip(page * limit).Take(limit); |
| | 0 | 55 | | return await qry.ToListAsync(); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Возвращает количество товаров по фильтру и категории |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="filter">фильтр по значимым полям</param> |
| | | 62 | | /// <param name="categoryId">код категории, null если возвращать все товары из всех категорий</param> |
| | | 63 | | /// <param name="activeOnly">возвращать только активные товары, иначе активные + неактивные</param> |
| | | 64 | | /// <returns></returns> |
| | | 65 | | public async Task<int> GetGoodsCount(string filter, long? categoryId, bool activeOnly) |
| | 0 | 66 | | { |
| | 0 | 67 | | var qry = PrepareGetNotDeletedGoodsQuery(filter, categoryId, activeOnly); |
| | 0 | 68 | | return await qry.CountAsync(); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | IQueryable<Good> PrepareGetNotDeletedGoodsQuery(string filter, long? categoryId, bool activeOnly) |
| | 0 | 72 | | { |
| | 0 | 73 | | var qry = PrepareNotDeletedGoods(); |
| | | 74 | | |
| | 0 | 75 | | if (activeOnly) |
| | 0 | 76 | | { |
| | 0 | 77 | | qry = qry.Where(x => x.RecState.Id == (long)RecordState.Active); |
| | 0 | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | if (categoryId.HasValue) |
| | 0 | 81 | | { |
| | 0 | 82 | | var categories = ExpandCategoryTree(new List<long>() { categoryId.Value }); |
| | 0 | 83 | | categories.Add(categoryId.Value); |
| | 0 | 84 | | qry = qry.Where(x => categories.Contains(x.Category.Id)); |
| | 0 | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if (!string.IsNullOrWhiteSpace(filter)) |
| | 0 | 88 | | { |
| | 0 | 89 | | qry = qry.Where(x => x.Name.ToUpper().Contains(filter.ToUpper()) |
| | 0 | 90 | | || x.DefaultBarCode.Code.ToUpper().Contains(filter.ToUpper()) |
| | 0 | 91 | | || x.GoodBarcodes.Any(g => g.BarCode.Code.ToUpper().Contains(filter.ToUpper())) |
| | 0 | 92 | | || (x.Manufacturer != null && x.Manufacturer.ShortName != null && x.Manufacturer.ShortName.ToUpper() |
| | 0 | 93 | | || (x.Manufacturer != null && x.Manufacturer.FullName != null && x.Manufacturer.FullName.ToUpper().C |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return qry; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | public List<long> ExpandCategoryTree(List<long> categories) |
| | 0 | 100 | | { |
| | 0 | 101 | | var children = _db.Categories |
| | 0 | 102 | | .Include(x => x.Parent) |
| | 0 | 103 | | .Include(d => d.RecState) |
| | 0 | 104 | | .Where(x => !x.IsDeleted && x.Parent != null && categories.Contains(x.Parent.Id)) |
| | 0 | 105 | | .Select(x => x.Id) |
| | 0 | 106 | | .ToList(); |
| | 0 | 107 | | if (children.Count > 0) |
| | 0 | 108 | | { |
| | 0 | 109 | | children.AddRange(ExpandCategoryTree(children)); |
| | 0 | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return children; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | //JOIN |
| | 66 | 116 | | public async Task<Good> GetGood(long id) => await PrepeareGoods().FirstOrDefaultAsync(d => d.Id == id); |
| | 1 | 117 | | public async Task<Good> GetGood(string mainBarcode) => await PrepeareGoods() |
| | 1 | 118 | | .FirstOrDefaultAsync(d => d.GoodBarcodes.Count(g => g.IsPrimary) > 0 |
| | 1 | 119 | | ? d.GoodBarcodes.FirstOrDefault(g => g.IsPrimary).BarCode.Code.ToLower().Equals(mainBarcode.NormalizeNam |
| | 1 | 120 | | : d.DefaultBarCode.Code.ToLower().Equals(mainBarcode.NormalizeName().ToLower())); |
| | | 121 | | //JOIN |
| | 0 | 122 | | public async Task<List<Good>> GetGoods(int page, int limit) => await PrepareNotDeletedGoods() |
| | 0 | 123 | | .Skip(page * limit) |
| | 0 | 124 | | .Take(limit) |
| | 0 | 125 | | .ToListAsync(); |
| | | 126 | | |
| | 0 | 127 | | public async Task<List<Good>> GetGoodsByCategory(int page, int limit, long categoryId) => await PrepareNotDelete |
| | 0 | 128 | | .Where(d => d.Category != null && d.Category.Id == categoryId) |
| | 0 | 129 | | .Skip(page * limit) |
| | 0 | 130 | | .Take(limit) |
| | 0 | 131 | | .ToListAsync(); |
| | 0 | 132 | | public async Task<List<Good>> GetGoodsBySuplier(int page, int limit, long suplierId) => await PrepareNotDeletedG |
| | 0 | 133 | | .Where(d => d.Supplier != null && d.Supplier.Id == suplierId) |
| | 0 | 134 | | .Skip(page * limit) |
| | 0 | 135 | | .Take(limit) |
| | 0 | 136 | | .ToListAsync(); |
| | | 137 | | |
| | 0 | 138 | | public async Task<List<Good>> GetActiveGoods(int page, int limit) => await PrepareNotDeletedGoods() |
| | 0 | 139 | | .Where(d => d.RecState.Id == (long)RecordState.Active) |
| | 0 | 140 | | .Skip(page * limit) |
| | 0 | 141 | | .Take(limit) |
| | 0 | 142 | | .ToListAsync(); |
| | | 143 | | |
| | | 144 | | public async Task<PaginatedData<List<Good>>> GetActiveGoods(int page, int limit, long departmentId, long categor |
| | 0 | 145 | | { |
| | 0 | 146 | | var goods = PrepareNotDeletedGoods(); |
| | 0 | 147 | | int total = await goods?.CountAsync(); |
| | 0 | 148 | | goods = goods.Where(d => d.RecState.Id == (long)RecordState.Active) |
| | 0 | 149 | | .Where(d => categoryId == 0 || d.Category.Id == categoryId) |
| | 0 | 150 | | .Where(d => string.IsNullOrWhiteSpace(filter) || d.GetActualVendorCode(departmentId).ToLower().Contains(filte |
| | 0 | 151 | | switch ((sort ?? "").ToLower()) |
| | | 152 | | { |
| | 0 | 153 | | case "name": goods = goods.OrderBy(x => x.Name); break; |
| | 0 | 154 | | case "name|desc": goods = goods.OrderByDescending(x => x.Name); break; |
| | 0 | 155 | | case "code": goods = goods.OrderBy(x => x.GetActualVendorCode(departmentId)); break; |
| | 0 | 156 | | case "code|desc": goods = goods.OrderByDescending(x => x.GetActualVendorCode(departmentId)); break; |
| | 0 | 157 | | default: goods = goods.OrderBy(x => x.Name); break; |
| | | 158 | | } |
| | 0 | 159 | | int filtered = await goods?.CountAsync(); |
| | 0 | 160 | | return new PaginatedData<List<Good>> |
| | 0 | 161 | | { |
| | 0 | 162 | | Result = await goods.Skip(page * limit).Take(limit).ToListAsync(), |
| | 0 | 163 | | TotalCount = total, |
| | 0 | 164 | | TotalFilteredCount = filtered |
| | 0 | 165 | | }; |
| | 0 | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | public IQueryable<Good> GetGoodsByName(string name) => PrepareNotDeletedGoods() |
| | 0 | 169 | | .AsNoTracking() |
| | 0 | 170 | | .Where(d => d.Name.ToUpper().Equals(name.NormalizeName().ToUpper())); |
| | | 171 | | |
| | 0 | 172 | | public async Task<List<Good>> SearchGoods(string name, int limit) => await _db.Goods |
| | 0 | 173 | | .AsNoTracking() |
| | 0 | 174 | | .Where(d => d.Name.ToUpper().StartsWith(name.NormalizeName().ToUpper())) |
| | 0 | 175 | | .Where(e => !e.IsDeleted) |
| | 0 | 176 | | .Take(limit) |
| | 0 | 177 | | .ToListAsync(); |
| | | 178 | | |
| | | 179 | | //JOIN |
| | 0 | 180 | | public IQueryable<Good> GetGoodsByBarcode(string barcode) => _db.Goods |
| | 0 | 181 | | .Include(d => d.GoodBarcodes) |
| | 0 | 182 | | .ThenInclude(b => b.BarCode) |
| | 0 | 183 | | .Where(d => !d.IsDeleted |
| | 0 | 184 | | && d.GoodBarcodes |
| | 0 | 185 | | .Any(b => b.BarCode.Code.ToUpper().Equals(barcode.NormalizeName().ToUpper()))); |
| | | 186 | | |
| | | 187 | | public Task<List<Good>> FindGoodsByBarCodes(List<string> codes) |
| | 0 | 188 | | { |
| | 0 | 189 | | return _db.Goods |
| | 0 | 190 | | .Include(d => d.GoodBarcodes).ThenInclude(b => b.BarCode) |
| | 0 | 191 | | .Include(d => d.DefaultBarCode) |
| | 0 | 192 | | .Expressionify() |
| | 0 | 193 | | .Where(d => !d.IsDeleted && codes.Contains(d.GetActualBarCode().ToLower())) |
| | 0 | 194 | | .AsNoTracking() |
| | 0 | 195 | | .ToListAsync(); |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | //JOIN |
| | 67 | 199 | | private IQueryable<Good> PrepeareGoods() => _db.Goods |
| | 67 | 200 | | .Include(d => d.Category).ThenInclude(d => d.Parent) |
| | 67 | 201 | | .Include(d => d.GoodBarcodes) |
| | 67 | 202 | | .ThenInclude(b => b.BarCode) |
| | 67 | 203 | | .Include(d => d.DefaultBarCode) |
| | 67 | 204 | | .Include(d => d.Barcodes) |
| | 67 | 205 | | .Include(d => d.Manufacturer) |
| | 67 | 206 | | .Include(d => d.Supplier) |
| | 67 | 207 | | .Include(d => d.Brand) |
| | 67 | 208 | | .Include(d => d.SubBrand) |
| | 67 | 209 | | .Include(d => d.Photos) |
| | 67 | 210 | | .Include(d => d.RecState) |
| | 67 | 211 | | .Include(d => d.VatsKind) |
| | 67 | 212 | | .Include(d => d.UnitsKind) |
| | 67 | 213 | | .Include(d => d.Country) |
| | 67 | 214 | | .Include(x => x.DepartmentGoodSettings).ThenInclude(x => x.Department) |
| | 67 | 215 | | .Include(d => d.Category) |
| | 67 | 216 | | .ThenInclude(x => x.DepartmentCategoryRatios).ThenInclude(x => x.Department) |
| | 67 | 217 | | .Include(d => d.Category) |
| | 67 | 218 | | .ThenInclude(x => x.DepartmentCategoryRatios).ThenInclude(x => x.Department) |
| | 67 | 219 | | .Include(x => x.Prices).ThenInclude(price => price.PriceTrend).ThenInclude(x => x.SupplierDepartment) |
| | 67 | 220 | | .Where(e => !e.IsDeleted) |
| | 67 | 221 | | .Expressionify() |
| | 67 | 222 | | .AsQueryable(); |
| | | 223 | | |
| | 0 | 224 | | private IQueryable<Good> PrepareNotDeletedGoods() => PrepeareGoods() |
| | 0 | 225 | | .Where(d => !d.IsDeleted); |
| | | 226 | | |
| | | 227 | | public async Task CreateGood(Good good) |
| | 368 | 228 | | { |
| | | 229 | | try |
| | 368 | 230 | | { |
| | 368 | 231 | | if (good.RecState != null) |
| | 368 | 232 | | _db.Entry(good.RecState).State = good.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 368 | 233 | | await _db.Goods.AddAsync(good); |
| | 368 | 234 | | await _db.SaveChangesAsync(CurrentUserId); |
| | | 235 | | |
| | 368 | 236 | | good.UpdateUniqueCode(); |
| | 368 | 237 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 368 | 238 | | } |
| | 0 | 239 | | catch (Exception e) |
| | 0 | 240 | | { |
| | 0 | 241 | | _logger.LogError("ошибка при создании товара"); |
| | 0 | 242 | | _logger.LogError(e, e.Message); |
| | 0 | 243 | | throw; |
| | | 244 | | } |
| | 368 | 245 | | } |
| | | 246 | | |
| | | 247 | | public async Task CreateGoodRange(List<Good> goods) |
| | 1 | 248 | | { |
| | 23 | 249 | | foreach (var good in goods) |
| | 10 | 250 | | { |
| | 10 | 251 | | if (good.Category != null) |
| | 10 | 252 | | { |
| | 10 | 253 | | _db.Entry(good.Category).State = good.Category.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 10 | 254 | | } |
| | 10 | 255 | | if (good.Manufacturer != null) |
| | 0 | 256 | | { |
| | 0 | 257 | | _db.Entry(good.Manufacturer).State = good.Manufacturer.Id == 0 ? EntityState.Detached : EntityState. |
| | 0 | 258 | | } |
| | 10 | 259 | | if (good.Supplier != null) |
| | 0 | 260 | | { |
| | 0 | 261 | | _db.Entry(good.Supplier).State = good.Supplier.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 0 | 262 | | } |
| | 10 | 263 | | if (good.Brand != null) |
| | 10 | 264 | | { |
| | 10 | 265 | | _db.Entry(good.Brand).State = good.Brand.Id == 0 ? EntityState.Detached : EntityState.Unchanged; |
| | 10 | 266 | | } |
| | 10 | 267 | | if (good.SubBrand != null) |
| | 0 | 268 | | { |
| | 0 | 269 | | _db.Entry(good.SubBrand).State = good.SubBrand.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 0 | 270 | | } |
| | 10 | 271 | | if (good.UnitsKind != null) |
| | 10 | 272 | | { |
| | 10 | 273 | | _db.Entry(good.UnitsKind).State = good.UnitsKind.Id == 0 ? EntityState.Detached : EntityState.Unchan |
| | 10 | 274 | | } |
| | 10 | 275 | | if (good.VatsKind != null) |
| | 10 | 276 | | { |
| | 10 | 277 | | _db.Entry(good.VatsKind).State = good.VatsKind.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 10 | 278 | | } |
| | 10 | 279 | | if (good.RecState != null) |
| | 10 | 280 | | { |
| | 10 | 281 | | _db.Entry(good.RecState).State = good.RecState.Id == 0 ? EntityState.Detached : EntityState.Unchange |
| | 10 | 282 | | } |
| | 10 | 283 | | } |
| | 1 | 284 | | await _db.Goods.AddRangeAsync(goods); |
| | 1 | 285 | | await _db.SaveChangesAsync(CurrentUserId); |
| | | 286 | | |
| | 23 | 287 | | foreach (var good in goods) |
| | 10 | 288 | | { |
| | 10 | 289 | | good.UpdateUniqueCode(); |
| | 10 | 290 | | } |
| | 1 | 291 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 1 | 292 | | } |
| | | 293 | | //JOIN |
| | | 294 | | public async Task UpdateGood(Good data) |
| | 0 | 295 | | { |
| | 0 | 296 | | if (!(await GoodExists(data.Id))) |
| | 0 | 297 | | { |
| | 0 | 298 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 299 | | } |
| | 0 | 300 | | _db.Goods.Update(data); |
| | 0 | 301 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 302 | | } |
| | | 303 | | public async Task SetPhoto(Good good, List<Photo> photo) |
| | 0 | 304 | | { |
| | 0 | 305 | | good.Photos = photo; |
| | 0 | 306 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 307 | | } |
| | | 308 | | public async Task DeleteGood(long goodId) |
| | 0 | 309 | | { |
| | 0 | 310 | | Good goodIn = _db.Goods.Where(e => !e.IsDeleted).FirstOrDefault(g => g.Id == goodId); |
| | 0 | 311 | | if (goodIn == null) |
| | 0 | 312 | | { |
| | 0 | 313 | | throw new KeyNotFoundException($"Record #{goodId} not found"); |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | if (goodIn.RecState?.Id == (long)RecordState.Empty) |
| | 0 | 317 | | { |
| | 0 | 318 | | _db.Entry(goodIn).State = EntityState.Deleted; |
| | 0 | 319 | | } |
| | | 320 | | else |
| | 0 | 321 | | { |
| | 0 | 322 | | goodIn.IsDeleted = true; |
| | 0 | 323 | | } |
| | 0 | 324 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 325 | | } |
| | 0 | 326 | | public async Task<bool> GoodExists(long id) => await _db.Goods |
| | 0 | 327 | | .AsNoTracking() |
| | 0 | 328 | | .Where(g => g.Id == id) |
| | 0 | 329 | | .Where(e => !e.IsDeleted) |
| | 0 | 330 | | .AnyAsync(); |
| | | 331 | | public async Task ChangeGoodState(long id, RecordState state) |
| | 0 | 332 | | { |
| | 0 | 333 | | Good goodIn = await _db.Goods.Where(e => !e.IsDeleted).FirstAsync(g => g.Id == id); |
| | 0 | 334 | | if (goodIn == null) |
| | 0 | 335 | | throw new KeyNotFoundException($"Record good #{id} not found"); |
| | 0 | 336 | | goodIn.RecState = _db.refRecordsState.Where(x=>!x.IsDeleted && x.Id == (long)state).FirstOrDefault(); |
| | 0 | 337 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 338 | | } |
| | | 339 | | |
| | | 340 | | public IQueryable<Good> GetShowcaseGoodsBaseQuery(long? categoryId, Cluster cluster, bool showNA) |
| | 0 | 341 | | { |
| | 0 | 342 | | var pricesIds = _db.PriceTrendDetails |
| | 0 | 343 | | .Include(x => x.PriceTrend) |
| | 0 | 344 | | .Where(x => !x.IsDeleted && x.PriceTrend.SupplierDepartmentId == cluster.WarehouseId && x.PriceTrend.Beg |
| | 0 | 345 | | .Select(x => x.Id) |
| | 0 | 346 | | .ToList(); |
| | | 347 | | // для данного склада коды видимых товаров |
| | 0 | 348 | | var settings = _db.DepartmentGoodSetting |
| | 0 | 349 | | .Where(x => !x.IsDeleted && x.DepartmentId == cluster.WarehouseId && x.ShowcaseVisible) |
| | 0 | 350 | | .Select(x => x.GoodId) |
| | 0 | 351 | | .ToList(); |
| | 0 | 352 | | var qry = _db.Goods |
| | 0 | 353 | | .Include(d => d.Category).ThenInclude(x => x.DepartmentCategoryRatios).ThenInclude(x => x.Department) |
| | 0 | 354 | | .Include(d => d.GoodBarcodes) |
| | 0 | 355 | | .ThenInclude(barcode => barcode.BarCode) |
| | 0 | 356 | | .Include(d => d.DefaultBarCode) |
| | 0 | 357 | | .Include(d => d.Barcodes) |
| | 0 | 358 | | .Include(d => d.Manufacturer) |
| | 0 | 359 | | .Include(d => d.Supplier) |
| | 0 | 360 | | .Include(d => d.Brand) |
| | 0 | 361 | | .Include(d => d.SubBrand) |
| | 0 | 362 | | .Include(d => d.Photos) |
| | 0 | 363 | | .Include(d => d.RecState) |
| | 0 | 364 | | .Include(d => d.VatsKind) |
| | 0 | 365 | | .Include(d => d.UnitsKind) |
| | 0 | 366 | | .Include(d => d.Country) |
| | 0 | 367 | | .Include(d => d.Prices).ThenInclude(x => x.PriceTrend) |
| | 0 | 368 | | .Include(d => d.Rests) |
| | 0 | 369 | | .Include(x => x.DepartmentGoodSettings).ThenInclude(x => x.GoodSettingsLabels).ThenInclude(x => x.GoodLa |
| | 0 | 370 | | .Include(d => d.Rests) |
| | 0 | 371 | | .Include(d => d.Prices).ThenInclude(x => x.PriceTrend) |
| | 0 | 372 | | .Expressionify() |
| | 0 | 373 | | .Where(e => !e.IsDeleted && e.RecState.Id == (int)RecordState.Active && !e.Category.IsDeleted |
| | 0 | 374 | | // только отображаемые товары для данного склада |
| | 0 | 375 | | && settings.Contains(e.Id) |
| | 0 | 376 | | // у товара должны быть действующие переоценки |
| | 0 | 377 | | && e.Prices.Any(x => pricesIds.Contains(x.Id))); |
| | | 378 | | |
| | | 379 | | // либо показываем все, либо должны быть остатки |
| | 0 | 380 | | if (!showNA) |
| | 0 | 381 | | { |
| | 0 | 382 | | var restsIds = _db.Rests |
| | 0 | 383 | | .Where(x => !x.IsDeleted && x.DepartmentId == cluster.WarehouseId && x.Quantity > 0) |
| | 0 | 384 | | .Select(x => x.GoodId) |
| | 0 | 385 | | .ToList(); |
| | 0 | 386 | | qry = qry.Where(x => restsIds.Contains(x.Id)); |
| | 0 | 387 | | } |
| | | 388 | | |
| | 0 | 389 | | if (categoryId.HasValue) |
| | 0 | 390 | | { |
| | 0 | 391 | | var cats = ExpandCategoryTree(new List<long> { categoryId.Value }); |
| | 0 | 392 | | cats.Add(categoryId.Value); |
| | 0 | 393 | | qry = qry.Where(e => cats.Contains(e.Category.Id)); |
| | 0 | 394 | | } |
| | | 395 | | |
| | 0 | 396 | | return qry; |
| | 0 | 397 | | } |
| | | 398 | | |
| | | 399 | | public IQueryable<Good> GetFilteredShowcaseGoodsQuery(IQueryable<Good> goodsQuery, Cluster cluster, decimal cont |
| | | 400 | | string filter, List<long> brands, List<long> countries, List<decimal> minQuantities, decimal? priceFrom, dec |
| | 0 | 401 | | { |
| | 0 | 402 | | if (!string.IsNullOrWhiteSpace(filter)) |
| | 0 | 403 | | { |
| | 0 | 404 | | goodsQuery = goodsQuery.Where(x => x.Name.ToUpper().Contains(filter.ToUpper()) |
| | 0 | 405 | | || x.DefaultBarCode.Code.ToUpper().Contains(filter.ToUpper()) |
| | 0 | 406 | | || x.GoodBarcodes.Any(g => g.BarCode.Code.ToUpper().Contains(filter.ToUpper()))); |
| | 0 | 407 | | } |
| | | 408 | | |
| | 0 | 409 | | if (!string.IsNullOrWhiteSpace(labelName)) |
| | 0 | 410 | | { |
| | 0 | 411 | | goodsQuery = goodsQuery.Where(x => x.DepartmentGoodSettings |
| | 0 | 412 | | .Any(d => d.DepartmentId == cluster.WarehouseId |
| | 0 | 413 | | && d.GoodSettingsLabels.Any(l => |
| | 0 | 414 | | !l.GoodLabel.IsDeleted |
| | 0 | 415 | | && l.GoodLabel.Name.ToUpper().Equals(labelName.ToUpper())))); |
| | 0 | 416 | | } |
| | | 417 | | |
| | 0 | 418 | | if (brands != null && brands.Any()) |
| | 0 | 419 | | { |
| | 0 | 420 | | goodsQuery = goodsQuery.Where(x => brands.Contains(x.Brand.Id)); |
| | 0 | 421 | | } |
| | | 422 | | |
| | 0 | 423 | | if (countries != null && countries.Any()) |
| | 0 | 424 | | { |
| | 0 | 425 | | goodsQuery = goodsQuery.Where(x => countries.Contains(x.Country.Id)); |
| | 0 | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | if (minQuantities != null && minQuantities.Any()) |
| | 0 | 429 | | { |
| | 0 | 430 | | goodsQuery = goodsQuery.Where(x => minQuantities.Contains(x.DepartmentGoodSettings.ActualMinQuantity(clu |
| | 0 | 431 | | } |
| | | 432 | | |
| | 0 | 433 | | if(priceFrom.HasValue) |
| | 0 | 434 | | { |
| | 0 | 435 | | goodsQuery = goodsQuery.Where(x => x.CurrentPrice(cluster, contractRatio) >= priceFrom.Value); |
| | 0 | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | if (priceTo.HasValue) |
| | 0 | 439 | | { |
| | 0 | 440 | | goodsQuery = goodsQuery.Where(x => x.CurrentPrice(cluster, contractRatio) <= priceTo.Value); |
| | 0 | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | return goodsQuery; |
| | 0 | 444 | | } |
| | | 445 | | |
| | | 446 | | public IQueryable<Good> GetPagedShowcaseGoodsQuery(IQueryable<Good> goodsQuery, Cluster cluster, int page, int l |
| | 0 | 447 | | { |
| | 0 | 448 | | switch ((sort ?? "").ToLower()) |
| | | 449 | | { |
| | 0 | 450 | | case "name": goodsQuery = goodsQuery.OrderBy(x => x.Name); break; |
| | 0 | 451 | | case "name|desc": goodsQuery = goodsQuery.OrderByDescending(x => x.Name); break; |
| | 0 | 452 | | case "brandname": goodsQuery = goodsQuery.OrderBy(x => x.Brand.Name); break; |
| | 0 | 453 | | case "brandname|desc": goodsQuery = goodsQuery.OrderByDescending(x => x.Brand.Name); break; |
| | 0 | 454 | | case "price": goodsQuery = goodsQuery.OrderBy(x => x.Prices.Actual(cluster.WarehouseId).PriceNew); break |
| | 0 | 455 | | case "price|desc": goodsQuery = goodsQuery.OrderByDescending(x => x.Prices.Actual(cluster.WarehouseId).P |
| | 0 | 456 | | case "discount": goodsQuery = goodsQuery.OrderBy(x => x.Prices.Actual(cluster.WarehouseId).Discount); br |
| | 0 | 457 | | case "discount|desc": goodsQuery = goodsQuery.OrderByDescending(x => x.Prices.Actual(cluster.WarehouseId |
| | 0 | 458 | | case "countlabel|desc": goodsQuery = goodsQuery.OrderByDescending(x => x.DepartmentGoodSettings.LabelsCo |
| | 0 | 459 | | case "countlabel": goodsQuery = goodsQuery.OrderBy(x => x.DepartmentGoodSettings.LabelsCount(cluster.War |
| | 0 | 460 | | default: goodsQuery = goodsQuery.OrderBy(x => x.Id); break; |
| | | 461 | | } |
| | 0 | 462 | | return goodsQuery.Skip(page * limit).Take(limit); |
| | 0 | 463 | | } |
| | | 464 | | |
| | | 465 | | public async Task<Good> GetShowcaseGood(long goodId, Cluster cluster) |
| | 0 | 466 | | { |
| | 0 | 467 | | return await GetShowcaseGoodsBaseQuery(null, cluster, true).FirstOrDefaultAsync(x => x.Id == goodId); |
| | 0 | 468 | | } |
| | | 469 | | |
| | | 470 | | public async Task<List<Good>> FindGoodsByNames(List<string> names) |
| | 0 | 471 | | { |
| | 0 | 472 | | return await _db.Goods |
| | 0 | 473 | | .Include(x => x.Category) |
| | 0 | 474 | | .Include(x => x.Country) |
| | 0 | 475 | | .Include(x => x.Brand) |
| | 0 | 476 | | .Include(x => x.SubBrand) |
| | 0 | 477 | | .Include(x => x.Photos) |
| | 0 | 478 | | .Where(x => !x.IsDeleted && x.Category != null && names.Contains(x.Name.ToLower())) |
| | 0 | 479 | | .ToListAsync(); |
| | 0 | 480 | | } |
| | | 481 | | |
| | | 482 | | public async Task<List<Good>> FindGoodsByUniqueCodes(List<string> codes) |
| | 0 | 483 | | { |
| | 0 | 484 | | return await _db.Goods |
| | 0 | 485 | | .Include(x => x.Category) |
| | 0 | 486 | | .Include(x => x.Country) |
| | 0 | 487 | | .Include(x => x.Brand) |
| | 0 | 488 | | .Include(x => x.SubBrand) |
| | 0 | 489 | | .Include(x => x.Photos) |
| | 0 | 490 | | .Where(x => !x.IsDeleted && x.Category != null && codes.Contains(x.UniqueCode.ToLower())) |
| | 0 | 491 | | .ToListAsync(); |
| | 0 | 492 | | } |
| | | 493 | | |
| | | 494 | | public async Task<List<Good>> FindGoodsByVendorsCodes(List<string> codes, long departmentId) |
| | 0 | 495 | | { |
| | 0 | 496 | | return await _db.Goods |
| | 0 | 497 | | .Include(x => x.Category) |
| | 0 | 498 | | .Include(x => x.DepartmentGoodSettings) |
| | 0 | 499 | | .Expressionify() |
| | 0 | 500 | | .Where(x => !x.IsDeleted && x.Category != null && codes.Contains(x.GetActualVendorCode(departmentId).ToL |
| | 0 | 501 | | .ToListAsync(); |
| | 0 | 502 | | } |
| | | 503 | | |
| | | 504 | | public async Task<List<Good>> FindGoodsByIds(List<long> ids) |
| | 0 | 505 | | { |
| | 0 | 506 | | return await _db.Goods |
| | 0 | 507 | | .Include(x => x.Category) |
| | 0 | 508 | | .Where(x => !x.IsDeleted && x.Category != null && ids.Contains(x.Id)) |
| | 0 | 509 | | .ToListAsync(); |
| | 0 | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <summary> |
| | | 513 | | /// Возвращает список товаров для работы в заявках\отгрузках |
| | | 514 | | /// </summary> |
| | | 515 | | /// <param name="goodsId">список идентификаторов</param> |
| | | 516 | | /// <returns></returns> |
| | 146 | 517 | | public async Task<List<Good>> GetGoods(List<long> goodsId) => await _db.Goods |
| | 146 | 518 | | .Include(d => d.Rests) |
| | 146 | 519 | | .Include(x => x.Prices) |
| | 146 | 520 | | .ThenInclude(x => x.PriceTrend) |
| | 146 | 521 | | .ThenInclude(x => x.SupplierDepartment) |
| | 146 | 522 | | .Include(Good => Good.VatsKind) |
| | 146 | 523 | | .Include(Good => Good.DefaultBarCode) |
| | 146 | 524 | | .Include(Good => Good.GoodBarcodes) |
| | 146 | 525 | | .ThenInclude(barcode => barcode.BarCode) |
| | 146 | 526 | | .Include(good => good.Category) |
| | 146 | 527 | | .ThenInclude(category => category.DepartmentCategoryRatios) |
| | 146 | 528 | | .Include(Good => Good.Photos) |
| | 146 | 529 | | .Include(Good => Good.DepartmentGoodSettings) |
| | 146 | 530 | | .ThenInclude(DepartmentGoodSetting => DepartmentGoodSetting.Department) |
| | 146 | 531 | | .Where(d => !d.IsDeleted) |
| | 146 | 532 | | .Where(d => goodsId.Contains(d.Id)) |
| | 146 | 533 | | .ToListAsync(); |
| | | 534 | | } |
| | | 535 | | } |