package com.malk.tuosi;


import com.alibaba.fastjson.JSONObject;
import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;




import java.rmi.RemoteException;

//@EnableJpaAuditing
@Slf4j
@SpringBootApplication(scanBasePackages = {"com.malk"})
public class MjavaOunuoApplication {

    public static void main(String... args) throws RemoteException {

       
        SpringApplication.run(MjavaOunuoApplication.class, args);
        System.out.println("Ounuo_SpringBoot项目启动----------");
    }

    /// ppExt: 钉钉 Stream 接入: 仅需要 dingtalk-stream 依赖, 但点击注册不会回传, 应用发布后注册的事件才会回调 --
    @Bean
    public void init() {
        log.info("starting dingtalk stream client...}");
        try {
            OpenDingTalkStreamClientBuilder
                    .custom()
//                .credential(new AuthClientCredential("${app.appKey}", "${app.appSecret}"))
                    .credential(new AuthClientCredential("dinghhozexm92tupobfo", "-zYNwc4j9hssiP0mTNbSe0D9v9ejh4OIaHnx_EhVZWJbwnatvgtNiSFPIwAg9AGb"))
                    //注册事件监听
                    .registerAllEventListener(new GenericEventListener() {
                        public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                            try {
                                //事件唯一Id
                                String eventId = event.getEventId();
                                //事件类型
                                String eventType = event.getEventType();
                                //事件产生时间
                                Long bornTime = event.getEventBornTime();
                                //获取事件体
                                JSONObject bizData = event.getData();
                                //处理事件
//                            process(bizData);
                                log.error("receive event, eventId={}, eventType={}, bornTime={}, data={}", eventId, eventType, bornTime, bizData);
                                return EventAckStatus.SUCCESS;
                            } catch (Exception e) {
                                //消费失败
                                log.error("process event error, eventId={}, eventType={}, bornTime={}, data={}", event.getEventId(), event.getEventType(), event.getEventBornTime(), event.getData(), e);
                                return EventAckStatus.LATER;
                            }
                        }
                    })
                    .build().start();
        } catch (Exception e) {
            log.error("start dingtalk stream client error", e);
        }
    }

}
