-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCustomerOrderDocumentBuilder.cs
More file actions
192 lines (160 loc) · 8.23 KB
/
CustomerOrderDocumentBuilder.cs
File metadata and controls
192 lines (160 loc) · 8.23 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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VirtoCommerce.InventoryModule.Core.Services;
using VirtoCommerce.OrdersModule.Core.Model;
using VirtoCommerce.OrdersModule.Core.Services;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.DynamicProperties;
using VirtoCommerce.SearchModule.Core.Extensions;
using VirtoCommerce.SearchModule.Core.Model;
using VirtoCommerce.SearchModule.Core.Services;
using VirtoCommerce.StoreModule.Core.Model;
using VirtoCommerce.StoreModule.Core.Services;
namespace VirtoCommerce.OrdersModule.Data.Search.Indexed
{
public class CustomerOrderDocumentBuilder : IIndexSchemaBuilder, IIndexDocumentBuilder
{
private readonly ICustomerOrderService _customerOrderService;
private readonly IDynamicPropertySearchService _dynamicPropertySearchService;
private readonly IStoreService _storeService;
private readonly IFulfillmentCenterService _fulfillmentCenterService;
public CustomerOrderDocumentBuilder(
ICustomerOrderService customerOrderService,
IDynamicPropertySearchService dynamicPropertySearchService,
IStoreService storeService,
IFulfillmentCenterService fulfillmentCenterService)
{
_customerOrderService = customerOrderService;
_dynamicPropertySearchService = dynamicPropertySearchService;
_storeService = storeService;
_fulfillmentCenterService = fulfillmentCenterService;
}
public Task BuildSchemaAsync(IndexDocument schema)
{
schema.AddFilterableStringAndContentString("Number");
schema.AddFilterableStringAndContentString("EmployeeName");
schema.AddFilterableStringAndContentString("OrganizationName");
schema.AddFilterableStringAndContentString("CustomerName");
schema.AddFilterableStringAndContentString("PurchaseOrderNumber");
schema.AddFilterableDateTime("CreatedDate");
schema.AddFilterableDateTime("ModifiedDate");
schema.AddFilterableString("CreatedBy");
schema.AddFilterableString("ModifiedBy");
schema.AddFilterableString("CustomerId");
schema.AddFilterableString("EmployeeId");
schema.AddFilterableString("OrganizationId");
schema.AddFilterableString("StoreId");
schema.AddFilterableString("StoreName");
schema.AddFilterableString("OuterId");
schema.AddFilterableString("Status");
schema.AddFilterableString("Currency");
schema.AddFilterableString("PromotionId");
schema.AddFilterableDecimal("Total");
schema.AddFilterableDecimal("SubTotal");
schema.AddFilterableDecimal("TaxTotal");
schema.AddFilterableDecimal("DiscountTotal");
schema.AddFilterableBoolean("IsCancelled");
schema.AddFilterableBoolean("IsPrototype");
return schema.AddDynamicProperties(_dynamicPropertySearchService, typeof(CustomerOrder).FullName);
}
public async Task<IList<IndexDocument>> GetDocumentsAsync(IList<string> documentIds)
{
var result = new List<IndexDocument>();
var orders = await _customerOrderService.GetNoCloneAsync(documentIds);
foreach (var order in orders)
{
result.Add(await CreateDocument(order));
}
return result;
}
protected virtual async Task<IndexDocument> CreateDocument(CustomerOrder order)
{
var document = new IndexDocument(order.Id);
document.AddFilterableStringAndContentString("Number", order.Number);
document.AddFilterableStringAndContentString("EmployeeName", order.EmployeeName);
document.AddFilterableStringAndContentString("OrganizationName", order.OrganizationName);
document.AddFilterableStringAndContentString("CustomerName", order.CustomerName);
document.AddFilterableStringAndContentString("PurchaseOrderNumber", order.PurchaseOrderNumber);
document.AddContentString(order.Comment);
document.AddFilterableDateTime("CreatedDate", order.CreatedDate);
document.AddFilterableDateTime("ModifiedDate", order.ModifiedDate ?? order.CreatedDate);
document.AddFilterableString("CreatedBy", order.CreatedBy);
document.AddFilterableString("ModifiedBy", order.ModifiedBy);
document.AddFilterableString("CustomerId", order.CustomerId);
document.AddFilterableString("EmployeeId", order.EmployeeId);
document.AddFilterableString("OrganizationId", order.OrganizationId);
document.AddFilterableString("StoreId", order.StoreId);
if (!order.StoreName.IsNullOrEmpty())
{
document.AddFilterableString("StoreName", order.StoreName);
}
else if (!order.StoreId.IsNullOrEmpty())
{
var store = await _storeService.GetNoCloneAsync(order.StoreId, StoreResponseGroup.StoreInfo.ToString());
document.AddFilterableString("StoreName", store?.Name);
}
document.AddFilterableString("OuterId", order.OuterId);
document.AddFilterableString("Status", order.Status);
document.AddFilterableString("Currency", order.Currency);
document.AddFilterableDecimal("Total", order.Total);
document.AddFilterableDecimal("SubTotal", order.SubTotal);
document.AddFilterableDecimal("TaxTotal", order.TaxTotal);
document.AddFilterableDecimal("DiscountTotal", order.DiscountTotal);
document.AddFilterableBoolean("IsCancelled", order.IsCancelled);
document.AddFilterableBoolean("IsPrototype", order.IsPrototype);
IndexDiscounts(order.Discounts, document);
foreach (var address in order.Addresses ?? Enumerable.Empty<Address>())
{
IndexAddress(address, document);
}
foreach (var lineItem in order.Items ?? Enumerable.Empty<LineItem>())
{
IndexDiscounts(lineItem.Discounts, document);
document.AddContentString(lineItem.Comment);
}
foreach (var payment in order.InPayments ?? Enumerable.Empty<PaymentIn>())
{
IndexAddress(payment.BillingAddress, document);
IndexDiscounts(payment.Discounts, document);
document.AddContentString(payment.Number);
document.AddContentString(payment.Comment);
}
foreach (var shipment in order.Shipments ?? Enumerable.Empty<Shipment>())
{
IndexAddress(shipment.DeliveryAddress, document);
IndexDiscounts(shipment.Discounts, document);
document.AddContentString(shipment.Number);
document.AddContentString(shipment.Comment);
if (!shipment.FulfillmentCenterName.IsNullOrEmpty())
{
document.AddContentString(shipment.FulfillmentCenterName);
}
else if (!shipment.FulfillmentCenterId.IsNullOrEmpty())
{
var fulfillmentCenter = await _fulfillmentCenterService.GetNoCloneAsync(shipment.FulfillmentCenterId);
document.AddContentString(fulfillmentCenter?.Name);
}
}
await document.AddDynamicProperties(_dynamicPropertySearchService, order);
return document;
}
protected virtual void IndexAddress(Address address, IndexDocument document)
{
if (address != null)
{
document.AddContentString($"{address.AddressType} {address}");
}
}
protected virtual void IndexDiscounts(ICollection<VirtoCommerce.CoreModule.Core.Common.Discount> discounts, IndexDocument document)
{
if(discounts!=null)
{
foreach (var discount in discounts.Where(d => d != null && !string.IsNullOrEmpty(d.PromotionId)))
{
document.AddFilterableString("PromotionId", discount.PromotionId);
}
}
}
}
}