const config = require("./etc/config.json");
const helper = require("./src/helper");
const HttpClient = require("./src/HttpClient");
const redis_help = require('./src/use_redis');
const mysql = require('mysql2/promise');
const tools = require("./tools");
const video_product_controllers = require('./src/data_manager/Controllers/video_product_controllers');
const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
const CMD = {}
let page_index = 1;

async function processTask(){
    let right_status = true
    try{
        const result = await video_product_controllers.getDataByBookPlatform({book_platform:config.platform_yuewen},1400)
            console.log("result:",result)
            if(result.success){
                for (let index = 0; index < result.data.length; index++) {
                    const product_item =  result.data[index];
                    let word = 0
                    if(helper.checkChannel(product_item.words,"万")){
                        const numMatch = product_item.words.match(/[\d.]+/);
                        const num = parseFloat(numMatch[0]);
                        word = Math.round(num * 10000)
                    }else{
                        word = Math.round(parseFloat(product_item.words))
                    }
               
                    await video_product_controllers.updateData({id:product_item.id},{
                        words:word
                    })
                    await new Promise(resolve => setTimeout(resolve,100));
                }
        }
    }catch(e){
        if(e==0){
            right_status = false
        }
        console.error("processTask error:",e)
    } finally{
        if(right_status){
            page_index++;
            global.setTimeout(processTask, 1000);
        }

    }

}


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

}

CMD.init()