//#import Util.js
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_user_update_log:services/modelService.jsx
//#import $owl_backend_session:services/modelService.jsx
//#import $owl_session:services/modelService.jsx
//#import $owlService:services/user/UserUtil.jsx
//#import $owlService:services/user/UserLogUtil.jsx
//#import DigestUtil.js
//#import @handlers/include/checklogin.jsx
//#import @owl:utils/PermissionUtil.jsx

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

    var targetUId = params.id;
    var password1 = params.data.password1;
    var password2 = params.data.password2;
    $.log("mall_u...changePassword.jsx...userId=" + targetUId);
    if (!password1) {
        $.log("mall_u...changePassword.jsx...密码不能为空...userId=" + targetUId);
        out.print(JSON.stringify({state: "err", msg: "密码不能为空"}));
        return;
    }
    if (password1 !== password2) {
        $.log("mall_u...changePassword.jsx...不一致...userId=" + targetUId);
        out.print(JSON.stringify({state: "err", msg: "两次密码不一致"}));
        return;
    }
    var user = owl_mall_uService.get(targetUId);
    if (!user) {
        $.log("mall_u...changePassword.jsx...用户不存在...userId=" + targetUId);
        out.print(JSON.stringify({state: "err", msg: "用户不存在"}));
        return;
    }

    var ip = $.getClientIp() + "";
    var ua = request.getHeader("user-agent") + "";

    var env = checklogin();
    var loginUserId = env.loginUserId;
    var roleId = env.roleId
    var loginUserName = UserUtil.getUserName(loginUserId);

    try {
        var rroleId = params.rroleId || roleId;
        var permissions = getPermissions(rroleId);

        if (!checkPluginPermission("mall_u_plugin_change_password", permissions)) {
            $.log("mall_u...changePassword.jsx...没有权限...userId=" + targetUId);
            out.print(JSON.stringify({state: "err", msg: "没有权限"}));
            return;
        }

        var random = Math.random();
        var passwordHash = DigestUtil.sha1(password2 + random);
        user.passwordhash = passwordHash;
        user.random = random;
        delete user.pwdV;

        owl_mall_uService.update(user);

        params.data.password1 = "";
        params.data.password2 = "";

        UserLogUtil.addUserLog("1", targetUId, "", "", loginUserId, loginUserName, "后台修改密码", ip, ua);

        removeBackendSessions(user.id, loginUserId);
        removeFrontSessions(user.id, loginUserId);

        out.print(JSON.stringify({state: "ok", msg: "操作成功"}));
        $.log("mall_u...changePassword.jsx...end...userId=" + targetUId);
    } catch (e) {
        $.log("mall_u...changePassword.jsx...error=" + e);
        out.print(JSON.stringify({state: "err", msg: "用户已经被修改了", error: e}));
    }

})();

function removeBackendSessions(userId, loginUserId) {
    var delNum = 0;
    try {
        var searchArgs = {
            userId: userId
        };
        var sr = owl_backend_sessionService.search(null, searchArgs, null, 0, 2000, [{"owl_createTime": {order: "desc"}}]);
        if (sr.state == 'ok') {
            var backendSessions = sr.list;
            for (var i = 0; i < backendSessions.length; i++) {
                var jBackendSession = owl_backend_sessionService.get(backendSessions[i].id);
                if (!jBackendSession) {
                    continue;
                }
                if (jBackendSession.userId != userId) {
                    continue;
                }
                owl_backend_sessionService.del(jBackendSession.id, loginUserId);
                delNum++;
            }
        }
    } catch (e) {
        $.log("mall_u...changePassword.jsx...removeBackendSessions...error=" + e);
    }
    $.log("mall_u...changePassword.jsx...removeBackendSessions...delNum=" + delNum);
}

function removeFrontSessions(userId, loginUserId) {
    var delNum = 0;
    try {
        var searchArgs = {};
        var sr = owl_sessionService.search(null, searchArgs, userId, 0, 2000, [{"owl_createTime": {order: "desc"}}]);
        if (sr.state == 'ok') {
            var frontSessions = sr.list;
            for (var k = 0; k < frontSessions.length; k++) {
                var jFrontSession = owl_sessionService.get(frontSessions[k].id);
                if (!jFrontSession || !jFrontSession.sessionObjs) {
                    continue;
                }
                var sessionObjs = JSON.parse(jFrontSession.sessionObjs);
                if (!sessionObjs.loginInfo) {
                    continue;
                }
                if (sessionObjs.loginInfo.userId != userId) {
                    continue;
                }
                owl_sessionService.del(jFrontSession.id, loginUserId);
                delNum++;
            }
        }
    } catch (e) {
        $.log("mall_u...changePassword.jsx...removeFrontSessions...error=" + e);
    }
    $.log("mall_u...changePassword.jsx...removeFrontSessions...delNum=" + delNum);
}