//#import Util.js
//#import $owlPasswordService:services/consts.jsx
//#import user.js
//#import login.js
//#import session.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_backend_session:services/modelService.jsx
//#import $owl_orgs:services/modelService.jsx
//#import DigestUtil.js
function getOrgsOfUser(userId){
    var searchArgs = {
        "staff.id":userId
    }
    var searchResult = owl_orgsService.search("0",searchArgs,0,0,1000,null);
    if(searchResult && searchResult.state==='ok'){
        $.log("state===ok, result=" + JSON.stringify(searchResult));
        return searchResult.list;
    }
    else{
        if(searchResult){
            $.log("searchResult=" + JSON.stringify(searchResult));
        }
        else{
            $.log("searchResult===null");
        }

        return [];
    }
}

(function(){
    var body = $body;
    var params = JSON.parse(body);
    var loginId = params.userName;
    var password = params.password;

    var stockuser = null;
    for(var i=0; i<stockusers.length; i++){
        var su = stockusers[i];
        if(su.loginId==loginId && su.password==password){
            stockuser = su;
            break;
        }
    }

    var isid=null;
    if(typeof _isid === 'undefined'){
        //通过cookie获得isid
        isid = SessionService.getSessionId(request);
    }
    else{
        isid=_isid;
    }


    var now = new Date().getTime();
    var sessionId = 'owl_backend_session_' + isid;
    var success = false;

    if(stockuser){
        var times = 0;
        while(times < 5){
            try{
                times+=1;
                var session = owl_backend_sessionService.get(sessionId);
                var isnew = false;
                if(!session){
                    session = {
                        createTime:now,
                        sessionObjs:{}
                    };
                    isnew = true;
                }
                var sessionObjs = session.sessionObjs;
                if(!sessionObjs){
                    sessionObjs = {};
                }
                else{
                    if(typeof sessionObjs=='string'){
                        sessionObjs = JSON.parse(sessionObjs);
                    }
                }
                sessionObjs.loginUser = stockuser;
                session.sessionObjs = JSON.stringify(sessionObjs);
                session.lastModified = now;
                session.loginTime =now;
                session.isid = isid;
                session.isLogin='y';
                session.role = "_none";
                session.userId=stockuser.uid;
                session.orgId="_none";
                session.orgIds = "_none";
                $.log("login.jsx......session.userId=" + stockuser.id + ","+ JSON.stringify(stockuser));
                if(isnew){
                    owl_backend_sessionService.add(session);
                }
                else{
                    owl_backend_sessionService.update(session);
                }
                success = true;
                break;
            }
            catch(e){
                $.log(e.toString());
            }

        }
        if(success){
            var ret = {
                state:"ok",
                name:stockuser.name,
                isid:isid
            }
            out.print(JSON.stringify(ret));
        }
        else{
            var ret = {
                state:'err',
                msg:'服务器太忙，请稍后再试试。'
            }
            out.print(JSON.stringify(ret));
        }

    }
    else{
        var admin = owl_mall_uService.getUniqueObj('mobile',loginId);
        if(!admin){
            admin = owl_mall_uService.getUniqueObj('loginId',loginId);
        }
        if(!admin){
            var ret = {
                state:'err',
                code:'7',
                msg:'用户不存在',
            }
            out.print(JSON.stringify(ret));
        }
        else if(admin.login_backend!=='yes'){
            var ret = {
                state:'err',
                code:'8',
                msg:'无权登录后台',
            }
            out.print(JSON.stringify(ret));
        }
        else{
            var passwordHash = DigestUtil.sha1(password+admin.random);
            var admin_passwordhash = admin.passwordhash;
            delete admin.passwordhash;
            delete admin.random;

            if(passwordHash == admin_passwordhash){

                var times = 0;
                while(times < 5){
                    times+=1;
                    var session = owl_backend_sessionService.get(sessionId);
                    var isnew = false;
                    if(!session){
                        session = {
                            createTime:now,
                            sessionObjs:{}
                        };
                        isnew = true;
                    }
                    var sessionObjs = session.sessionObjs;
                    if(!sessionObjs){
                        sessionObjs = {};
                    }
                    else{
                        if(typeof sessionObjs=='string'){
                            sessionObjs = JSON.parse(sessionObjs);
                        }

                    }
                    var ip = $.getClientIp();
                    sessionObjs.loginUser = admin;
                    session.sessionObjs = JSON.stringify(sessionObjs);
                    session.lastModified = now;
                    session.loginTime =now;
                    session.isLogin='y';
                    session.userId=admin.id;
                    session.role = '_none';
                    session.orgId = '_none';
                    session.isid = isid;
                    session.ip = ip;
                    if(isnew){
                        owl_backend_sessionService.add(session);
                    }
                    else{
                        owl_backend_sessionService.update(session);
                    }

                    success = true;
                    break;

                }
                if(success){
                    var ret = {
                        state:'ok',
                        msg:'登录成功',
                        name:admin.name,
                        isid:isid,
                        session:session
                    }
                    out.print(JSON.stringify(ret));
                    return;
                }
                else{
                    var ret = {
                        state:'err',
                        msg:'服务器太忙，请稍后再试试。'
                    }
                    out.print(JSON.stringify(ret));
                }

            }
            else{
                var ret = {
                    state:'err',
                    admin:admin,
                    msg:'用户名或者密码错误'
                }
                out.print(JSON.stringify(ret));
                return;
            }

        }
    }
})();

