//#import $owl_org_entity:services/modelService.jsx
//#import $owl_reseller:services/modelService.jsx
//#import $owl_resell_products:services/modelService.jsx
//#import $owlService:services/product/SkuSearchUtil.jsx

var ResellerUtil = (function () {
    var f = {
        getBranchcodeByUserId: function (resell_user_id) {
            if (resell_user_id) {
                var jReseller = f.getResellerByUserId(resell_user_id);
                if (jReseller && jReseller.resell_orgId) {
                    var jOrgEntity = owl_org_entityService.get(jReseller.resell_orgId);
                    if (jOrgEntity) {
                        return jOrgEntity.branchcode || "";
                    }
                }
            }
            return "124";//默认124
        },
        getOrgEntityByUserId: function (resell_user_id) {
            if (resell_user_id) {
                var jReseller = f.getResellerByUserId(resell_user_id);
                if (jReseller && jReseller.resell_orgId) {
                    return owl_org_entityService.get(jReseller.resell_orgId);
                }
            }
            return null;
        },
        getResellerByUserId: function (userId) {
            var searchArgs = {
                user_id: userId
            }

            var sort = null;
            var sr = owl_resellerService.search(null, searchArgs, null, 0, 10, sort);
            if (sr.state == 'ok') {
                if (sr.list.length > 0) {
                    for (var i = 0; i < sr.list.length; i++) {
                        var jData = sr.list[i];
                        if (jData.state == "active") {
                            return jData;
                        }
                    }
                    return sr.list[0];
                }
            }
            return null;
        },
        updateResellProductPublishState: function (skuId, state, userId) {
            try {
                var id = "owl_resell_products_" + skuId;
                var jResellProduct = owl_resell_productsService.get(id);
                if (jResellProduct && jResellProduct.publish_state != state) {
                    jResellProduct.publish_state = state;
                    owl_resell_productsService.update(jResellProduct);
                    $.log("ResellerUtil.jsx...updateResellProductPublishState...updated...skuId=" + skuId + "...state=" + state + "....userId=" + userId);
                }
            } catch (e) {
                $.log("ResellerUtil.jsx...updateResellProductPublishState...error...skuId=" + skuId + "...state=" + state + "....userId=" + userId + "...error=" + e);
            }
        },

        getProductTotalResellState: function (productId, curSkuId) {
            var skus = SkuSearchUtil.getSkuList(productId);
            if (skus.length == 0) {
                return "n";
            }
            var resellProducts = f.getResellProducts(productId);
            if (resellProducts.length == 0) {
                return "n";
            }
            for (var i = 0; i < skus.length; i++) {
                var jSku = skus[i];
                if (curSkuId && jSku.id == curSkuId) {
                    continue;
                }

                if (!f.isExistResellProduct(resellProducts, jSku)) {
                    return "n";
                }
            }
            return "y";
        },

        getResellProducts: function (productId) {
            var searchArgs = {
                "product_id": productId
            }
            var skuResult = owl_resell_productsService.search(null, searchArgs, null, 0, 1000, null);
            if (skuResult.state == 'ok') {
                return skuResult.list;
            }
            return [];
        },

        isExistResellProduct: function (list, sku) {
            for (var i = 0; i < list.length; i++) {
                if (sku.id == list[i].sku_Id) {
                    return true;
                }
            }
            return false;
        }
    }
    return f;
})();

