package cn.superdesk.uniorder.event.invoiceApplyCustomerTrigger;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderTypeDimEnum;
import cn.superdesk.uniorder.common.enums.order.UnifiedOrderCreditBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.order.UnifiedOrderCreditInvoiceApplyStatusEnum;
import cn.superdesk.uniorder.common.enums.order.UnifiedOrderCreditInvoiceMakeStatusEnum;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderCreditService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyXGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/12/28 上午11:04
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/12/28 上午11:04
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class InvoiceApplyCustomerTriggerEventListener {

    @Autowired
    private InvoiceApplyCustomerService invoiceApplyCustomerService;

    @Autowired
    private InvoiceApplyXGoodsService invoiceApplyXGoodsService;

//    @Resource
//    private DelayQueueHandler delayQueueHandler;

    @Autowired
    private UnifiedOrderCreditService unifiedOrderCreditService;

    @Async
    @EventListener
    public void handleTailUpdateAccountingOrderInvoiceStatusEvent(TailUpdateAccountingScoreCustomerFieldInvoiceStatusEvent event) {

        log.info("handleTailUpdateAccountingOrderInvoiceStatusEvent -> TailUpdateAccountingOrderInvoiceStatusEvent -> {}", JSONUtil.toJsonStr(event));


        List<Long>                 affectedAccountingOrderIds = event.getAffectedAccountingOrderIds();
        AccountingOrderTypeDimEnum dim                        = event.getDim();

        Long invoiceApplyId = event.getInvoiceApplyId();

        LoginSysUser loginSysUser = event.getLoginSysUser();

        // 如果为空就没必要下一步了
        if (CollUtil.isEmpty(affectedAccountingOrderIds)) {

            affectedAccountingOrderIds = invoiceApplyXGoodsService.getAffectedAccountingOrderIdsByInvoiceApplyId(invoiceApplyId);

        }

        // 本身是直接处理的
        invoiceApplyCustomerService.processTailUpdateAccountingOrderInvoiceStatus(affectedAccountingOrderIds, dim, loginSysUser);


		/*

		String identifier = StrUtil.format("{}_{}", AnStringUtils.longListConvertToString(affectedAccountingOrderIds), invoiceApplyId);
		log.info("事件 handleTailUpdateAccountingOrderInvoiceStatusEvent add {}", identifier);

//		delayQueueHandler.remove(identifier);
		delayQueueHandler.put(new DelayTaskAsync(DelayTaskInstanceAccountingOrderInvoiceStatusTailUpdate.builder()
				.identifier(identifier)
				.affectedAccountingOrderIds(affectedAccountingOrderIds)
				.dim(dim.getKey())
				.invoiceApplyId(invoiceApplyId)
				.loginSysUser(JSONUtil.toJsonStr(loginSysUser))
				.build(), 1500));


		 */

    }

    /**
     * 面条代码
     *
     * @param event
     */
    @Async
    @EventListener
    public void handleTailUpdateUnifiedOrderCreditFieldBackAccountStatusEvent(TailUpdateUnifiedOrderCreditFieldBackAccountStatusEvent event) {

        List<Long> processUnifiedOrderCreditIds = event.getProcessUnifiedOrderCreditIds();

        UnifiedOrderCreditBackAccountStatusEnum state = event.getState();

        LoginSysUser loginSysUser = event.getLoginSysUser();

        unifiedOrderCreditService.updateFieldBackAccountStatusByUnifiedOrderCreditIds(processUnifiedOrderCreditIds, state);
    }

    /**
     * 面条代码
     *
     * @param event
     */
    @Async
    @EventListener
    public void handleTailUpdateUnifiedOrderCreditFieldInvoiceApplyStatusEvent(TailUpdateUnifiedOrderCreditFieldInvoiceApplyStatusEvent event) {

        List<Long> processUnifiedOrderCreditIds = event.getProcessUnifiedOrderCreditIds();

        UnifiedOrderCreditInvoiceApplyStatusEnum state = event.getState();

        LoginSysUser loginSysUser = event.getLoginSysUser();

        unifiedOrderCreditService.updateFieldInvoiceApplyStatusByUnifiedOrderCreditIds(processUnifiedOrderCreditIds, state);
    }

    /**
     * 面条代码
     *
     * @param event
     */
    @Async
    @EventListener
    public void handleTailUpdateUnifiedOrderCreditFieldInvoiceMakeStatusEvent(TailUpdateUnifiedOrderCreditFieldInvoiceMakeStatusEvent event) {

        List<Long> processUnifiedOrderCreditIds = event.getProcessUnifiedOrderCreditIds();

        UnifiedOrderCreditInvoiceMakeStatusEnum state = event.getState();

        LoginSysUser loginSysUser = event.getLoginSysUser();

        unifiedOrderCreditService.updateFieldInvoiceMakeStatusByUnifiedOrderCreditIds(processUnifiedOrderCreditIds, state);
    }

}
