//#import Util.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_r_apps:services/modelService.jsx
//#import $owl_backend_session:services/modelService.jsx
//#import session.js
function main(){
    var params = JSON.parse($body);
    var isid = SessionService.getSessionId(request);
    var sessionId = 'owl_backend_session_' + isid;

    var session = owl_backend_sessionService.get(sessionId);
    var roleId = session.roleId;
    var userId = session.userId;
    var orgId = session.orgId;
    var apps = params.apps;
    var ip = "" + $.getClientIp();

    var id = "owl_r_apps_" + orgId + userId + roleId;

    var rapps = owl_r_appsService.get(id);
    if(!rapps){
        rapps = {
            userId:userId,
            orgId:orgId,
            roleId:roleId,
            apps:apps,
            ip:ip,
            ts:new Date().getTime()
        }
        owl_r_appsService.add(rapps);
    }
    else{
        rapps.apps = apps;
        rapps.ip = ip;
        rapps.ts = new Date().getTime();
        owl_r_appsService.update(rapps);
    }
    var ret = {
        state: 'ok',
        id:rapps.id
    }
    out.print(JSON.stringify(ret));
}
main();