15313670163 4 месяцев назад
Родитель
Сommit
6ddde8103d
3 измененных файлов с 184 добавлено и 41 удалено
  1. 175 41
      app/JsonRpc/PublicRpcService.php
  2. 5 0
      app/Model/Component.php
  3. 4 0
      app/Model/SectorComponent.php

+ 175 - 41
app/JsonRpc/PublicRpcService.php

@@ -2705,7 +2705,7 @@ class PublicRpcService implements PublicRpcServiceInterface
         }
         $rule = TemplateRule::first();
         $index_rule = json_decode($rule['index_rule']);
-        // $class_rule = json_decode($rule['class_rule']);
+        $class_rule = json_decode($rule['class_rule']);
         // return Result::success($index_rule);
         $tempalte['index'] = $this->randomPage($data,1,$index_rule);
         // $tempalte['class'] = $this->randomPage($data,2,$class_rule);
@@ -2722,7 +2722,7 @@ class PublicRpcService implements PublicRpcServiceInterface
         $sectors = Sector::where('sector.template_id',$data['template_id'])
         ->where('sector.page_type','like','%'.$page.'%')
         ->select('sector.sector_name', 'sector.sector_id','sector.pic_height','sector.sector_img',
-        'sector.sector_type','sector.sector_code')  
+        'sector.sector_type','sector.sector_code','sector.id')  
         ->orderBy('id')
         ->get()->all();
         if(empty($sectors)){
@@ -2742,6 +2742,8 @@ class PublicRpcService implements PublicRpcServiceInterface
                 $result[$value] = [];
             }
             $result[$value][$key] = $key;
+            $result[$value] = array_values($result[$value]);
+
             return $result;
         }, []);
         // return $sector_types;
@@ -2753,9 +2755,11 @@ class PublicRpcService implements PublicRpcServiceInterface
         if( $is_must != count($must_type)){
             return '此页面缺少必要通栏!';
         }
-        var_dump("sector_types",$all_sector_types);
-        var_dump("must_type",$must_type);
+        // var_dump("sector_types",$all_sector_types);
+        // var_dump("must_type",$must_type);
         // return $type_num;
+
+
         // 非必须通栏的随机处理
         $not_must_type = array_diff($all_sector_types,$must_type);
         $total = count($not_must_type);
@@ -2773,6 +2777,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             if (!is_array($randomKeys)) {
                 $randomKeys = [$randomKeys];
             }
+
             // 通栏关联性规则暂时无法实现---------------
             // 假设 $oneDimensionalArray 是一维数组,$randomKeys 是随机数组
             // 判断 $oneDimensionalArray 是否为一维数组
@@ -2790,65 +2795,194 @@ class PublicRpcService implements PublicRpcServiceInterface
             // 根据随机键名提取元素(保留原键值关系)
             // $randomKeys = array_intersect($sector_type_keys,$randomKeys);
             var_dump("randomKeys",$randomKeys);
-            $random_sector = array_intersect_key($sectors, array_flip($randomKeys));
-            // $random_sector = array_intersect_key($sectors, array_flip($random_type));
+            $random_sector = array_values(array_intersect_key($sectors, array_flip($randomKeys)));
             $rand_count = count($random_sector);
         }
         // var_dump("randomKeys",$randomKeys);
         // var_dump("random_sector",$random_sector);
-        // var_dump("rand_count",$rand_count);
+        var_dump("rand_count",$rand_count);
         // return $random_type;
 
         // 必需通栏
-        $must_type = array_intersect($sector_type_keys,$must_type);
-        $must_sector = array_intersect_key($sectors, array_flip(array_keys($must_type)));
+        $must_type = array_intersect($all_sector_types,$must_type);
+        // return $must_type;
+        $must_sector = array_values(array_intersect_key($sectors, array_flip(array_keys($must_type))));
         $must_count = count($must_sector);
         // var_dump("must_sector",$must_sector);
-        // var_dump("must_count",$must_count);
+        var_dump("must_count",$must_count);
         // return $must_sector;
 
 
 
 
-        // 计算随机之后减去必需的通栏,查询重复通栏可用数量;
+        // 计算随机之后减去必需的通栏,查询重复通栏可用数量;(目前的这些通栏没有重复的同类别通栏)
         $repeat_num = $sector_num-$rand_count-$must_count;
