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

var UserUtil = (function () {
    var f = {
        getUserName: function (userId) {
            if (!userId) {
                return "";
            }
            if (userId == "0") {
                return "root";
            }
            if (userId == "u_0") {
                return "root";
            }
            if (userId == "u_sys") {
                return "系统";
            }
            var jUser = owl_mall_uService.get(userId);
            if (jUser) {
                return jUser.realName || jUser.loginId || jUser.nickName || "";
            }
            return "";
        },
        getShowName: function (jUser) {
            if (!jUser) {
                return "";
            }
            if (jUser.nickName) {
                if (jUser.nickName.length > 2) {
                    return jUser.nickName.substring(0, 2) + '***';
                } else {
                    return jUser.nickName.substring(0, 1) + '***';
                }
            }
            if (jUser.realName) {
                var realName = jUser.realName.substring(0, 1) + '***';
                return realName;
            }
            if (jUser.loginId) {
                var loginId = jUser.loginId.substring(0, 2) + '***' + jUser.loginId.substring(jUser.loginId.length - 1);
                return loginId;
            }
            if (jUser.mobile) {
                return jUser.mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            }
            return "";
        },
        checkUserGroups: function (userGroups, jUser) {
            var user_categories = jUser.user_categories || [];
            if (!userGroups || userGroups.length == 0) {
                //未设置，则认为支持所有
                return true;
            }

            for (var i = 0; i < userGroups.length; i++) {
                var jGroup = userGroups[i];
                if (f.isIncludeCategory(user_categories, jGroup.id)) {
                    return true;
                }
            }
            return false;
        },
        isIncludeCategory: function (categoryList, catId) {
            for (var i = 0; i < categoryList.length; i++) {
                var jCat = categoryList[i];
                if (jCat.id == catId) {
                    return true;
                }
            }
            return false;
        }

    }
    return f;
})();

