-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountingItemManagerImpl.java
More file actions
355 lines (289 loc) · 18 KB
/
Copy pathAccountingItemManagerImpl.java
File metadata and controls
355 lines (289 loc) · 18 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.tcbs.portfolio.feature.accounting.manager;
import com.google.common.collect.Iterables;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.tcbs.portfolio.common.Constants;
import com.tcbs.portfolio.common.PagingUtils;
import com.tcbs.portfolio.exception.PortfolioExceptions;
import com.tcbs.portfolio.feature.accounting.entity.AccountingItemEntity;
import com.tcbs.portfolio.feature.accounting.entity.QAccountingItemEntity;
import com.tcbs.portfolio.feature.accounting.manager.base.AccountingItemProvider;
import com.tcbs.portfolio.feature.accounting.manager.base.AccountingItemProviderFactory;
import com.tcbs.portfolio.feature.accounting.model.*;
import com.tcbs.portfolio.feature.accounting.repository.AccountingItemRepository;
import com.tcbs.portfolio.feature.accounting.repository.QueryAspectAccountingItemRepository;
import com.tcbs.portfolio.feature.accounting.task.AccountingTaskRunner;
import org.apache.commons.lang3.StringUtils;
import org.javatuples.Quartet;
import org.javatuples.Triplet;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import javax.persistence.EntityManager;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static com.tcbs.portfolio.feature.accounting.entity.QAccountingItemEntity.accountingItemEntity;
@Component
public class AccountingItemManagerImpl implements AccountingItemManager, AccountingItemProvider, InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(AccountingItemManagerImpl.class);
@Autowired
@Qualifier("accountingItemTaskRunner")
AccountingTaskRunner accountingTaskRunner;
@Autowired
AccountingItemRepository accountingItemRepository;
@Autowired
QueryAspectAccountingItemRepository queryAspectAccountingItemRepository;
@Autowired
AccountingItemProviderFactory accountingItemProviderFactory;
@Autowired
PortfolioExceptions portfolioExceptions;
@Autowired
ModelMapper modelMapper;
@Autowired
EntityManager entityManager;
private QAccountingItemEntity qAccountingItemEntity = accountingItemEntity;
@Override
public void performAccountingItem(AccountingItem accountingItem) {
accountingTaskRunner.run(accountingItem);
}
@Override
public Optional<AccountingItem> getByNameAndAccountAndProductAndSource(AccountingItem accountingItem, Date toDateTime) {
BooleanExpression accountingExp = qAccountingItemEntity.accountId.eq(accountingItem.getAccountItem().getTcbsId())
.and(qAccountingItemEntity.accountSource.eq(accountingItem.getAccountItem().getSource()));
BooleanExpression productExp = accountingItem.getProductItem().getProductCode() != null ?
qAccountingItemEntity.productItemEntity.productCode.eq(accountingItem.getProductItem().getProductCode()) :
qAccountingItemEntity.productItemEntity.productCode.isNull();
BooleanExpression applyTimeExp = buildApplyTimeBeforeOrEqualExp(toDateTime);
Predicate predicate = isValidItemExp().and(accountingExp).and(productExp).and(applyTimeExp);
JPAQuery<AccountingItemEntity> query = new JPAQuery<>(entityManager);
AccountingItemEntity rs = query.from(qAccountingItemEntity)
.where(predicate)
.orderBy(qAccountingItemEntity.applyTime.desc())
.orderBy(qAccountingItemEntity.updatedDate.desc())
.fetchFirst();
TypeMap<AccountingItemEntity, AccountingItem> typeMap = modelMapper.getTypeMap(AccountingItemEntity.class, AccountingItem.class);
return rs != null ? Optional.ofNullable(typeMap.map(rs)) : Optional.empty();
}
@Override
public void afterPropertiesSet() {
accountingItemProviderFactory.addProvider(Constants.AI_TYPE.NORMAL, this);
}
@Override
public List<AccountingItem> getAllAccountingItemByAccountId(String accountId, Date toDateTime) {
logger.trace("start query");
long now1 = System.currentTimeMillis();
List<QueryAspectAccountingItemAttr> entities = queryAspectAccountingItemRepository.getByAccountIdAndBeforeTime(
accountId, toDateTime
);
logger.trace("end query, spend {} ms", System.currentTimeMillis() - now1);
logger.trace("entities size {}", Iterables.size(entities));
List<AccountingItem> accountingItemsResult = groupToItem(entities);
logger.trace("accountingItemsResult size {}", accountingItemsResult.size());
return accountingItemsResult;
}
@Override
public List<AccountingItem> getAllAccountingItemByProductCode(String productCode, Date toDateTime) {
logger.trace("start query");
long now1 = System.currentTimeMillis();
List<QueryAspectAccountingItemAttr> entities = queryAspectAccountingItemRepository.getByProductCodeAndBeforeTime(
productCode, toDateTime
);
logger.trace("end query, spend {} ms", System.currentTimeMillis() - now1);
logger.trace("entities size {}", Iterables.size(entities));
List<AccountingItem> accountingItemsResult = groupToItem(entities);
logger.trace("accountingItemsResult size {}", accountingItemsResult.size());
return accountingItemsResult;
}
@Override
public List<AccountingItem> getAllAccountingItemByBondCode(String bondCode, Date toDateTime) {
logger.trace("start query");
long now1 = System.currentTimeMillis();
List<QueryAspectAccountingItemAttr> entities = queryAspectAccountingItemRepository.getByUnderlyingAndBeforeTime(
bondCode, toDateTime
);
logger.trace("end query, spend {} ms", System.currentTimeMillis() - now1);
logger.trace("entities size {}", Iterables.size(entities));
List<AccountingItem> accountingItemsResult = groupToItem(entities);
logger.trace("accountingItemsResult size {}", accountingItemsResult.size());
return accountingItemsResult;
}
// TODO: 8/2/18 should change to proper solution
@Override
public Page<AccountingItem> getAllAccountingItemByBondCode(String bondCode, Pageable pageable) {
AccountingPageableRequest accountingPageableRequest = (AccountingPageableRequest) pageable;
List<AccountingItem> accountingItemsResult = getAllAccountingItemByBondCode(
bondCode,
accountingPageableRequest.getToDateTime());
return PagingUtils.getPage(accountingItemsResult, pageable);
}
// TODO: 7/24/18 this API is till in used?
@Override
public List<AccountingItem> getAllAccountingItemByCondition(AccountingItemParam accountingItemParam, Date toDateTime) {
String bondCode = accountingItemParam.getBondCode();
String productCode = accountingItemParam.getProductCode();
String accountId = accountingItemParam.getAccountId();
BooleanExpression productItemExp = StringUtils.isEmpty(bondCode) ?
qAccountingItemEntity.productItemEntity.underlyingCode.isNotNull() : qAccountingItemEntity.productItemEntity.underlyingCode.eq(bondCode)
.and(StringUtils.isEmpty(productCode) ?
qAccountingItemEntity.productItemEntity.productCode.isNotNull() : qAccountingItemEntity.productItemEntity.productCode.eq(productCode));
BooleanExpression accountingItemExp = StringUtils.isEmpty(accountId) ?
qAccountingItemEntity.accountId.isNotNull() : qAccountingItemEntity.accountId.eq(accountId);
Predicate accountingItemQuery = isValidItemExp().and(accountingItemExp).and(productItemExp);
Iterable<AccountingItemEntity> accountingItemEntities = accountingItemRepository.findAll(accountingItemQuery);
TypeMap<AccountingItemEntity, AccountingItem> typeMap = modelMapper.getTypeMap(AccountingItemEntity.class, AccountingItem.class);
List<AccountingItem> accountingItems = StreamSupport.stream(accountingItemEntities.spliterator(), false)
.map(typeMap::map).collect(Collectors.toList());
// <ProductCode, ProductUnderlyingCode, Source>
Map<Triplet<String, String, String>, List<AccountingItem>> groups = accountingItems.stream().collect(Collectors.groupingBy(ai ->
Triplet.with(ai.getProductItem().getProductCode(),
ai.getProductItem().getUnderlyingCode(),
ai.getSource())
));
groups.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue().isEmpty());
List<AccountingItem> accountingItemsResult = groups.values().stream().map(ais -> getLatestByApplyTime(ais).get()).collect(Collectors.toList());
return accountingItemsResult;
}
@Override
public void importAccountingItem(AccountingItem accountingItem) {
logger.debug("================= {}", accountingItem);
TypeMap<AccountingItem, AccountingItemEntity> toEntityTypeMap = modelMapper.getTypeMap(AccountingItem.class, AccountingItemEntity.class);
AccountingItemEntity entity = toEntityTypeMap.map(accountingItem);
accountingItemRepository.saveAndFlush(entity);
}
// AccountingItemProvider implementations
@Override
public AccountingItem save(AccountingItem accountingItem) {
logger.info("save accounting item {}", accountingItem);
TypeMap<AccountingItem, AccountingItemEntity> toEntityTypeMap = modelMapper.getTypeMap(
AccountingItem.class, AccountingItemEntity.class);
AccountingItemEntity entity = toEntityTypeMap.map(accountingItem);
logger.info("save accounting item entity {}", entity);
long now1 = System.currentTimeMillis();
AccountingItemEntity savedEntity = accountingItemRepository.saveAndFlush(entity);
logger.trace("time: {}", System.currentTimeMillis() - now1);
if (savedEntity != null) {
TypeMap<AccountingItemEntity, AccountingItem> fromEntityTypeMap = modelMapper.getTypeMap(
AccountingItemEntity.class, AccountingItem.class);
return fromEntityTypeMap.map(savedEntity);
} else throw portfolioExceptions.dbErrorException.get();
}
@Override
public AccountingItem update(AccountingItem accountingItem) {
logger.info("update accounting item {}", accountingItem);
TypeMap<AccountingItem, AccountingItemEntity> toEntityTypeMap = modelMapper.getTypeMap(
AccountingItem.class, AccountingItemEntity.class);
AccountingItemEntity entity = toEntityTypeMap.map(accountingItem);
AccountingItemEntity savedEntity = accountingItemRepository.saveAndFlush(entity);
if (savedEntity != null) {
TypeMap<AccountingItemEntity, AccountingItem> fromEntityTypeMap = modelMapper.getTypeMap(
AccountingItemEntity.class, AccountingItem.class);
return fromEntityTypeMap.map(savedEntity);
} else throw portfolioExceptions.dbErrorException.get();
}
@Override
public Optional<AccountingItem> getLatestByNameAndAccountAndProductAndSource(AccountingItem accountingItem) {
BooleanExpression accountingExp = qAccountingItemEntity.accountId.eq(accountingItem.getAccountItem().getTcbsId())
.and(qAccountingItemEntity.accountSource.eq(accountingItem.getAccountItem().getSource()));
BooleanExpression productExp = accountingItem.getProductItem().getProductCode() != null ?
qAccountingItemEntity.productItemEntity.productCode.eq(accountingItem.getProductItem().getProductCode()) :
qAccountingItemEntity.productItemEntity.productCode.isNull();
Predicate predicate = isValidItemExp().and(accountingExp).and(productExp);
JPAQuery<AccountingItemEntity> query = new JPAQuery<>(entityManager);
long now1 = System.currentTimeMillis();
AccountingItemEntity rs = query.from(qAccountingItemEntity)
.where(predicate)
.orderBy(qAccountingItemEntity.applyTime.desc())
.orderBy(qAccountingItemEntity.updatedDate.desc())
.fetchFirst();
logger.trace("time: {}", System.currentTimeMillis() - now1);
TypeMap<AccountingItemEntity, AccountingItem> typeMap = modelMapper.getTypeMap(AccountingItemEntity.class, AccountingItem.class);
return rs != null ? Optional.ofNullable(typeMap.map(rs)) : Optional.empty();
}
// Optional<T> getByNameAndAccountAndProductAndSource(AccountingItem accountingItem, Date toDateTime)
// is already implemented above
@Override
public List<AccountingItem> getAllExistingNewerByNameAndAccountAndProductAndSource(AccountingItem accountingItem, Date fromDateTime) {
BooleanExpression accountingExp = qAccountingItemEntity.accountId.eq(accountingItem.getAccountItem().getTcbsId())
.and(qAccountingItemEntity.accountSource.eq(accountingItem.getAccountItem().getSource()));
BooleanExpression productExp = accountingItem.getProductItem().getProductCode() != null ?
qAccountingItemEntity.productItemEntity.productCode.eq(accountingItem.getProductItem().getProductCode()) :
qAccountingItemEntity.productItemEntity.productCode.isNull();
BooleanExpression applyTimeExp = buildApplyTimeAfterExp(fromDateTime);
Predicate predicate = isValidItemExp().and(accountingExp).and(productExp).and(applyTimeExp);
JPAQuery<AccountingItemEntity> query = new JPAQuery<>(entityManager);
List<AccountingItemEntity> accountingItemEntities = query.from(qAccountingItemEntity)
.where(predicate)
.fetch();
logger.trace("entities {}", accountingItemEntities);
TypeMap<AccountingItemEntity, AccountingItem> typeMap = modelMapper.getTypeMap(AccountingItemEntity.class, AccountingItem.class);
return accountingItemEntities.stream().map(typeMap::map).collect(Collectors.toList());
}
// private methods
private Optional<AccountingItem> getLatestByApplyTime(List<AccountingItem> accountingItems) {
return accountingItems.stream().max((AccountingItem ai1, AccountingItem ai2) -> {
Optional<Date> applyTimeOpt1 = Optional.ofNullable(ai1.getApplyTime());
Optional<Date> applyTimeOpt2 = Optional.ofNullable(ai2.getApplyTime());
Date applyTime1 = applyTimeOpt1.orElse(ai1.getUpdatedDate());
Date applyTime2 = applyTimeOpt2.orElse(ai2.getUpdatedDate());
int applyTimeCmp = applyTime1.compareTo(applyTime2);
return applyTimeCmp != 0 ? applyTimeCmp : ai1.getUpdatedDate().compareTo(ai2.getUpdatedDate());
});
}
private Optional<AccountingItemEntity> getLatestEntityByApplyTime(List<AccountingItemEntity> accountingItems) {
return accountingItems.stream().max((AccountingItemEntity aiEntity1, AccountingItemEntity aiEntity2) -> {
Optional<Date> applyTimeOpt1 = Optional.ofNullable(aiEntity1.getApplyTime());
Optional<Date> applyTimeOpt2 = Optional.ofNullable(aiEntity2.getApplyTime());
Date applyTime1 = applyTimeOpt1.orElse(aiEntity1.getUpdatedDate());
Date applyTime2 = applyTimeOpt2.orElse(aiEntity2.getUpdatedDate());
int applyTimeCmp = applyTime1.compareTo(applyTime2);
return applyTimeCmp != 0 ? applyTimeCmp : aiEntity1.getUpdatedDate().compareTo(aiEntity2.getUpdatedDate());
});
}
private BooleanExpression buildApplyTimeBeforeOrEqualExp(Date conditionTime) {
return qAccountingItemEntity.applyTime.before(conditionTime)
.or(qAccountingItemEntity.applyTime.eq(conditionTime));
}
private BooleanExpression buildApplyTimeAfterExp(Date conditionTime) {
return qAccountingItemEntity.applyTime.after(conditionTime);
}
private BooleanExpression isValidItemExp() {
return qAccountingItemEntity.status.isNull()
.or(qAccountingItemEntity.status.eq(Constants.AI_STATUS.VALID.name()));
}
private List<AccountingItem> groupAndFindClosestItemsByApplyTime(List<AccountingItemEntity> accountingItemEntities) {
TypeMap<AccountingItemEntity, AccountingItem> typeMap = modelMapper.getTypeMap(AccountingItemEntity.class, AccountingItem.class);
// <account id, source, product code, underlying code>
return StreamSupport.stream(accountingItemEntities.spliterator(), false)
.collect(Collectors.groupingBy(ai ->
Quartet.with(
ai.getAccountId(),
ai.getAccountSource(),
ai.getProductItemEntity().getProductCode(),
ai.getProductItemEntity().getUnderlyingCode())
)).values().parallelStream().map(ais -> getLatestEntityByApplyTime(ais).get()).map(typeMap::map).collect(Collectors.toList());
}
private List<AccountingItem> groupToItem(List<QueryAspectAccountingItemAttr> accountingAttrEntities) {
TypeMap<QueryAspectAccountingItemAttr, AccountingItemAttr> typeMap = modelMapper.getTypeMap(
QueryAspectAccountingItemAttr.class, AccountingItemAttr.class);
return accountingAttrEntities.stream().collect(Collectors.groupingBy(attr ->
attr.getAccountingItemId()
)).values().parallelStream().map(attrs -> {
QueryAspectAccountingItemAttr behalfOfGroup = attrs.get(0);
AccountingItem accountingItem = modelMapper.map(behalfOfGroup, AccountingItem.class);
accountingItem.setAccountingItemAttr(attrs.stream().map(typeMap::map).collect(Collectors.toList()));
return accountingItem;
}).collect(Collectors.toList());
}
}