def save_market_app_template(self, user, tenant, app_templates):
        template_version = app_templates["template_version"]
        is_v1 = bool(template_version == "v1")
        if is_v1:
            v2_template = template_transform_service.v1_to_v2(app_templates)
        else:
            v2_template = app_templates
        rainbond_app = rainbond_app_repo.get_enterpirse_app_by_key_and_version(
            tenant.enterprise_id, v2_template["group_key"],
            v2_template["group_version"])

        if not rainbond_app:
            if common_services.is_public() and user.is_sys_admin:
                enterprise_id = "public"
            else:
                enterprise_id = tenant.enterprise_id
            rainbond_app = RainbondCenterApp(
                group_key=app_templates["group_key"],
                group_name=app_templates["group_name"],
                version=app_templates['group_version'],
                share_user=0,
                record_id=0,
                share_team="",
                source="market",
                scope="goodrain",
                describe=app_templates["info"],
                pic=app_templates["pic"],
                app_template="",
                enterprise_id=enterprise_id,
                template_version=app_templates.get("template_version", ""))
        if is_v1:
            rainbond_app.share_user = v2_template["share_user"]
            rainbond_app.share_team = v2_template["share_team"]
            rainbond_app.pic = v2_template["pic"]
            rainbond_app.describe = v2_template["describe"]
            rainbond_app.app_template = json.dumps(v2_template)
            rainbond_app.is_complete = True
            rainbond_app.update_time = current_time_str("%Y-%m-%d %H:%M:%S")
            rainbond_app.save()
        else:
            user_name = v2_template.get("publish_user", None)
            user_id = 0
            if user_name:
                try:
                    user = user_repo.get_user_by_username(user_name)
                    user_id = user.user_id
                except Exception as e:
                    logger.exception(e)
            rainbond_app.share_user = user_id
            rainbond_app.share_team = v2_template.get("publish_team", "")
            rainbond_app.pic = v2_template.get("pic", rainbond_app.pic)
            rainbond_app.describe = v2_template.get("update_note",
                                                    rainbond_app.describe)
            rainbond_app.app_template = v2_template["template_content"]
            rainbond_app.is_complete = True
            rainbond_app.update_time = current_time_str("%Y-%m-%d %H:%M:%S")
            rainbond_app.save()
    def sync_market_plugin_templates(self, user, tenant, plugin_data):
        plugin_template = market_api.get_plugin_templates(
            tenant.tenant_id, plugin_data.get('plugin_key'),
            plugin_data.get('version'))
        market_plugin = plugin_template.get('plugin')
        if not market_plugin:
            return True
        rcps = RainbondCenterPlugin.objects.filter(
            plugin_key=market_plugin.get('plugin_key'),
            version=market_plugin.get('major_version'),
            enterprise_id__in=[tenant.enterprise_id, "public"])

        rcp = None
        if rcps:
            # 优先获取企业的插件
            enter_rcp = rcps.filter(enterprise_id=tenant.enterprise_id)
            if enter_rcp:
                rcp = enter_rcp[0]
            else:
                rcp = rcps[0]
        if rcp:
            rcp.share_user = 0
            user_name = market_plugin.get('share_user')
            if user_name:
                try:
                    user = user_repo.get_user_by_username(user_name)
                    rcp.share_user = user.user_id
                except Exception as e:
                    logger.exception(e)

            rcp.plugin_template = market_plugin.get("template").get(
                'template_content')
            rcp.pic = market_plugin.get('logo')
            rcp.desc = market_plugin.get('intro')
            rcp.version = market_plugin.get('major_version')
            rcp.details = market_plugin.get("desc", "")
            rcp.is_complete = True
            rcp.save()
            return True
        else:
            enterprise_id = tenant.enterprise_id
            if common_services.is_public() and user.is_sys_admin:
                enterprise_id = "public"
            rcp = RainbondCenterPlugin(
                plugin_key=market_plugin.get('plugin_key'),
                plugin_name=market_plugin.get('name'),
                version=market_plugin.get('major_version'),
                desc=market_plugin.get('intro'),
                pic=market_plugin.get('logo'),
                build_version=market_plugin.get('build_version'),
                record_id=0,
                category=market_plugin.get('category'),
                scope='goodrain',
                source='market',
                share_user=0,
                share_team='',
                enterprise_id=enterprise_id,
                plugin_template=market_plugin.get("template").get(
                    'template_content'),
                is_complete=True,
                details=market_plugin.get("desc", ""))
            rcp.save()
            return True