AI 1 mese fa
parent
commit
ae738c9f86
2 ha cambiato i file con 165 aggiunte e 4 eliminazioni
  1. 138 4
      app/JsonRpc/NewsService.php
  2. 27 0
      app/Model/ArticleSurvey.php

+ 138 - 4
app/JsonRpc/NewsService.php

@@ -5,9 +5,12 @@ use App\Model\Article;
 use App\Model\ArticleData;
 use App\Model\Category;
 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
@@ -227,6 +230,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']);
@@ -236,14 +248,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]);
     }
 
     /**
@@ -253,6 +317,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("删除失败");
         }
@@ -267,6 +333,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;
@@ -281,13 +357,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([]);
-
     }
 
     /**
@@ -314,6 +446,8 @@ class NewsService implements NewsServiceInterface
             'article.id' => $data['id'],
         ];
         $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
+        $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
+        $result['survey_array'] = $articleSurvey->toArray();
         if ($result) {
             return Result::success($result->toArray());
         } else {

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