//#import $owl_mall_u:services/modelService.jsx
//#import $owl_backend_session:services/modelService.jsx

var BackendSessionUtil = (function () {
    var f = {
        getSessionObject: function (isid) {
            var sessionRec = owl_backend_sessionService.get("owl_backend_session_" + isid);
            if (sessionRec && sessionRec.del != 'T') {
                var sessionObjs = {};
                if (!sessionRec.sessionObjs) {
                    sessionObjs = {};
                } else {
                    sessionObjs = JSON.parse(sessionRec.sessionObjs)
                }
                return sessionObjs;
            } else {
                return {};
            }
        },
        saveSessionObject: function (isid, sessionObject) {
            var sessionRec = owl_backend_sessionService.get("owl_backend_session_" + isid);
            if (sessionRec && sessionRec.del != 'T') {
                sessionRec.lastModified = new Date().getTime();
                sessionRec.sessionObjs = JSON.stringify(sessionObject);
                owl_backend_sessionService.update(sessionRec);
            } else {
                var now = new Date().getTime();
                sessionRec = {
                    lastModified: now,
                    createTime: now,
                    isid: isid,
                    sessionObjs: JSON.stringify(sessionObject)
                }
                owl_backend_sessionService.add(sessionRec);
            }
        },
        getUserId: function (isid) {
            if (isid) {
                var session = f.getSessionObject(isid);
                if (session && session.loginInfo) {
                    var now = new Date().getTime();

                    // $.log("BackendSessionUtil.jsx....getUserId...begin....userId=" + session.loginInfo.userId + "...lastActiveTime=" + session.lastActiveTime);
                    if (!session.lastActiveTime) {
                        //以免功能更新后，所有用户都自动退出
                        var jUser = owl_mall_uService.get(session.loginInfo.userId);
                        if (!jUser || jUser.isEnable != "1") {
                            //账号异常，自动退出
                            $.log("BackendSessionUtil.jsx....getUserId...lastActiveTime is null...用户以冻结....userId=" + session.loginInfo.userId);
                            return "";
                        }
                        session.lastActiveTime = now;
                        f.saveSessionObject(isid, session);

                        $.log("BackendSessionUtil.jsx....getUserId...lastActiveTime init updated....userId=" + session.loginInfo.userId);
                    } else {
                        if (now - session.lastActiveTime > 1000 * 3600 * 24 * 30) {
                            //7天内未做任何操作将自动退出系统
                            $.log("BackendSessionUtil.jsx....getUserId...lastActiveTime > 7day....userId=" + session.loginInfo.userId);
                            return "";
                        }
                        if (now - session.lastActiveTime > 1000 * 60 * 10) {
                            var jUser = owl_mall_uService.get(session.loginInfo.userId);
                            if (!jUser || jUser.isEnable != "1") {
                                //账号异常，自动退出
                                $.log("BackendSessionUtil.jsx....getUserId...用户以冻结....userId=" + session.loginInfo.userId);
                                return "";
                            }

                            //每10分钟更新一次活跃时间
                            session.lastActiveTime = now;
                            f.saveSessionObject(isid, session);
                            $.log("BackendSessionUtil.jsx....getUserId...lastActiveTime updated....userId=" + session.loginInfo.userId);
                        }
                    }
                    return session.loginInfo.userId;
                }
            }
            return "";
        }
    }
    return f;
})();

