AI 1 month ago
parent
commit
a820e6d0da
1 changed files with 132 additions and 132 deletions
  1. 132 132
      app/JsonRpc/NewsService.php

+ 132 - 132
app/JsonRpc/NewsService.php

@@ -380,39 +380,41 @@ class NewsService implements NewsServiceInterface
                 var_dump($suveys_array, '---------------------1');
                 var_dump($suvey_array, '---------------------2');
                 $suvey_data = [];
-                foreach ($suveys_array as $key => $value) {
-                    if ($value == '') {
-                        continue;
-                    }
-                    if (is_array($value)) {
-                        $suvey_data[] = [
-                            'sur_id' => $uuid,
-                            'art_id' => $id,
-                            'website_id' => $website_id ?? 2,
-                            'survey_name' => $survey_name,
-                            'choice_name' => $value[1],
-                            'is_other' => 1,
-                            'orther_id' => 0,
-                            'results' => 0,
-
-                        ];
-                    } else {
-                        $suvey_data[] = [
-                            'sur_id' => $uuid,
-                            'art_id' => $id,
-                            'website_id' => $website_id ?? 2,
-                            'survey_name' => $survey_name,
-                            'choice_name' => $value,
-                            'is_other' => 0,
-                            'orther_id' => 0,
-                            'results' => 0,
+                if (is_array($suveys_array)) {
+                    foreach ($suveys_array as $key => $value) {
+                        if ($value == '') {
+                            continue;
+                        }
+                        if (is_array($value)) {
+                            $suvey_data[] = [
+                                'sur_id' => $uuid,
+                                'art_id' => $id,
+                                'website_id' => $website_id ?? 2,
+                                'survey_name' => $survey_name,
+                                'choice_name' => $value[1],
+                                'is_other' => 1,
+                                'orther_id' => 0,
+                                'results' => 0,
+
+                            ];
+                        } else {
+                            $suvey_data[] = [
+                                'sur_id' => $uuid,
+                                'art_id' => $id,
+                                'website_id' => $website_id ?? 2,
+                                'survey_name' => $survey_name,
+                                'choice_name' => $value,
+                                'is_other' => 0,
+                                'orther_id' => 0,
+                                'results' => 0,
+
+                            ];
+                        }
+                        if (empty($suvey_data)) {
+                            throw new \Exception("投票数据为空");
+                        }
 
-                        ];
-                    }
-                    if (empty($suvey_data)) {
-                        throw new \Exception("投票数据为空");
                     }
-
                 }
                 $result = ArticleSurvey::insert($suvey_data);
                 if (!$result) {
@@ -453,8 +455,8 @@ class NewsService implements NewsServiceInterface
     public function getArticleInfo(array $data): array
     {
         $where = [
-            'article.id'=>$data['id'],
-            'article.status'=>1
+            'article.id' => $data['id'],
+            'article.status' => 1,
         ];
         $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
         $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
@@ -556,26 +558,26 @@ class NewsService implements NewsServiceInterface
 
     }
 /**
-     *获取新闻列表 
-     * @param array $data
-     * @return array
-     */
+ *获取新闻列表
+ * @param array $data
+ * @return array
+ */
     public function getWebsiteArticleList(array $data): array
     {
         $where[] = ['status', '=', 1];
-       if(isset($data['keyword'])  && !empty($data['keyword'])){
-            array_push($where,['article.title','like','%'.$data['keyword'].'%']);
-        }
-        if(isset($data['catid'])  && !empty($data['catid'])){
-            if(is_array($data['catid'])){
-                $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
-                array_push($where,['catid', 'in', $data['catid']]);
-            }else{
-                $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
-                array_push($where,['catid', '=', $data['catid']]);
+        if (isset($data['keyword']) && !empty($data['keyword'])) {
+            array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
+        }
+        if (isset($data['catid']) && !empty($data['catid'])) {
+            if (is_array($data['catid'])) {
+                $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
+                array_push($where, ['catid', 'in', $data['catid']]);
+            } else {
+                $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
+                array_push($where, ['catid', '=', $data['catid']]);
             }
-            if(empty($category)){
-               return Result::error("此网站暂无此栏目",0); 
+            if (empty($category)) {
+                return Result::error("此网站暂无此栏目", 0);
             }
         }
         // return Result::success($where);
@@ -589,12 +591,12 @@ class NewsService implements NewsServiceInterface
                 }
             }
         })
-        ->orderBy("created_at", "desc")
-        ->limit($data['pageSize'])
-        ->offset(($data['page'] - 1) * $data['pageSize'])
-        ->get();
+            ->orderBy("created_at", "desc")
+            ->limit($data['pageSize'])
+            ->offset(($data['page'] - 1) * $data['pageSize'])
+            ->get();
 
-        $count =  Article::where(function ($query) use ($where) {
+        $count = Article::where(function ($query) use ($where) {
             foreach ($where as $condition) {
                 if ($condition[1] === 'in') {
                     $query->whereIn($condition[0], $condition[2]);
@@ -605,15 +607,15 @@ class NewsService implements NewsServiceInterface
         })->count();
 
         $data = [
-            'rows'=>$rep->toArray(),
-            'count'=>$count
+            'rows' => $rep->toArray(),
+            'count' => $count,
         ];
-        
-        if(empty($rep)){
+
+        if (empty($rep)) {
             return Result::error("没有信息数据");
         }
         return Result::success($data);
-        
+
     }
     /**
      * 前端-获取新闻详情
@@ -623,23 +625,22 @@ class NewsService implements NewsServiceInterface
     public function selectWebsiteArticleInfo(array $data): array
     {
         $where = [
-            'article.id'=>$data['id'],
-            'article.status'=>1
+            'article.id' => $data['id'],
+            'article.status' => 1,
         ];
-        $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
-        if(empty($result)){
-            return Result::error("查询失败",0);
+        $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
+        if (empty($result)) {
+            return Result::error("查询失败", 0);
         }
-        $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
-        if(empty($category)){
-            return Result::error("查询失败",0);
+        $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
+        if (empty($category)) {
+            return Result::error("查询失败", 0);
         }
         $result['category_id'] = $category['category_id'];
         $result['cat_name'] = $category['name'];
         return Result::success($result);
     }
 
-
     /**
      * 前端-获取网站调查问卷
      * @param array $data
@@ -647,16 +648,16 @@ class NewsService implements NewsServiceInterface
      */
     public function getWebsiteSurvey(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['website_id']) && !empty($data['website_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'])->where('status',1)->first();
-            if(empty($article)){
-                return Result::error("暂无此文章",0);
+        if (isset($data['art_id']) && !empty($data['art_id'])) {
+            $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
+            if (empty($article)) {
+                return Result::error("暂无此文章", 0);
             }
             // return Result::error($data,0);
             $where['art_id'] = $data['art_id'];
@@ -670,14 +671,14 @@ class NewsService implements NewsServiceInterface
             $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['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
-        $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
-        if(empty($result)){
-            return Result::error("此文章暂无调查问卷",0); 
+        $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['other'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
+        $result['others'] = 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);
     }
@@ -728,10 +729,10 @@ class NewsService implements NewsServiceInterface
      */
     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['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');
@@ -758,13 +759,13 @@ class NewsService implements NewsServiceInterface
                         }
                     }
                     // return Result::success($data['choice_id']);
-                    $other = ArticleSurvey::whereIn('id',$data['choice_id'])
-                    ->where('website_id',$data['website_id'])
-                    ->where('is_other',1)
-                    ->where('other_id',0)
-                    ->first();
-                    if(!empty($other)){
-                        return Result::error("请选择已有的选项!",0);
+                    $other = ArticleSurvey::whereIn('id', $data['choice_id'])
+                        ->where('website_id', $data['website_id'])
+                        ->where('is_other', 1)
+                        ->where('other_id', 0)
+                        ->first();
+                    if (!empty($other)) {
+                        return Result::error("请选择已有的选项!", 0);
                     }
                     $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
                         ->where('website_id', $data['website_id'])
@@ -804,80 +805,79 @@ class NewsService implements NewsServiceInterface
     public function getSurveyList(array $data): array
     {
         $where = [];
-        if(isset($data['survey_name']) &&!empty($data['survey_name'])){
-            array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
+        if (isset($data['survey_name']) && !empty($data['survey_name'])) {
+            array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
         }
-        if(isset($data['survey_type']) && $data['survey_type'] != null){
-            array_push($where,['survey_type','=',$data['survey_type']]);
+        if (isset($data['survey_type']) && $data['survey_type'] != null) {
+            array_push($where, ['survey_type', '=', $data['survey_type']]);
         }
-        if(isset($data['is_survey']) && $data['is_survey'] != null){
-            array_push($where,['is_survey','=',$data['is_survey']]);
+        if (isset($data['is_survey']) && $data['is_survey'] != null) {
+            array_push($where, ['is_survey', '=', $data['is_survey']]);
         }
         // return Result::success($where);
-        if(!empty($where)){
+        if (!empty($where)) {
             $query = Article::where($where)->whereNotNull('survey_name');
-        }else{
+        } else {
             $query = Article::whereNotNull('survey_name');
         }
         $count = $query->count();
         $survey = $query->orderByDesc('id')
-        ->limit($data['pageSize'])
-        ->offset(($data['page']-1)*$data['pageSize'])
-        ->get();
-        if(empty($survey->toArray())){
-            return Result::error("暂无调查问卷!",0);
+            ->limit($data['pageSize'])
+            ->offset(($data['page'] - 1) * $data['pageSize'])
+            ->get();
+        if (empty($survey->toArray())) {
+            return Result::error("暂无调查问卷!", 0);
         }
         $result = [
-            'rows'=>$survey,
-            'count'=>$count
+            'rows' => $survey,
+            'count' => $count,
         ];
         return Result::success($result);
     }
     /**
      * 后端-获取网站调查问卷详情
      * @param array $data
-     * @return array 
+     * @return array
      */
     public function getSurveyInfo(array $data): array
     {
-        if(isset($data['sur_id']) &&!empty($data['sur_id'])){
-            $where = [ 'sur_id'=>$data['sur_id']];
-            $choose =  ArticleSurvey::where($where)->where('is_other',0)
-            ->leftJoin('article','article_survey.art_id','article.id')
-            ->select('article_survey.*','article.survey_type')
-            ->get()->all();
-            if(empty($choose)){
-                return Result::error("此调查问卷不存在",0);
+        if (isset($data['sur_id']) && !empty($data['sur_id'])) {
+            $where = ['sur_id' => $data['sur_id']];
+            $choose = ArticleSurvey::where($where)->where('is_other', 0)
+                ->leftJoin('article', 'article_survey.art_id', 'article.id')
+                ->select('article_survey.*', 'article.survey_type')
+                ->get()->all();
+            if (empty($choose)) {
+                return Result::error("此调查问卷不存在", 0);
             }
-            $resultsArray  = array_column($choose, 'results');
+            $resultsArray = array_column($choose, 'results');
             $total = array_sum($resultsArray);
-            $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
-            $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
+            $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
+            $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
             // $total = 0;
-            if(!empty($other)){
+            if (!empty($other)) {
                 $total = $total + $other['results'];
-                $other['choice_name'] = $other['choice_name'].'(其他)';
-                if(!empty($others)){
+                $other['choice_name'] = $other['choice_name'] . '(其他)';
+                if (!empty($others)) {
                     $other['hasChildren'] = true;
                     // array_push($other,['hasChildren','=',true]);
                     $other['children'] = $others;
                     $other_choices = [$other->toArray()];
-                    $mer_choice = array_merge($choose,$other_choices);
+                    $mer_choice = array_merge($choose, $other_choices);
                     $value_choice = array_values($mer_choice);
-                }
-                else{
+                } else {
                     // return Result::error('1111');
                     $other_choices = [$other->toArray()];
-                    $other_choices = array_merge($choose,$other_choices);
+                    $other_choices = array_merge($choose, $other_choices);
                     $value_choice = array_values($other_choices);
                     // return Result::success($result);
                 }
-            }else{
+            } else {
                 $value_choice = $choose;
             }
             $result = [
-                'choose'=>$value_choice,
-                'total'=>$total 
+                'choose' => $value_choice,
+                'total' => $total,
             ];
         }
         return Result::success($result);