
const video_product_controllers = require('./src/data_manager/Controllers/video_product_controllers');
const CMD = {}
const conifg = require('./etc/config.json')
let task_list = []
async function processTask(){
    try{
        const result = task_list.pop()
        let product_result = await video_product_controllers.getProductData({ book_id: result.distribution_id });
        if(product_result.success){

            //判断免费是否存在
            let mf_book = await video_product_controllers.getProductData({ book_id: result.book_id });
            if(!mf_book.success){
                let new_data = result
                let book_info =  await require("./src/api/fq/fq_search_book").search_id(new_data.book_id,1819222136130603)
                if(book_info.code==200){
                    console.log("book_info:",book_info.result)
                    if(book_info.result.length<=0){
                        throw "数据错误"
                    }
                    let book = book_info.result[0]
                    if(book.genre=="8"&&book.word_count==0){
                        book.word_count = 1
                    }
                    await video_product_controllers.createProductData({
                        book_platform:conifg.platform_fanqiemf,
                        product_name:result.book_name,
                        genre:CMD.getFqGenre(book.genre),
                        product_id:result.book_id,
                        words:book.word_count
                    })
                }else{
                    console.log("error:",book_info)
                }
            }
        }

    }catch(e){
        console.error("processTask error:",e)
    } finally{
        if(task_list.length>0){
            global.setTimeout(processTask, 100);
        }else{
            console.log("完成所有任务:")
        }
    }

}


CMD.getFqGenre = function(genre){
    if(genre=="0"){
        return 1;
    }else{
        return 3;
    }
    if(genre=="8"){
        return 3;
    }

    if(genre=="202"){
        return 202;
    }
    return 999;
}

const fs = require('fs');


CMD.init = async function(){
    // processTask();

    
// 读取文件并处理内容
fs.readFile('fqmfbook.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('读取文件出错:', err);
        return;
    }
    // 按行分割文本
    const lines = data.trim().split('\n');
    // 解析每行数据到数组
    task_list = lines.map(line => {
        const parts = line.trim().split('\t');
            return {
                distribution_id: parts[0],
                book_name: parts[1], 
                book_id: parts[2],
                status: parts[3]
            };
        });
        console.log(task_list);
        processTask()
    });
}

CMD.init()