const CMD = {}
const redis_help = require('../src/use_redis');
const video_applet_product_controllers = require('../src/data_manager/Controllers/video_applet_product_controllers');
const rabbitMq = require('../src/mq/rabbit-mq');


const messageHandler = async (msg) => {
    CMD.start_task(msg['data']['data'])
};

// 启动消费者
async function startConsumer() {
    try {
        await rabbitMq.consumerDirectMsg(messageHandler,"exchange_system","addMain");
    } catch (error) {
        console.error('启动消费者失败:', error);
    }
}

CMD.init = async function(){
    redis_help.connect(()=>{

    })
    await startConsumer();
}


CMD.start_task = async function(msgBody){
    let PlatformConfig = JSON.parse(await redis_help.getKeyValue("PlatformConfig"))
    let main_info = msgBody.main_info
    let list  =  msgBody.list
    let PlatformInfo = null
    if(main_info.running_status==0){
        console.log("创建了一个关闭的主体：",main_info)
        return
    }
    console.log("main_info:",main_info)
    console.log("list:",list)
    for (let index = 0; index < PlatformConfig.length; index++) {
        const element = PlatformConfig[index];
        if(element.tg_platform_id == main_info.tg_platform_id){
            PlatformInfo = element
            break
        }
    }
    if(PlatformInfo==null){
        console.log("平台配置错误：",msgBody)
    }else{
        redis_help.setKeyValue("isPauseTask","true") 
        console.log("list::",list)
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            let n_data = {book_id:element.product_id,
                book_name:element.product_name,
                tg_platform_id:element.book_platform,
                app_id:element.dy_small_applet_app_id,
                main_id:main_info.id
            }

            const result = await video_applet_product_controllers.createAppletProductData({
                book_platform:n_data.tg_platform_id,
                product_name:n_data.book_name,
                product_id:n_data.book_id,
                dy_small_applet_app_id:n_data.app_id,
                status:0,
                main_id:n_data.main_id,
                promotion_id:'',
                dy_small_program_start:'',
                dy_small_program_start_data:'',
                wait_status:0,
            })
        }
        redis_help.setKeyValue("isPauseTask","false") 
    }
}

CMD.init()