Browse Source

修改接口:添加、修改、删除通栏的接口;删除组件的接口;获取所有通栏版式、获取所有组件的接口

15313670163 1 day ago
parent
commit
ce60d27fe6
2 changed files with 206 additions and 50 deletions
  1. 179 50
      app/JsonRpc/PublicRpcService.php
  2. 27 0
      app/Model/SectorComponent.php

+ 179 - 50
app/JsonRpc/PublicRpcService.php

@@ -41,7 +41,7 @@ use App\Model\Article;
 use App\Model\WebsiteCategory;
 use App\Model\AdPlace;
 use App\Model\WebsiteImg;
-
+use App\Model\SectorComponent;
 #[RpcService(name: "PublicRpcService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class PublicRpcService implements PublicRpcServiceInterface
 {
@@ -1172,17 +1172,28 @@ class PublicRpcService implements PublicRpcServiceInterface
         if(empty($sector_place)){
             return Result::error('位置不存在!');
         }
-        $sector = [
-            'template_id' => $data['template_id'],
-            'sector_type' => $data['sector_type'],
-            'sector_name' => $data['sector_name'],
-            'sector_id' => $data['sector_id'],
-            'place_type' => $data['place_type'],
-            'sector_width' => $data['sector_width'],
-            'sector_height' => $data['sector_height'],
-            'component_num' => $data['component_num'],
-            'page_type' => $data['page_type'],
-        ];
+        $sort = json_decode($data['component_code'],true);
+        $kw = 0;
+        $flattened = [];
+        $component = [];
+        $sector_component = [];
+        foreach ($sort as $key => $subArray) {
+            foreach ($subArray as $k => $item) {
+                $component[] = $item;
+                $sector_component[] = [
+                    'sector_id' => $data['sector_id'],
+                    'component_id' => $item,
+                    'sort_id' => $key + 1,
+                    'place_id' => $data['place_type'],
+                ];
+            }
+        }
+        $component_num = count($component);
+        $count = count(array_unique($component));
+        //   return Result::success($sector_component);
+        if(count($sort) != $data['component_num'] || $component_num != $count){
+            return Result::error('组件关联错误!');
+        }
         // 通栏分类:1:资讯类:2:通栏广告类;3:混合类;4:头条类;5:轮播图类;
         $sector_code = [
             'sectorName' => $data['sector_name'],
@@ -1191,12 +1202,30 @@ class PublicRpcService implements PublicRpcServiceInterface
             'sectorPlace' => $data['place_type'],
         ];
         // return Result::success($sector_code);
-         $data['sector_code'] = json_encode($sector_code);
-        $result = Sector::insertGetId($data);
-        if (empty($result)) {
-            return Result::error('添加失败');
+        $data['sector_code'] = json_encode($sector_code);
+        Db::beginTransaction();
+        try {
+            $com_sector = SectorComponent::insert($sector_component);
+            if(empty($com_sector)){
+                Db::rollBack();
+                return Result::error('通栏关联组件失败!');
+            }
+            // $up_component = Component::whereIn('component_type',$sector_component['component_id'])->update(['status' => 2]);
+            // if(empty($up_component)){
+            //     Db::rollBack();
+            //     return Result::error('通栏关联组件失败!');
+            // }
+            $result = Sector::insertGetId($data);
+            if (empty($result)) {
+                Db::rollBack();
+                return Result::error('添加失败');
+            }
+            Db::commit();
+            return Result::success($result);
+        } catch (\Exception $e) {
+            Db::rollBack();
+            return Result::error($e->getMessage());
         }
-        return Result::success($result);
     }
     /**
      * 删除通栏
@@ -1205,15 +1234,27 @@ class PublicRpcService implements PublicRpcServiceInterface
      */
     public function delSector(array $data): array
     {
-        $component_id = Component::where('sector_id', $data['id'])->pluck('id')->toArray();
-        if (!empty($component_id)) {
-            return Result::error('请先删除相关组件!');
+        $sector = Sector::where('id', $data['id'])->first();
+        if (empty($sector)) {
+            return Result::error('通栏不存在!');
         }
-        $result = Sector::where('id', $data['id'])->delete();
-        if ($result == 1) {
-            return Result::success('删除成功');
-        } else {
-            return Result::error('删除失败');
+        Db::beginTransaction();
+        try {
+            $component_id = SectorComponent::where('sector_id', $sector['sector_id'])->delete();
+            if (empty($component_id)) {
+                Db::rollBack();
+                return Result::error('解除相关组件关联关系失败!');
+            }
+            $result = Sector::where('id', $data['id'])->delete();
+            if (empty($result)) {
+                Db::rollBack();
+                return Result::error('通栏删除失败!');
+            }
+            Db::commit();
+            return Result::success('通栏删除成功!');
+        } catch (\Exception $e) {
+            Db::rollBack();
+            return Result::error($e->getMessage());
         }
     }
     /**
@@ -1251,11 +1292,69 @@ class PublicRpcService implements PublicRpcServiceInterface
         if (empty($template)) {
             return Result::error('皮肤不存在!');
         }
-        $result = Sector::where('id', $data['id'])->update($data);
-        if ($result == 1) {
-            return Result::success('修改成功');
-        } else {
-            return Result::error('修改失败');
+        $sort = json_decode($data['component_code'],true);
+        $kw = 0;
+        $flattened = [];
+        $component = [];
+        $sector_component = [];
+        foreach ($sort as $key => $subArray) {
+            foreach ($subArray as $k => $item) {
+                $component[] = $item;
+                $sector_component[] = [
+                    'sector_id' => $data['sector_id'],
+                    'component_id' => $item,
+                    'sort_id' => $key + 1,
+                    'place_id' => $data['place_type'],
+                ];
+            }
+        }
+        // // 批量插入数据到 ComponentSector 表
+        // \App\Model\ComponentSector::insert($sector_component);
+        $component_num = count($component);
+        $count = count(array_unique($component));
+        //   return Result::success($sector_component);
+        if(count($sort) != $data['component_num'] || $component_num != $count){
+            return Result::error('组件关联错误!');
+        }
+        $data['sector_code'] = json_encode($sector_code);
+        Db::beginTransaction();
+        try {
+            $com_sector = SectorComponent::where('sector_id', $data['sector_id'])->pluck('component_id');
+            if(empty($com_sector)){
+                Db::rollBack();
+                return Result::error('通栏解除组件关联失败!');
+            }
+            // $up_component = Component::whereIn('component_type',$com_sector)->update(['status' => 1]);
+            //     if(empty($up_component)){
+            //         Db::rollBack();
+            //         return Result::error('通栏解除组件关联失败!');
+            // }
+            $del_SectorComponent = SectorComponent::where('sector_id', $data['sector_id'])->delete();
+            if(empty($del_SectorComponent)){
+                Db::rollBack();
+                return Result::error('通栏解除组件关联失败!');
+            }
+            $com_sector = SectorComponent::insert($sector_component);
+            if(empty($com_sector)){
+                Db::rollBack();
+                return Result::error('通栏关联组件失败!');
+            }
+            // $up_component = Component::whereIn('component_id',$sector_component['component_type'])
+            // ->update(['status' => 2,]);
+            // if(empty($up_component)){
+            //     Db::rollBack();
+            //     return Result::error('通栏关联组件失败!');
+            // }
+            $result = Sector::where('id', $data['id'])->update($data);
+            if (empty($result)) {
+                Db::rollBack();
+                return Result::error('修改失败');
+            }
+            Db::commit();
+            return Result::success($result);
+        } catch (\Exception $e) {
+            Db::rollBack();
+            return Result::error($e->getMessage());
         }
     }
     public function getComponentList(array $data): array
@@ -1514,15 +1613,34 @@ class PublicRpcService implements PublicRpcServiceInterface
         if(empty($component)){
             return Result::error('组件不存在!');
         }
-        $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
-        if(!empty($ad_img)){
-            $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
+        $component_id = SectorComponent::where('component_id', $component['component_type'])->first();
+        if (!empty($component_id)) {
+            return Result::error('请先解除相关组件关联关系!');
         }
-        $result = Component::where('id',$data['id'])->delete();
-        if(empty($result)){
+        Db::beginTransaction();
+        try{
+            if($component['type_id'] == 11 || $component['type_id'] == 14){
+                $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->first();
+                if(!empty($ad_img)){
+                    $ad_img = WebsiteImg::where('id',$component['ad_imgid'])->delete();
+                    if(empty($ad_img)){
+                        Db::rollBack();
+                        return Result::error('删除广告默认图失败!');
+                    }
+                }
+            }
+            $result = Component::where('id',$data['id'])->delete();
+            if(empty($result)){
+                Db::rollBack();
+                return Result::error('删除失败!');
+            }
+            Db::commit();
+            return Result::success($result);
+        }catch(\Exception $e){
+            Db::rollback();
             return Result::error('删除失败!');
         }
-        return Result::success($result);
+        
     }
     public function updateComponent(array $data): array
     {
@@ -2254,7 +2372,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             if (isset($sector['component_num']) && is_numeric($sector['component_num'])) {
                 $num = intval($sector['component_num']);
                 if ($num > 0 && $num <= 10) {
-// 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
+                    // 使用 range 函数生成一个从 1 到 $num 的连续整数数组,存储到 $sector_arr 中
                     $sector_arr = range(1, $num);
                 } else {
                     $sector_arr = [];
@@ -2281,9 +2399,9 @@ class PublicRpcService implements PublicRpcServiceInterface
     public function getAllSectorPlace(array $data): array
     {
         $where['type_id'] = $data['type_id'];
-        if($data['type_id'] == 1){
-            $where['status'] = 1;
-        }
+        // if($data['type_id'] == 1){
+        //     $where['status'] = 1;
+        // }
         if(isset($data['sector_type']) && !empty($data['sector_type'])){
             $where['sector_type'] = $data['sector_type'];
         }
@@ -2291,8 +2409,7 @@ class PublicRpcService implements PublicRpcServiceInterface
             $where['component_num'] = $data['component_num'];
         }
        $sector_place = SectorPlace::where($where)
-       ->leftJoin('size','sector_place.size_id','=','size.id') 
-       ->select('sector_place.*','size.width','size.height')
+    //    ->select('sector_place.*','size.width','size.height')
        ->get()->all();
        if(empty($sector_place)){
             return Result::error('通栏版式不存在!');
@@ -2331,15 +2448,27 @@ class PublicRpcService implements PublicRpcServiceInterface
     public function getAllComponent(array $data): array
     {
         $where = [];
-        if(isset($data['sector_id']) && !empty($data['sector_id'])){
-            $where['component.sector_id'] = $data['sector_id'];
-        }
-        if(isset($data['sort_id']) && !empty($data['sort_id'])){
-            $where['component.sort_id'] = $data['sort_id'];
+        if((isset($data['sector_id']) && !empty($data['sector_id'])) || (isset($data['sort_id']) && !empty($data['sort_id']))){
+            $sector['sector_component.sector_id'] = $data['sector_id'];
+            $sector['sector_component.sort_id'] = $data['sort_id'];
+            $where = SectorComponent::where($sector)->pluck('component_id')->toArray();
+            $component = Component::whereIn('id',$where)->get()->all();
+        }else{
+            if(isset($data['type_id']) && !empty($data['type_id'])){
+                $where['component.type_id'] = $data['type_id'];
+            }
+            if(isset($data['width']) && !empty($data['width'])){
+                $where['component.component_width'] = $data['width'];
+            }
+            if(isset($data['height']) && !empty($data['height'])){
+                $where['component.component_height'] = $data['height'];
+            }
+            $component = Component::where($where)
+            // ->leftJoin('size','component.size_id','=','size.id')
+            // ->select('component.*','size.width','size.height')
+            ->get()->all();
         }
-        $component = Component::where($where)->leftJoin('size','component.size_id','=','size.id')
-        ->select('component.*','size.width','size.height')
-        ->get()->all();
+        
         if(empty($component)){
             return Result::error('组件不存在!');
         }

+ 27 - 0
app/Model/SectorComponent.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class  SectorComponent extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'sector_component';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}