//#import session.js
//#import $owl_backend_session:services/modelService.jsx
//#import $owlPermission:services/permissionService.jsx

function checklogin(){
    var magic = $.getSystemProperty("magic");
    var magicParams = $.params.ma;
    var m = '0';//默认是 平台
    if(magic === magicParams){
        //代表是内部操作
        var user = {
            uid: 'internal',
            loginId: "internal",
            name: "系统内部访问",
            allowed_appIds: ["*"]
        }

        return user;
    }
    else{
        var isid = SessionService.getSessionId(request);
        var now = new Date().getTime();
        var sessionId = 'owl_backend_session_' + isid;
        var session = owl_backend_sessionService.get(sessionId);
        if(session && session.lastModified > now - 7*24*3600*1000){
            var loginUserId = session.userId
            var roleId = session.role;
            var orgId = session.orgId;
            var orgIds = session.orgIds;
            if(loginUserId){
                var env = {
                    now: new Date().getTime(),
                    loginId: loginUserId,
                    roleId:roleId,
                    shopId:m,
                    warehouseId:"",
                    roleId:roleId,
                    orgId:orgId,
                    orgIds:orgIds,
                    m:m
                }
                return env;
            }
            else{
                var ret = {
                    state:'err',
                    code:'no login',
                    msg:"not login!"
                }
                out.print(JSON.stringify(ret));
                return null;
            }
        }
        else{
            var ret = {
                state:'err',
                code:'no login',
                isid:isid,
                msg:"not login!"
            }
            out.print(JSON.stringify(ret));
            return null;
        }
    }
}

function getTablePermission(table, loginUserId,roleId){

    if(typeof PermissionService  != 'undefined' && PermissionService.getTablePermissions){
        var tablePermission = PermissionService.getTablePermissions(table,loginUserId,roleId);
        return tablePermission;
    }
    return null;

    /*var searchArgs = {table:table, roleId:roleId};
    //m, searchArgs, keyword,from, pageSize, sort, dataSource
    var ret = owl_table_permissionService.search("0",searchArgs,0,1000,null);
    if(ret.state==='ok'){
        if(ret.total===0){
            return null;
        }
        else{
            var filters = [];
            var list = ret.list;
            list.forEach(function(obj){
                var linkField = obj.linkField;
                if(obj.searchArgs){
                    var expanded = obj.searchArgs;
                    if(loginUserId){
                        expanded = obj.searchArgs.replaceAll("@loginUserId", loginUserId);
                    }
                    var linkSearchArgs =  JSON.parse(expanded);
                    var linkRet = owl_table_permissionService.search("0",linkSearchArgs,0,1000,null,obj.linkDataSource);
                    if(linkRet.state==='ok'){
                        if(linkRet.total>0){
                            var linkIds = linkRet.list.map(function(o,index){return o[linkField]});
                            var filter = {terms:{}};
                            filter.terms[obj.filterField] = linkIds;
                            filters.push(filter);
                        }
                    }
                }
            });
            return filters;
        }
    }
    return null;*/
}
