LiuJ 1 day ago
parent
commit
72c1f34b1b
1 changed files with 204 additions and 40 deletions
  1. 204 40
      app/JsonRpc/WebsiteService.php

+ 204 - 40
app/JsonRpc/WebsiteService.php

@@ -559,46 +559,47 @@ class WebsiteService implements WebsiteServiceInterface
             $where[] = ['ad_place.ad_tag', 'like', '%' . $data['ad_tag'] . '%'];
             // return Result::success($where);
             $result =  AdPlace::where($where)
-            ->leftJoin("ad", function ($join) use ($now) {
-                $join->on("ad.pid", "=", "ad_place.id")
-                     ->where('ad.status', 1)
-                     ->where('ad.fromtime', '<=', $now)
-                     ->where('ad.totime', '>=', $now);
-            })
-            ->select(
-                'ad_place.name as place_name',
-                'ad_place.thumb',
-                'ad_place.ad_tag',
-                'ad_place.introduce',
-                'ad.name as ad_name',
-                'ad.image_src',
-                'ad.image_url',
-                'ad.image_alt',
-                'ad_place.ad_url')
-            ->get()->all();
-            
-        }else{
+                ->leftJoin("ad", function ($join) use ($now) {
+                    $join->on("ad.pid", "=", "ad_place.id")
+                        ->where('ad.status', 1)
+                        ->where('ad.fromtime', '<=', $now)
+                        ->where('ad.totime', '>=', $now);
+                })
+                ->select(
+                    'ad_place.name as place_name',
+                    'ad_place.thumb',
+                    'ad_place.ad_tag',
+                    'ad_place.introduce',
+                    'ad.name as ad_name',
+                    'ad.image_src',
+                    'ad.image_url',
+                    'ad.image_alt',
+                    'ad_place.ad_url'
+                )
+                ->get()->all();
+        } else {
             $now = Carbon::now()->format('Y-m-d H:i:s'); // 获取当前时间
             // return Result::success($where);
             $result =  AdPlace::where($where)
-            ->where('ad_place.website_id',$data['website_id'])
-            ->leftJoin("ad", function ($join) use ($now) {
-                $join->on("ad.pid", "=", "ad_place.id")
-                     ->where('ad.status', 1)
-                     ->where('ad.fromtime', '<=', $now)
-                     ->where('ad.totime', '>=', $now);
-            })
-            ->select(
-                'ad_place.name as place_name',
-                'ad_place.thumb',
-                'ad_place.ad_tag',
-                'ad_place.introduce',
-                'ad.name as ad_name',
-                'ad.image_src',
-                'ad.image_url',
-                'ad.image_alt',
-                'ad_place.ad_url',)
-            ->get()->all();
+                ->where('ad_place.website_id', $data['website_id'])
+                ->leftJoin("ad", function ($join) use ($now) {
+                    $join->on("ad.pid", "=", "ad_place.id")
+                        ->where('ad.status', 1)
+                        ->where('ad.fromtime', '<=', $now)
+                        ->where('ad.totime', '>=', $now);
+                })
+                ->select(
+                    'ad_place.name as place_name',
+                    'ad_place.thumb',
+                    'ad_place.ad_tag',
+                    'ad_place.introduce',
+                    'ad.name as ad_name',
+                    'ad.image_src',
+                    'ad.image_url',
+                    'ad.image_alt',
+                    'ad_place.ad_url',
+                )
+                ->get()->all();
             // return Result::error("请选择广告位!",0);
         }
         if (empty($result)) {
@@ -1421,6 +1422,43 @@ class WebsiteService implements WebsiteServiceInterface
                     );
                 }
             }
+            //处理所有path
+            var_dump($data, '--------111----');
+            $website_id = $data['website_id'];
+            //获取所有数据
+            $result = WebsiteCategory::where(['website_id' => $website_id, 'path' => NULL])->get()->toArray();
+            if (empty($result)) {
+                return Result::error("没有数据", 0);
+            }
+            //处理path
+            foreach ($result as $key => $value) {
+                $array_category_arr_id = json_decode($value['category_arr_id'], true);
+                $result[$key]['path'] = WebsiteCategory::where('website_id', $website_id)
+                    ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
+                    ->whereIn('category_id', $array_category_arr_id)
+                    ->pluck('aLIas_pinyin')
+                    ->implode('/');
+            }
+            //批量更新数据path
+            $updateData = [];
+            foreach ($result as $item) {
+                $updateData[] = [
+                    'id' => $item['id'],
+                    'path' => $item['path']
+                ];
+            }
+            //批量更新
+            DB::beginTransaction();
+            try {
+                foreach ($result as $item) {
+                    WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
+                }
+                DB::commit();
+            } catch (\Exception $e) {
+                DB::rollBack();
+                throw $e; // 或者返回错误信息
+            }
+
             Db::commit();
             return Result::success();
         } catch (\Throwable $ex) {
@@ -1527,7 +1565,91 @@ class WebsiteService implements WebsiteServiceInterface
                 return Result::error("更新失败", 0);
             }
         }
