order_id); \Yii::error([__METHOD__, $this, $this->oldAttributes['worker_id'], $this->attributes['worker_id']]); if($order){ $up_worker_id = $this->oldAttributes['worker_id'] != $this->attributes['worker_id']; if($up_worker_id){ $profit = self::getWorkerProfit($order, $this->worker_id); $this->worker_price = $profit; } } return true; } return false; } public static function afterOrderSave($insert, $changedAttributes, $order) { self::statusAfterOrderSave($insert, $changedAttributes, $order); } public static function statusAfterOrderSave($insert, $changedAttributes, $order) { if($insert){ $self = new self(); $self->order_id = $order->id; $self->store_id = $order->store_id; $addr = json_decode($order['address_data'], true); if($addr){ $self->lat = $addr['latitude']; $self->lng = $addr['longitude']; } $save = $self->save(); if(!$save){ //debug_log([$self->getErrors()], __CLASS__ . '.log'); } } if(isset($changedAttributes['is_pay']) && ($order->is_pay == 1)){ self::updateAll(['status_ext' => self::STATUS_EXT_WAIT_BIND], ['order_id' => $order->id, 'worker_id' => 0]); self::updateAll(['status_ext' => self::STATUS_EXT_WAIT_BIND_OK], ['and', ['order_id' => $order->id], ['>', 'worker_id', 0]]); if($order['order_type_ext'] == Order::ORDER_TYPE_EXT_WORKER_ADD_TIME){ self::updateAll(['status_ext' => self::STATUS_EXT_START, 'time_start_service' => time()], ['order_id' => $order->id]); } if($order['order_type_ext'] == Order::ORDER_TYPE_EXT_WORKER_LAST_PAY){ self::updateAll(['status_ext' => self::STATUS_EXT_FINISH, 'time_end_service' => time()], ['order_id' => $order->id]); Order::updateAll(['trade_status' => Order::ORDER_FLOW_CONFIRM, 'confirm_time' => time()], ['id' => $order->id]); } } if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CANCEL)){ self::updateAll(['status_ext' => self::STATUS_EXT_CANCEL], ['order_id' => $order->id]); } if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CONFIRM)){ self::updateAll(['status_ext' => self::STATUS_EXT_FINISH], ['order_id' => $order->id]); } if(!$insert && isset($changedAttributes['address_data']) && $order['address_data']){ $addr = json_decode($order['address_data'], true); if($addr){ self::updateAll(['lat' => $addr['latitude'], 'lng' => $addr['longitude']], ['order_id' => $order->id]); } } // //debug_log(['statusAfterOrderSave', $insert, $changedAttributes, $addr], __CLASS__ . '.log'); } public static function getWorkerOrderGroupStatus($worker_id) { $order_count = self::find()->where(['worker_id' => $worker_id]) ->andWhere(['!=', 'status_ext', self::STATUS_EXT_CANCEL]) ->select('count(1) cc, status_ext') ->groupBy('status_ext')->asArray()->all(); return $order_count; } public static function cacheKeyIgnoreOrderIds($worker_id) { return self::IGNORE_ORDER_IDS . $worker_id; } public static function cacheIgnoreOrderId($worker_id, $order_id = null) { $cacheK = self::cacheKeyIgnoreOrderIds($worker_id); $cacheV = cache()->get($cacheK); if($cacheV){ foreach ($cacheV as $i => $item) { if($item[1] < (time() - 86400)){ unset($cacheV[$i]); } } } if($order_id && empty($cacheV[$order_id])){ $cacheV[$order_id] = [$order_id, time()]; cache()->set($cacheK, $cacheV, 86400); } return $cacheV; } public static function getWorkerProfit($order, $worker_id = 0) { if(!$worker_id || ($order['pay_price'] == 0)){ return 0; } $w = Worker::findOne($worker_id); if(!$w){ return 0; } $wl = WorkerLevel::findOne(['store_id' => $w['store_id'], 'level' => $w['level'], 'status' => 1, 'is_delete' => 0]); if(!$wl){ return 0; } $profit = floatval($order['pay_price'] * $wl['profit_rate'] / 100); return $profit; } }