Răsfoiți Sursa

调查问卷

15313670163 1 lună în urmă
părinte
comite
f9d6ba8bb6
3 a modificat fișierele cu 175 adăugiri și 1 ștergeri
  1. 131 1
      app/JsonRpc/NewsService.php
  2. 17 0
      app/JsonRpc/NewsServiceInterface.php
  3. 27 0
      app/Model/ArticleSurvey.php

+ 131 - 1
app/JsonRpc/NewsService.php

@@ -9,7 +9,7 @@ use App\Model\WebsiteCategory;
 use Hyperf\DbConnection\Db;
 use Hyperf\DbConnection\Db;
 use Hyperf\RpcServer\Annotation\RpcService;
 use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use App\Tools\Result;
-
+use App\Model\ArticleSurvey;
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
 class NewsService implements NewsServiceInterface
 {
 {
@@ -457,4 +457,134 @@ class NewsService implements NewsServiceInterface
         $result['cat_name'] = $category['name'];
         $result['cat_name'] = $category['name'];
         return Result::success($result);
         return Result::success($result);
     }
     }
+    /**
+     * 前端-获取网站调查问卷
+     * @param array $data
+     * @return array
+     */
+     public function getWebsiteSurvey(array $data): array
+    {
+        if(isset($data['survey_id']) && !empty($data['survey_id'])){
+            $website = Website::where('id',$data['website_id'])->first();
+            if(empty($website)){
+                return Result::error("暂无此网站",0);
+            }
+        }
+        if(isset($data['art_id'])  && !empty($data['art_id'])){
+            $article = Article::where('id',$data['art_id'])->first();
+            if(empty($article)){
+                return Result::error("暂无此文章",0);
+            }
+            // return Result::error($data,0);
+            $where['art_id'] = $data['art_id'];
+            // $query = ArticleSurvey::where('art_id',$data['art_id']); 
+            
+        }else{
+            $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
+            if(empty($survey)){
+                return Result::error("暂无调查问卷",0); 
+            }
+            $where['sur_id'] = $survey['sur_id'];
+            // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
+        }
+        $result['survey'] =  ArticleSurvey::where($where)->where('is_other',0)
+        ->leftJoin('article','article_survey.art_id','article.id')
+        ->select('article_survey.*','article.survey_type')
+        ->get()->all();
+        $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
+        $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
+        if(empty($result)){
+            return Result::error("此文章暂无调查问卷",0); 
+        }
+        return Result::success($result);
+    }
+    /**
+     * 前端-添加网站调查问卷选项
+     * @param array $data
+     * @return array
+     */
+    public function addWebsiteSurveyOption(array $data): array
+    {
+        if(isset($data['website_id']) && !empty($data['website_id'])){
+            $website = Website::where('id',$data['website_id'])->first();
+            if(empty($website)){
+                return Result::error("暂无此网站",0);
+            }
+            if(isset($data['sur_id']) && !empty($data['sur_id'])){
+                $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first(); 
+                if(empty($survey)){
+                    return Result::error("此调查问卷不可添加选项",0); 
+                }
+                if(isset($data['choice_name']) &&!empty($data['choice_name'])){
+                    $choice = [
+                        'art_id'=>$survey['art_id'],
+                        'website_id'=>$data['website_id'],
+                        'survey_name'=>$survey['survey_name'],
+                        'choice_name'=>$data['choice_name'],
+                        'sur_id'=>$survey['sur_id'],                   
+                        'is_other'=>1,
+                        'other_id'=>$survey['id'],
+    
+                    ];
+                    $result = ArticleSurvey::insertGetId($choice);
+                    if(empty($result)){
+                        return Result::error("添加失败",0);
+                    }
+                    return Result::success($result);
+                }
+            }
+            return Result::error("添加失败",0);
+        }
+        return Result::error("添加失败",0);
+        
+    }
+    /**
+     * 前端-调查问卷投票   --------未完待续-------
+     * @param array $data
+     * @return array
+     */
+    public function addWebsiteSurveyVote(array $data): array
+    {
+        if(isset($data['website_id']) &&!empty($data['website_id'])){
+            $website = Website::where('id',$data['website_id'])->first(); 
+            if(empty($website)){
+                return Result::error("暂无此网站",0); 
+            }
+            if(isset($data['sur_id']) && !empty($data['sur_id'])){
+                $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
+                if(empty($survey)){
+                    return Result::error("此调查问卷不存在",0);
+                }
+                // 调查问卷类型   0:单选 1:多选
+                $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
+                // return Result::success($type);
+                if(empty($type) || (!in_array($type, [0, 1], true))){
+                    return Result::error("此调查问卷不可投票",0); 
+                }
+                return Result::success($type[0]);
+                if(isset($data['choice_id']) &&!empty($data['choice_id'])){
+                    if($type[0] == 0){
+                        $choice = ArticleSurvey::where('id',$data['choice_id'])->where('website_id',$data['website_id'])->first();
+                    }else{
+                        $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->get()->all();
+                    }
+                    if(empty($choice)){
+                        return Result::error("请选择已有的选项!",0);
+                    }
+                    return Result::success($choice);
+                    $where = [
+                        'website_id'=>$data['website_id'],
+                        'sur_id'=>$data['sur_id'],
+                        'choice_id'=>$data['choice_id']
+                    ];
+                }
+                return Result::success("111");
+                // if(isset($data['choice_id']) && !empty($data['choice_id'])){
+                //     $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
+                // }
+            }
+            return Result::error("此调查问卷不存在",0);
+        }
+        return Result::success("2222");
+    }
 }
 }

+ 17 - 0
app/JsonRpc/NewsServiceInterface.php

@@ -95,5 +95,22 @@ interface NewsServiceInterface
      * @return array
      * @return array
      */
      */
     public function selectWebsiteArticleInfo(array $data):array;
     public function selectWebsiteArticleInfo(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getWebsiteSurvey(array $data):array;
+
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function addWebsiteSurveyOption(array $data):array;
+
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function addWebsiteSurveyVote(array $data):array;
 
 
 }
 }

+ 27 - 0
app/Model/ArticleSurvey.php

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