-        //处理所有数据
+        //处理所有path
+        if (isset($data['pathone']) && $data['pathone'] == 'pathone') {
+            var_dump($data, '--------111----');
+            $website_id = $data['website_id'];
+            //获取所有数据
+            $result = WebsiteCategory::where(['website_id' => $website_id, 'path' => NULL])->get()->toArray();
+            if (empty($result)) {
+                return Result::error("没有数据", 0);
+            }
+            //处理path
+            foreach ($result as $key => $value) {
+                $array_category_arr_id = json_decode($value['category_arr_id'], true);
+                $result[$key]['path'] = WebsiteCategory::where('website_id', $website_id)
+                    ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
+                    ->whereIn('category_id', $array_category_arr_id)
+                    ->pluck('aLIas_pinyin')
+                    ->implode('/');
+            }
+            //批量更新数据path
+            $updateData = [];
+            foreach ($result as $item) {
+                $updateData[] = [
+                    'id' => $item['id'],
+                    'path' => $item['path']
+                ];
+            }
+            //批量更新
+            DB::beginTransaction();
+            try {
+                foreach ($result as $item) {
+                    WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
+                }
+                DB::commit();
+            } catch (\Exception $e) {
+                DB::rollBack();
+                throw $e; // 或者返回错误信息
+            }
+            return Result::success($result);
+        }
+
+        if (isset($data['pathall']) && $data['pathall'] == 'pathall') {
+            //获取所有数据
+            $result = WebsiteCategory::where(['path' => NULL])->get()->toArray();
+            if (empty($result)) {
+                return Result::error("没有数据", 0);
+            }
+            //处理path
+            foreach ($result as $key => $value) {
+                if (empty($value['category_arr_id'])) continue;
+                if ($key >= 4) {  // 改为 >= 4 如果你想处理最多4个元素
+                    // break;
+                }
+                $array_category_arr_id = json_decode($value['category_arr_id'], true);
+                $result[$key]['path'] = WebsiteCategory::where('website_id', $value['website_id'])
+                    ->orderByRaw('FIELD(category_id, ' . implode(',', $array_category_arr_id) . ')')
+                    ->whereIn('category_id', $array_category_arr_id)
+                    ->pluck('aLIas_pinyin')
+                    ->implode('/');
+            }
+            //批量更新数据path
+            $updateData = [];
+            foreach ($result as $item) {
+                $updateData[] = [
+                    'id' => $item['id'],
+                    'category_arr_id' => $item['category_arr_id'],
+                    'path' => $item['path']
+                ];
+            }
+            // var_dump($updateData);
+            // return  Result::success($updateData);
+            //批量更新
+            DB::beginTransaction();
+            try {
+                foreach ($result as $item) {
+                    WebsiteCategory::where('id', $item['id'])->update(['path' => $item['path']]);
+                }
+                DB::commit();
+            } catch (\Exception $e) {
+                DB::rollBack();
+                throw $e; // 或者返回错误信息
+            }
+            return Result::success($result);
+        }
+
+
         $where = [
             'website_id' => $data['website_id'],
             'category_id' => $data['category_id'],
@@ -1537,14 +1659,56 @@ class WebsiteService implements WebsiteServiceInterface
         $aLIas_pinyin = $pinyin->permalink($alias, '');
         //从WebsiteCategory取出alis ,并转成拼音存放到aLIas_pinyin
         $result = WebsiteCategory::where($where)->first();
+        if (empty($result)) {
+            return Result::error("没有数据", 0);
+        }
         $result = $result->toArray();
         //+四位随机字母
         $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         // 打乱字符串顺序并截取前四位
         // $randss = substr(str_shuffle($letters), 0, 4);
-        if ($data['alias_pinyin']  == '') {
-        }
+
         $data['aLIas_pinyin'] = $aLIas_pinyin;
+        //处理path
+        $category_arr_id = json_decode($result['category_arr_id'], true);
+        //取出来设置path
+        $path = WebsiteCategory::where('website_id', $data['website_id'])
+            ->orderByRaw('FIELD(category_id, ' . implode(',', $category_arr_id) . ')')
+            ->whereIn('category_id', $category_arr_id)
+            ->pluck('aLIas_pinyin')
+            ->implode('/');
+        $data['path'] = $path;
+        //处理包含此id的所有path
+        // 取出category_arr_id包含此id的所有数据
+        $all_other = WebsiteCategory::where('website_id', $data['website_id'])
+            ->whereJsonContains('category_arr_id', $data['category_id'])
+            ->get();
+        var_dump($all_other, '------------');
+
+        // 遍历所有数据,更新path
+        $newAliasPinyin = $aLIas_pinyin ?? ''; // 添加空值判断
+        $oldAliasPinyin = $result['alias_pinyin'] ?? ''; // 修正键名大小写
+
+        // 收集需要更新的ID和路径
+        $updateData = [];
+        foreach ($all_other as $item) {
+            $newPath = str_replace($oldAliasPinyin, $newAliasPinyin, $item->path);
+            if ($newPath !== $item->path) { // 仅在路径变化时更新
+                $updateData[] = [
+                    'id' => $item->id,
+                    'path' => $newPath
+                ];
+            }
+        }
+
+        // 批量更新提高性能
+        if (!empty($updateData)) {
+            // 假设模型对应的数据表为 'table_name',请替换为实际表名
+            Db::table('website_category')->updateOrInsert(
+                ['id' => DB::raw('VALUES(id)')],
+                $updateData
+            );
+        }
 
         $result = WebsiteCategory::where($where)->update($data);
         if ($result) {