xielin 5 년 전
부모
커밋
8ffa1c9d4a

+ 98 - 0
app/Console/Commands/BehaviorRecord.php

@@ -0,0 +1,98 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Repositories\BehaviorRecordRepositories;
+use App\Repositories\ProductSkuRepository;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class BehaviorRecord extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'behavior:record';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '记录行为账本';
+
+    protected $connection;
+    protected $channel;
+
+    protected $PRODUCT_STOCK_MYSQL_QUEUE = 'behavior_record_queue';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(BehaviorRecordRepositories $behaviorRecordRepositories)
+    {
+        parent::__construct();
+        $this->behaviorRecordRepositories = $behaviorRecordRepositories;
+    }
+
+    public function getConnection()
+    {
+        $conn = false;
+        if ($this->connection) {
+            return $this->connection;
+        }
+        for ($i = 0; $i < 3; $i++) {
+            $connection = $this->createConn();
+            if ($connection) {
+                $conn = $connection;
+                break;
+            }
+            Log::info("create amqp conn retry=" . $i);
+        }
+        return $conn;
+    }
+
+    public function createConn()
+    {
+        try {
+            $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
+        } catch (\Exception $exception) {
+            Log::info("AMQP connection Error" . $exception->getMessage());
+            $connection = false;
+        }
+        return $connection;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->connection = $this->getConnection();
+        $this->channel = $this->connection->channel();
+        $this->channel->queue_declare($this->PRODUCT_STOCK_MYSQL_QUEUE, false, true, false, false);
+
+        $callback = function ($msg) {
+            $param = \GuzzleHttp\json_decode($msg->body,true);
+            $row = $this->behaviorRecordRepositories->addRecord($param);
+            if($row){
+                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
+            }
+        };
+        $this->channel->basic_qos(null, 1, null);
+        $this->channel->basic_consume($this->PRODUCT_STOCK_MYSQL_QUEUE, '', false, false, false, false, $callback);
+
+        while (count($this->channel->callbacks)) {
+            $this->channel->wait();
+        }
+        $this->channel->close();
+        $this->connection->close();
+    }
+}

+ 63 - 0
app/Console/Commands/CalcPostWeight.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\PostData;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class CalcPostWeight extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:calc_weight';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '计算内容权重';
+
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line('开始计算权重');
+        $key = "community_calc_post_score";
+        $postIds = Redis::smembers($key);
+        foreach ($postIds as $postId) {
+            $postInfo = PostData::where("post_id", $postId)->first();
+            $temp = $postInfo['pv'] +
+                (5 * $postInfo->share_cout) +
+                (2 * $postInfo->praise_count) +
+                (10 * $postInfo->collect_count) +
+                (3 * $postInfo->comment_count) -
+                (10 * $postInfo->dislike_count);
+            $fresh = (Carbon::parse($postInfo['created_at'])->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
+            $score = log10($temp) + $fresh / 86400;
+            $postInfo->weight = $score;
+            $postInfo->save();
+            $this->line(date("Y-m-d H:i:s")."设置帖子".$postInfo->post_id."的权重分为:".$score);
+        }
+    }
+}

+ 98 - 0
app/Console/Commands/ContentFeedCreate.php

