//#import $owl_orgs:services/modelService.jsx
//#import $owlService:services/org/OrgUtil.jsx

function main() {
    var env = ctx.get("env");
    var jOrg = ctx.get("data");

    var roleId = env.roleId;
    var orgId = env.orgId;
    var userId = env.loginUserId;

    // $.log("doCheckBeforeAddOrUpdate.jsx...env=" + JSON.stringify(env));
    $.log("doCheckBeforeAddOrUpdate.jsx...env.orgId=" + orgId + "...jOrg.id=" + jOrg.id + "...userId=" + userId + "...roleId=" + roleId);
    if(orgId && orgId != "0" && jOrg.orgType == "org") {
        throw "抱歉，您无权新增和管理组织机构";
    }

    if (orgId != "0" && orgId == jOrg.id) {
        throw "抱歉，您无权修改自己的商家信息";
    }

    if (jOrg.parent_id != "0") {
        var jParentOrg = owl_orgsService.get(jOrg.parent_id);
        if (jParentOrg.merType == "gp") {
            //如果上级组织是集采商家，则自动设置为集采部门
            jOrg.orgType = "department";
            jOrg.merType = "dp";
        }
    }
}

main();
