//#import @handlers/include/checklogin.jsx
//#import $owl_batch_import:services/modelService.jsx

function main(){
    var params = JSON.parse($body);
    var appId = params.appId;
    var env = checklogin();
    if(!env){
        return;
    }
    var roleId = env.roleId
    var orgId = env.orgId;
    try {
        var permissions = getPermissions(roleId,appId);
        if(roleId!=='internal'  && roleId!='0') {
            if (!checkPermission({_orgId: orgId}, permissions, orgId, "add")) {
                var ret = {
                    state: 'err',
                    msg: '没有权限'
                }
                out.print(JSON.stringify(ret));
                return;
            }
        }


        var params = JSON.parse($body);
        var appId = params.appId;
        var url = params.url;
        var name = params.name;
        var msg = params.msg;
        var override = params.override || "false";
        var zipPass = params.password;
        var contentFileName = params.contentFileName;



        var batch_import = {
            app_id:appId,
            app_name:appId,
            importFile:[{url:url,name:name}],
            contentFileName:contentFileName,
            msg:msg,
            state:"notstart",
            override:override,
            zipPass:zipPass,
            _orgId:orgId,
            _orgIds:[orgId].concat(getParentOrgIds(orgId))
        }
        owl_batch_importService.add(batch_import);
        var ret = {
            state: 'ok',
            id: batch_import.id
        }
        out.print(JSON.stringify(ret));

    }
    catch(e){
        var msg = e;
        if(typeof e == 'string'){
            msg = e;
        }
        else if(e.msg){
            msg = e.msg;
        }
        var ret = {
            state: 'err',
            msg:msg
        }
        out.print(JSON.stringify(ret));
    }


}
main();