const CMD = {}
const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
const filter_data_controllers = require('../src/data_manager/Controllers/filter_data_controllers');
const other_book_controllers = require('../src/data_manager/Controllers/other_book_controllers');
const video_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
const redis_help = require('../src/use_redis');
const config = require('../etc/config.json');
const helper = require('../src/helper');
const { default: axios } = require('axios');
const time_count = 1000;
const task_queue = []

function getProductById(product_list,product_id){
    for (let index = 0; index < product_list.length; index++) {
        const product_item = product_list[index];
        if(product_item.product_id==product_id){
            return product_item
        }
    }
    return null
}
async function processTask(){
    let result = null
    try{

        let bookIds = []
        let product_list = []
        result = await video_product_controllers.getDataByBookPlatformAndOriginBook({book_platform:config.platform_heiyanmf})
        if(result.success){
            for (let index = 0; index < result.data.length; index++) {
                const element = result.data[index];
                bookIds.push(element.product_id)
                product_list[element.product_id] = element.id
            }
        }
        console.log("product_list:",product_list)
        if(bookIds.length>0){
            let timestamp = helper.getCurrentUnixTimestamp()
            let sign = helper.generateSign({
                "pid":config.heiyan_config.pid,
                "timestamp":timestamp,
                "sign":"",
                "bookIds":bookIds.join(',')
            },config.heiyan_config.key)
            var url = config.heiyan_config.copyBooks
            var base_params = `/?sign=${sign}&pid=${config.heiyan_config.pid}&timestamp=${timestamp}`
            var other_params = `&bookIds=${bookIds.join(',')}`
            url = `${url}${base_params}${other_params}`
            // console.log("url:",url)
            let response = await axios.get(url)
            let data  = response.data
            console.log("response:",response)
            if(data.code==1){
                let list = data.data
                console.log("get_hy_origin_books:",list)
                for (let index = 0; index < list.length; index++) {
                    const element = list[index];
                    await CMD.update_product({id:product_list[element.copyBookId],product_parent_id:element.bookId})
                }
            }
        }
    }catch(e){
        console.error("processTask:",e)
    } finally{
        global.setTimeout(processTask, 1000);
    }
}

CMD.init = async function(){
    redis_help.connect((results)=>{
        if(results){
        }
    })
    processTask();
}


CMD.update_product = async function(data){
    return  await video_product_controllers.updateData({id:data.id},{
                        product_parent_id:data.product_parent_id,
    })
}

CMD.init()