最新服务器上的版本,以后用这个
commit | author | age
2207d6 1 <?php
W 2 /**
3  * @copyright ©2018 浙江禾匠信息科技
4  * @author Lu Wei
5  * @link http://www.zjhejiang.com/
6  * Created by IntelliJ IDEA
7  * Date Time: 2019/2/14 9:51
8  */
9
10
11 namespace app\jobs;
12
13
14 use app\events\OrderEvent;
15 use app\models\Mall;
16 use app\models\Model;
17 use app\models\Order;
18 use yii\base\BaseObject;
19 use yii\queue\JobInterface;
20 use yii\queue\Queue;
21
22 class OrderCancelJob extends BaseJob implements JobInterface
23 {
24     public $orderId;
25
26     /**
27      * @param Queue $queue which pushed and is handling the job
28      */
29     public function execute($queue)
30     {
31         $this->setRequest();
32         $order = Order::findOne([
33             'id' => $this->orderId,
34             'is_pay' => 0,
35             'pay_type' => 0,
36             'is_delete' => 0,
37         ]);
38         if (!$order) {
39             return;
40         }
41         if ($order->cancel_status == 1) {
42             return ;
43         }
44         \Yii::warning('----订单自动取消----');
45         $mall = Mall::findOne(['id' => $order->mall_id]);
46         \Yii::$app->setMall($mall);
47         $t = \Yii::$app->db->beginTransaction();
48         try {
49             $order->cancel_status = 1;
50             $order->cancel_time = mysql_timestamp();
51             if ($order->save()) {
52                 $event = new OrderEvent([
53                     'order' => $order,
54                 ]);
55                 \Yii::$app->trigger(Order::EVENT_CANCELED, $event);
56                 $t->commit();
57             } else {
58                 throw new \Exception((new Model())->getErrorMsg($order));
59             }
60         } catch (\Exception $exception) {
61             $t->rollBack();
62         }
63     }
64 }