package cn.superdesk.uniorder.executor.scheduler.kafka._inner.invoice.customer.biz;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderTypeDimEnum;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.accounting.score.customer.AccountingScoreCustomer;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.BaseInnerHandleService;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner._constants.InnerTopicChannelConstants;
import cn.superdesk.uniorder.executor.scheduler.kafka._inner.dto.invoice.customer.InvoiceApplyCustomerKafkaEventCheckAndUpdateAccountingOrderFieldInvoiceStatusEvent;
import cn.superdesk.uniorder.service.accounting.score.customer.AccountingScoreCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyXGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/5/23 12:18 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/5/23 12:18 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class InvoiceApplyCustomerKafkaEventHandlerService extends BaseInnerHandleService {

    private final InvoiceApplyCustomerService invoiceApplyCustomerService;
    private final InvoiceApplyXGoodsService   invoiceApplyXGoodsService;

    private final AccountingScoreCustomerService accountingScoreCustomerService;

    public int checkAndUpdateAccountingOrderFieldInvoiceStatusEventHandle(String messageKey, String messageBody, Date now) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            log.info("");
        }
        InvoiceApplyCustomerKafkaEventCheckAndUpdateAccountingOrderFieldInvoiceStatusEvent event = AnJsonUtils.toObject(messageBody, InvoiceApplyCustomerKafkaEventCheckAndUpdateAccountingOrderFieldInvoiceStatusEvent.class);

        LoginSysUser loginSysUser = AnJsonUtils.toObject(event.getLoginSysUser(), LoginSysUser.class);
        Date         touchTime    = event.getTouchTime();

        String msg = event.getMsg();

        Integer accountingOrderTypeDim = event.getAccountingOrderTypeDim();

        Long         invoiceApplyId             = event.getInvoiceApplyId();
        InvoiceApply invoiceApplyCustomerEntity = invoiceApplyCustomerService.findById(invoiceApplyId);

        String accountingOrderSn = invoiceApplyCustomerEntity.getAccountingOrderSn();

        List<Long> affectedAccountingOrderIds = invoiceApplyXGoodsService.getAffectedAccountingOrderIdsByInvoiceApplyId(invoiceApplyId);

        List<AccountingScoreCustomer> affectedAccountingOrder = accountingScoreCustomerService.findByIds(affectedAccountingOrderIds);
        if (CollUtil.isEmpty(affectedAccountingOrder)) {
            log.error("AccountingScoreCustomerKafkaEventHandlerService.checkAndUpdateAccountingOrderFieldInvoiceStatusEventHandle 对账单异常");
        }

        String accountingOrderSns = affectedAccountingOrder.stream().map(o -> o.getAccountingOrderSn()).collect(Collectors.joining(","));

        tmpLogWithAccountingOrderSn(
                InnerTopicChannelConstants.TOPIC_EVENT_CHANNEL_INNER_INVOICE_APPLY_CUSTOMER_SERVICE,
                accountingOrderSns,
                messageKey,
                messageBody,
                DateUtil.format(now, DatePattern.PURE_DATETIME_PATTERN)
        );

        invoiceApplyCustomerService.processTailUpdateAccountingOrderInvoiceStatus(
                affectedAccountingOrderIds,
                AccountingOrderTypeDimEnum.get(accountingOrderTypeDim),
                loginSysUser
        );

        return 0;
    }
}
