//执行一个创建番茄链接任务
const axios = require('axios');
const helper = require('../src/helper');
const config = require('../etc/config.json')
const CMD = {}


CMD.runTask = async function(data,main_info,PlatformInfo,call_back){

    try {
        let  tg_link_config = JSON.parse(main_info.tg_link_config)
        let huichuan_id = tg_link_config['huichuan_id']
        let kadian_id = tg_link_config['kadian_id']
        let huichuan_list = JSON.parse(PlatformInfo.huichuan)
        let kadian_list = JSON.parse(PlatformInfo.kadian)
        let ad_callback_config_id = ""
        let kadian_value = 5
    
    
        for (let index = 0; index < huichuan_list.length; index++) {
            const hc_obj = huichuan_list[index];
            if(hc_obj.id==huichuan_id){
              ad_callback_config_id = hc_obj.value
              break
            }
        }
    
    
        for (let index = 0; index < kadian_list.length; index++) {
            const kd_obj = kadian_list[index];
            if(kd_obj.id==kadian_id){
                kadian_value = kd_obj.name
              break
            }
        }

        let create_fanqie_book_link = require('../src/api/fq/create_fanqiemf_book_link')
        let remark  =  data.product_name.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '')
        console.log("main_info:",main_info)
        let distributorId = main_info.distributor_id
        await create_fanqie_book_link.send(distributorId,data.product_id,kadian_value,`${remark}${main_info.id}`,1,ad_callback_config_id,(start_page,start_param, info)=>{
            if(start_page==null||start_param==null){
                data.start_page = start_page
                data.start_param = start_param
                call_back(data,info)
            }else{
                data.start_page = start_page
                data.start_param = start_param
                data.promotion_id = info.promotion_id
                call_back(data,null)
            }
        })
    } catch (error) {
        console.error('fq_create_link :Error:', error);
        // 处理错误
    }

}

module.exports = CMD;