-        var_dump("rand_count",$rand_count);
-        var_dump("must_sector",$must_count);
-        var_dump("repeat_num",$repeat_num);
-
+        // 组合起来通栏
+        $sector_zuhe1 = array_merge($must_sector,$random_sector);
+        $rule_repeat = (array)$rule->type_max;
+        $sector_zuhe1_type = array_count_values(array_column($sector_zuhe1,'sector_type'));
+        // $sector_arr = array_intersect_key($rule_repeat,$sector_zuhe1_type);
+        foreach($rule_repeat as $key => $value){
+            if(in_array($key,array_keys($sector_zuhe1_type))){
+                $rule_repeat[$key] = $value-1;
+            }
+        }
+        // return $sector_zuhe1;
 
         // 剩余通栏进行重复处理
-        $rule_repeat = $rule->type_max;
+        $rule_repeat = (array)$rule->type_max;
         // 取到可以重复的通栏类别
-        $rule_repaet_type =  array_keys((array)$rule_repeat);
-        $repeat_type = array_intersect($all_sector_types,$rule_repaet_type);
+        $rule_repaet_type =  array_keys($rule_repeat);
+        $repeat_type = array_intersect_key($sector_types, array_flip($rule_repaet_type));
         // 获取可以随机重复的通栏
-        $repeat_sector = array_intersect_key($sectors, array_flip($repeat_type));
+        $repaet_key = 0;
+        $num = 0;
+        $type = [];
+        // return $repeat_type;
+        for($repaet_key; $repaet_key < $repeat_num;){
+            foreach($repeat_type as $key => $value){
+                if($repaet_key < $repeat_num ){
+                    // $max_repaert_num = $rule_repeat[$key];
+                    // if($max_repaert_num < $num ){
+                        // $arr_key = $num+$key;
+                        if (count($value) > 0) {
+                            // 若 可以随机的通栏类型存在多个通栏,随机取其一
+                            $repeat_sector_key[$repaet_key] = $value[array_rand($value)];
+                        } else {
+                            $repeat_sector_key[$repaet_key] = $value[0];
+                        }
+                        $type[$repaet_key] = $repeat_sector_key[$repaet_key];
+                        $repaet_key++;
+                    // }
+                }else{
+                    break;
+                }
+                // var_dump("rule_repeat",$rule_repeat[$key]);
+            }
+            $num++;
+        }
+        
+        // var_dump("num", $num);
+        var_dump("type", $repeat_type);
+       
+        $repeat_sector = [];
+        foreach ($repeat_sector_key as $key => $value) {
+            if (isset($sectors[$key])) {
+                $repeat_sector[$key] = $sectors[$value];
+            }
+        }
+        $repeat_count = count($repeat_sector);
+        var_dump("repeat_count",$repeat_count);
 
 
-        // $change_type_max = array_intersect($random_type,$repaet_type);
-        
-        // var_dump("repeat_type_num",$repeat_type_num);
-        // for($repeat_num;$repeat_num == 0;$repeat_num--){
-            
-        // }
-        // $count = count($filteredTypes);
-        // $all_sector_type = array_merge($random_sector,$must_sector);
-        // $type_maxnum = $rule->type_max;
-        // // 将 $type_maxnum 转换为数组类型
-        // $type_maxnum_array = (array)$type_maxnum;
-        // // return gettype($type_maxnum_array);
-        // $max_type = array_keys($type_maxnum_array);
-        // if(count(array_intersect($max_type,$must_type))>0 || count(array_intersect($max_type,$randomKeys))>0){
-        //     $rand_count = count($random_sector)+count($must_type)-1;
-        // }
-        // foreach($type_maxnum_array as $key => $value){
-        //     if($sector_num-$rand_count > 0 && ){
-        //         $repeat_sector[$key] = rand();
-        //     } 
+        $sector = array_merge($repeat_sector,$sector_zuhe1);
+        // if($page ==1 && array_search(6,array_column($sector_zuhe1,'sector_type'))){
+        //     $sector_arr 
         // }
