//执行一个创建掌阅链接任务
const axios = require('axios');
const helper = require('../src/helper');
const config = require('../etc/config.json');
const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
const tools = require('../tools');
const CMD = {}

CMD.runTask = async function(t_data,main_info,PlatformInfo,call_back){
    // let recharge =  await get_recharge_template()
    let  zy_conifg = JSON.parse(main_info.zy_conifg)
    try {
        let token = await require('../src/api/zy/get_zy_token').get_token()
        console.log("token:",token)
        let clinet = tools.getOneNewClinet()
        let book_info = await video_product_controllers.getProductData({book_id:t_data.product_id})
        if(book_info.success==false){
            throw "没有找到这本书："+t_data.product_id
        }
        let pay_type = zy_conifg.pay_type!=0?zy_conifg.pay_type:book_info.data.fee_unit
        let price = zy_conifg.price!=0?zy_conifg.price:book_info.data.default_price
        let start_pay_section = zy_conifg.start_pay_section!=0?zy_conifg.start_pay_section:book_info.data.default_pay_section
        let response = await clinet.post(`https://openapi-gaia.zhangyue.com/opendata/reseller/pay/book/link/create`,{
            access_token:token,
            name:`${t_data.product_name}_${main_info.id}`,
            source:"toutiao2",        //媒体 - 抖音小程序 只能使用 "toutiao2" - 微信小程序可选项如下   - bili B站/   - toutiao2 头条2.0   - gdt3 广点通3.0
            start_pay_section:start_pay_section, //开始付费章节
            book_chapter:zy_conifg.book_chapter, //调起章节
            book_id:parseInt(t_data.product_id),
            first_recharge:zy_conifg.first_recharge,  //首充收银台
            follow_recharge:zy_conifg.follow_recharge, //复充收银台
            pay_type:pay_type, //书籍定价方式- 10 整本 - 20 千字价格
            price:price, //书籍价格
            callback_conf_reseller_id:zy_conifg.callback_conf_reseller_id, //回传策略ID
            owner:zy_conifg.owner, //创建用户id
            app_key:zy_conifg.app_key, //应用Key
        })
        let promotion_info = null
        if(response.data.code!=0){
            console.error("get_zy_account_list error:",response)
        }else{
            promotion_info = response.data.body
        }
        if(promotion_info==null){
            console.error("promotion_info==null")
            throw response.data
        }
        let start_page = promotion_info.start_page
        let start_param = promotion_info.start_params
        t_data.promotion_id = promotion_info.channel_id
        t_data.start_page = start_page
        t_data.start_param = start_param
        console.log('t_data:',t_data)
        call_back(t_data,null)
    } catch (error) {
        call_back(t_data,error)
        return null;
    }

}

module.exports = CMD;