-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCustomerOrderChangesProvider.cs
More file actions
122 lines (102 loc) · 4.53 KB
/
CustomerOrderChangesProvider.cs
File metadata and controls
122 lines (102 loc) · 4.53 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VirtoCommerce.OrdersModule.Core.Model;
using VirtoCommerce.OrdersModule.Data.Repositories;
using VirtoCommerce.Platform.Core.ChangeLog;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.SearchModule.Core.Model;
using VirtoCommerce.SearchModule.Core.Services;
namespace VirtoCommerce.OrdersModule.Data.Search.Indexed
{
public class CustomerOrderChangesProvider : IIndexDocumentChangesProvider
{
public const string ChangeLogObjectType = nameof(CustomerOrder);
protected Func<IOrderRepository> OrderRepositoryFactory { get; }
protected IChangeLogSearchService ChangeLogSearchService { get; }
public CustomerOrderChangesProvider(Func<IOrderRepository> orderRepositoryFactory, IChangeLogSearchService changeLogSearchService)
{
OrderRepositoryFactory = orderRepositoryFactory;
ChangeLogSearchService = changeLogSearchService;
}
public virtual async Task<IList<IndexDocumentChange>> GetChangesAsync(DateTime? startDate, DateTime? endDate, long skip, long take)
{
if (startDate == null && endDate == null)
{
return GetChangesFromRepository(skip, take);
}
return await GetChangesFromOperationLog(startDate, endDate, skip, take);
}
public virtual async Task<long> GetTotalChangesCountAsync(DateTime? startDate, DateTime? endDate)
{
if (startDate == null && endDate == null)
{
// Get total products count
using var repository = OrderRepositoryFactory();
return repository.CustomerOrders.Count();
}
var criteria = GetChangeLogSearchCriteria(startDate, endDate, 0, 0);
// Get changes count from operation log
return (await ChangeLogSearchService.SearchAsync(criteria)).TotalCount;
}
/// <summary>
/// Get documents from repository and return them as changes
/// </summary>
protected virtual IList<IndexDocumentChange> GetChangesFromRepository(long skip, long take)
{
using var repository = OrderRepositoryFactory();
var productIds = repository.CustomerOrders
.OrderBy(x => x.CreatedDate)
.Select(x => new { x.Id, ModifiedDate = x.ModifiedDate ?? x.CreatedDate })
.Skip((int)skip)
.Take((int)take)
.ToArray();
return productIds
.Select(x =>
new IndexDocumentChange
{
DocumentId = x.Id,
ChangeType = IndexDocumentChangeType.Modified,
ChangeDate = x.ModifiedDate,
})
.ToArray();
}
/// <summary>
/// Get changes from operation log
/// </summary>
protected virtual async Task<IList<IndexDocumentChange>> GetChangesFromOperationLog(DateTime? startDate, DateTime? endDate, long skip, long take)
{
var criteria = GetChangeLogSearchCriteria(startDate, endDate, skip, take);
var operations = (await ChangeLogSearchService.SearchAsync(criteria)).Results;
return operations
.Select(x =>
new IndexDocumentChange
{
DocumentId = x.ObjectId,
ChangeType = x.OperationType == EntryState.Deleted ? IndexDocumentChangeType.Deleted : IndexDocumentChangeType.Modified,
ChangeDate = x.ModifiedDate ?? x.CreatedDate,
})
.ToArray();
}
protected virtual ChangeLogSearchCriteria GetChangeLogSearchCriteria(DateTime? startDate, DateTime? endDate, long skip, long take)
{
var criteria = AbstractTypeFactory<ChangeLogSearchCriteria>.TryCreateInstance();
var types = AbstractTypeFactory<CustomerOrder>.AllTypeInfos.Select(x => x.TypeName).ToList();
if (types.Count != 0)
{
types.Add(ChangeLogObjectType);
criteria.ObjectTypes = types;
}
else
{
criteria.ObjectType = ChangeLogObjectType;
}
criteria.StartDate = startDate;
criteria.EndDate = endDate;
criteria.Skip = (int)skip;
criteria.Take = (int)take;
return criteria;
}
}
}