const CMD = {}
const time_count = 2000;
const redis_help = require('../src/use_redis');
const origin_data_controllers = require('../src/data_manager/Controllers/origin_data_controllers');
const helper = require('../src/helper');

async function processTask(){
    try{
        let FilterConfig = await redis_help.getKeyValue("FilterConfig")
        FilterConfig = JSON.parse(FilterConfig)
        let meaningless_Ids = []
        const result = await origin_data_controllers.getOriginDataByStatusAndLimit({status:3},500)
        if(result.success){
            let origin_data_list = result.data
            if(origin_data_list.length<=0){
                throw "no data!"
            }
            for (let index = 0; index < origin_data_list.length; index++) {
                const element = origin_data_list[index];
                if(helper.checkChannel(element.guajian_link,'title=%')){

                    let d = await require('./parse_wx_guajian').fetchData(element.guajian_link)
                    // console.log("d:",d)
                    if(d!=null){
                        let guajian_link = `wx_gua_jian=1$${d.wechat_microapp_jump_url}&name=${d.wechat_microapp_title}`
                        if(helper.checkChannel(guajian_link,'-smls-')){
                            meaningless_Ids.push(element.id)
                        }else{
                            await origin_data_controllers.updateOriginData({id:element.id},{status:0,guajian_link:guajian_link})
                        }
                    }else{
                        // console.log("element:",element.id)
                        meaningless_Ids.push(element.id)
                    }
                    await new Promise(resolve => setTimeout(resolve, 500));
                }else{
                    meaningless_Ids.push(element.id)
                }
            }

            // console.log("meaningless_Ids:",meaningless_Ids)
            
            if(meaningless_Ids.length>0){
                // console.log("meaningless_Ids:",meaningless_Ids)
                let update_result =  await origin_data_controllers.updateOriginDataByIds({ids:meaningless_Ids},{status:4})
                if(update_result.success){
                    console.log(update_result.message)
                }
            }

          
        }
    }catch(e){
        console.log("筛选数据错误：",e)
    } finally{
        global.setTimeout(processTask,time_count)
    }
}

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

    })
    processTask()
}

CMD.init()
