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 dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
const CMD = {}
let page_index = 1;

async function processTask(){
    let connection  = null
    let right_status = true
    try{
        const url = 'https://new-media-fx.qimao.com/api/content/book/list';
    
        // 请求参数
        let params = new URLSearchParams({
            page: page_index,
            page_size: '200',
            env: '',
            t: Date.now()  // 当前时间戳
        });

        var headers = {
            "accept": "application/json, text/plain, */*",
            "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
            "app-external-id": "4c6e8d8709c14de9a4397b6cbb978f85",
            "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SUQiOjU0NDA1NjQxNjM3MTI4MjExMSwiQWNjb3VudE5hbWUiOiJ6aHVveXVlIiwiVGltZVN0YW1wIjoxNzQyMjgzOTA0LCJDbGllbnRJUCI6IjIxOS4xMzYuMTMwLjEwNyIsIkJ1ZmZlclRpbWUiOjAsImV4cCI6MTc0MjM3MDMwNCwiaXNzIjoicW1fbmV3X21lZGlhX2Z4IiwibmJmIjoxNzQyMjgzOTA0LCJFbnYiOiJyZWxlYXNlIiwiSXNTaW11bGF0aW9uTG9naW4iOmZhbHNlfQ.smEjQMawHRwsd8VGIGfvBthX67v6BjyPGAisPkQ9oBw",
            "project": "reader_paid_dyminiapp",
            "sec-ch-ua": "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Microsoft Edge\";v=\"134\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "signature": "80f85f7318aea80d5257d65e522c8c51c34f82a46ed5af2b03d1dc70ca9cb224",
            "target-id": "544180064332673816",
            "cookie": "sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%7D; sa_jssdk_2015_new-media-fx_qimao_com=%7B%22distinct_id%22%3A%22193bdc124645e2-0cbeaee172d4ce8-4c657b58-921600-193bdc12465567%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22http%3A%2F%2F120.79.128.142%3A4999%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkzYmRjMTI0NjQ1ZTItMGNiZWFlZTE3MmQ0Y2U4LTRjNjU3YjU4LTkyMTYwMC0xOTNiZGMxMjQ2NTU2NyJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%7D; acw_tc=0a47314817422911429748370e00476f42aaf4f0543f9e1ce0bd6746886469",
            "Referer": "https://new-media-fx.qimao.com/backend/book-manage/index?projectId=reader_paid_dyminiapp&appId=4c6e8d8709c14de9a4397b6cbb978f85&accountId=544180064332673816",
            "Referrer-Policy": "strict-origin-when-cross-origin"
        };
        headers['authorization'] = await tools.getQMToken()
        console.log("`${url}?${params}`:",`${url}?${params}`)
        const response = await fetch(`${url}?${params}`, {
                method: 'GET',
                headers: headers
            });
        
        const data = await response.json();

        if(data.code!=200){
            throw data
        }

        if(data.data.list.length<=0){
            throw 0
        }
        connection = await mysql.createConnection({
            ...dbConfig,
            multipleStatements: true
        });
        const insertSQL = `
        INSERT IGNORE INTO qm_iap_lib
        (book_id, book_name, book_origin_name, book_author, 
        book_type_text, book_word_count, book_serial_status_text, book_channel_text, 
        book_category_text,
        booklist_type_text, book_first_pay_chapter, book_paid_type, book_price_type,
        free_ratio,price,total_price,book_type)
        VALUES (?) 
        `;
        for (let index = 0; index < data.data.list.length; index++) {
            const element = data.data.list[index];
   
            await connection.query(insertSQL, [[
                element.book_id+"",
                element.book_name,
                element.book_origin_name,
                element.book_author,
                element.book_type_text,
                element.book_word_count,
                element.book_serial_status_text,
                element.book_channel_text,
                element.book_category_text,
                element.booklist_type_text,
                element.book_first_pay_chapter,
                element.book_paid_type,
                element.book_price_type,
                element.free_ratio,
                element.price,
                element.total_price,
                element.book_type
            ]]);
        }


    }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()