//#import $owl_table_permission:services/modelService.jsx
//#import session.js
//#import $owl_backend_session:services/modelService.jsx
//#import $owl_roles:services/modelService.jsx


var PermissionService = {
    getTablePermissions:function(appId, loginUserId,roleId){
        $.log("PermissionService.getTablePermission, appId="+appId + ",roleId=" + roleId + ",loginUserId=" + loginUserId);
        var table = appId;
        if(appId.indexOf('owl_')===0){
            table = appId.substring(4);
        }
        var searchArgs = {table:table, roleId:roleId};
        //m, searchArgs, keyword,from, pageSize, sort, dataSource
        var ret = owl_table_permissionService.search("0",searchArgs,null,0,1000,null);
        var rw = false;
        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; //取出的字段
                    var searchArgs = {};
                    searchArgs[obj.linkUserId] = loginUserId;
                    if(obj.permissionType === 'rw'){
                        rw = true;
                    }

                    var searchModelId = obj.linkDataSource;
                    if(searchModelId.indexOf("owl_")!==0){
                        searchModelId = "owl_" + searchModelId;
                    }

                    $.log("search link dataSource , searchArgs=" + JSON.stringify(searchArgs) + ",linkDataSource=" + searchModelId);
                    var linkRet = owl_table_permissionService.search("0",searchArgs,null,0,1000,null,searchModelId);
                    $.log("linRet=" + JSON.stringify(linkRet));

                    if(linkRet.state==='ok'){
                        if(linkRet.total.value>0){
                            var linkIds = linkRet.list.map(function(o){return o[linkField]});
                            var filter = {terms:{}};
                            filter.terms[obj.filterField] = linkIds;
                            filters.push(filter);
                        }
                        else{
                            var filter = {terms:{}};
                            filter.terms[obj.filterField] = [];
                            filters.push(filter);
                        }

                    }

                });
                return {
                    rw:rw,
                    filters:filters
                }
            }
        }
        return null;
    },
    getAllAppNamesOfLoginUser:function(){
        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 roleId = session.role;
            if(!roleId){
                return ['*'];
            }
            var role = owl_rolesService.get(roleId);
            // $.log("roleId not found:" + roleId);
            if(!role){
                return ['*'];
            }
            var modules = role.modules;
            if(modules){
              return  modules.split(",");
            }
            return ["*"];

        }
        else{
          return ["*"];
        }
    }

}