|
@@ -1001,89 +1001,72 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
* @param array $data
|
|
|
* @return array
|
|
|
*/
|
|
|
- public function upWebsiteCategory(array $data): array
|
|
|
+ public function upWebsiteCategory(array $data): array
|
|
|
{
|
|
|
-
|
|
|
- var_dump("更新数据:", $data);
|
|
|
$pinyin = new Pinyin();
|
|
|
Db::beginTransaction();
|
|
|
try {
|
|
|
- if ($data['category_arr_id']) {
|
|
|
- $oldCategoryList = WebsiteCategory::where(['website_id' => $data['website_id']])->get();
|
|
|
- if ($oldCategoryList) {
|
|
|
- $oldCategoryList = $oldCategoryList->toArray();
|
|
|
- $oldCategoryListIds = [];
|
|
|
- foreach ($oldCategoryList as $val) {
|
|
|
- array_push($oldCategoryListIds, $val['category_id']);
|
|
|
- }
|
|
|
- $newCategoryListIds = [];
|
|
|
- foreach ($data['category_arr_id'] as $v) {
|
|
|
- array_push($newCategoryListIds, end($v));
|
|
|
- }
|
|
|
- $differenceIDS = array_merge(array_diff($oldCategoryListIds, $newCategoryListIds), array_diff($newCategoryListIds, $oldCategoryListIds));
|
|
|
- if ($differenceIDS) {
|
|
|
- foreach ($differenceIDS as $v) {
|
|
|
- WebsiteCategory::where(['website_id' => $data['website_id'], 'category_id' => $v])->delete();
|
|
|
- }
|
|
|
- }
|
|
|
- foreach ($data['category_arr_id'] as $key => $value) {
|
|
|
- $info = Category::where(['id' => end($value)])->first();
|
|
|
- $info = $info->toArray();
|
|
|
- // 假设 $data、$value 和 $info 是已经定义好的变量
|
|
|
- $attributes = [
|
|
|
- 'website_id' => $data['website_id'],
|
|
|
- 'category_id' => end($value),
|
|
|
- ];
|
|
|
- // 先复制一份要更新或创建的数据
|
|
|
- $aLIas_pinyin = $pinyin->permalink($info['name'], '');
|
|
|
- var_dump($aLIas_pinyin, '别名');
|
|
|
- $values = [
|
|
|
- 'website_id' => $data['website_id'],
|
|
|
- 'name' => $info['name'] ?? '',
|
|
|
- 'alias' => $info['name'] ?? '',
|
|
|
- 'aLIas_pinyin' => $aLIas_pinyin ?? '',
|
|
|
- 'sort' => $info['sort'] ?? 0,
|
|
|
- 'pid' => $info['pid'] ?? 0,
|
|
|
- 'pid_arr' => $info['pid_arr'] ?? json_encode([]),
|
|
|
- 'seo_title' => $info['seo_title'] ?? "",
|
|
|
- 'seo_keywords' => $info['seo_keywords'] ?? "",
|
|
|
- 'seo_description' => $info['seo_description'] ?? "",
|
|
|
- 'is_url' => $info['is_url'] ?? 0,
|
|
|
- 'web_url' => $info['web_url'] ?? '',
|
|
|
- 'category_id' => end($value),
|
|
|
- 'category_arr_id' => $value ? json_encode($value) : json_encode([])
|
|
|
- ];
|
|
|
- // 检查数据库中是否已有该记录
|
|
|
- $existingRecord = WebsiteCategory::where($attributes)->first();
|
|
|
- if ($existingRecord && $existingRecord->name) {
|
|
|
- unset($values['name']);
|
|
|
- }
|
|
|
- if ($existingRecord && $existingRecord->sort >= 0) {
|
|
|
- unset($values['sort']);
|
|
|
- }
|
|
|
- if ($existingRecord && $existingRecord->seo_title) {
|
|
|
- unset($values['seo_title']);
|
|
|
- }
|
|
|
- if ($existingRecord && $existingRecord->seo_keywords) {
|
|
|
- unset($values['seo_keywords']);
|
|
|
- }
|
|
|
- if ($existingRecord && $existingRecord->seo_description) {
|
|
|
- unset($values['seo_description']);
|
|
|
- }
|
|
|
- if ($existingRecord && $existingRecord->alias) {
|
|
|
- unset($values['alias']);
|
|
|
- }
|
|
|
- // 执行 updateOrCreate 操作
|
|
|
- var_dump("更新数据:", $values);
|
|
|
- WebsiteCategory::updateOrCreate($attributes, $values);
|
|
|
- }
|
|
|
+ if (!empty($data['category_arr_id'])) {
|
|
|
+ $websiteId = $data['website_id'];
|
|
|
+ // 1. 获取旧的 category_id 列表
|
|
|
+ $oldCategoryListIds = WebsiteCategory::where('website_id', $websiteId)->pluck('category_id')->toArray();
|
|
|
+ // 2. 获取新的 category_id 列表(去重)
|
|
|
+ $newCategoryListIds = [];
|
|
|
+ foreach ($data['category_arr_id'] as $arr) {
|
|
|
+ $lastId = end($arr);
|
|
|
+ $newCategoryListIds[] = $lastId;
|
|
|
+ }
|
|
|
+ $newCategoryListIds = array_unique($newCategoryListIds);
|
|
|
+ // 3. 需要删除的 category_id
|
|
|
+ $toDelete = array_diff($oldCategoryListIds, $newCategoryListIds);
|
|
|
+ if (!empty($toDelete)) {
|
|
|
+ WebsiteCategory::where('website_id', $websiteId)
|
|
|
+ ->whereIn('category_id', $toDelete)
|
|
|
+ ->delete();
|
|
|
+ }
|
|
|
+ // 4. 批量查出所有需要的 category 信息
|
|
|
+ $categories = Category::whereIn('id', $newCategoryListIds)->get()->keyBy('id');
|
|
|
+ // 5. 组装 upsertData,确保 category_id 不重复
|
|
|
+ $upsertData = [];
|
|
|
+ $handledCategoryIds = [];
|
|
|
+ foreach ($data['category_arr_id'] as $arr) {
|
|
|
+ $categoryId = end($arr);
|
|
|
+ if (!isset($categories[$categoryId]) || in_array($categoryId, $handledCategoryIds)) continue;
|
|
|
+ $info = $categories[$categoryId];
|
|
|
+ $aLIas_pinyin = $pinyin->permalink($info->name, '');
|
|
|
+ $upsertData[] = [
|
|
|
+ 'website_id' => $websiteId,
|
|
|
+ 'category_id' => $categoryId,
|
|
|
+ 'name' => $info->name ?? '',
|
|
|
+ 'alias' => $info->name ?? '',
|
|
|
+ 'aLIas_pinyin' => $aLIas_pinyin ?? '',
|
|
|
+ 'sort' => $info->sort ?? 0,
|
|
|
+ 'pid' => $info->pid ?? 0,
|
|
|
+ 'pid_arr' => $info->pid_arr ?? json_encode([]),
|
|
|
+ 'seo_title' => $info->seo_title ?? "",
|
|
|
+ 'seo_keywords' => $info->seo_keywords ?? "",
|
|
|
+ 'seo_description' => $info->seo_description ?? "",
|
|
|
+ 'is_url' => $info->is_url ?? 0,
|
|
|
+ 'web_url' => $info->web_url ?? '',
|
|
|
+ 'category_arr_id' => json_encode($arr)
|
|
|
+ ];
|
|
|
+ $handledCategoryIds[] = $categoryId;
|
|
|
+ }
|
|
|
+ // 6. upsert
|
|
|
+ if (!empty($upsertData)) {
|
|
|
+ WebsiteCategory::upsert(
|
|
|
+ $upsertData,
|
|
|
+ ['website_id', 'category_id'],
|
|
|
+ [
|
|
|
+ 'aLIas_pinyin', 'pid', 'pid_arr', 'is_url', 'web_url', 'category_arr_id'
|
|
|
+ ]
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
Db::commit();
|
|
|
return Result::success();
|
|
|
} catch (\Throwable $ex) {
|
|
|
Db::rollBack();
|
|
|
- // var_dump($ex->getMessage());
|
|
|
return Result::error("修改失败" . $ex->getMessage(), 0);
|
|
|
}
|
|
|
}
|