//#import Util.js
//#import $owl_orgs:services/modelService.jsx

var OrgUtil = (function () {
    var f = {
        checkPrivilege: function (jObj, orgId) {
            if (orgId == "0") {
                return true;
            }
            if (!jObj || !orgId) {
                return false;
            }
            var orgIds = jObj._orgIds;
            if (orgIds) {
                for (var i = 0; i < orgIds.length; i++) {
                    if (orgIds[i] == orgId) {
                        return true;
                    }
                }
            }
            return false;
        },
        /**
         * 判断某个商家是否在该组织机构下面
         * @param orgId
         * @param merchantId
         * @returns {boolean}
         */
        checkPrivilegeExt: function (orgId, merchantId) {
            if (!orgId || !merchantId) {
                return false;
            }
            var orgIds = f.getSaveOrgIds(merchantId);

            for (var i = 0; i < orgIds.length; i++) {
                if (orgIds[i] == orgId) {
                    return true;
                }
            }

            return false;
        },
        getSaveOrgIds: function (_orgId) {
            if (!_orgId || _orgId == '0') {
                return ['0'];
            }
            return ["0", _orgId].concat(f.getParentOrgIds(_orgId));
        },
        getParentOrgIds: function (orgId) {
            if (!orgId || orgId == '0') {
                return ['0'];
            }

            var org = owl_orgsService.get(orgId);
            if (org) {
                return org.parentPathIds;
            }
            return [orgId];
        },
        getOrgLogo: function (jOrg) {
            if (jOrg && jOrg.logo && jOrg.logo.length > 0) {
                return jOrg.logo[0];
            }
            return null;
        },
        getOrgLogoUrl: function (jOrg, width, height) {
            var jImage = f.getOrgLogo(jOrg);
            if (jImage) {
                return jImage.url + "?x-oss-process=image/resize,w_" + width + ",h_" + height;
            }
            return "";
        },
        getOrgLogoUrlEx: function (jOrg, spex) {
            if (spex) {
                var imageSize = spex.split("X");

                return f.getOrgLogoUrl(jOrg, imageSize[0], imageSize[1]);
            }
            var jImage = f.getOrgLogo(jOrg);
            if (jImage) {
                return jImage.url;
            }
            return "";
        },
        checkSupportPayment: function (jOrg, payInterfaceId) {
            if (jOrg && jOrg.payment) {
                for (var i = 0; i < jOrg.payment.length; i++) {
                    var jPayment = jOrg.payment[i];
                    if (jPayment.id == payInterfaceId) {
                        return true;
                    }
                }
            }
            return false;
        },
        getOrgName: function (orgId) {
            if (orgId == "0") {
                return "平台";
            }
            var jOrg = owl_orgsService.get(orgId);
            if (jOrg) {
                return jOrg.name;
            }
            return "";
        },
        /**
         * 自提验证码类型
         * @param orgId
         * @returns {string|*}
         */
        getSelfDeliveryType: function (orgId) {
            return f.getOrgSettingValue(orgId, null, "selfDeliveryType");
        },
        /**
         * 订单未支付自动关闭时间
         * @param orgId
         * @returns {string|*}
         */
        getOrderAutoCloseTime: function (orgId) {
            var value = f.getOrgSettingValue(orgId, "isEnableAutoClose", "orderAutoCloseTime");
            if (!value) {
                value = 86400;//默认24小时自动取消
            }
            return value;
        },
        getOrderAutoCompletedTime: function (orgId) {
            var value = f.getOrgSettingValue(orgId, "isEnableAutoCompleted", "orderAutoCompletedTime");
            if (!value) {
                value = 86400 * 7;//默认7天自动签收
            }
            return value;
        },
        /**
         * 预存款充值订单未支付自动关闭时间
         * @param orgId
         * @returns {string|*}
         */
        getRechargeOrderAutoCloseTime: function (orgId) {
            var value = f.getOrgSettingValueForRechargeOrderInfo(orgId, "rc_isEnableAutoClose", "rc_orderAutoCloseTime");
            if (!value) {
                value = 86400;//默认24小时自动取消
            }
            return value;
        },
        /**
         * 买券订单未支付自动关闭时间
         * @param orgId
         * @returns {string|*}
         */
        getBuyCouponOrderAutoCloseTime: function (orgId) {
            var value = f.getOrgSettingValueForBuyCouponOrderInfo(orgId, "bc_isEnableAutoClose", "bc_orderAutoCloseTime");
            if (!value) {
                value = 86400;//默认24小时自动取消
            }
            return value;
        },
        /**
         * 【昂捷券平台】买券订单未支付自动关闭时间
         * @param orgId
         * @returns {string|*}
         */
        getEnjoyBuyCouponOrderAutoCloseTime: function (orgId) {
            var value = f.getOrgSettingValueForBuyCouponOrderInfo(orgId, "enjoy_bc_isEnableAutoClose", "enjoy_bc_orderAutoCloseTime");
            if (!value) {
                value = 600;//默认10分钟自动取消
            }
            return value;
        },
        /**
         * 商品库存为0后仍然缺货自动下架时间
         * @param orgId
         * @returns {string|*}
         */
        getProductAutoPublishDownTime: function (orgId) {
            var value = f.getOrgSettingValueForProductInfo(orgId, "isEnableProAutoDown", "proAutoDownTime");
            return value;
        },
        getOrgSettingValue: function (orgId, enableKey, argKey) {
            if (!orgId || orgId == '0') {
                return "";
            }

            var jSetting = owl_orgsService.get("owl_mall_org_setting_" + orgId);
            if (jSetting && jSetting.isEnable === "y" && jSetting.common_info && jSetting.common_info[argKey]) {
                //如果商家自身有设置，则以设置为准
                if (enableKey) {
                    if (jSetting.common_info[enableKey] === "y") {
                        return jSetting.common_info[argKey];
                    } else {
                        //未启用，则返回空
                        return "";
                    }
                } else {
                    //对于不需要判断是否启用状态的参数，则直接返回
                    return jSetting.common_info[argKey];
                }

            }
            //todo:可以优化一下？

            var org = owl_orgsService.get(orgId);
            if (org) {
                var parentPathIds = org.parentPathIds;
                if (parentPathIds && parentPathIds.length > 0) {
                    for (var i = parentPathIds.length - 1; i >= 0; i--) {
                        var tempOrgId = parentPathIds[i];
                        jSetting = owl_orgsService.get("owl_mall_org_setting_" + tempOrgId);
                        if (jSetting && jSetting.isEnable === "y" && jSetting.common_info && jSetting.common_info[argKey]) {
                            //如果商家自身有设置，则以设置为准
                            if (enableKey) {
                                if (jSetting.common_info[enableKey] === "y") {
                                    return jSetting.common_info[argKey];
                                } else {
                                    //未启用，则返回空
                                    return "";
                                }
                            } else {
                                //对于不需要判断是否启用状态的参数，则直接返回
                                return jSetting.common_info[argKey];
                            }
                        }
                    }
                }
            }
            return "";
        },
        getOrgSettingValueForRechargeOrderInfo: function (orgId, enableKey, argKey) {
            if (!orgId || orgId == '0') {
                return "";
            }

            var jSetting = owl_orgsService.get("owl_mall_org_setting_" + orgId);
            if (jSetting && jSetting.isEnable === "y" && jSetting.rechargeOrder_info && jSetting.rechargeOrder_info[argKey]) {
                //如果商家自身有设置，则以设置为准
                if (enableKey) {
                    if (jSetting.rechargeOrder_info[enableKey] === "y") {
                        return jSetting.rechargeOrder_info[argKey];
                    } else {
                        //未启用，则返回空
                        return "";
                    }
                } else {
                    //对于不需要判断是否启用状态的参数，则直接返回
                    return jSetting.rechargeOrder_info[argKey];
                }

            }
            //todo:可以优化一下？

            var org = owl_orgsService.get(orgId);
            if (org) {
                var parentPathIds = org.parentPathIds;
                if (parentPathIds && parentPathIds.length > 0) {
                    for (var i = parentPathIds.length - 1; i >= 0; i--) {
                        var tempOrgId = parentPathIds[i];
                        jSetting = owl_orgsService.get("owl_mall_org_setting_" + tempOrgId);
                        if (jSetting && jSetting.isEnable === "y" && jSetting.rechargeOrder_info && jSetting.rechargeOrder_info[argKey]) {
                            //如果商家自身有设置，则以设置为准
                            if (enableKey) {
                                if (jSetting.rechargeOrder_info[enableKey] === "y") {
                                    return jSetting.rechargeOrder_info[argKey];
                                } else {
                                    //未启用，则返回空
                                    return "";
                                }
                            } else {
                                //对于不需要判断是否启用状态的参数，则直接返回
                                return jSetting.rechargeOrder_info[argKey];
                            }
                        }
                    }
                }
            }
            return "";
        },
        getOrgSettingValueForBuyCouponOrderInfo: function (orgId, enableKey, argKey) {
            if (!orgId || orgId == '0') {
                return "";
            }

            var jSetting = owl_orgsService.get("owl_mall_org_setting_" + orgId);
            if (jSetting && jSetting.isEnable === "y" && jSetting.buyCouponOrder_info && jSetting.buyCouponOrder_info[argKey]) {
                //如果商家自身有设置，则以设置为准
                if (enableKey) {
                    if (jSetting.buyCouponOrder_info[enableKey] === "y") {
                        return jSetting.buyCouponOrder_info[argKey];
                    } else {
                        //未启用，则返回空
                        return "";
                    }
                } else {
                    //对于不需要判断是否启用状态的参数，则直接返回
                    return jSetting.buyCouponOrder_info[argKey];
                }

            }
            //todo:可以优化一下？

            var org = owl_orgsService.get(orgId);
            if (org) {
                var parentPathIds = org.parentPathIds;
                if (parentPathIds && parentPathIds.length > 0) {
                    for (var i = parentPathIds.length - 1; i >= 0; i--) {
                        var tempOrgId = parentPathIds[i];
                        jSetting = owl_orgsService.get("owl_mall_org_setting_" + tempOrgId);
                        if (jSetting && jSetting.isEnable === "y" && jSetting.buyCouponOrder_info && jSetting.buyCouponOrder_info[argKey]) {
                            //如果商家自身有设置，则以设置为准
                            if (enableKey) {
                                if (jSetting.buyCouponOrder_info[enableKey] === "y") {
                                    return jSetting.buyCouponOrder_info[argKey];
                                } else {
                                    //未启用，则返回空
                                    return "";
                                }
                            } else {
                                //对于不需要判断是否启用状态的参数，则直接返回
                                return jSetting.buyCouponOrder_info[argKey];
                            }
                        }
                    }
                }
            }
            return "";
        },
        getOrgSettingValueForProductInfo: function (orgId, enableKey, argKey) {
            if (!orgId || orgId == '0') {
                return "";
            }

            var jSetting = owl_orgsService.get("owl_mall_org_setting_" + orgId);
            if (jSetting && jSetting.isEnable === "y" && jSetting.product_info && jSetting.product_info[argKey]) {
                //如果商家自身有设置，则以设置为准
                if (enableKey) {
                    if (jSetting.product_info[enableKey] === "y") {
                        return jSetting.product_info[argKey];
                    } else {
                        //未启用，则返回空
                        return "";
                    }
                } else {
                    //对于不需要判断是否启用状态的参数，则直接返回
                    return jSetting.product_info[argKey];
                }

            }
            //todo:可以优化一下？

            var org = owl_orgsService.get(orgId);
            if (org) {
                var parentPathIds = org.parentPathIds;
                if (parentPathIds && parentPathIds.length > 0) {
                    for (var i = parentPathIds.length - 1; i >= 0; i--) {
                        var tempOrgId = parentPathIds[i];
                        jSetting = owl_orgsService.get("owl_mall_org_setting_" + tempOrgId);
                        if (jSetting && jSetting.isEnable === "y" && jSetting.product_info && jSetting.product_info[argKey]) {
                            //如果商家自身有设置，则以设置为准
                            if (enableKey) {
                                if (jSetting.product_info[enableKey] === "y") {
                                    return jSetting.product_info[argKey];
                                } else {
                                    //未启用，则返回空
                                    return "";
                                }
                            } else {
                                //对于不需要判断是否启用状态的参数，则直接返回
                                return jSetting.product_info[argKey];
                            }
                        }
                    }
                }
            }
            return "";
        },
        getOrgSettingValueEx: function (orgId, catKey, argKey) {
            if (!orgId || orgId == '0') {
                return "";
            }

            var jSetting = owl_orgsService.get("owl_mall_org_setting_" + orgId);
            if (jSetting && jSetting[catKey]) {
                //如果商家自身有设置，则以设置为准
                var jCatInfo = jSetting[catKey];
                if (jCatInfo) {
                    return jCatInfo[argKey];
                }
            }

            var org = owl_orgsService.get(orgId);
            if (org) {
                var parentPathIds = org.parentPathIds;
                if (parentPathIds && parentPathIds.length > 0) {
                    for (var i = parentPathIds.length - 1; i >= 0; i--) {
                        var tempOrgId = parentPathIds[i];
                        jSetting = owl_orgsService.get("owl_mall_org_setting_" + tempOrgId);
                        if (jSetting && jSetting[catKey]) {
                            jCatInfo = jSetting[catKey];
                            if (jCatInfo) {
                                return jCatInfo[argKey];
                            }
                        }
                    }
                }
            }
            return "";
        }
    };
    return f;
})();