/**
* @(#)ErrorNotificationServiceImpl.java
*
* Copyright (c) 2018 Fast Retailing Corporation.
*/
package com.fastretailing.dcp.sales.errornotification.service;
import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastretailing.dcp.sales.common.dto.PayOffData;
import com.fastretailing.dcp.sales.common.dto.SalesTransactionErrorDetail;
import com.fastretailing.dcp.sales.common.repository.PayOffDataMapper;
import com.fastretailing.dcp.sales.common.repository.SalesTransactionErrorDetailMapper;
import com.fastretailing.dcp.sales.common.type.ErrorNotificationFlag;
import com.fastretailing.dcp.sales.common.type.ErrorNotificationPattern;
import com.fastretailing.dcp.sales.common.type.ErrorType;
import com.fastretailing.dcp.sales.common.type.IntegrityCheckFlag;
import com.fastretailing.dcp.sales.errornotification.dto.ErrorNotificationMessageMaster;
import com.fastretailing.dcp.sales.errornotification.dto.ErrorNotificationPatternMaster;
import com.fastretailing.dcp.sales.errornotification.dto.TransactionErrorWithPattern;
import com.fastretailing.dcp.sales.errornotification.repository.ErrorNotificationMessageMasterMapper;
import com.fastretailing.dcp.sales.errornotification.repository.ErrorNotificationPatternMasterMapper;
import lombok.extern.slf4j.Slf4j;
/**
* Error notification service class.
*/
@Service
@Slf4j
public class ErrorNotificationServiceImpl implements ErrorNotificationService {
/** DB access parts(master error notification message). */
@Autowired
private ErrorNotificationMessageMasterMapper errorNotificationMessageMasterMapper;
/** DB access parts(payOff data). */
@Autowired
private PayOffDataMapper payOffDataMapper;
/** DB access parts(sales transaction error detail). */
@Autowired
private SalesTransactionErrorDetailMapper salesTransactionErrorDetailMapper;
/**
* DB access parts(master error notification pattern left join sales transaction error detail).
*/
@Autowired
private ErrorNotificationPatternMasterMapper errorNotificationPatternMasterMapper;
/** Model mapped. */
@Autowired
private ModelMapper mapper;
/**
* {@inheritDoc}
*/
@Override
public void processErrorNotification() {
// Get sales transaction error detail records.
TransactionErrorWithPattern transactionErrorWithPattern = new TransactionErrorWithPattern();
transactionErrorWithPattern.setErrorNotificationFlag(
String.valueOf(ErrorNotificationFlag.UNSENT.getErrorNotificationFlag()));
List<TransactionErrorWithPattern> transactionErrorWithPatternList =
errorNotificationPatternMasterMapper
.selectByErrorNotificationFlag(transactionErrorWithPattern);
// Sales transaction error detail no data.
if (CollectionUtils.isEmpty(transactionErrorWithPatternList)) {
// Output the empty sales information error log.
outputEmptyLog();
} else {
transactionErrorWithPatternList = transactionErrorWithPatternList.stream()
.filter(entity -> ErrorNotificationPattern.COUNTRY_UNIT
.getErrorNotificationPattern()
.equals(entity.getErrorNotificationPattern()))
.collect(Collectors.toList());
transactionErrorWithPatternList.stream()
.collect(Collectors.groupingBy(entity -> entity.getSystemCountryCode(),
Collectors.toList()))
.forEach((countryCode, list) ->
// Output log.
outputSalesTransactionErrorDetailLog(list));
}
// Get pay off data records.
PayOffData payOffData = new PayOffData();
payOffData.setIntegrityCheckFlag(
String.valueOf(IntegrityCheckFlag.MISMATCH.getIntegrityCheckFlag()));
payOffData.setPayoffIntegrityErrorNotificationFlag(
String.valueOf(ErrorNotificationFlag.UNSENT.getErrorNotificationFlag()));
List<PayOffData> payOffDataList = payOffDataMapper.selectErrorNotification(payOffData);
// Pay off data no data.
if (CollectionUtils.isEmpty(payOffDataList)) {
// Output the empty sales information error log is output.
outputEmptyLog();
} else {
payOffDataList.stream()
.collect(Collectors.groupingBy(entity -> entity.getSystemCountryCode(),
Collectors.toList()))
.forEach((countryCode, list) ->
// Output log.
outputPayOffDataLog(list));
}
}
/**
* Output empty log.
*/
private void outputEmptyLog() {
// Get error output message.
ErrorNotificationMessageMaster errorNotificationMessageMaster =
getErrorOutputMessage(ErrorType.NO_ERROR.getErrorType());
// Output log.
log.error(errorNotificationMessageMaster.getErrorOutputMessage());
}
/**
* Get error output message.
*
* @param errorType Error type.
* @return Master error notification message entity.
*/
private ErrorNotificationMessageMaster getErrorOutputMessage(String errorType) {
ErrorNotificationPatternMaster errorNotificationPatternMaster =
new ErrorNotificationPatternMaster();
errorNotificationPatternMaster.setErrorType(errorType);
ErrorNotificationMessageMaster errorNotificationMessageMaster =
errorNotificationMessageMasterMapper
.selectByErrorType(errorNotificationPatternMaster);
return errorNotificationMessageMaster;
}
/**
* Output sales transaction error detail log.
*
* @param transactionErrorWithPatternList Transaction error with pattern list.
*/
private void outputSalesTransactionErrorDetailLog(
List<TransactionErrorWithPattern> transactionErrorWithPatternList) {
// Error type validation records log output.
outputSameErrorTypeSalesTransactionErrorDetailLog(transactionErrorWithPatternList,
ErrorType.VALIDATION_ERROR.getErrorType());
// Error type relation records log output.
outputSameErrorTypeSalesTransactionErrorDetailLog(transactionErrorWithPatternList,
ErrorType.RELATION_ERROR.getErrorType());
// Error type business records log output.
outputSameErrorTypeSalesTransactionErrorDetailLog(transactionErrorWithPatternList,
ErrorType.BUSINESS_ERROR.getErrorType());
// Error type unique constraints records log output.
outputSameErrorTypeSalesTransactionErrorDetailLog(transactionErrorWithPatternList,
ErrorType.UNIQUE_CONSTRAINTS_ERROR.getErrorType());
// Update error notification flag.
transactionErrorWithPatternList.forEach(transactionError -> {
SalesTransactionErrorDetail salesTransactionErrorDetail =
new SalesTransactionErrorDetail();
mapper.map(transactionError, salesTransactionErrorDetail);
salesTransactionErrorDetailMapper.updateErrorNotificationFlag(
salesTransactionErrorDetail,
ErrorNotificationFlag.SENT.getErrorNotificationFlag());
});
}
/**
* Output same error type sales transaction error detail log.
*
* @param transactionErrorList Transaction error list.
* @param errorType Error type.
*/
private void outputSameErrorTypeSalesTransactionErrorDetailLog(
List<TransactionErrorWithPattern> transactionErrorList, String errorType) {
// Get same error type data from transaction error list.
List<TransactionErrorWithPattern> sameErrorTypeList = transactionErrorList.stream()
.filter(detail -> detail.getErrorType().equals(errorType))
.collect(Collectors.toList());
int count = sameErrorTypeList.size();
// Data is exist.
if (count != 0) {
TransactionErrorWithPattern transactionErrorWithPattern = sameErrorTypeList.get(0);
// Get output massage.
String errorOutputMessage = getOutputMassage(transactionErrorWithPattern.getErrorType(),
transactionErrorWithPattern.getSystemCountryCode(), count);
// Output log.
log.error(errorOutputMessage);
}
}
/**
* Output payoff data log.
*
* @param payOffDataList Payoff data list.
*/
private void outputPayOffDataLog(List<PayOffData> payOffDataList) {
PayOffData payOffData = payOffDataList.stream().findFirst().get();
// Get output massage.
String errorOutputMessage =
getOutputMassage(ErrorType.PAYOFF_CONFORMITY_CHECK.getErrorType(),
payOffData.getSystemCountryCode(), payOffDataList.size());
// Output log.
log.error(errorOutputMessage);
// Update error notification flag.
payOffDataList.forEach(entity -> {
payOffDataMapper.updateErrorNotificationFlag(entity,
ErrorNotificationFlag.SENT.getErrorNotificationFlag());
});
}
/**
* Get output massage.
*
* @param errorType Error type.
* @param countryCode Country code.
* @param count Message count.
* @return Output massage.
*/
private String getOutputMassage(String errorType, String countryCode, int count) {
ErrorNotificationMessageMaster errorNotificationMessage = getErrorOutputMessage(errorType);
MessageFormat messageFormat =
new MessageFormat(errorNotificationMessage.getErrorOutputMessage());
return messageFormat.format(new String[] {countryCode, String.valueOf(count)});
}
}
group by
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 今天在链接远程数据库(我的远程数据库是mysql5.7的)时报了一个“[Err] 1055 - Expressio...
- 下面的SQL语句执行时,MySQL提示问题:“Expression #1 of SELECT list is no...
- 解决方法: 参考: #1055 - Expression of SELECT list is not in GRO...
- group by是关系型数据库中较为常用的方法,rails也提供了类似的group_by方法,但两者还是有不小区别...