فهرست منبع

修改获取投票列表、添加mq涉及撤回资讯;添加调研选题、删除调研选题、获取调研选题列表、修改调研选题状态、获取调研选题详情

FengR 2 ساعت پیش
والد
کامیت
3415bf64d4

+ 20 - 0
app/Amqp/Producer/MqProducer.php

@@ -0,0 +1,20 @@
+<?php
+
+declare (strict_types = 1);
+
+namespace App\Amqp\Producer;
+
+use Hyperf\Amqp\Annotation\Producer;
+use Hyperf\Amqp\Message\ProducerMessage;
+
+#[Producer(exchange: 'chatprod', routingKey: 'chatprod')]
+class MqProducer extends ProducerMessage
+{
+    public function __construct($data)
+    {
+//        $this->poolName = 'default';
+        var_dump($data, '生产者数据');
+
+        $this->payload = $data;
+    }
+}

+ 329 - 21
app/JsonRpc/NewsService.php

@@ -1959,9 +1959,16 @@ class NewsService implements NewsServiceInterface
       return Result::error("暂无调查问卷!", 0);
     }
     $survey = array_map(function ($item) {
-      $item['survey_type'] = $item['article']['survey_type'];
-      $item['created_at'] = $item['article']['created_at'];
-      $item['updated_at'] = $item['article']['updated_at'];
+      if (isset($item['article'])) {
+        $item['survey_type'] = $item['article']['survey_type'];
+        $item['created_at'] = $item['article']['created_at'];
+        $item['updated_at'] = $item['article']['updated_at'];
+      } else {
+        // 处理article为null的情况,设置默认值
+        $item['survey_type'] = null;
+        $item['created_at'] = null;
+        $item['updated_at'] = null;
+      }
       unset($item['article']);
       return $item;
     }, $survey->toArray());
