//执行一个寻找番茄任务
const CMD = {}
const axios = require('axios');
const get_fq_book_tab_controllers= require('../data_manager/Controllers/get_fq_book_tab_controllers');
// genre
// - 0：长篇网文
// - 8：短故事
// - 202：付费短剧
CMD.runTask = async function(data,call_back){
    // console.log("执行一个寻找番茄任务1::",data)
    //先从 get_fq_book_tab 数据库里查找
    let key = data.key+""

    try{
        const result = await  get_fq_book_tab_controllers.getData({book_id:key})
        console.log("result:",result)
        if(result.success){
            let book_info = result.data

            let genre = book_info.info.genre

            let book_name =  book_info.info.book_name
            let book_id = book_info.info.book_id
            let words = book_info.info.word_number

            data.genre = 0
            if(genre=="0"){
                data.genre = 1;
            }
            if(genre=="8"){
                data.genre = 3;
            }
        
            if(genre=="202"){
                data.genre = 202;
            }
            data.words = words
            data.book_name = book_name
            data.book_id = book_id
            call_back(data)
        }else{
            let postData = {
                cmd:"get_fq_book",
                key:key
            }
            axios.post('http://127.0.0.1:8080', postData)
            .then(response => {
                 CMD.action_task(data,response,call_back)
            })
            .catch(error => {
            //   call_back(null)
              console.error('There was an error!', error);
            });
        }
    }catch(e){
        console.log("执行一个寻找番茄任务1 error:",e)
    }
    // let sql = `SELECT * FROM get_fq_book_tab where book_key =  "${key}" `
    // // console.log("sql",sql)
    // sql_help.selectfind(sql,(is_find,list)=>{
    //     if(is_find){    
    //         // console.log("selectfind",list[0])
    //         let book_info = list[0]

    //         let genre = book_info.info.genre

    //         let book_name =  book_info.info.book_name
    //         let book_id = book_info.info.book_id
    //         let words = book_info.info.word_number

    //         data.genre = 0
    //         if(genre=="0"){
    //             data.genre = 1;
    //         }
    //         if(genre=="8"){
    //             data.genre = 3;
    //         }
        
    //         if(genre=="202"){
    //             data.genre = 202;
    //         }
    //         data.words = words
    //         data.book_name = book_name
    //         data.book_id = book_id
    //         call_back(data)
    //     }else{
    //         console.log("没找到")
    //         let postData = {
    //             cmd:"get_fq_book",
    //             key:data.key
    //         }
    //         axios.post('http://127.0.0.1:8080', postData)
    //         .then(response => {
    //             CMD.action_task(data,response,call_back)
    //         })
    //         .catch(error => {
    //         //   call_back(null)
    //           console.error('There was an error!', error);
    //         });
    //     }
    // })


}

CMD.action_task = async function(data,response,call_back){
    let obj = response['data']['received_data']['body']
    obj = JSON.parse(obj)
    // for (const key in obj['data']) {
    //     if (Object.prototype.hasOwnProperty.call(obj['data'], key)) {
    //         const element = obj['data'][key];
    //         console.log("key:",key)
    //         console.log("v:",obj['data'][key])
    //     }
    // }
    let book_name =  obj['data']['novel_data'].book_name
    let book_id = obj['data']['novel_data'].book_id
    let genre = obj['data']['novel_data'].genre
    let words = obj['data']['novel_data'].word_number
    data.genre = 0
    if(genre=="0"){
        data.genre = 1;
    }
    if(genre=="8"){
        data.genre = 3;
    }

    if(genre=="202"){
        data.genre = 202;
    }
    data.words = words
    data.book_name = book_name
    data.book_id = book_id

    await get_fq_book_tab_controllers.createData( {
        book_key:data.key,
        book_id:data.book_id,
        info:JSON.stringify(obj['data']['novel_data'])
    })
    // sql_help.query('INSERT INTO get_fq_book_tab SET ?',
    // {
    //     book_key:data.key,
    //     book_id:data.book_id,
    //     info:JSON.stringify(obj['data']['novel_data'])
    // }, 
    // async ()=>{
    //     // console.log("成功创建！")
    // })
    call_back(data)
}

module.exports = CMD;