@@ -0,0 +1,98 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Repositories\FeedRepositories;
+use App\Repositories\ProductSkuRepository;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class ContentFeedCreate extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'content:feed';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'virus通知异步处理-feed生成,帖子数据统计';
+
+    protected $connection;
+    protected $channel;
+
+    protected $queue = 'content_feed_queue';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(FeedRepositories $feedRepositories)
+    {
+        parent::__construct();
+        $this->feedRepositories = $feedRepositories;
+    }
+
+    public function getConnection()
+    {
+        $conn = false;
+        if ($this->connection) {
+            return $this->connection;
+        }
+        for ($i = 0; $i < 3; $i++) {
+            $connection = $this->createConn();
+            if ($connection) {
+                $conn = $connection;
+                break;
+            }
+            Log::info("create amqp conn retry=" . $i);
+        }
+        return $conn;
+    }
+
+    public function createConn()
+    {
+        try {
+            $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
+        } catch (\Exception $exception) {
+            Log::info("AMQP connection Error" . $exception->getMessage());
+            $connection = false;
+        }
+        return $connection;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->connection = $this->getConnection();
+        $this->channel = $this->connection->channel();
+        $this->channel->queue_declare($this->queue, false, true, false, false);
+
+        $callback = function ($msg) {
+            $param = \GuzzleHttp\json_decode($msg->body,true);
+            $row = $this->feedRepositories->feedCreate($param);
+            if($row){
+                $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
+            }
+        };
+        $this->channel->basic_qos(null, 1, null);
+        $this->channel->basic_consume($this->queue, '', false, false, false, false, $callback);
+
+        while (count($this->channel->callbacks)) {
+            $this->channel->wait();
+        }
+        $this->channel->close();
+        $this->connection->close();
+    }
+}

+ 22 - 0
app/PostData.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class PostData extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'post_data';
+    protected $guarded = [];
+
+
+}

+ 7 - 67
app/Repositories/BehaviorRecordRepositories.php

@@ -9,8 +9,6 @@
 namespace App\Repositories;
 
 
-use App\Models\BindPhoneRecord;
-use App\Models\BindWxRecord;
 use App\Models\CommentAccountRecord;
 use App\Models\GeneralLedgerRecord;
 use App\Models\RegisteredAccountsRecord;
@@ -23,23 +21,19 @@ class BehaviorRecordRepositories
     public function __construct(RegisteredAccountsRecord $registeredAccountsRecord,
                                 GeneralLedgerRecord $generalLedgerRecord,
                                 ReleaseRecord $releaseRecord,
-                                CommentAccountRecord $commentAccountRecord,
-                                BindPhoneRecord $bindPhoneRecord,
-                                BindWxRecord $bindWxRecord)
+                                CommentAccountRecord $commentAccountRecord)
     {
         $this->registeredAccountsRecord = $registeredAccountsRecord;
         $this->generalLedgerRecord = $generalLedgerRecord;
         $this->releaseRecord = $releaseRecord;
         $this->commentAccountRecord = $commentAccountRecord;
-        $this->bindPhoneRecord = $bindPhoneRecord;
-        $this->bindWxRecord = $bindWxRecord;
     }
 
     /**
      * 根据行为标识获取行为记录表
      * @param $behavior
      */
-    public function getBehaviorTable($behavior)
+    private function getBehaviorTable($behavior)
     {
         $tables = [
             'register' => 'registered_accounts_record',
@@ -49,9 +43,7 @@ class BehaviorRecordRepositories
             'unlike' => 'general_ledger_record',
             'forward' => 'general_ledger_record',
             'collect' => 'general_ledger_record',
-            'comment' => 'comment_account_record',
-            'bind_phone' => 'bind_phone_record',
-            'bind_wechat' => 'bind_wx_record'
+            'comment' => 'comment_account_record'
         ];
         return isset($tables[$behavior]) ? $tables[$behavior] : false;
     }
@@ -76,10 +68,6 @@ class BehaviorRecordRepositories
                 return $this->addGeneralRecord($info);
             } elseif ($behaviorTable == 'comment_account_record') {
                 return $this->addCommentRecord($info);
-            } elseif ($behaviorTable == 'bind_phone_record') {
-                return $this->addBindPhoneRecord($info);
-            } elseif ($behaviorTable == 'bind_wx_record') {
-                return $this->addBindWxRecord($info);
             }
         }
         return false;
@@ -90,7 +78,7 @@ class BehaviorRecordRepositories
      * @param $register
      * @return bool
      */
