package com.tzld.piaoquan.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tzld.commons.aliyun.log.AliyunLogManager;
import com.tzld.piaoquan.api.feign.LogCenterFeign;
import com.tzld.piaoquan.api.service.LoghubService;
import com.tzld.piaoquan.growth.common.common.base.BaseLogInfoDTO;
import com.tzld.piaoquan.growth.common.common.base.CommonLogRequest;
import com.tzld.piaoquan.growth.common.common.base.LogDetailInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author ehlxr
 */
@Service
public class LoghubServiceImpl implements LoghubService {
    private static final Logger log = LoggerFactory.getLogger(LoghubServiceImpl.class);
    @Autowired
    private AliyunLogManager aliyunLogManager;
    @Autowired
    private ThreadPoolTaskExecutor logHubThreadPool;
    @Value("${aliyun.log.threadpool.corePoolSize:100}")
    private int corePoolSize;
    @Value("${aliyun.log.threadpool.maxPoolSize:100}")
    private int maxPoolSize;
    @Value("${aliyun.log.threadpool.keepAliveSeconds:200}")
    private int keepAliveSeconds;
    @Value("${aliyun.log.threadpool.queueCapacity:100000}")
    private int queueCapacity;
    @Value("${aliyun.log.project:}")
    private String project;

    @Autowired
    private LogCenterFeign logCenterFeign;

    @PostConstruct
    public void init() {
        logHubThreadPool.setCorePoolSize(corePoolSize);
        logHubThreadPool.setMaxPoolSize(maxPoolSize);
        logHubThreadPool.setKeepAliveSeconds(keepAliveSeconds);
        logHubThreadPool.setQueueCapacity(queueCapacity);
        logHubThreadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    }

    @Override
    public void logUploadByKey(Object data, String key) {
        CommonLogRequest<List<JSONObject>> commonLogRequest = new CommonLogRequest<>();
        BaseLogInfoDTO baseInfo = new BaseLogInfoDTO();
        List<JSONObject> detail = new ArrayList<>();

        LogDetailInfo logDetailInfo = new LogDetailInfo();
        logDetailInfo.setLogUploadType(key);
        JSONObject params = JSONObject.parseObject(JSONObject.toJSONString(data));
        logDetailInfo.setParams(params);

        detail.add((JSONObject) JSONObject.toJSON(logDetailInfo));

        commonLogRequest.setBaseInfo(baseInfo);
        commonLogRequest.setDetail(detail);
        logCenterFeign.logUpload(JSON.toJSONString(commonLogRequest));
    }

    private void asyncSubmitLog(String project, String logStore, String topic, Map<String, Object> data) {
        logHubThreadPool.submit(() -> {
            try {
                aliyunLogManager.sendLog(project, logStore, topic, data);
            } catch (Exception e) {
                log.error("调用阿里云loghub异常", e);
            }
        });
    }
}
