const axios = require('axios');
const config = require('../etc/config.json');
const helper = require('./helper');
class PullDataServiceNew {
    constructor(redis_help) {
        this.mRedis = redis_help
        this.session = axios.create({
            baseURL: config.isDebug?config.pull_data_new_config.debug_host:config.pull_data_new_config.release_host,
            headers: {
              'Accept': 'application/json, text/plain, */*',
              'api-key': config.pull_data_new_config['api-key'],
              'api-secret': config.pull_data_new_config['api-secret']
            }
        });
        this.token = ""
    }


    async get_novel_material_list(cur_timeRange,n_start_time,size=500) {
        let maxRetries = 3
        let delay = 1000;

        let otherStr = null

        if(cur_timeRange.pull_day!=null&&cur_timeRange.pull_day!=""&&cur_timeRange.pull_day!=undefined){
            otherStr = cur_timeRange.pull_day
        }
        for (let i = 0; i < maxRetries; i++) {
            try {
                var now = new Date(); // 当前时间

                if(cur_timeRange!=null){
                    if(cur_timeRange.start_time==null||cur_timeRange.start_time==""||cur_timeRange.start_time==undefined){
                        now = helper.getTimeStampByHourMinute(cur_timeRange.start,otherStr)
                    }else{
                        now = helper.getTimeStampByHourMinute(cur_timeRange.start_time,otherStr)
                    }
                }
                let interval_minute = 60;
            
                if(cur_timeRange!=null){
                    interval_minute = cur_timeRange.interval_minute
                }
                // const fiveMinutesAgo = new Date(now - 5 * 60 * 1000); // 5分钟前的时间
                const fiveMinutesAgo = new Date(now - interval_minute * 60 * 1000); // 60分钟前的时间
                let data = {
                    startTime:fiveMinutesAgo.getTime(),
                    endTime:now.getTime(),
                    count:false
                }
            
                if(cur_timeRange!=null){
                    cur_timeRange.pull_time = data
                }
                let params = `?startTime=${n_start_time==0?data.startTime:n_start_time}&pageSize=500`
                let url = config.pull_data_new_config.get_novel_material_list+params
                console.log(url,data)
                var response = await this.session.get(url);
                if(response.data.msg!="success"){
                    throw response.data
                }else{
                    return response.data;
                }
            } catch (error) {
                if (i === maxRetries - 1) return {data:null,success:false,msg:error};
                console.log(`Retry get_novel_material_list ${i + 1} of ${maxRetries}`);
                await new Promise(resolve => setTimeout(resolve, delay * (i + 1)));
            }
        }

     
    }

}

module.exports = PullDataServiceNew;