-    public function addRegisterRecord($register)
+    private function addRegisterRecord($register)
     {
         try {
             $data['virus_behavior_id'] = $register['virus_behavior_id'];
@@ -115,7 +103,7 @@ class BehaviorRecordRepositories
      * @param $release
      * @return bool
      */
-    public function addReleaseRecord($release)
+    private function addReleaseRecord($release)
     {
         try {
             $data['virus_behavior_id'] = $release['virus_behavior_id'];
@@ -139,7 +127,7 @@ class BehaviorRecordRepositories
      * @param $general
      * @return bool
      */
-    public function addGeneralRecord($general)
+    private function addGeneralRecord($general)
     {
         try {
             $data['virus_behavior_id'] = $general['virus_behavior_id'];
@@ -164,7 +152,7 @@ class BehaviorRecordRepositories
      * @param $comment
      * @return bool
      */
-    public function addCommentRecord($comment)
+    private function addCommentRecord($comment)
     {
         try {
             $data['virus_behavior_id'] = $comment['virus_behavior_id'];
@@ -184,52 +172,4 @@ class BehaviorRecordRepositories
             return false;
         }
     }
-
-    /**
-     * 记录账本-绑定手机
-     * @param $bindphone
-     * @return bool
-     */
-    public function addBindPhoneRecord($bindphone)
-    {
-        try {
-            $data['virus_behavior_id'] = $bindphone['virus_behavior_id'];
-            $data['uid'] = $bindphone['uid'];
-            $data['trigger_time'] = $bindphone['trigger_time'];
-            $data['phone'] = $bindphone['phone'];
-            $data['physical_exertion'] = $bindphone['physical_exertion'];
-            $data['trigger_type'] = $bindphone['trigger_type'];
-            $data['generation_type'] = $bindphone['generation_type'];
-            $data['release_status'] = $bindphone['release_status'];
-            $data['generation_quantity'] = $bindphone['generation_quantity'];
-            $data['quantity_issued'] = $bindphone['quantity_issued'];
-            return $this->bindPhoneRecord->create($data);
-        } catch (QueryException $exception) {
-            return false;
-        }
-    }
-
-    /**
-     * 记录账本-绑定微信
-     * @param $bindwx
-     * @return bool
-     */
-    public function addBindWxRecord($bindwx)
-    {
-        try {
-            $data['virus_behavior_id'] = $bindwx['virus_behavior_id'];
-            $data['uid'] = $bindwx['uid'];
-            $data['trigger_time'] = $bindwx['trigger_time'];
-            $data['weixin'] = $bindwx['weixin'];
-            $data['physical_exertion'] = $bindwx['physical_exertion'];
-            $data['trigger_type'] = $bindwx['trigger_type'];
-            $data['generation_type'] = $bindwx['generation_type'];
-            $data['release_status'] = $bindwx['release_status'];
-            $data['generation_quantity'] = $bindwx['generation_quantity'];
-            $data['quantity_issued'] = $bindwx['quantity_issued'];
-            return $this->bindWxRecord->create($data);
-        } catch (QueryException $exception) {
-            return false;
-        }
-    }
 }

+ 1 - 25
app/Repositories/BehaviorRepositories.php

@@ -14,34 +14,10 @@ use Illuminate\Support\Facades\Log;
 
 class BehaviorRepositories
 {
-    public function __construct(MemberRepository $memberRepository)
+    public function __construct()
     {
-        $this->memberRepository = $memberRepository;
     }
 
-
-    /**
-     * 消费virus通知
-     * @param array $request
-     */
-    public function consumeNotify($request)
-    {
-        $identify = $request['behavior_flag'];
-        $behavior = $this->getBehavior($identify);
-        $userInfo = $this->memberRepository->getUserInfo();
-        if (empty($userInfo) || empty($behavior)) {
-            return true;
-        }
-
-        $behaviorStatus = 0;    //触发类型
-        if ($userInfo && $userInfo['strength'] > 0) {
-            $behaviorStatus = 1;
-        }
-        $strength = $behaviorStatus ? $behavior['physical_strength'] : 0;
-        $beans = $behaviorStatus ? $behavior['rainbow_beans'] : 0;
-    }
-
-
     /**
      * 获取行为绑定规则
      * @param $info

+ 29 - 0
app/Repositories/FeedRepositories.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: edz
+ * Date: 2019-06-10
+ * Time: 17:53
+ */
+
+namespace App\Repositories;
+
+
+use App\Models\Behavior;
+use Illuminate\Support\Facades\Log;
+
+class FeedRepositories
+{
+    public function __construct()
+    {
+    }
+
+    public function feedCreate($request){
+        $fans = $request['fans'];
+        foreach ($fans as $fan){
+
+        }
+    }
+
+
+}

+ 18 - 5
app/Repositories/MemberRepository.php

@@ -10,17 +10,19 @@ namespace App\Repositories;
 
 
 use App\Service\RabbitMqUtil;
+use Illuminate\Support\Facades\Log;
 use Tymon\JWTAuth\Facades\JWTAuth;
 
 class MemberRepository
 {
-    public function getUserInfo()
+
+    public function getUserInfo($uid)
     {
         try {
-            $sign = generateSign([], config('customer.app_secret'));
+            $sign = generateSign(['uid' => $uid], config('customer.app_secret'));
             $url = config("customer.user_service_url") . '/userInfo';
             $array = [
-                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
+                'json' => ['sign' => $sign, 'uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
             ];
             return http($url, $array);
         } catch (\Exception $e) {
@@ -37,7 +39,13 @@ class MemberRepository
     {
         $rabbitmq = RabbitMqUtil::getInstance();
         $queueName = "user_strength_queue";
-        $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+        if($value){
+            $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+            Log::debug('操作用户 ' . $uid . ' 体力' . $value . '备注:' . $remark);
+        }else{
+            Log::debug('放弃操作用户体力,原因:体力值为'.$value);
+        }
+
     }
 
     /**
@@ -50,6 +58,11 @@ class MemberRepository
     {
         $rabbitmq = RabbitMqUtil::getInstance();
         $queueName = "user_rainbownbean_queue";
-        $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+        if($value){
+            $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+            Log::debug('操作用户 ' . $uid . ' 彩虹豆' . $value . '备注:' . $remark);
+        }else{
+            Log::debug('放弃操作用户彩虹豆,原因:彩虹豆值为'.$value);
+        }
     }
 }

+ 64 - 0
app/Repositories/PostRepositories.php

@@ -0,0 +1,64 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: edz
+ * Date: 2019-06-10
+ * Time: 17:53
+ */
+
+namespace App\Repositories;
+
+
+use App\Models\Behavior;
+use App\Models\PostData;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class PostRepositories
+{
+    public function __construct()
+    {
+    }
+
+    /**
+     * 更新帖子统计数量
+     * @param $request
+     * @return mixed
+     */
+    public function updatePostData($request)
+    {
+        $postId = $request['post_id'];
+        $post = PostData::where('post_id', $postId)->first();
+        if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
+            $post->pv += 1;
+            $post->pv_real += 1;
+        } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
+            $post->dislike += 1;
+        } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
+            $post->praise_count += 1;
+            $post->praise_real_count += 1;
+        } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
+            $post->share_count += 1;
+            $post->share_real_count += 1;
+        } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
+            $post->comment_count += 1;
+        } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
+            $post->collect_count += 1;
+            $post->collect_real_count += 1;
+        }
+        return $post->save();
+    }
+
+    /**
+     * 收集所有有操作的帖子,存入redis
+     * 供后续计算帖子权重
+     * @param $id
+     */
+    public function collectPostId($id)
+    {
+        $key = "community_calc_post_score";
+        Redis::sadd($key,$id);
+    }
+
+
+}