const CMD = {}
const sql_task_help = require('../use_task_mysql');
const axios = require('axios');
const time_count = 2000;
CMD.init = function(){
    sql_task_help.connect((results)=>{
        if(results){
            console.log("send_data_server：sql_task_help")
        }
    })
    // {
    //     id: 212,
    //     product_name: '丈夫缺席婚礼，我选择离婚',
    //     product_id: '7428872617198619699',
    //     create_at: 2024-11-26T04:27:13.000Z,
    //     book_platform: 2,
    //     industry: 1,
    //     dy_small_program_start: 'pages/bookstore/index',
    //     dy_small_program_start_data: 'key=PHVZIZ2U2VT&aid=40014221&item_source=1&media_source=1&click_id=__CLICKID__&request_id=__REQUESTID__&mid1=__MID1__&mid2=__MID2__&mid3=__MID3__&mid4=__MID4__&mid5=__MID5__',
    //     dy_small_program_safety: '',
    //     dy_small_applet_app_id: 'tt46893034d9b8232301',
    //     check_url: '',
    //     status: 0,
    //     main_id: 3,
    //     promotion_id: '1816757719398425',
    //     wait_status: 0
    //   }
    //如果要和当前时间比较
    //取出 wait_status 等待的数据 如果大于10 条 就取出1一条插入
    // let sql = `SELECT * FROM table_name WHERE wait_status = 0 ORDER BY created_time ASC LIMIT 1;`
    let sql = `SELECT * FROM video_applet_product WHERE TIMESTAMPDIFF(SECOND, create_at, NOW()) > 5 AND status = 0 ORDER BY create_at DESC LIMIT 1;`
    setInterval(() => {
        sql_task_help.get(sql,(is_right,data)=>{
            if(is_right){
                if(data.length>0){
                    // console.log("sql:",data)
                    CMD.build_check_url( data[0])
                }
            }
        })
    }, time_count);
}

CMD.build_check_url = function(data){
    let url = 'https://clipvideoup.s6kuwan.com/sapi/check/get_check_url';
    const formData = new FormData();
    formData.append('platform_id', data.book_platform);
    formData.append('cp_spread_id', data.promotion_id);
    // console.log("formData:", formData);
    axios.post(url, formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
    .then(response => {
        if(response.data.code==10000){
            CMD.update_video_applet_product({promotion_id:data.promotion_id,
                product_id:data.product_id,check_url:response.data.content.check_url
            })
        }
    })
    .catch(error => {
        console.error('create_link was an error!', error);
    });
}

CMD.update_video_applet_product = function(data){
     sql_task_help.update('UPDATE video_applet_product SET status = ?, wait_status = ?  , check_url = ?   WHERE promotion_id = ? and product_id = ?', 
        [1,1,data.check_url,data.promotion_id,data.product_id],
        ()=>{
            console.log("update_video_applet_product:更新成功！")
        })
    }
module.exports = CMD;