浏览代码

Merge branch 'rtest' of 39.97.239.116:gyfl/xyzshop into rtest

xiaoyu 3 年之前
父节点
当前提交
e4ef509633

+ 46 - 38
admin/control/refill_order.php

@@ -1,8 +1,8 @@
 <?php
 <?php
 
 
 require_once(BASE_HELPER_PATH . '/refill/util.php');
 require_once(BASE_HELPER_PATH . '/refill/util.php');
+require_once(BASE_HELPER_PATH . '/task/task_helper.php');
 
 
-use refill\util;
 class refill_orderControl extends SystemControl
 class refill_orderControl extends SystemControl
 {
 {
     public function __construct()
     public function __construct()
@@ -19,66 +19,66 @@ class refill_orderControl extends SystemControl
     public function refill_order($fShowStat = false)
     public function refill_order($fShowStat = false)
     {
     {
         $model_refill_order = Model('refill_order');
         $model_refill_order = Model('refill_order');
-        $condition['inner_status'] = 0;
+        $cond['inner_status'] = 0;
         $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours"));
         $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d H:i:s", strtotime("-1 hours"));
 
 
         if (!empty($_GET['order_sn'])) {
         if (!empty($_GET['order_sn'])) {
-            $condition['refill_order.order_sn'] = $_GET['order_sn'];
+            $cond['refill_order.order_sn'] = $_GET['order_sn'];
 
 
         }
         }
         if (!empty($_GET['mch_order'])) {
         if (!empty($_GET['mch_order'])) {
-            $condition['refill_order.mch_order'] = $_GET['mch_order'];
+            $cond['refill_order.mch_order'] = $_GET['mch_order'];
 
 
         }
         }
         if (!empty($_GET['ch_trade_no'])) {
         if (!empty($_GET['ch_trade_no'])) {
-            $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
+            $cond['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
 
 
         }
         }
         if (!empty($_GET['card_no'])) {
         if (!empty($_GET['card_no'])) {
-            $condition['refill_order.card_no'] = $_GET['card_no'];
+            $cond['refill_order.card_no'] = $_GET['card_no'];
 
 
         }
         }
         if (!empty($_GET['no_mchid'])) {
         if (!empty($_GET['no_mchid'])) {
             $no_mchid = explode(',', $_GET['no_mchid']);
             $no_mchid = explode(',', $_GET['no_mchid']);
-            $condition['refill_order.mchid'] = ['not in', $no_mchid];
+            $cond['refill_order.mchid'] = ['not in', $no_mchid];
         }
         }
         if (!empty($_GET['no_amount'])) {
         if (!empty($_GET['no_amount'])) {
             $no_amount = explode(',', $_GET['no_amount']);
             $no_amount = explode(',', $_GET['no_amount']);
-            $condition['refill_order.refill_amount'] = ['not in', $no_amount];
+            $cond['refill_order.refill_amount'] = ['not in', $no_amount];
         }
         }
 
 
         if (!empty($_GET['mchid'])) {
         if (!empty($_GET['mchid'])) {
-            $condition['refill_order.mchid'] = $_GET['mchid'];
+            $cond['refill_order.mchid'] = $_GET['mchid'];
         }
         }
         if (!empty($_GET['channel_name'])) {
         if (!empty($_GET['channel_name'])) {
-            $condition['refill_order.channel_name'] = $_GET['channel_name'];
+            $cond['refill_order.channel_name'] = $_GET['channel_name'];
         }
         }
         if (!empty($_GET['store_id'])) {
         if (!empty($_GET['store_id'])) {
-            $condition['vr_order.store_id'] = $_GET['store_id'];
+            $cond['vr_order.store_id'] = $_GET['store_id'];
         }
         }
         if (!empty($_GET['refill_amount'])) {
         if (!empty($_GET['refill_amount'])) {
-            $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
+            $cond['refill_order.refill_amount'] = $_GET['refill_amount'];
         }
         }
         if (!empty($_GET['quality'])) {
         if (!empty($_GET['quality'])) {
-            $condition['refill_order.quality'] = $_GET['quality'];
+            $cond['refill_order.quality'] = $_GET['quality'];
         }
         }
         if(!empty($_GET['official_status'])) {
         if(!empty($_GET['official_status'])) {
             if($_GET['official_status'] == 1) {
             if($_GET['official_status'] == 1) {
-                $condition['official_sn'] = '';
+                $cond['official_sn'] = '';
             } elseif ($_GET['official_status'] == 2) {
             } elseif ($_GET['official_status'] == 2) {
-                $condition['official_sn'] = ['neq', ''];
+                $cond['official_sn'] = ['neq', ''];
             }
             }
         }
         }
 
 
         if (!empty($_GET['card_type'])) {
         if (!empty($_GET['card_type'])) {
             if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) {
             if (in_array($_GET['card_type'], ['1', '2', '4', '5', '6', '7'])) {
-                $condition['refill_order.card_type'] = intval($_GET['card_type']);
+                $cond['refill_order.card_type'] = intval($_GET['card_type']);
             }
             }
             if ($_GET['card_type'] == 'oil') {
             if ($_GET['card_type'] == 'oil') {
-                $condition['refill_order.card_type'] = ['in', [1, 2]];
+                $cond['refill_order.card_type'] = ['in', [1, 2]];
             }
             }
             if ($_GET['card_type'] == 'phone') {
             if ($_GET['card_type'] == 'phone') {
-                $condition['refill_order.card_type'] = ['in', [4, 5, 6]];
+                $cond['refill_order.card_type'] = ['in', [4, 5, 6]];
             }
             }
         }
         }
 
 
@@ -87,56 +87,58 @@ class refill_orderControl extends SystemControl
         $end_unixtime = intval(strtotime($_GET['query_end_time']));
         $end_unixtime = intval(strtotime($_GET['query_end_time']));
 
 
         if($end_unixtime <= 0) {
         if($end_unixtime <= 0) {
-            $end_unixtime = time();
+            $cur_time = time();
+            $cur_time = $cur_time - ($cur_time % 300) + 300;
+            $end_unixtime = $cur_time;
         }
         }
 
 
         if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
         if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
-            $condition['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
-            $condition['vr_order.add_time'] = [['egt', $start_unixtime],['lt', $end_unixtime], 'and'];
+            $cond['refill_order.order_time'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
+            $cond['vr_order.add_time'] = [['egt', $start_unixtime],['lt', $end_unixtime], 'and'];
         } elseif ($start_unixtime > 0) {
         } elseif ($start_unixtime > 0) {
-            $condition['refill_order.order_time'] = ['egt', $start_unixtime];
-            $condition['vr_order.add_time'] = ['egt', $start_unixtime];
+            $cond['refill_order.order_time'] = ['egt', $start_unixtime];
+            $cond['vr_order.add_time'] = ['egt', $start_unixtime];
         } elseif ($end_unixtime > 0) {
         } elseif ($end_unixtime > 0) {
-            $condition['refill_order.order_time'] = ['lt', $end_unixtime];
+            $cond['refill_order.order_time'] = ['lt', $end_unixtime];
         } else {
         } else {
             $start = time() - 3600;
             $start = time() - 3600;
             $_GET['query_start_time'] = date("Y-m-d H:i:s",$start);
             $_GET['query_start_time'] = date("Y-m-d H:i:s",$start);
-            $condition['refill_order.order_time'] = ['egt', $start];
-            $condition['vr_order.add_time'] = ['egt', $start-1];
+            $cond['refill_order.order_time'] = ['egt', $start];
+            $cond['vr_order.add_time'] = ['egt', $start-1];
             $fToday = true;
             $fToday = true;
         }
         }
 
 
         if(in_array($_GET['cardno_state'], ['0','1', '2', '4', '5'])) {
         if(in_array($_GET['cardno_state'], ['0','1', '2', '4', '5'])) {
-            $condition['refill_order.cardno_state'] = $_GET['cardno_state'];
+            $cond['refill_order.cardno_state'] = $_GET['cardno_state'];
         }
         }
         if(in_array($_GET['is_transfer'], ['0','1'])) {
         if(in_array($_GET['is_transfer'], ['0','1'])) {
-            $condition['refill_order.is_transfer'] = $_GET['is_transfer'];
+            $cond['refill_order.is_transfer'] = $_GET['is_transfer'];
         }
         }
 
 
         if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
         if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40']))
         {
         {
-            $condition['vr_order.order_state'] = $_GET['order_state'];
+            $cond['vr_order.order_state'] = $_GET['order_state'];
 
 
             if ($_GET['order_state'] == ORDER_STATE_SEND) {
             if ($_GET['order_state'] == ORDER_STATE_SEND) {
                 if ($_GET['time'] == 1) {
                 if ($_GET['time'] == 1) {
-                    $condition['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
+                    $cond['refill_order.order_time'] = ['between', [(time() - 3600), (time() - 1800)]];
                 }
                 }
                 if ($_GET['time'] == 2) {
                 if ($_GET['time'] == 2) {
-                    $condition['refill_order.order_time'] = ['lt', (time() - 3600)];
+                    $cond['refill_order.order_time'] = ['lt', (time() - 3600)];
                 }
                 }
             }
             }
         }
         }
 
 
         if (!empty($_GET['export']) || !empty($_GET['export_stats'])) {
         if (!empty($_GET['export']) || !empty($_GET['export_stats'])) {
-            $this->RefillOrderExport($condition);
+            $this->RefillOrderExport($cond);
             exit;
             exit;
         }
         }
         if (!empty($_GET['mch_notify'])) {
         if (!empty($_GET['mch_notify'])) {
-            $this->mch_notify($condition);
+            $this->mch_notify($cond);
             exit;
             exit;
         }
         }
         if (!empty($_GET['order_query'])) {
         if (!empty($_GET['order_query'])) {
-            $this->updateOrderSend($condition);
+            $this->updateOrderSend($cond);
             exit;
             exit;
         }
         }
         $merchants = [];
         $merchants = [];
@@ -144,7 +146,7 @@ class refill_orderControl extends SystemControl
         foreach ($merchant_list as $value) {
         foreach ($merchant_list as $value) {
             $merchants[$value['mchid']] = $value;
             $merchants[$value['mchid']] = $value;
         }
         }
-        $order_list = $model_refill_order->getMerchantOrderList($condition, 200, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
+        $order_list = $model_refill_order->getMerchantOrderList($cond, 200, 'refill_order.*,vr_order.order_state', 'refill_order.order_time desc');
 
 
         foreach ($order_list as $order_id => $order_info) {
         foreach ($order_list as $order_id => $order_info) {
             $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
             $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
@@ -162,9 +164,15 @@ class refill_orderControl extends SystemControl
 
 
         $provider_list = $this->providers();
         $provider_list = $this->providers();
 
 
-        if ($fShowStat) {
-            $stat = $this->all_order_state_stat($condition);
-            Tpl::output('stat', $stat);
+        if ($fShowStat)
+        {
+            $manager = new task\manager();
+            $task = $manager->add_task('refill_order_stat',$cond,0);
+            if($task->completed() && $task->success())
+            {
+                $stat = $task->result();
+                Tpl::output('stat', $stat);
+            }
         }
         }
 
 
         $dispatcher_queue_length = refill\util::dispatcher_queue_length();
         $dispatcher_queue_length = refill\util::dispatcher_queue_length();

+ 24 - 1
crontab/control/minutes.php

@@ -15,6 +15,7 @@ require_once(BASE_HELPER_PATH . '/refill/divert_account.php');
 require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
 require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
 require_once(BASE_CORE_PATH . '/framework/function/http.php');
 require_once(BASE_CORE_PATH . '/framework/function/http.php');
 require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
 require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
+require_once(BASE_HELPER_PATH . '/task/task_helper.php');
 
 
 
 
 class minutesControl extends BaseCronControl
 class minutesControl extends BaseCronControl
@@ -33,7 +34,6 @@ class minutesControl extends BaseCronControl
         $this->_fetch_order_unavaliable();
         $this->_fetch_order_unavaliable();
         $this->_check_merchant_alarm_amount();
         $this->_check_merchant_alarm_amount();
 
 
-//        $this->_refill_task();
         Log::record(__FUNCTION__ . " end",Log::DEBUG);
         Log::record(__FUNCTION__ . " end",Log::DEBUG);
 
 
 //        $this->_web_index_update();
 //        $this->_web_index_update();
@@ -409,6 +409,29 @@ class minutesControl extends BaseCronControl
         }
         }
     }
     }
 
 
+    //任务队列处理
+    public function taskOp()
+    {
+        $task_manager = new task\manager();
+        $task_id = 0;
+        while (true)
+        {
+            try
+            {
+                $task = $task_manager->pop_task($task_id);
+                if (!empty($task)) {
+                    $task_manager->handle($task);
+                    $task_id = $task['task_id'];
+                }
+            }
+            catch (Exception $ex) {
+                Log::record($ex->getMessage(),Log::ERR);
+            }
+
+            sleep(1);
+        }
+    }
+
     /**
     /**
      * 执行通用任务
      * 执行通用任务
      */
      */

+ 72 - 73
data/model/refill_task.model.php

@@ -1,74 +1,73 @@
-<?php
-
-defined('InShopNC') or exit('Access Invalid!');
-
-class refill_taskModel extends Model
-{
-    const DisposeState = 2;
-    const FinishState = 3;
-    const ErrState = 4;
-
-    public function __construct()
-    {
-        parent::__construct('refill_task');
-    }
-
-    public function getList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '', $master = false): array
-    {
-        $condition['is_del'] = 0;
-        $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
-        if (empty($list)) return [];
-        return $list;
-    }
-
-    public function HashCheck($hash): bool
-    {
-        $condition['is_del'] = 0;
-        $condition['task_hash'] = $hash;
-        return empty($this->where($condition)->find());
-    }
-
-    public function UnDispose(): array
-    {
-        $condition['is_del'] = 0;
-        $condition['task_state'] = 1;
-        $task = $this->where($condition)->order('add_time asc')->find();
-        if (empty($task)) return [];
-        return $task;
-    }
-
-    public function Dispose($task_id)
-    {
-        $condition['is_del'] = 0;
-        $condition['task_id'] = $task_id;
-        return $this->where($condition)->update(
-            ['task_state' => self::DisposeState, 'dispose_time' => time()]
-        );
-    }
-
-    public function DisposeErr($task_id, $err_msg)
-    {
-        $condition['is_del'] = 0;
-        $condition['task_id'] = $task_id;
-        return $this->where($condition)->update(
-            ['task_state' => self::ErrState, 'task_result' => $err_msg, 'finish_time' => time()]
-        );
-    }
-
-    public function DisposeFinish($task_id, $result)
-    {
-        $condition['is_del'] = 0;
-        $condition['task_id'] = $task_id;
-        return $this->where($condition)->update(
-            ['task_state' => self::FinishState, 'task_result' => $result, 'finish_time' => time()]
-        );
-    }
-
-    public function Del($task_id)
-    {
-        $condition['task_id'] = $task_id;
-        return $this->where($condition)->update(
-            ['is_del' => 1]
-        );
-    }
+<?php
+
+defined('InShopNC') or exit('Access Invalid!');
+
+class taskModel extends Model
+{
+    const DisposeState = 2;
+    const FinishState = 3;
+    const ErrState = 4;
+
+    public function __construct()
+    {
+        parent::__construct('task');
+    }
+
+    public function getList($condition, $pagesize = '', $field = '*', $order = 'add_time desc', $limit = '', $master = false): array
+    {
+        $list = $this->field($field)->where($condition)->page($pagesize)->order($order)->limit($limit)->master($master)->select();
+        if (empty($list)) return [];
+        return $list;
+    }
+
+    public function getByHash($hash)
+    {
+        $condition['task_hash'] = $hash;
+        return $this->where($condition)->find();
+    }
+
+
+    public function UnDispose(): array
+    {
+        $condition['is_del'] = 0;
+        $condition['task_state'] = 1;
+        $task = $this->where($condition)->order('add_time asc')->find();
+        if (empty($task)) return [];
+        return $task;
+    }
+
+    public function Dispose($task_id)
+    {
+        $condition['is_del'] = 0;
+        $condition['task_id'] = $task_id;
+        return $this->where($condition)->update(
+            ['task_state' => self::DisposeState, 'dispose_time' => time()]
+        );
+    }
+
+    public function DisposeErr($task_id, $err_msg)
+    {
+        $condition['is_del'] = 0;
+        $condition['task_id'] = $task_id;
+        return $this->where($condition)->update(
+            ['task_state' => self::ErrState, 'task_result' => $err_msg, 'finish_time' => time()]
+        );
+    }
+
+    public function DisposeFinish($task_id, $result)
+    {
+        $condition['is_del'] = 0;
+        $condition['task_id'] = $task_id;
+        return $this->where($condition)->update(
+            ['task_state' => self::FinishState, 'task_result' => $result, 'finish_time' => time()]
+        );
+    }
+
+    public function Del($task_id)
+    {
+        $condition['task_id'] = $task_id;
+        return $this->where($condition)->update(
+            ['is_del' => 1]
+        );
+    }
 }
 }

+ 12 - 1
docker/compose/stanley/master-cron/docker-compose.yml

@@ -33,4 +33,15 @@ services:
       - ../conf/etc/localtime:/etc/localtime:ro
       - ../conf/etc/localtime:/etc/localtime:ro
       - ../conf/php/php-debug.ini:/usr/local/etc/php/php.ini
       - ../conf/php/php-debug.ini:/usr/local/etc/php/php.ini
     container_name: "panda-accedit"
     container_name: "panda-accedit"
-    command: [php,"/var/www/html/crontab/index.php",'minutes','account_edit']
+    command: [php,"/var/www/html/crontab/index.php",'minutes','account_edit']
+
+  crontask:
+    image: php-zts-debug:7.3.18
+    volumes:
+      - ../../../../:/var/www/html
+      - /Volumes/Transcend/upload:/var/www/html/data/upload
+      - /Users/stanley-king/work/PHPProject/shoplog:/var/www/html/data/log
+      - ../conf/etc/localtime:/etc/localtime:ro
+      - ../conf/php/php-debug.ini:/usr/local/etc/php/php.ini
+    container_name: "panda-task"
+    command: [php,"/var/www/html/crontab/index.php",'minutes','task']

+ 13 - 1
docker/compose/xyz/slave-crond/docker-compose.yml

@@ -11,4 +11,16 @@ services:
       - /nfs/upload:/var/www/html/data/upload
       - /nfs/upload:/var/www/html/data/upload
       - /mnt/shoplog:/var/www/html/data/log
       - /mnt/shoplog:/var/www/html/data/log
     container_name: "panda-slavecron"
     container_name: "panda-slavecron"
-    command: [crond,"-f"]
+    command: [crond,"-f"]
+
+  crontask:
+    image: php-zts-debug:7.3.18
+    volumes:
+      - ../../../../:/var/www/html
+      - ../conf/etc/localtime:/etc/localtime:ro
+      - ../conf/php/php.ini:/usr/local/etc/php/php.ini
+      - ../conf/crontab/slave_root:/var/spool/cron/crontabs/root
+      - /nfs/upload:/var/www/html/data/upload
+      - /mnt/shoplog:/var/www/html/data/log
+    container_name: "panda-task"
+    command: [php,"/var/www/html/crontab/index.php",'minutes','task']

+ 56 - 0
helper/task/handler.php

@@ -0,0 +1,56 @@
+<?php
+
+
+namespace task;
+
+use Exception;
+
+class handler
+{
+    public function refill_order_stat($condition)
+    {
+        try
+        {
+            $items = Model('')->table('refill_order,vr_order')
+                ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
+                ->join('inner')
+                ->on('refill_order.order_id=vr_order.order_id')
+                ->where($condition)
+                ->group('order_state')
+                ->select();
+
+            $all = [];
+            $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
+            $sending = $success = $cancel = $data;
+            foreach ($items as $item)
+            {
+                if ($item['order_state'] == ORDER_STATE_SEND) {
+                    $sending = $item;
+                } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
+                    $success = $item;
+                } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
+                    $cancel = $item;
+                }
+
+                $all['order_count'] += $item['order_count'];
+                $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
+                $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
+                $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
+            }
+
+            $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
+
+            return [true,$result];
+
+        }
+        catch (Exception $ex)
+        {
+            return [false,false];
+        }
+    }
+
+    public function refill_order_stat_title($condition)
+    {
+        return md5("refill_order_stat-".serialize($condition));
+    }
+}

+ 105 - 0
helper/task/manager.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace task;
+
+use Exception;
+use trans_wapper;
+
+class manager
+{
+    private $mHandler;
+    public function __construct()
+    {
+        $this->mHandler = new handler();
+    }
+
+    public function add_task($method,$params,$is_show = 1)
+    {
+        if(empty($method) || empty($params)) {
+            return false;
+        }
+
+        //task 唯一性
+        $hash_fun = function ($method,$params) {
+            $val = $method . '-' . serialize($params);
+            return md5($val);
+        };
+        $task_hash = $hash_fun($method,$params);
+
+        $title_fun = "{$method}_title";
+        $title = $this->mHandler->$title_fun($params);
+
+        $mod_task = Model('task');
+        $item = $mod_task->getByHash($task_hash);
+        if(empty($item))
+        {
+            $item = ['type' => $method, 'title' => $title,
+                     'params' => serialize($params), 'task_hash' => $task_hash,
+                     'add_time' => time(), 'is_show' => $is_show];
+            $task_id = $mod_task->insert($item);
+            $item = $mod_task->where(['task_id' => $task_id])->find();
+        }
+
+        return task_wrapper::CreateTask($item);
+    }
+
+    //弹出一个任务,并且将任务设为进行中
+    public function pop_task($task_id)
+    {
+        $mod_task = Model('task');
+        $trans = new trans_wapper($mod_task,'pop_task');
+
+        try {
+            $item = $mod_task->field('*')
+                             ->where(['task_id' => ['gt',$task_id],'state' => task_wrapper::Waiting])
+                             ->master(true)->lock(true)->find();
+
+            if(!empty($item)) {
+                $mod_task->where(['task_id' => $item['task_id']])->update(['state' => task_wrapper::Processing,'act_time' => time()]);
+            }
+            $trans->commit();
+
+            return $item;
+        }
+        catch (Exception $ex)
+        {
+            $trans->rollback();
+            return [];
+        }
+    }
+
+    //任务结束,并且
+    public function fini_task($task_id,$succ)
+    {
+
+    }
+
+    public function del_task()
+    {
+
+    }
+
+    public function getResult($task)
+    {
+
+    }
+
+    public function handle($task)
+    {
+        $method = $task['type'];
+        $params = unserialize($task['params']);
+
+        $handler = new handler();
+        [$succ,$result] = $handler->$method($params);
+
+        $mod_task = Model('task');
+        if(!empty($result))
+        {
+            $mod_task->where(['task_id' => $task['task_id']])->update(['state' => task_wrapper::Handled,
+                'result_state' => $succ ? 1 : 0, 'result' => serialize($result),
+                'finish_time' => time()]);
+        }
+
+        return $task['task_id'];
+    }
+}

+ 7 - 0
helper/task/task_helper.php

@@ -0,0 +1,7 @@
+<?php
+
+namespace task;
+
+require_once(BASE_HELPER_PATH . '/task/manager.php');
+require_once(BASE_HELPER_PATH . '/task/handler.php');
+require_once(BASE_HELPER_PATH . '/task/task_wrapper.php');

+ 42 - 0
helper/task/task_wrapper.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace task;
+
+
+class task_wrapper
+{
+    //1待处理、2处理中、3已处理、4已经取消'
+    const Waiting = 1;
+    const Processing = 2;
+    const Handled = 3;
+    const Canceled = 4;
+
+    private $mRecord;
+    public function __construct($item)
+    {
+        $this->mRecord = $item;
+    }
+
+    public function completed() : bool
+    {
+        return $this->mRecord['state'] == self::Handled;
+    }
+
+    public function success() : bool {
+        return $this->mRecord['result_state'] == 1;
+    }
+
+    public function result()
+    {
+        if($this->success()) {
+            return unserialize($this->mRecord['result']);
+        } else {
+            return [];
+        }
+    }
+
+    public static function CreateTask($data)
+    {
+        return new task_wrapper($data);
+    }
+}

+ 5 - 6
test/TestTask.php

@@ -16,6 +16,11 @@ class TestTask extends TestCase
         Base::run_util();
         Base::run_util();
     }
     }
 
 
+    public function testAddTask()
+    {
+
+    }
+
     public function testXRange()
     public function testXRange()
     {
     {
         $xrange = function ($start, $end, $step = 1) {
         $xrange = function ($start, $end, $step = 1) {
@@ -63,11 +68,5 @@ class TestTask extends TestCase
         Log::record($ret,Log::DEBUG);
         Log::record($ret,Log::DEBUG);
         $ret = $gen->send('ret2');
         $ret = $gen->send('ret2');
         Log::record($ret,Log::DEBUG);
         Log::record($ret,Log::DEBUG);
-
-        //var_dump($gen->send('ret1')); // string(4) "ret1"   (the first var_dump in gen)
-        // string(6) "yield2" (the var_dump of the ->send() return value)
-//        var_dump($gen->send('ret2')); // string(4) "ret2"   (again from within gen)
-        // NULL               (the return value of ->send())
-
     }
     }
 }
 }