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")
        }
    })
    let sql = `SELECT * FROM video_applet_product WHERE TIMESTAMPDIFF(HOUR, create_at, NOW()) < 24 AND status = 0 AND wait_status = 1 ORDER BY create_at DESC LIMIT 1;`
    setInterval(() => {
        sql_task_help.get(sql,(is_right,data)=>{
            if(is_right){
                // console.log("sql:",data)
                if(data.length>0){
                    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,2,data.check_url,data.promotion_id,data.product_id],
        ()=>{
            // console.log("update_video_applet_product:更新成功！")
        })
}
CMD.init()