package com.malk.hake;

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 com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import javax.persistence.EntityManager;

/**
 * corp项目: 扫描公共模块
 * -
 * 若是无需数据库模块, 配置无效地址也可启动, 引入mjava不支持直接 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 配置
 * 需要配置 jpa.hibernate.ddl-auto 为 none. 标识对表没有任何操作. 若不设置为 none, flyway.enabled 配置会无效, 在没有数库连接情况下程序无法启动
 */
@Slf4j
@EnableJpaAuditing
@SpringBootApplication(scanBasePackages = {"com.malk"})
public class Boot {

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

        SpringApplication.run(Boot.class, args);
    }

    /// 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("dingjykifqwvw84ejysw", "lpACaGiBgOV55I_0Zmk19Ns3YfMRzgqdc4CUjy2hvLwJ6Gb3sQxjPNuWARsr1gTy"))
                    //注册事件监听
                    .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);
        }
    }

    /**
     * 让Spring管理JPAQueryFactory [不使用Qualifier详见mjava-Boot]
     */
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }
}