const video_product_controllers = require('./src/data_manager/Controllers/video_product_controllers');
const config = require("./etc/config.json")
const redis_help = require('./src/use_redis');
const CMD = {}

async function processTask(){
    try{
        const result = await video_product_controllers.getProductDataByBookPlatformAndWordsZero({book_platform:config.platform_heiyan})
        console.log("result:",result)
        if(result.success){
            const product_item =  result.data;
            let hy_book_data = await require('./src/api/hy/hy_search_book').search_id(product_item.product_id)
            if(hy_book_data!=null){
                let genre = 1
                if(hy_book_data==undefined||hy_book_data==null){
                    return null
                }
                if(hy_book_data.rows==null||hy_book_data.rows==undefined){
                    return null
                }
            
                if(hy_book_data.rows.lenght<=0){
                    return null
                }
                let words = hy_book_data.rows[0].words; //,

                if(words>100000){
                    genre = 1;
                }else{
                    genre = 3;
                }
                await video_product_controllers.updateData({id:product_item.id},{
                     genre:genre,
                     words:words
                })
            }
        }
    }catch(e){
        console.error("processTask error:",e)
    } finally{
        global.setTimeout(processTask, 100);
        console.log("finish all!!!!!")
    }

}

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

}

CMD.init()