| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%worker_order_ext}}".
- *
- * @property integer $id
- */
- class WorkerOrderExt extends \yii\db\ActiveRecord
- {
-
- const IGNORE_ORDER_IDS = 'IGNORE_ORDER_IDS';
-
- const STATUS_EXT_WAIT_PAY = 5; //待支付
- const STATUS_EXT_WAIT_BIND = 10; //待分配/待抢单
- const STATUS_EXT_WAIT_BIND_OK = 15; //待接单
- const STATUS_EXT_HAS_BIND = 20; //已分配/待出发
- const STATUS_EXT_ON_ROAD = 30; //已出发
- const STATUS_EXT_ARRIVE = 40; //已到达/待服务
- const STATUS_EXT_START = 50; //进行中
- const STATUS_EXT_CANCEL = 70; //取消
- const STATUS_EXT_FINISH = 100; //完成
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%worker_order_ext}}';
- }
- public function behaviors()
- {
- return [];
- }
-
- public function beforeSave($insert) {
- if(parent::beforeSave($insert)){
- $order = Order::findOne($this->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;
- }
- }
|