package cn.superdesk.uniorder.event;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderReturn;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelProfitSync;
import cn.superdesk.uniorder.dao.entity.channel.UnifiedChannelSync;
import cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise;
import cn.superdesk.uniorder.dao.entity.organisation.SysOrganization;
import cn.superdesk.uniorder.dto.param.operation.UnifiedOrderIssuesFlowingParam;
import cn.superdesk.uniorder.event.channelTrigger.ChannelProfitSyncCacheMappingEvent;
import cn.superdesk.uniorder.event.channelTrigger.ChannelProfitSyncCreateSysRoleExtDataSampleEvent;
import cn.superdesk.uniorder.event.channelTrigger.ChannelSyncCreateSysRoleExtDataEvent;
import cn.superdesk.uniorder.event.enterpriseTrigger.EnterpriseEvent;
import cn.superdesk.uniorder.event.operationTrigger.UnifiedOrderDevelopCallBackEvent;
import cn.superdesk.uniorder.event.orderTrigger.UnifiedOrderProfitCalculationEvent;
import cn.superdesk.uniorder.event.orderTrigger.UnifiedOrderReturnProfitCalculationEvent;
import cn.superdesk.uniorder.event.orderTrigger.ProxyClientOrderSupplierCheckUpEvent;
import cn.superdesk.uniorder.event.organizationTrigger.OrganizationEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * @Description: 事件Event其实也是线程, 通过异步执行的方式, 减少业务的冗余, 使请求专注核心业务代码的操作, 提高系统的响应速度
 * @Author: linjinyu
 * @CreateDate: 5/28/20 11:37 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 5/28/20 11:37 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class CommonEventPublisher {

    @Autowired
    private ApplicationContext applicationContext;

    /****************************************** channelTrigger start ******************************************/

    /**
     * 码表处理
     *
     * @param direction
     * @param unifiedChannelProfitSync
     */
    public void triggerChannelProfitSyncCacheMappingEvent(String direction, UnifiedChannelProfitSync unifiedChannelProfitSync) {

        log.info("CommonEventPublisher->triggerChannelProfitSyncCacheMappingEvent 事件触发 ...");

        applicationContext.publishEvent(ChannelProfitSyncCacheMappingEvent.builder()
                .direction(direction)
                .unifiedChannelProfitSync(unifiedChannelProfitSync)
                .build()
        );
    }


    /**
     * 订单 分润计算
     *
     * @param unifiedOrder
     */
    public void triggerUnifiedOrderProfitCalculationEvent(UnifiedOrder unifiedOrder) {

        log.info("CommonEventPublisher->triggerUnifiedOrderProfitCalculationEvent 事件触发 ...");

        applicationContext.publishEvent(UnifiedOrderProfitCalculationEvent.builder()
                .unifiedOrder(unifiedOrder)
                .build()
        );
    }

    /**
     * 代客下单 供应商(商户) 目前是没有同步的,在订单过来的时候检查,如果没有就添加 mark by linjinyu at 20210507
     *
     * @param unifiedOrder
     */
    public void triggerProxyClientOrderSupplierCheckUpEvent(UnifiedOrder unifiedOrder) {

        log.info("CommonEventPublisher->triggerUnifiedOrderSupplierCheckUpEvent 事件触发 ...");

        applicationContext.publishEvent(ProxyClientOrderSupplierCheckUpEvent.builder()
                .unifiedOrder(unifiedOrder)
                .build()
        );
    }

    /**
     * 完成后回调 只更改状态
     *
     * @param unifiedOrderIssuesFlowingParam
     */
    public void triggerUnifiedOrderDevelopCallBack(UnifiedOrderIssuesFlowingParam unifiedOrderIssuesFlowingParam) {

        log.info("CommonEventPublisher->triggerUnifiedOrderDevelopCallBack 事件触发 ...\n{}", AnJsonUtils.toJson(unifiedOrderIssuesFlowingParam));

        applicationContext.publishEvent(
                UnifiedOrderDevelopCallBackEvent
                        .builder()
                        .unifiedOrderIssuesFlowingParam(unifiedOrderIssuesFlowingParam)
                        .build()
        );
    }


    /**
     * 退单 分润计算
     *
     * @param unifiedOrderReturn
     */
    public void triggerUnifiedOrderReturnProfitCalculationEvent(UnifiedOrderReturn unifiedOrderReturn) {

        log.info("CommonEventPublisher->triggerUnifiedOrderReturnProfitCalculationEvent 事件触发 ...");

        applicationContext.publishEvent(UnifiedOrderReturnProfitCalculationEvent.builder()
                .unifiedOrderReturn(unifiedOrderReturn)
                .build()
        );
    }

    /****************************************** channelTrigger   end ******************************************/


    /****************************************** orderTrigger start ******************************************/

    /**
     * @param direction
     * @param unifiedChannelSync
     */
    public void triggerChannelSyncCreateSysRoleExtDataEvent(String direction, UnifiedChannelSync unifiedChannelSync) {

        log.info("CommonEventPublisher->triggerChannelSyncCreateSysRoleExtDataEvent 事件触发 ...");

        applicationContext.publishEvent(ChannelSyncCreateSysRoleExtDataEvent.builder()
                .unifiedChannelSync(unifiedChannelSync)
                .direction(direction)
                .build()
        );
    }

    public void triggerChannelProfitSyncCreateSysRoleExtDataSampleEvent(String direction, UnifiedChannelProfitSync unifiedChannelProfitSync) {

        log.info("CommonEventPublisher->triggerChannelProfitSyncCreateSysRoleExtDataSampleEvent 事件触发 ...");

        applicationContext.publishEvent(ChannelProfitSyncCreateSysRoleExtDataSampleEvent.builder()
                .unifiedChannelProfitSync(unifiedChannelProfitSync)
                .direction(direction)
                .build()
        );
    }

    /****************************************** orderTrigger   end ******************************************/

    /****************************************** organizationTrigger start ******************************************/

    public void triggerOrganizationEvent(SysOrganization sysOrganization, SysOrganization snapshot) {

        log.info("CommonEventPublisher->triggerOrganizationEvent 事件触发 ...");

        applicationContext.publishEvent(OrganizationEvent.builder()
                .sysOrganization(sysOrganization)
                .snapshot(snapshot)
                .build()
        );
    }

    /****************************************** organizationTrigger   end ******************************************/

    /****************************************** enterpriseTrigger start ******************************************/

    public void triggerEnterpriseEvent(SysEnterprise sysEnterprise) {

        log.info("CommonEventPublisher->triggerEnterpriseEvent 事件触发 ...");

        applicationContext.publishEvent(EnterpriseEvent.builder()
                .sysEnterprise(sysEnterprise)
                .build()
        );
    }

    /****************************************** enterpriseTrigger   end ******************************************/
}
