//#import $@projectCode:services/modelService.jsx
//#import $owl_batch_import:services/modelService.jsx
//#import $owl_process_job_log:services/modelService.jsx


var batchId = batchImportId;

function main(){
    var batchImport = owl_batch_importService.get(batchId);
    if(!batchImport || !batchImport.importFile || batchImport.importFile.length==0){
        return;
    }


    try{
        var s = null;
        var url = batchImport.importFile[0].url;
        if(!url){
            return;
        }
        if(url.endsWith(".zip")){
            s = $.unzipUrl(url,batchImport.zipPass,batchImport.contentFileName);
        }
        else{
            s = $.get(url);
        }

        var now = new Date().getTime();
        var log = {
            objId:batchImport.id,
            startTime:now,
            endTime:now,
            name:"批量导入" + batchImport.id,
            remark:"下载完成，文件长度" + s.length

        }
        owl_process_job_logService.add(log);

        var contentType = batchImport.contentType;
        if(!contentType){
            if(s.startsWith("[")){
                contentType = "json";
            }
            else {
                contentType = "jsonl";
            }
        }
        var rows = null;

        if(contentType==="jsonl"){
            rows = s.split("\n");
        }
        else{
            rows = JSON.parse(s);
        }

        var log = {
            objId:batchImport.id,
            name:"批量导入" + batchImport.id,
            remark:"读取记录完成，记录数" + rows.length

        }

        owl_process_job_logService.add(log);

        var nImported = 0;
        var nError = 0;
        for(var i=0; i<rows.length; i++){
            var row = rows[i];
            try{
                var row = rows[i];
                var rec = null;
                if(contentType==="jsonl") {
                    rec = JSON.parse(row);
                }
                else{
                    rec = row;
                }
                if(!rec._orgId){
                    rec._orgId = batchImport._orgId;
                    rec._orgIds = batchImport._orgIds;
                }
                if(rec._orgId!=batchImport._orgId && batchImport._orgId!='0'){
                    throw "记录的_orgId不正确,应该是" + batchImport._orgId + ",实际是" + rec._orgId;
                }

                //检查
                var oldObj = @projectCodeService.get(rec.id);
                if(oldObj && !batchImport.override){
                    throw "记录已经存在，id=" + rec.id;
                }
                if(oldObj && batchImport.override==true && oldObj._orgId!=rec._orgId){
                    throw "记录的_orgId不正确,不能覆盖其他人的数据，应该是" + rec._orgId + ",实际是" + oldObj._orgId;
                }
            @projectCodeService.add( rec);
                nImported++;

                if(i%1000==0){
                    var now = new Date().getTime();
                    var log = {
                        objId:batchImport.id,
                        startTime:now,
                        endTime:now,
                        name:"批量导入" + batchImport.id,
                        remark:"成功导入记录：" + i
                    }
                    owl_process_job_logService.add(log);
                }
            }
            catch(e){
                var msg = "";
                if(typeof e === "string"){
                    msg = e;
                }
                else{
                    msg = e.msg;
                    if(!msg && e.getCause){
                        msg = e.getCause();
                    }
                }
                var now = new Date().getTime();
                var log = {
                    objId:batchImport.id,
                    startTime:now,
                    endTime:now,
                    name:"批量导入" + batchImport.id,
                    remark:"错误记录，行号" + i + ",内容:" + row + ",错误信息:" + msg
                }
                owl_process_job_logService.add(log);
                nError++;
                if(nError==50){
                    throw "错误记录超过50条，停止导入";
                }
            }
        }

        var now = new Date().getTime();
        var log = {
            objId:batchImport.id,
            startTime:now,
            endTime:now,
            name:"批量导入" + batchImport.id,
            remark:"导入成功"+ nImported + "条记录"
        }
        owl_process_job_logService.add(log);

        batchImport.state = "success";
        owl_batch_importService.update(batchImport);
    }catch(e){
        var msg = "";
        if(typeof e === "string"){
            msg = e;
        }
        else{
            msg = e.msg;
            if(!msg && e.getCause){
                msg = e.getCause();
            }
        }
        var now = new Date().getTime();
        var log = {
            objId:batchImport.id,
            startTime:now,
            endTime:now,
            name:"批量导入" + batchImport.id,
            remark:"出现错误:" + msg
        }
        owl_process_job_logService.add(log);
        batchImport.state = "failed";
        owl_batch_importService.update(batchImport);
    }

}
main();


