package com.fs.common.event;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TemplateListener  implements SmartApplicationListener {
    @Autowired
    private WeixinTemplateService weixinTemplateService;

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {
        return aClass == TemplateEvent.class;
    }

    @Async
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        //转换事件类型
        TemplateEvent templateEvent = (TemplateEvent) applicationEvent;
        //获取注册用户对象信息
        TemplateBean templateBean = templateEvent.getTemplateBean();
        log.info("模板事件类型：{}", templateBean.getTemplateType());
        switch (TemplateListenEnum.toType(templateBean.getTemplateType())) {
            case TYPE_1:
                weixinTemplateService.cancelNotice(templateBean.getOrderId()
                        , templateBean.getOrderCode(),templateBean.getRemark(), templateBean.getUserId());
                break;
            case TYPE_2:
                weixinTemplateService.deliveryNotice(templateBean.getOrderId()
                        , templateBean.getOrderCode(),templateBean.getDeliveryName(),templateBean.getDeliveryId(), templateBean.getUserId());
                break;
            case TYPE_3:
                weixinTemplateService.finishNotice(templateBean.getOrderId()
                        , templateBean.getOrderCode(),templateBean.getRemark(),templateBean.getFinishTime(), templateBean.getUserId());
                break;
            case TYPE_4:
                weixinTemplateService.prescribeNotice(templateBean.getOrderId()
                        , templateBean.getRpId(),templateBean.getRpCreateTime(),templateBean.getRpStatus(), templateBean.getUserId());
                break;


        }


    }
}
