lip hace 3 meses
padre
commit
daf7c9ca46

+ 42 - 0
app/admin/command/OperationData.php

@@ -0,0 +1,42 @@
+<?php
+namespace app\admin\command;
+
+use think\console\Command;
+use think\console\Input;
+use think\console\Output;
+use app\admin\model\OperationData as OperationDataModel;
+use app\admin\model\KefuTime;
+
+/**
+ * Worker 命令行类
+ */
+class OperationData extends Command
+{
+    public function configure()
+    {
+        $this->setName('operation:data')
+            ->setDescription('每日0点统计前一天的数据');
+    }
+
+    public function execute(Input $input, Output $output)
+    {
+        //统计前一天的接线总数
+        OperationDataModel::create([
+            'type' => 1,
+            'num' => $this->chatNum(),
+            'date' => date('Y-m-d', strtotime('-1 day')),
+        ]);
+    }
+
+    /**
+     * 统计前一天的接线总数
+     */
+    public function chatNum()
+    {
+        return  KefuTime::where('type', 3)
+                    ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-1 day')))
+                    ->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime('-1 day')))
+                    ->count();
+    }
+
+}

+ 45 - 7
app/admin/controller/Kefu.php

@@ -8,19 +8,23 @@ use app\admin\model\Admin;
 use app\admin\model\Sign;
 use app\admin\model\KefuWork;
 use app\admin\model\User;
+use app\admin\model\UserView;
+use app\admin\model\KefuTime;
+use app\admin\model\OperationData;
 use Exception;
 
 
 class Kefu extends BaseController
 {
 
+    //当前数据统计
     public function statistics()
     {
         // 统计
-        $data['visit_count'] = 0; //当前客服页访问量
-        $data['online_count'] = 0; //当前客服在线人数
-        $data['handling_count'] = 0; //当前客服接线中的总数
-        $data['today_handling_count'] = 0; //今日客服接线总数
+        $data['visit_count'] = UserView::count(); //当前客服页访问量
+        $data['online_count'] = User::where('is_online', '>', 0)->count(); //当前客服在线人数
+        $data['handling_count'] = User::where('service_status',2)->count(); //当前客服接线中的总数
+        $data['today_handling_count'] = KefuTime::where('type', 3)->where('created_at', '>=', date('Y-m-d'))->count(); //今日客服接线总数
 
         return $this->success($data);
     }
@@ -34,7 +38,9 @@ class Kefu extends BaseController
             $params = $this->request->param();
             $page = $params['page'] ?? 1;
             $limit = $params['limit'] ?? 15;
-            $query = Admin::alias('admin')->join('kefu_work', 'kefu_work.admin_id = admin.id', 'left');
+            $query = Admin::alias('admin')->join('kefu_work', 'kefu_work.admin_id = admin.id', 'left')
+                        ->where('kefu_work.created_at', '>=', date('Y-m-d 00:00:00'));
+
             if (isset($params['is_online'])) {
                 $query->where('is_online', $params['is_online']);
             }
@@ -69,7 +75,8 @@ class Kefu extends BaseController
             $params = $this->request->param();
             $page = $params['page'] ?? 1;
             $limit = $params['limit'] ?? 15;
-
+            $order_field = $params['field'] ?? 'kefu_work.id';
+            $order = $params['order'] ?? 'desc';
             $query = KefuWork::alias('kefu_work')
                         ->join('admin', 'kefu_work.admin_id = admin.id', 'left')
                         ->join('user', 'kefu_work.admin_id = user.uid', 'left')
@@ -87,7 +94,7 @@ class Kefu extends BaseController
 
             $count = $query->count();
             $list = $query->field(['admin.username','admin.nickname','user.is_online', 'kefu_work.*'])
-                        ->order('kefu_work.id', 'desc')           
+                        ->order($order_field, $order)
                         ->limit($limit)
                         ->page($page)
                         ->select();
@@ -101,6 +108,37 @@ class Kefu extends BaseController
         return $this->success(['count' => $count, 'list' => $list]);
     }
 
+    /**
+     * @api {get} /chat 总接线数据
+     */
+    public function chat()
+    {
+        try {
+            $params = $this->request->param();
+            $start_time = empty($params['start_time']) ? date('Y-m-d') : $params['start_time'];
+            $end_time = empty($params['end_time']) ? date('Y-m-d') : $params['end_time'];
+
+            $list = OperationData::where('type', 1)
+                    ->where('date', '>=', $start_time)
+                    ->where('date', '<=', $end_time)
+                    ->limit(100)
+                    ->field(['num','date'])
+                    ->select();
+            if ($start_time == date('Y-m-d') || $end_time == date('Y-m-d')) {
+                $list[] = [
+                    'num' => KefuTime::where('type', 3)
+                        ->where('created_at', '>=', date('Y-m-d 00:00:00'))
+                        ->where('created_at', '<=', date('Y-m-d 23:59:59'))
+                        ->count(),
+                    'date' => date('Y-m-d'),
+                ];
+            }  
+        } catch (Exception $e) {
+            return $this->error($e->getMessage());
+        }
+        return $this->success(['count' => count($list), 'list' => $list]);
+    }
+
     //客服签到
     public function sign()
     {

+ 12 - 0
app/admin/model/OperationData.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace app\admin\model;
+
+use app\BaseModel;
+
+
+class OperationData extends BaseModel
+{
+    
+
+}