-        // $max_ad = 0;
-        return $sector_type_keys;
+        // $specal_sector_rule = ;
+        // 处理头条及轮播图在随机通栏中的特殊位置特殊处理
+        $sector_specal_sort = array_column($sector,'sector_type');
+        if(in_array(6,$sector_specal_sort) || in_array(7,$sector_specal_sort)){
+            $toutiao_key = array_search(6,$sector_specal_sort) ?? null;
+            $pic_key = array_search(7,$sector_specal_sort) ?? null;
+            $toutiao = $sector[$toutiao_key];
+            $pic =  $sector[$pic_key];
+            $soty_1 = $sector[0];
+            $sort_2 = $sector[1];
+            if(!empty($toutiao_key)){
+                $sector[0] = $toutiao;
+                $sector[$toutiao_key] = $soty_1;
+                $sector[1] = $pic;
+                $sector[$pic_key] = $sort_2;
+            }else{
+                 $sector[0] = $pic;
+                $sector[$pic_key] = $sort_2;
+            }
+
+        }
+        $sector_id = array_column($sector,'id');
+        $sector_code = array_column($sector,'sector_code');
+        // $sector_code = array_flip($sector_code);
+        foreach ($sector_code as &$item) {
+            $item = json_decode($item, true);
+        }
+        unset($item);
+        $component = $this->randomComponent($sector_id,$data,$sector_code);
+
+        return ['sector_code'=>$sector_code,'sector'=>$sector,'component'=>$component];
+    }
+    public function randomComponent($sector_id,$data,$sector_code){
+        // 获取原始查询结果
+        $rawResults = SectorComponent::whereIn('sector_component.sectorid', $sector_id)
+            ->leftJoin('component','component.component_type','sector_component.component_id')
+            ->leftJoin('component_img','component_img.component_id','component.component_type')
+            ->where('component_img.template_id','=',$data['template_id'])
+            ->select('component.*','component_img.img_name','component_img.img_id','component_img.img_url',
+            'sector_component.sort_id','sector_component.sectorid')
+            ->get();
+        // return $rawResults; 
+        // 按sectorid和sort_id分组数据
+        $groupedResults = [];
+        foreach ($rawResults as $item) {
+            $sectorId = $item->sectorid;
+            $sortId = $item->sort_id;
+            $componentId = $item->component_type;
+            
+            if (!isset($groupedResults[$sectorId])) {
+                $groupedResults[$sectorId] = [];
+            }
+            
+            if (!isset($groupedResults[$sectorId][$sortId])) {
+                $groupedResults[$sectorId][$sortId] = [];
+            }
+            
+            if (!isset($groupedResults[$sectorId][$sortId][$componentId])) {
+                $groupedResults[$sectorId][$sortId][$componentId] = [
+                    'component' => $item->component_type,
+                    'images' => []
+                ];
+            }
+            
+            if (!empty($item->component_img)) {
+                $groupedResults[$sectorId][$sortId][$componentId]['images'][] = [
+                    'img_name' => $item->img_name,
+                    'img_id' => $item->img_id,
+                    'img_url' => $item->img_url,
+                ];
+            }
+        }
+        
+        // 重组数据结构为: sectorid -> sort_id -> {component, img}
+        $result = [];
+        foreach ($sector_code as $key => $value) {
+            // array_push($value['sort_id'],$key);
+            $page['template'][$key] = $value; 
+            $sector_key[$key] = array_keys($value);
+            // foreach ($rawResults as $sortId => $components) {
+            //     // 随机选择一个组件
+            //     $componentIds = array_keys($components);
+            //     $selectedComponentId = $componentIds[array_rand($componentIds)];
+            //     $selectedComponent = $components[$selectedComponentId];
+            //     // 随机选择一个样式
+            //     $images = $selectedComponent['images'];
+            //     $selectedImage = [];
+            //     if (!empty($images)) {
+            //         $selectedImage = $images[array_rand($images)];
+            //     }
+                
+            //     // 构建最终结构
+            //     if (!isset($result[$sectorId])) {
+            //         $result[$sectorId] = [];
+            //     }
+            //     $result[$sectorId][$sortId] = [
+            //         'component' => $selectedComponent['component'],
+            //         'img' => $selectedImage
+            //     ];
+            // }
+            // $page['template'][$key][$sector_key]['componentData'] = $result[$sectorId];
+        }
+        
+        return $sector_key;
     }
     public function getPageCode($sector,$page){
         if(empty($sector)){

+ 5 - 0
app/Model/Component.php

@@ -24,4 +24,9 @@ class  Component extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function componentImgs()
+    {
+        return $this->hasMany(ComponentImg::class, 'component_id', 'component_type');
+    }
 }
+

+ 4 - 0
app/Model/SectorComponent.php

@@ -24,4 +24,8 @@ class  SectorComponent extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function component()
+    {
+        return $this->hasOne(Component::class, 'component_type', 'component_id');
+    }
 }