-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesDataProvider.cs
More file actions
191 lines (146 loc) · 8.41 KB
/
SalesDataProvider.cs
File metadata and controls
191 lines (146 loc) · 8.41 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
using Microsoft.EntityFrameworkCore;
using SmartTalk.Core.Data;
using SmartTalk.Core.Domain.PhoneOrder;
using SmartTalk.Core.Domain.Sales;
using SmartTalk.Core.Ioc;
using SmartTalk.Messages.Enums.PhoneOrder;
using SmartTalk.Messages.Enums.Sales;
namespace SmartTalk.Core.Services.Sale;
public interface ISalesDataProvider : IScopedDependency
{
Task<List<Sales>> GetAllSalesAsync(CancellationToken cancellationToken);
Task<Sales> GetCallInSalesByNameAsync(string assistantName, SalesCallType? type, CancellationToken cancellationToken);
Task<List<AiSpeechAssistantKnowledgeVariableCache>> GetCustomerItemsCacheBySoldToIdsAsync(List<string> soldToIds, CancellationToken cancellationToken);
Task UpsertCustomerItemsCacheAsync(string soldToId, string itemsString, bool forceSave, CancellationToken cancellationToken);
Task UpsertCustomerInfoCacheAsync(string phoneNumber, string itemsString, bool forceSave, CancellationToken cancellationToken);
Task<AiSpeechAssistantKnowledgeVariableCache> GetCustomerInfoCacheByPhoneNumberAsync(string phoneNumber, CancellationToken cancellationToken);
Task AddPhoneOrderPushTaskAsync(PhoneOrderPushTask task, bool forceSave = true, CancellationToken cancellationToken = default);
Task MarkSendingAsync(int taskId, bool forceSave, CancellationToken cancellationToken = default);
Task MarkSentAsync(int taskId, bool forceSave, CancellationToken cancellationToken = default);
Task MarkFailedAsync(int taskId, bool forceSave, CancellationToken cancellationToken = default);
Task<bool> IsParentCompletedAsync(int? parentRecordId, CancellationToken cancellationToken);
Task<bool> HasPendingTasksByRecordIdAsync(int recordId, CancellationToken cancellationToken);
Task<PhoneOrderPushTask> GetRecordPushTaskByRecordIdAsync(int recordId, CancellationToken cancellationToken);
}
public class SalesDataProvider : ISalesDataProvider
{
private readonly IRepository _repository;
private readonly IUnitOfWork _unitOfWork;
public SalesDataProvider(IRepository repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public async Task<List<Sales>> GetAllSalesAsync(CancellationToken cancellationToken)
{
return await _repository.GetAllAsync<Sales>(cancellationToken).ConfigureAwait(false);
}
public async Task<Sales> GetCallInSalesByNameAsync(string assistantName, SalesCallType? type, CancellationToken cancellationToken)
{
var query = _repository.Query<Sales>().Where(s => s.Name == assistantName);
if (type.HasValue) query = query.Where(s => s.Type == type.Value);
return await query.FirstOrDefaultAsync(cancellationToken);
}
public async Task<List<AiSpeechAssistantKnowledgeVariableCache>> GetCustomerItemsCacheBySoldToIdsAsync(List<string> soldToIds, CancellationToken cancellationToken)
{
return await _repository.Query<AiSpeechAssistantKnowledgeVariableCache>().Where(x => soldToIds.Contains(x.Filter)).ToListAsync(cancellationToken);
}
public async Task UpsertCustomerItemsCacheAsync(string soldToId, string itemsString, bool forceSave, CancellationToken cancellationToken)
{
var cache = await _repository.FirstOrDefaultAsync<AiSpeechAssistantKnowledgeVariableCache>(x => x.Filter == soldToId, cancellationToken);
if (cache == null)
{
cache = new AiSpeechAssistantKnowledgeVariableCache
{
CacheKey = "customer_items",
Filter = soldToId,
CacheValue = itemsString,
LastUpdated = DateTimeOffset.UtcNow
};
await _repository.InsertAsync(cache, cancellationToken).ConfigureAwait(false);
}
else
{
cache.CacheValue = itemsString;
cache.LastUpdated = DateTimeOffset.UtcNow;
await _repository.UpdateAsync(cache, cancellationToken).ConfigureAwait(false);
}
if (forceSave)
{
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
}
public async Task UpsertCustomerInfoCacheAsync(string phoneNumber, string itemsString, bool forceSave, CancellationToken cancellationToken)
{
var cache = await _repository.FirstOrDefaultAsync<AiSpeechAssistantKnowledgeVariableCache>(x => x.Filter == phoneNumber, cancellationToken);
if (cache == null)
{
cache = new AiSpeechAssistantKnowledgeVariableCache
{
CacheKey = "customer_info",
Filter = phoneNumber,
CacheValue = itemsString,
LastUpdated = DateTimeOffset.UtcNow
};
await _repository.InsertAsync(cache, cancellationToken).ConfigureAwait(false);
}
else
{
cache.CacheValue = itemsString;
cache.LastUpdated = DateTimeOffset.UtcNow;
await _repository.UpdateAsync(cache, cancellationToken).ConfigureAwait(false);
}
if (forceSave)
{
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
}
public async Task<AiSpeechAssistantKnowledgeVariableCache> GetCustomerInfoCacheByPhoneNumberAsync(string phoneNumber, CancellationToken cancellationToken)
{
return await _repository.Query<AiSpeechAssistantKnowledgeVariableCache>().Where(x => x.Filter == phoneNumber).FirstOrDefaultAsync(cancellationToken);
}
public async Task AddPhoneOrderPushTaskAsync(PhoneOrderPushTask task, bool forceSave = true, CancellationToken cancellationToken = default)
{
await _repository.InsertAsync(task, cancellationToken).ConfigureAwait(false);
if (forceSave)
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public async Task MarkSendingAsync(int taskId, bool forceSave = true, CancellationToken cancellationToken = default)
{
var task = await _repository.Query<PhoneOrderPushTask>().Where(t => t.Id == taskId).FirstOrDefaultAsync(cancellationToken);
if (task == null) return;
task.Status = PhoneOrderPushTaskStatus.Sending;
await _repository.UpdateAsync(task, cancellationToken).ConfigureAwait(false);
if (forceSave) await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public async Task MarkSentAsync(int taskId, bool forceSave = true, CancellationToken cancellationToken = default)
{
var task = await _repository.Query<PhoneOrderPushTask>().Where(t => t.Id == taskId).FirstOrDefaultAsync(cancellationToken);
if (task == null) return;
task.Status = PhoneOrderPushTaskStatus.Sent;
await _repository.UpdateAsync(task, cancellationToken).ConfigureAwait(false);
if (forceSave) await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public async Task MarkFailedAsync(int taskId, bool forceSave, CancellationToken cancellationToken = default)
{
var task = await _repository.Query<PhoneOrderPushTask>().Where(t => t.Id == taskId).FirstOrDefaultAsync(cancellationToken);
if (task == null) return;
task.Status = PhoneOrderPushTaskStatus.Failed;
await _repository.UpdateAsync(task, cancellationToken).ConfigureAwait(false);
if (forceSave) await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
public async Task<bool> IsParentCompletedAsync(int? parentRecordId, CancellationToken cancellationToken)
{
if (!parentRecordId.HasValue) return true;
return await _repository.Query<PhoneOrderRecord>().Where(r => r.Id == parentRecordId.Value).Select(r => r.IsCompleted).FirstOrDefaultAsync(cancellationToken);
}
public async Task<bool> HasPendingTasksByRecordIdAsync(int recordId, CancellationToken cancellationToken)
{
return await _repository.Query<PhoneOrderPushTask>().AnyAsync(t => t.RecordId == recordId && t.Status != PhoneOrderPushTaskStatus.Sent, cancellationToken).ConfigureAwait(false);
}
public async Task<PhoneOrderPushTask> GetRecordPushTaskByRecordIdAsync(int recordId, CancellationToken cancellationToken)
{
return await _repository.Query<PhoneOrderPushTask>().Where(t => t.RecordId == recordId && t.Status == PhoneOrderPushTaskStatus.Pending)
.OrderBy(t => t.CreatedAt).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
}
}