|
|
@@ -0,0 +1,94 @@
|
|
|
+<?php
|
|
|
+namespace app\admin\command;
|
|
|
+
|
|
|
+use think\console\Command;
|
|
|
+use think\console\Input;
|
|
|
+use think\console\Output;
|
|
|
+use app\admin\model\User;
|
|
|
+use app\admin\model\Config;
|
|
|
+use app\admin\model\KefuWork;
|
|
|
+use app\enterprise\model\{Message,Friend};
|
|
|
+use Exception;
|
|
|
+use think\facade\Db;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Worker 命令行类
|
|
|
+ */
|
|
|
+class UserQueue extends Command
|
|
|
+{
|
|
|
+ public function configure()
|
|
|
+ {
|
|
|
+ $this->setName('user:queue')
|
|
|
+ ->setDescription('用户排队,自动分配客服');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function execute(Input $input, Output $output)
|
|
|
+ {
|
|
|
+ $where = [
|
|
|
+ ['status', '=', 1],
|
|
|
+ ['is_online', '=', 1],
|
|
|
+ ['role', '=', 3],
|
|
|
+ ];
|
|
|
+
|
|
|
+ while(true) {
|
|
|
+ //查询当前最少接线数的客服
|
|
|
+ $kefu_chat_max = Config::getKefuChatMax();//单个客服接线上限
|
|
|
+ if ($kefu_chat_max) {
|
|
|
+ $where[] = ['chat_num', '<', $kefu_chat_max];
|
|
|
+ }
|
|
|
+
|
|
|
+ //排队用户
|
|
|
+ $list = User::where('status', 1)->where('is_online', 1)->where('service_status', 1)
|
|
|
+ ->order('service_start', 'asc')
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ if (empty($list)) {
|
|
|
+ sleep(10);
|
|
|
+ }
|
|
|
+ foreach ($list as $item) {
|
|
|
+ $cs_user = User::where($where)->order('chat_num', 'asc')->find();
|
|
|
+ if (!$cs_user) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ $this->handleChat($item, $cs_user->user_id, $cs_user->uid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自动接线
|
|
|
+ */
|
|
|
+ public function handleChat($user, $cs_uid, $admin_id)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ Db::startTrans();
|
|
|
+ $user_id = $user['user_id'];
|
|
|
+ $user->cs_uid = $cs_uid;
|
|
|
+ $user->service_status = 1;
|
|
|
+ $user->service_start = time();
|
|
|
+ $user->save();
|
|
|
+
|
|
|
+ //更新客服接线数量
|
|
|
+ User::where('user_id', $cs_uid)->update(['chat_num'=>Db::raw('chat_num+1')]);
|
|
|
+
|
|
|
+ $friend = Friend::where('create_user', $user_id)->order('create_time', 'desc')->find();
|
|
|
+ if ($friend) {
|
|
|
+ $robot_id = $friend->friend_user_id;
|
|
|
+ $friend->friend_user_id = $cs_uid;
|
|
|
+ $friend->save();
|
|
|
+ $chat_identify = $cs_uid . '-' . $user_id;
|
|
|
+ Message::where(['from_user' => $user_id, 'to_user' => $robot_id])->update(['to_user' => $cs_uid, 'chat_identify' => $chat_identify]);
|
|
|
+ Message::where(['from_user' => $robot_id, 'to_user' => $user_id])->update(['from_user' => $cs_uid, 'chat_identify' => $chat_identify]);
|
|
|
+ }
|
|
|
+
|
|
|
+ KefuWork::addNum($admin_id, 'chat_num');//客服接线次数更新
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ //通知客服已接线
|
|
|
+ wsSendMsg(0,'handleChat',['user_id'=>$user_id]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|