-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathManufacturerService.cs
More file actions
324 lines (262 loc) · 11.9 KB
/
Copy pathManufacturerService.cs
File metadata and controls
324 lines (262 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using SmartStore.Collections;
using SmartStore.Core;
using SmartStore.Core.Caching;
using SmartStore.Core.Data;
using SmartStore.Core.Domain.Catalog;
using SmartStore.Core.Domain.Stores;
using SmartStore.Core.Events;
using SmartStore.Data.Caching;
namespace SmartStore.Services.Catalog
{
public partial class ManufacturerService : IManufacturerService
{
private const string PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY = "SmartStore.productmanufacturer.allbymanufacturerid-{0}-{1}-{2}-{3}-{4}";
private const string PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY = "SmartStore.productmanufacturer.allbyproductid-{0}-{1}-{2}";
private const string MANUFACTURERS_PATTERN_KEY = "SmartStore.manufacturer.*";
private const string PRODUCTMANUFACTURERS_PATTERN_KEY = "SmartStore.productmanufacturer.*";
private readonly IRepository<Manufacturer> _manufacturerRepository;
private readonly IRepository<ProductManufacturer> _productManufacturerRepository;
private readonly IRepository<Product> _productRepository;
private readonly IRepository<StoreMapping> _storeMappingRepository;
private readonly IWorkContext _workContext;
private readonly IStoreContext _storeContext;
private readonly IEventPublisher _eventPublisher;
private readonly IRequestCache _requestCache;
public ManufacturerService(IRequestCache requestCache,
IRepository<Manufacturer> manufacturerRepository,
IRepository<ProductManufacturer> productManufacturerRepository,
IRepository<Product> productRepository,
IRepository<StoreMapping> storeMappingRepository,
IWorkContext workContext,
IStoreContext storeContext,
IEventPublisher eventPublisher)
{
_requestCache = requestCache;
_manufacturerRepository = manufacturerRepository;
_productManufacturerRepository = productManufacturerRepository;
_productRepository = productRepository;
_storeMappingRepository = storeMappingRepository;
_workContext = workContext;
_storeContext = storeContext;
_eventPublisher = eventPublisher;
this.QuerySettings = DbQuerySettings.Default;
}
public DbQuerySettings QuerySettings { get; set; }
public virtual void DeleteManufacturer(Manufacturer manufacturer)
{
if (manufacturer == null)
throw new ArgumentNullException("manufacturer");
manufacturer.Deleted = true;
UpdateManufacturer(manufacturer);
}
public virtual IQueryable<Manufacturer> GetManufacturers(bool showHidden = false, int storeId = 0)
{
var query = _manufacturerRepository.Table
.Where(m => !m.Deleted);
if (!showHidden)
query = query.Where(m => m.Published);
if (!QuerySettings.IgnoreMultiStore && storeId > 0)
{
query = from m in query
join sm in _storeMappingRepository.Table
on new { c1 = m.Id, c2 = "Manufacturer" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into m_sm
from sm in m_sm.DefaultIfEmpty()
where !m.LimitedToStores || storeId == sm.StoreId
select m;
query = from m in query
group m by m.Id into mGroup
orderby mGroup.Key
select mGroup.FirstOrDefault();
}
return query;
}
public virtual IList<Manufacturer> GetAllManufacturers(bool showHidden = false)
{
return GetAllManufacturers(null, 0, showHidden);
}
public virtual IList<Manufacturer> GetAllManufacturers(string manufacturerName, int storeId = 0, bool showHidden = false)
{
var query = GetManufacturers(showHidden, storeId);
if (manufacturerName.HasValue())
query = query.Where(m => m.Name.Contains(manufacturerName));
query = query.OrderBy(m => m.DisplayOrder)
.ThenBy(m => m.Name);
var manufacturers = query.ToList();
return manufacturers;
}
public virtual IPagedList<Manufacturer> GetAllManufacturers(string manufacturerName,
int pageIndex, int pageSize, int storeId = 0, bool showHidden = false)
{
var manufacturers = GetAllManufacturers(manufacturerName, storeId, showHidden);
return new PagedList<Manufacturer>(manufacturers, pageIndex, pageSize);
}
public virtual Manufacturer GetManufacturerById(int manufacturerId)
{
if (manufacturerId == 0)
return null;
return _manufacturerRepository.GetByIdCached(manufacturerId, "db.manu.id-" + manufacturerId);
}
public virtual void InsertManufacturer(Manufacturer manufacturer)
{
if (manufacturer == null)
throw new ArgumentNullException("manufacturer");
_manufacturerRepository.Insert(manufacturer);
//cache
_requestCache.RemoveByPattern(MANUFACTURERS_PATTERN_KEY);
_requestCache.RemoveByPattern(PRODUCTMANUFACTURERS_PATTERN_KEY);
}
public virtual void UpdateManufacturer(Manufacturer manufacturer)
{
if (manufacturer == null)
throw new ArgumentNullException("manufacturer");
_manufacturerRepository.Update(manufacturer);
//cache
_requestCache.RemoveByPattern(MANUFACTURERS_PATTERN_KEY);
_requestCache.RemoveByPattern(PRODUCTMANUFACTURERS_PATTERN_KEY);
}
public virtual void UpdateHasDiscountsApplied(Manufacturer manufacturer)
{
Guard.NotNull(manufacturer, nameof(manufacturer));
manufacturer.HasDiscountsApplied = manufacturer.AppliedDiscounts.Count > 0;
UpdateManufacturer(manufacturer);
}
public virtual void DeleteProductManufacturer(ProductManufacturer productManufacturer)
{
if (productManufacturer == null)
throw new ArgumentNullException("productManufacturer");
_productManufacturerRepository.Delete(productManufacturer);
_requestCache.RemoveByPattern(MANUFACTURERS_PATTERN_KEY);
_requestCache.RemoveByPattern(PRODUCTMANUFACTURERS_PATTERN_KEY);
}
public virtual IPagedList<ProductManufacturer> GetProductManufacturersByManufacturerId(int manufacturerId, int pageIndex, int pageSize, bool showHidden = false)
{
if (manufacturerId == 0)
return new PagedList<ProductManufacturer>(new List<ProductManufacturer>(), pageIndex, pageSize);
string key = string.Format(PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY, showHidden, manufacturerId, pageIndex, pageSize, _workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id);
return _requestCache.Get(key, () =>
{
var query = from pm in _productManufacturerRepository.Table
join p in _productRepository.Table on pm.ProductId equals p.Id
where pm.ManufacturerId == manufacturerId &&
!p.Deleted && !p.IsSystemProduct &&
(showHidden || p.Published)
orderby pm.DisplayOrder
select pm;
if (!showHidden)
{
if (!QuerySettings.IgnoreMultiStore)
{
//Store mapping
var currentStoreId = _storeContext.CurrentStore.Id;
query = from pm in query
join m in _manufacturerRepository.Table on pm.ManufacturerId equals m.Id
join sm in _storeMappingRepository.Table
on new { c1 = m.Id, c2 = "Manufacturer" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into m_sm
from sm in m_sm.DefaultIfEmpty()
where !m.LimitedToStores || currentStoreId == sm.StoreId
select pm;
}
//only distinct manufacturers (group by ID)
query = from pm in query
group pm by pm.Id into pmGroup
orderby pmGroup.Key
select pmGroup.FirstOrDefault();
query = query.OrderBy(pm => pm.DisplayOrder);
}
var productManufacturers = new PagedList<ProductManufacturer>(query, pageIndex, pageSize);
return productManufacturers;
});
}
public virtual IList<ProductManufacturer> GetProductManufacturersByProductId(int productId, bool showHidden = false)
{
if (productId == 0)
return new List<ProductManufacturer>();
string key = string.Format(PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY, showHidden, productId, _workContext.CurrentCustomer.Id, _storeContext.CurrentStore.Id);
return _requestCache.Get(key, () =>
{
var query = from pm in _productManufacturerRepository.Table
join m in _manufacturerRepository.Table on pm.ManufacturerId equals m.Id
where pm.ProductId == productId && !m.Deleted && (showHidden || m.Published)
orderby pm.DisplayOrder
select pm;
query = query.Include(x => x.Manufacturer.Picture);
if (!showHidden)
{
if (!QuerySettings.IgnoreMultiStore)
{
// Store mapping
var currentStoreId = _storeContext.CurrentStore.Id;
query = from pm in query
join m in _manufacturerRepository.Table on pm.ManufacturerId equals m.Id
join sm in _storeMappingRepository.Table
on new { c1 = m.Id, c2 = "Manufacturer" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into m_sm
from sm in m_sm.DefaultIfEmpty()
where !m.LimitedToStores || currentStoreId == sm.StoreId
select pm;
}
// Only distinct manufacturers (group by ID)
query = from pm in query
group pm by pm.Id into mGroup
orderby mGroup.Key
select mGroup.FirstOrDefault();
query = query.OrderBy(pm => pm.DisplayOrder);
}
var productManufacturers = query.ToList();
return productManufacturers;
});
}
public virtual Multimap<int, ProductManufacturer> GetProductManufacturersByManufacturerIds(int[] manufacturerIds)
{
Guard.NotNull(manufacturerIds, nameof(manufacturerIds));
var query = _productManufacturerRepository.TableUntracked
.Where(x => manufacturerIds.Contains(x.ManufacturerId))
.OrderBy(x => x.DisplayOrder);
var map = query
.ToList()
.ToMultimap(x => x.ManufacturerId, x => x);
return map;
}
public virtual Multimap<int, ProductManufacturer> GetProductManufacturersByProductIds(int[] productIds)
{
Guard.NotNull(productIds, nameof(productIds));
var query =
from pm in _productManufacturerRepository.TableUntracked
//join m in _manufacturerRepository.TableUntracked on pm.ManufacturerId equals m.Id // Eager loading does not work with this join
where !pm.Manufacturer.Deleted && productIds.Contains(pm.ProductId)
select pm;
query = query.Include(x => x.Manufacturer.Picture);
var map = query
.OrderBy(x => x.ProductId)
.ThenBy(x => x.DisplayOrder)
.ToList()
.ToMultimap(x => x.ProductId, x => x);
return map;
}
public virtual ProductManufacturer GetProductManufacturerById(int productManufacturerId)
{
if (productManufacturerId == 0)
return null;
return _productManufacturerRepository.GetById(productManufacturerId);
}
public virtual void InsertProductManufacturer(ProductManufacturer productManufacturer)
{
if (productManufacturer == null)
throw new ArgumentNullException("productManufacturer");
_productManufacturerRepository.Insert(productManufacturer);
_requestCache.RemoveByPattern(MANUFACTURERS_PATTERN_KEY);
_requestCache.RemoveByPattern(PRODUCTMANUFACTURERS_PATTERN_KEY);
}
public virtual void UpdateProductManufacturer(ProductManufacturer productManufacturer)
{
if (productManufacturer == null)
throw new ArgumentNullException("productManufacturer");
_productManufacturerRepository.Update(productManufacturer);
_requestCache.RemoveByPattern(MANUFACTURERS_PATTERN_KEY);
_requestCache.RemoveByPattern(PRODUCTMANUFACTURERS_PATTERN_KEY);
}
}
}