@@ -9156,16 +9163,19 @@ class NewsService implements NewsServiceInterface
    */
   public function getAllResearcher(array $data): array
   {
-    $users = User::where('type_id', 4)
-    ->leftJoin('user_info', 'user.id', '=', 'user_info.user_id')
-    ->select('user.id', 'user_info.real_name','user_info.number')
+    $where['user.type_id'] = 4;
+    if(!empty($data['id'])){
+      $where['user.id'] = $data['id'];
+    }
+    if(!empty($data['real_name'])){
+      array_push($where, ['user_info.real_name', 'like', '%'.$data['real_name'].'%']);
+    }
+    $users = User::leftJoin('user_info', 'user.id', '=', 'user_info.user_id')
+    ->where($where)
+    ->select('user.id', 'user_info.real_name')
     ->get()->toArray();
     if (empty($users)) {
-      return Result::error('Researchers不存在!');
-    }
-    $num_key = array_search($data['user_id'], array_column($users, 'id'));
-    if(!empty($num_key)){
-      $result['number'] = $users[$num_key]['number'] ?? '';
+      return Result::error('此调研员不存在!');
     }
     $result['users'] = $users;
     $result['user_id'] = $data['user_id'];
@@ -9221,18 +9231,316 @@ class NewsService implements NewsServiceInterface
     $users_id = empty($data['users_id']) ? null : $data['users_id'];
     unset($data['users_id']);
     // return Result::success($data);
-    $result = ResearchTopic::insertGetId($data);
-    if (empty($result)) {
-      return Result::error('添加失败');
+    Db::beginTransaction();
+    try{
+      $result = ResearchTopic::insertGetId($data);
+      if (empty($result)) {
+        Db::rollBack();
+        return Result::error('添加失败');
+      }
+      $users = json_decode($users_id ?? '[]',true);
+      $users_type4 = User::where('type_id', 4)->pluck('id');
+      $type4_num = array_intersect($users, $users_type4->toArray());
+      if(count($users) != count($type4_num)){
+        Db::rollBack();
+        return Result::error('请选择调研员!');
+      }
+      // return Result::success($users);
+      if(!empty($users)){
+        // 去重:确保同一 retopic_id + user_id 组合唯一
+        $users = array_values(array_unique($users));
+        // 组装关联数据
+        $users_topic = [];
+        foreach ($users as $key => $value) {
+          $users_topic[$key] = [
+            'user_id'    => $value,
+            'retopic_id' => $result,
+          ];
+        }
+        // 使用 insertOrIgnore 忽略重复主键/唯一索引冲突
+        $retopic_user = RetopicUser::insert($users_topic);
+        if(empty($retopic_user)){
+          Db::rollBack();
+          return Result::error('添加失败');
+        }
+      }
+      Db::commit();
+      return Result::success($result);
+    }catch(\Exception $e){
+      Db::rollBack();
+      return Result::error($e->getMessage());
+    }
+  }
+  /**
+   * 调研选题-编辑
+   * @param array $data
+   * @return array
+   */
+  public function upResearchTopic(array $data): array
+  {
+    $user = User::where('id', $data['user_id'])->first();
+    if (empty($user)) {
+      return Result::error('用户不存在');
+    }
+    if($user['type_id'] == 10000){
+      $data['status'] = 1;
+    }else{
+      $data['status'] = 0;
+    }
+    $topic = ResearchTopic::where('id', $data['id'])->first();
+    if (empty($topic)) {
+      return Result::error('此调研选题不存在');
+    }
+    if ($data['keyword'] == '') {
+      //提取标题+内容中的关键词
+      $data['keyword'] = $data['title'];
+      //  . substr(str_replace(' ', '', strip_tags($data['content'])), 0, 20);
+      Jieba::init(); // 初始化 jieba-php
+      Finalseg::init();
+      $segList = Jieba::cut($data['keyword']);
+      $segList1 = array_slice($segList, 0, 8);
+      $data['keyword'] = implode(',', $segList1);
+    }
+    if ($data['description'] == '') {
+      //提取内容中的描述
+      $content = $data['content'];
+      // 去除 <style> 和 <script> 标签及其内容
+      $content = preg_replace('/<(style|script)[^>]*>.*?<\/\1>/is', '', $content);
+      // 去除所有 HTML 标签
+      $content = strip_tags($content);
+      // 去除 HTML 实体
+      $content = html_entity_decode($content, ENT_QUOTES | ENT_HTML5, 'UTF-8');
+      // 只保留文本和标点符号(去除所有字母数字以外的特殊符号,可根据需要调整正则)
+      $content = preg_replace('/[^\p{L}\p{N}\p{P}\p{Zs}]+/u', '', $content);
+      // 去除多余空白
+      $content = preg_replace('/\s+/u', '', $content);
+      // 截取 100 个字符
+      $data['description'] = mb_substr($content, 0, 100);
     }
-    $users = json_decode($data['users_id'] ?? '[]',true);
-    if(!empty($users)){
-      foreach($users as $key => $value){
-        $users[$key]['user_id'] = $value;
-        $users[$key]['retopic_id'] = $result;
+    $data['level'] = empty($data['level']) ? null : $data['level'];
+    $data['province_id'] = empty($data['province_id']) ? null : $data['province_id'];
+    $data['city_id'] = empty($data['city_id']) ? null : $data['city_id'];
+    $data['due_time'] = empty($data['due_time']) ? null : $data['due_time'];
+    $users_id = empty($data['users_id']) ? null : $data['users_id'];
+    $id = $data['id'];
+    unset($data['users_id']);
+    unset($data['id']);
+    // return Result::success($data);
+    Db::beginTransaction();
+    try{
+      $result = ResearchTopic::where('id', $id)->update($data);
+      if (empty($result)) {
+        Db::rollBack();
+        return Result::error('编辑调研选题失败!');
+      }
+      $users = json_decode($users_id ?? '[]',true);
+      // return Result::success($users);
+      $retopic_users = RetopicUser::where('retopic_id', $id)->pluck('user_id')->toArray();
+      if(!empty($retopic_users)){
+        $del_users = RetopicUser::where('retopic_id', $id)->delete();
+        if(empty($del_users)){
+          Db::rollBack();
+          return Result::error('编辑同行人员失败!');
+        }
+      }
+      if(!empty($users)){
+        // 去重:确保同一 retopic_id + user_id 组合唯一
+        $users = array_values(array_unique($users));
+        // 组装关联数据
+        $users_topic = [];
+        foreach ($users as $key => $value) {
+          $users_topic[$key] = [
+            'user_id'    => $value,
+            'retopic_id' => $id,
+          ];
+        }
+        // 使用 insertOrIgnore 忽略重复主键/唯一索引冲突
+        $retopic_user = RetopicUser::insert($users_topic);
+        if(empty($retopic_user)){
+          Db::rollBack();
+          return Result::error('编辑失败');
+        }
       }
-      $retopic_user = RetopicUser::insert($users);
+      Db::commit();
+      return Result::success($result);
+    }catch(\Exception $e){
+      Db::rollBack();
+      return Result::error($e->getMessage());
+    }
+  }
+  /**
+   * 调研选题-删除
+   * @param array $data
+   * @return array
+   */
+  public function delResearchTopic(array $data): array
+  {
+    $user = User::where('id', $data['user_id'])->first();
+    if (empty($user)) {
+      return Result::error('用户不存在');
+    }
+    $topic = ResearchTopic::where('id', $data['id'])->first();
+    if (empty($topic)) {
+      return Result::error('此调研选题不存在');
+    }
+    Db::beginTransaction();
+    try{
+      $result = ResearchTopic::where('id', $data['id'])->delete();
+      if (empty($result)) {
+        Db::rollBack();
+        return Result::error('删除调研选题失败!');
+      }
+      $topic_users = RetopicUser::where('retopic_id', $data['id'])->pluck('user_id')->toArray();
+      if(!empty($topic_users)){
+        $del_topic_users = RetopicUser::where('retopic_id', $data['id'])->delete();
+        if(empty($del_topic_users)){
+          Db::rollBack();
+          return Result::error('删除同行人员失败!');
+        }
+      }
+      Db::commit();
+      return Result::success($result);
+    }catch(\Exception $e){
+      Db::rollBack();
+      return Result::error($e->getMessage());
+    }
+  }
+  /**
+   * 调研选题-列表
+   * @param array $data
+   * @return array
+   */
+  public function getResearchTopicList(array $data): array
+  {
+    $user = User::where('id', $data['user_id'])->first();
+    if (empty($user)) {
+      return Result::error('用户不存在!');
+    }
+    $where = [];
+    if($user->type_id != 10000){
+      $where['research_topic.user_id'] = $data['user_id'];
+      $website = Website::where('id', $data['website_id'])->first();
+      if (empty($website)) {
+        return Result::error('网站不存在!');
+      }
+      $web_column = json_decode($website->website_column_arr_id ?? '[]',true);
+      if(empty($web_column)){
+        return Result::error('网站未配置栏目!');
+      }
+    }
+    if($data['is_master'] != 0){
+      if($data['is_master'] == 3){
+        $where['research_topic.status'] = 0;
+      }else{
+        $where['research_topic.status'] = $data['is_master'];
+      }
+    }
+    if(!empty($data['title'])){
+      array_push($where, ['research_topic.title', 'like', '%'.$data['title'].'%']);
+    }
+    $page = $data['page'] ?? 1;
+    $page_size = $data['page_size'] ?? 10;
+    $research_topic = ResearchTopic::when($data['is_master'] == 0, function($query) use($data){
+      $query->whereIn('research_topic.status', [0,2]);
+    })
+    ->when(!empty($where), function($query) use($where){
+      $query->where($where);
+    })
+    ->with(['retopicUsers' => function ($query) {
+          $query->select('id', 'user_id', 'retopic_id');
+        }
+    ])
+    ->leftJoin('user', 'user.id', '=', 'research_topic.user_id')
+    ->select('research_topic.id','research_topic.title','research_topic.updated_at','research_topic.status',
+    'research_topic.due_time','user.nickname')
+    ->orderBy('research_topic.updated_at', 'desc')
+    ->paginate($data['page_size'], ['*'], 'page', $data['page']);
+    if(empty($research_topic->items())){
+      return Result::error('暂无调研选题!');
+    }
+    $result = [
+      'rows' => $research_topic->items(),
+      'count' => $research_topic->total()
+    ];
+    foreach($result['rows'] as $key => $value){
+      if($user->type_id != 10000){
+        if(in_array($value['column_id'], $web_column)){
+          $result['rows'][$key]['is_update'] = 1;
+        }else{
+          $result['rows'][$key]['is_update'] = 0;
+        }
+      }
+      if(!empty($value['retopicUsers'])){
+        $retopic_users = $value['retopicUsers'];
+        $retopic_user = $retopic_users->pluck('user_id')->toArray();
+        $result['rows'][$key]['txry_users'] = implode(',', $retopic_user);
+        unset($result['rows'][$key]['retopicUsers']);
+
+      }
+    }  
+    return Result::success($result);
+  }
+  /**
+   * 调研选题-修改状态
+   * @param array $data
+   * @return array
+   */
+  public function checkResearchTopic(array $data): array
+  {
+    $user = User::where('id', $data['user_id'])->first();
+    if (empty($user)) {
+      return Result::error('用户不存在!');
+    }
+    if($user->type_id != 10000 && ($data['status'] == 1 || $data['status'] == 2)){
+      return Result::error('此用户暂无权限!');
+    }
+    $topic = ResearchTopic::where('id', $data['id'])->first();
+    if (empty($topic)) {
+      return Result::error('此调研选题不存在');
+    }
+    $id = $data['id'];
+    unset($data['id']);
+    unset($data['user_id']);
+    $result = ResearchTopic::where('id', $id)->update($data);
+    if (empty($result)) {
+      return Result::error('修改调研选题状态失败!');
     }
     return Result::success($result);
   }
-}
+  /**
+   * 调研选题-详情
+   * @param array $data
+   * @return array
+   */
+  public function getResearchTopicInfo(array $data): array
+  {
+    $user = User::where('id', $data['user_id'])->first();
+    if (empty($user)) {
+      return Result::error('用户不存在!');
+    }
+    $research_topic = ResearchTopic::where('research_topic.id', $data['id'])
+    ->with(['retopicUsers' => function ($query) {
+          $query->select('user_id','retopic_id');
+        }
+    ])
+    ->leftJoin('user', 'user.id', '=', 'research_topic.user_id')
+    ->leftJoin('user_info', 'user_info.user_id', '=', 'research_topic.user_id')
+    ->select('research_topic.*','user_info.number')
+    ->first();
+    if(empty($research_topic)){
+      return Result::error('获取调研选题详情失败!');
+    }
+    $retopic_user = $research_topic['retopicUsers'];
+    if(!empty($retopic_user)){
+      $retopic_user = $retopic_user->pluck('user_id')->toArray();
+      foreach($retopic_user as $key => $value){
+        $real_name = UserInfo::where('user_id', $value)->value('real_name');
+        $research_topic['retopicUsers'][$key]['real_name'] = $real_name;
+        $research_topic['retopicUsers'][$key]['user_id'] = $value;
+        unset($research_topic['retopicUsers'][$key]['retopic_id']);
+      }
+    }
+    
+    return Result::success($research_topic);
+  }
+}

+ 25 - 0
app/JsonRpc/NewsServiceInterface.php

@@ -414,4 +414,29 @@ interface NewsServiceInterface
      * @return array
      */
     public function addResearchTopic(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function upResearchTopic(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function delResearchTopic(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getResearchTopicList(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function checkResearchTopic(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getResearchTopicInfo(array $data):array;
 }

+ 4 - 0
app/Model/ResearchTopic.php

@@ -24,4 +24,8 @@ class ResearchTopic extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function retopicUsers()
+    {
+        return $this->hasMany(RetopicUser::class, 'retopic_id', 'id');
+    }
 }

+ 42 - 0
config/autoload/amqp.php

@@ -0,0 +1,42 @@
+<?php
+
+declare (strict_types = 1);
+use function Hyperf\Support\env;
+return [
+    'enable' => true,
+    'default' => [
+        'host' => env('AMQP_HOST', '192.168.1.127'),
+        'port' => (int) env('AMQP_PORT', 5672),
+        'user' => env('AMQP_USER', 'guest'),
+        'password' => env('AMQP_PASSWORD', 'guest'),
+        'vhost' => '/',
+        'concurrent' => [
+            'limit' => 1,
+        ],
+        'pool' => [
+            'min_connections' => 1,
+            'max_connections' => 1, // 限制最大连接数为1
+            'connect_timeout' => 1000.0,
+            'wait_timeout' => 3.0,
+            'heartbeat' => -1,
+            
+        ],
+        'params' => [
+            'insist' => false,
+            'login_method' => 'AMQPLAIN',
+            'login_response' => null,
+            'locale' => 'en_US',
+            'connection_timeout' => 3.0,
+            // 尽量保持是 heartbeat 数值的两倍
+            'read_write_timeout' => 6.0,
+            'context' => null,
+            'keepalive' => false,
+            // 尽量保证每个消息的消费时间小于心跳时间
+            'heartbeat' => 3,
+            'close_on_destruct' => false,
+        ],
+    ],
+    'pool2' => [
+
+    ],
+];

+ 1 - 0
config/autoload/dependencies.php

@@ -10,4 +10,5 @@ declare(strict_types=1);
  * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
  */
 return [
+    // App\JsonRpc\ChatServiceInterface::class => App\JsonRpc\ChatService::class,
 ];