//#import $owl_batch_import:services/modelService.jsx
//#import $owl_process_job:services/modelService.jsx
//#import $owl_process_job_log:services/modelService.jsx
//#import $owl_astro_product_que:services/modelService.jsx
//#import jobs.js
var process_id = "batch_import_task";
function main(){
    // $.log("doImportTask.jsx...main...begin");
    if(checkTaskRunning()){
        return;
    }
    updateTaskRunning()
    var nUpdate = 0;
    try{
        var batchImport = getWaitingBatchImport();
        if(!batchImport){
            return;
        }
        var appId = batchImport.app_id;
        JobsService.submitTask(appId,"tasks/doBatchImport.jsx", {batchImportId:batchImport.id}, new Date().getTime());

    }
    finally{
        updateTaskEnd();
        var endTime = new Date().getTime();
        var beginTime = endTime ;
        if(nUpdate>0){
            var log = {
                objId:batchImport.id,
                startTime:beginTime,
                endTime:endTime,
                name:"批量导入",
                process_id:process_id,
                remark:appId + ", 开始导入:" + batchImport.id

            }
            owl_process_job_logService.add(log);
        }

    }

}
main();




function checkTaskRunning(){
    var jobId = "owl_process_job_" + process_id;
    var process = owl_process_jobService.get(jobId);
    if(!process){
        return false;
    }
    if(process.state == 'running'){
        return true;
    }
}

function updateTaskRunning(){
    var jobId = "owl_process_job_" + process_id;
    var process = owl_process_jobService.get(jobId);
    if(!process){
        process = {
            process_id:process_id,
            state:"running",
            name:"商品定时上下架",
            startTime:new Date().getTime(),
            remark:"每分钟执行一次"
        }
        owl_process_jobService.add(process);
    }
    else{
        process.state = "running";
        process.startTime = new Date().getTime();
        owl_process_jobService.update(process);
    }
}

function updateTaskEnd(){
    var jobId = "owl_process_job_" + process_id;
    var process = owl_process_jobService.get(jobId);
    if(!process){
        process = {
            process_id:process_id,
            state:"ended",
            name:"批量导入",
            endTime:new Date().getTime(),
            remark:"每分钟执行一次"
        }
        owl_process_jobService.add(process);
    }
    else{
        process.state = "ended";
        process.endTime = new Date().getTime();
        owl_process_jobService.update(process);
    }
}
function getWaitingBatchImport(){

    var now = new Date().getTime();
    var lastTime = now - 1000*60*30;
    var searchArgs = {
        state:'notstart'
    }

    var dataSource = null;
    var mfilters = null;
    var keyword = null;
    var pageSize = 1000;
    var from = 0;
    var sort = {
        owl_createTime:{order:"desc"}
    }


    var sr = owl_batch_importService.search(mfilters, searchArgs, keyword,from, pageSize, sort, dataSource );
    if(sr.state == 'ok'){
        if(sr.list && sr.list.length>0){
            var batchImport =  sr.list[0];
            batchImport  = owl_batch_importService.get(batchImport.id);
            if(batchImport.state == 'notstart'){
                batchImport.state = 'running';
                owl_batch_importService.update(batchImport);
                return batchImport;
            }
        }
    }
    return null;

}
