|
@@ -2858,4 +2858,83 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
$result->website_id = json_decode($result->website_id, true);
|
|
|
return Result::success($result);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 克隆网站
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function cloneWebsite(array $data): array
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ // 获取原始网站信息
|
|
|
+ $originalWebsite = Website::find($data['website_id']);
|
|
|
+ if (!$originalWebsite) {
|
|
|
+ return Result::error("找不到该网站", 0);
|
|
|
+ }
|
|
|
+ // 提取基础名称(去除已有 _copy 或 _copy_xxx)
|
|
|
+ $baseName = preg_replace('/_copy(\d+)?$/', '', $originalWebsite->website_name);
|
|
|
+ // 查询当前系统中已存在的克隆名称数量
|
|
|
+ $count = Website::where('website_name', 'like', $baseName . '_copy%')
|
|
|
+ ->orWhere('website_name', $baseName)
|
|
|
+ ->count();
|
|
|
+ // 构造新名称:第一次是 _copy,之后是 _copy_1, _copy_2 ...
|
|
|
+ $newName = $baseName . ($count === 0 ? '_copy' : '_copy_' . $count);
|
|
|
+ // 创建克隆网站实例
|
|
|
+ $clone_website = $originalWebsite->replicate();
|
|
|
+ $clone_website->website_name = $newName;
|
|
|
+ $clone_website->save();
|
|
|
+ $clone_website->website_url = "[]";
|
|
|
+ $clone_website->save();
|
|
|
+// var_dump("返回值:",$clone_website->id);
|
|
|
+ //克隆网站基础信息 website_template_info website_category
|
|
|
+ $resultWebsiteTemplateInfo = WebsiteTemplateInfo::where([ 'website_id' => $data['website_id']])->first();
|
|
|
+ $clone_website_template_info = $resultWebsiteTemplateInfo->replicate();
|
|
|
+ $clone_website_template_info->website_id = $clone_website->id;
|
|
|
+ $clone_website_template_info->save();
|
|
|
+ //克隆网站导航 获取原始 website_id 对应的所有 WebsiteCategory 记录
|
|
|
+ $originalCategories = WebsiteCategory::where('website_id', $data['website_id'])->get();
|
|
|
+ // 克隆网站后的新 ID(假设 $clone_website->id 已存在)
|
|
|
+ $newWebsiteId = $clone_website->id;
|
|
|
+ // 遍历每条记录并克隆
|
|
|
+ foreach ($originalCategories as $category) {
|
|
|
+ // 使用 replicate() 方法创建模型副本(不包含主键)
|
|
|
+ $clonedCategory = $category->replicate();
|
|
|
+ // 修改外键字段指向新网站 ID
|
|
|
+ $clonedCategory->website_id = $newWebsiteId;
|
|
|
+ // 保存克隆后的记录到数据库
|
|
|
+ $clonedCategory->save();
|
|
|
+ }
|
|
|
+ //克隆友情链接 Link 对应的是多个
|
|
|
+ $originalLinks = Link::where('website_id', $data['website_id'])->get();
|
|
|
+ foreach ($originalLinks as $link) {
|
|
|
+ // 使用 replicate() 方法创建模型副本(不包含主键)
|
|
|
+ $clonedLink = $link->replicate();
|
|
|
+ // 修改外键字段指向新网站 ID
|
|
|
+ $clonedLink->website_id = $newWebsiteId;
|
|
|
+ // 保存克隆后的记录到数据库
|
|
|
+ $clonedLink->save();
|
|
|
+ }
|
|
|
+ //克隆网站底部信息 footer_category 表 和footer_content 表 footer_category.id = footer_content.fcat_id 都是一对多关系
|
|
|
+ $originalFooterCategories = FooterCategory::where('website_id', $data['website_id'])->get();
|
|
|
+ foreach ($originalFooterCategories as $footerCategory) {
|
|
|
+ $clonedFooterCategory = $footerCategory->replicate();
|
|
|
+ $clonedFooterCategory->website_id = $newWebsiteId;
|
|
|
+ $clonedFooterCategory->save();
|
|
|
+ $originalFooterContents = FooterContent::where('fcat_id', $footerCategory->id)->get();
|
|
|
+ foreach ($originalFooterContents as $footerContent){
|
|
|
+ $clonedFooterContent = $footerContent->replicate();
|
|
|
+ $clonedFooterContent->fcat_id = $clonedFooterCategory->id;
|
|
|
+ $clonedFooterContent->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ return Result::success([]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollBack();
|
|
|
+ return Result::error($e->getMessage(), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|