group by

/**
 * @(#)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 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 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 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 transactionErrorList, String errorType) {

        // Get same error type data from transaction error list.
        List 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 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)