Ver Fonte

Merge branch '20250210_lj_zhanqun' into dev

AI há 2 meses atrás
pai
commit
0765b08818
2 ficheiros alterados com 213 adições e 51 exclusões
  1. 186 51
      app/JsonRpc/NewsService.php
  2. 27 0
      app/Model/ArticleSurvey.php

+ 186 - 51
app/JsonRpc/NewsService.php

@@ -6,9 +6,12 @@ use App\Model\ArticleData;
 use App\Model\Category;
 use App\Model\Website;
 use App\Model\WebsiteCategory;
+use App\Model\ArticleSurvey;
 use Hyperf\DbConnection\Db;
 use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
+use Ramsey\Uuid\Uuid;
+use Hyperf\Utils\Random;
 
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
@@ -228,6 +231,15 @@ class NewsService implements NewsServiceInterface
     {
         Db::beginTransaction();
         try {
+            //处理投票
+            $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
+            $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
+            $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
+            $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
+            unset($data['is_survey']);
+            unset($data['survey_name']);
+            unset($data['suvey_array']);
+            unset($data['website_id']);
 
             $articleData = $data;
             unset($articleData['content']);
@@ -237,14 +249,66 @@ class NewsService implements NewsServiceInterface
                 'content' => $data['content'],
             ];
             ArticleData::insertGetId($articleDataContent);
+
+            //处理投票
+            if ($is_survey == 1) {
+                //生成年月日时分秒+8位随机数
+                $uuid = date('YmdHis') . rand(10000000, 99999999);
+                $suveys_array = json_decode($suvey_array);
+                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 (empty($suvey_data)) {
+                        throw new \Exception("投票数据为空");
+                    }
+
+                }
+                $result = ArticleSurvey::insert($suvey_data);
+                if (!$result) {
+                    throw new \Exception("投票失败,ArticleSurvey插入失败");
+                }
+                $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
+                if (!$result) {
+                    throw new \Exception("投票失败,更新主表失败");
+                }
+            }
+
             Db::commit();
+            return Result::success(['id' => $id]);
         } catch (\Throwable $ex) {
             Db::rollBack();
             var_dump($ex->getMessage());
             return Result::error("创建失败", 0);
         }
-
-        return Result::success(['id' => $id]);
     }
 
     /**
@@ -254,6 +318,8 @@ class NewsService implements NewsServiceInterface
     public function delArticle(array $data): array
     {
         $result = Article::where($data)->delete();
+        //survey投票删除
+        articleSurvey::where(['art_id' => $data['id']])->delete();
         if (!$result) {
             return Result::error("删除失败");
         }
@@ -268,6 +334,16 @@ class NewsService implements NewsServiceInterface
     {
         Db::beginTransaction();
         try {
+            //处理投票
+            $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
+            $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
+            $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
+            $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
+            unset($data['is_survey']);
+            unset($data['survey_name']);
+            unset($data['suvey_array']);
+            unset($data['website_id']);
+
             $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
             $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
             $articleData = $data;
@@ -282,13 +358,69 @@ class NewsService implements NewsServiceInterface
                 'content' => $data['content'],
             ];
             ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
+            //处理投票
+            $id = $data['id'];
+            $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
+            var_dump($surveydata, 'suvey_array________delete');
+
+            //处理投票
+            if ($is_survey == 1) {
+                //生成年月日时分秒+8位随机数
+                $uuid = date('YmdHis') . rand(10000000, 99999999);
+                $suveys_array = json_decode($suvey_array);
+                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 (empty($suvey_data)) {
+                        throw new \Exception("投票数据为空");
+                    }
+
+                }
+                $result = ArticleSurvey::insert($suvey_data);
+                if (!$result) {
+                    throw new \Exception("投票失败");
+                }
+                $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
+                if (!$result) {
+                    throw new \Exception("投票失败");
+                }
+            }
+            Db::commit();
+            return Result::success([]);
         } catch (\Throwable $ex) {
             Db::rollBack();
             var_dump($ex->getMessage());
             return Result::error("更新失败", 0);
         }
-        return Result::success([]);
-
     }
 
     /**
@@ -313,12 +445,20 @@ 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();
-        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);
+        }
+        //添加投票
+        $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
+        $result['survey_array'] = $articleSurvey->toArray();
+        if ($result) {
+            return Result::success($result->toArray());
+        } else {
+            return Result::error("查询失败", 0);
         }
         return Result::success($result);
     }
@@ -375,11 +515,11 @@ class NewsService implements NewsServiceInterface
                     2 => '5',
                     3 => '0',
                 ];
-                
-                $result = Article::where($where)->whereIn('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
-            }elseif($data['level']==2){
-                $level='2';
-                $result = Article::where($where)->where('level',$level)->orderBy("created_at","desc")->limit($data['pagesize'])->get();
+
+                $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
+            } elseif ($data['level'] == 2) {
+                $level = '2';
+                $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
 
             } else {
                 $level = '3';
@@ -415,32 +555,29 @@ class NewsService implements NewsServiceInterface
 
     }
 
-
-
-
     /**
-     *获取新闻列表 
+     *获取新闻列表
      * @param array $data
      * @return array
      */
     public function getWebsiteArticleList(array $data): array
     {
 
-       $where[] = ['status', '=', 1];
+        $where[] = ['status', '=', 1];
 
-       if(isset($data['keyword'])  && !empty($data['keyword'])){
-            array_push($where,['article.title','like','%'.$data['keyword'].'%']);
+        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');
+        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');
                 $where[] = ['catid', 'in', $data['catid']];
-            }else{
-                $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
+            } else {
+                $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
                 $where[] = ['catid', '=', $data['catid']];
             }
-            if(empty($category)){
-               return Result::error("此网站暂无此栏目",0); 
+            if (empty($category)) {
+                return Result::error("此网站暂无此栏目", 0);
             }
         }
         // return Result::success($category);
@@ -453,11 +590,11 @@ class NewsService implements NewsServiceInterface
                 }
             }
         })
-        ->orderBy("created_at", "desc")
-        ->limit($data['pageSize'])
-        ->offset(($data['page'] - 1) * $data['pageSize'])
-        ->get();
-        $count =  Article::where(function ($query) use ($where) {
+            ->orderBy("created_at", "desc")
+            ->limit($data['pageSize'])
+            ->offset(($data['page'] - 1) * $data['pageSize'])
+            ->get();
+        $count = Article::where(function ($query) use ($where) {
             foreach ($where as $condition) {
                 if ($condition[1] === 'in') {
                     $query->whereIn($condition[0], $condition[2]);
@@ -467,10 +604,10 @@ 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);
@@ -483,23 +620,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);
     }
 
-
     /**
      * 验证导航名称是否重复
      * @return void
@@ -507,18 +643,17 @@ class NewsService implements NewsServiceInterface
     public function checkCategoryName(array $data): array
     {
         $result = Category::when($data, function ($query) use ($data) {
-            if(isset($data['name']) && $data['name']) {
+            if (isset($data['name']) && $data['name']) {
                 $query->where("name", $data['name']);
             }
-            if(isset($data['id']) && $data['id']) {
-                $query->where("id","!=" ,$data['id']);
+            if (isset($data['id']) && $data['id']) {
+                $query->where("id", "!=", $data['id']);
             }
         })->first();
-        if($result){
+        if ($result) {
             return Result::error("已存在");
-        }else{
+        } else {
             return Result::success();
         }
     }
 }
-

+ 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 = [];
+}