WorkerOrderExt.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\ActiveRecord;
  11. /**
  12. * This is the model class for table "{{%worker_order_ext}}".
  13. *
  14. * @property integer $id
  15. */
  16. class WorkerOrderExt extends \yii\db\ActiveRecord
  17. {
  18. const IGNORE_ORDER_IDS = 'IGNORE_ORDER_IDS';
  19. const STATUS_EXT_WAIT_PAY = 5; //待支付
  20. const STATUS_EXT_WAIT_BIND = 10; //待分配/待抢单
  21. const STATUS_EXT_WAIT_BIND_OK = 15; //待接单
  22. const STATUS_EXT_HAS_BIND = 20; //已分配/待出发
  23. const STATUS_EXT_ON_ROAD = 30; //已出发
  24. const STATUS_EXT_ARRIVE = 40; //已到达/待服务
  25. const STATUS_EXT_START = 50; //进行中
  26. const STATUS_EXT_CANCEL = 70; //取消
  27. const STATUS_EXT_FINISH = 100; //完成
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%worker_order_ext}}';
  34. }
  35. public function behaviors()
  36. {
  37. return [];
  38. }
  39. public function beforeSave($insert) {
  40. if(parent::beforeSave($insert)){
  41. $order = Order::findOne($this->order_id);
  42. \Yii::error([__METHOD__, $this, $this->oldAttributes['worker_id'], $this->attributes['worker_id']]);
  43. if($order){
  44. $up_worker_id = $this->oldAttributes['worker_id'] != $this->attributes['worker_id'];
  45. if($up_worker_id){
  46. $profit = self::getWorkerProfit($order, $this->worker_id);
  47. $this->worker_price = $profit;
  48. }
  49. }
  50. return true;
  51. }
  52. return false;
  53. }
  54. public static function afterOrderSave($insert, $changedAttributes, $order) {
  55. self::statusAfterOrderSave($insert, $changedAttributes, $order);
  56. }
  57. public static function statusAfterOrderSave($insert, $changedAttributes, $order) {
  58. if($insert){
  59. $self = new self();
  60. $self->order_id = $order->id;
  61. $self->store_id = $order->store_id;
  62. $addr = json_decode($order['address_data'], true);
  63. if($addr){
  64. $self->lat = $addr['latitude'];
  65. $self->lng = $addr['longitude'];
  66. }
  67. $save = $self->save();
  68. if(!$save){
  69. //debug_log([$self->getErrors()], __CLASS__ . '.log');
  70. }
  71. }
  72. if(isset($changedAttributes['is_pay']) && ($order->is_pay == 1)){
  73. self::updateAll(['status_ext' => self::STATUS_EXT_WAIT_BIND], ['order_id' => $order->id, 'worker_id' => 0]);
  74. self::updateAll(['status_ext' => self::STATUS_EXT_WAIT_BIND_OK], ['and', ['order_id' => $order->id], ['>', 'worker_id', 0]]);
  75. if($order['order_type_ext'] == Order::ORDER_TYPE_EXT_WORKER_ADD_TIME){
  76. self::updateAll(['status_ext' => self::STATUS_EXT_START, 'time_start_service' => time()], ['order_id' => $order->id]);
  77. }
  78. if($order['order_type_ext'] == Order::ORDER_TYPE_EXT_WORKER_LAST_PAY){
  79. self::updateAll(['status_ext' => self::STATUS_EXT_FINISH, 'time_end_service' => time()], ['order_id' => $order->id]);
  80. Order::updateAll(['trade_status' => Order::ORDER_FLOW_CONFIRM, 'confirm_time' => time()], ['id' => $order->id]);
  81. }
  82. }
  83. if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CANCEL)){
  84. self::updateAll(['status_ext' => self::STATUS_EXT_CANCEL], ['order_id' => $order->id]);
  85. }
  86. if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CONFIRM)){
  87. self::updateAll(['status_ext' => self::STATUS_EXT_FINISH], ['order_id' => $order->id]);
  88. }
  89. if(!$insert && isset($changedAttributes['address_data']) && $order['address_data']){
  90. $addr = json_decode($order['address_data'], true);
  91. if($addr){
  92. self::updateAll(['lat' => $addr['latitude'], 'lng' => $addr['longitude']], ['order_id' => $order->id]);
  93. }
  94. }
  95. // //debug_log(['statusAfterOrderSave', $insert, $changedAttributes, $addr], __CLASS__ . '.log');
  96. }
  97. public static function getWorkerOrderGroupStatus($worker_id) {
  98. $order_count = self::find()->where(['worker_id' => $worker_id])
  99. ->andWhere(['!=', 'status_ext', self::STATUS_EXT_CANCEL])
  100. ->select('count(1) cc, status_ext')
  101. ->groupBy('status_ext')->asArray()->all();
  102. return $order_count;
  103. }
  104. public static function cacheKeyIgnoreOrderIds($worker_id) {
  105. return self::IGNORE_ORDER_IDS . $worker_id;
  106. }
  107. public static function cacheIgnoreOrderId($worker_id, $order_id = null) {
  108. $cacheK = self::cacheKeyIgnoreOrderIds($worker_id);
  109. $cacheV = cache()->get($cacheK);
  110. if($cacheV){
  111. foreach ($cacheV as $i => $item) {
  112. if($item[1] < (time() - 86400)){
  113. unset($cacheV[$i]);
  114. }
  115. }
  116. }
  117. if($order_id && empty($cacheV[$order_id])){
  118. $cacheV[$order_id] = [$order_id, time()];
  119. cache()->set($cacheK, $cacheV, 86400);
  120. }
  121. return $cacheV;
  122. }
  123. public static function getWorkerProfit($order, $worker_id = 0) {
  124. if(!$worker_id || ($order['pay_price'] == 0)){
  125. return 0;
  126. }
  127. $w = Worker::findOne($worker_id);
  128. if(!$w){
  129. return 0;
  130. }
  131. $wl = WorkerLevel::findOne(['store_id' => $w['store_id'], 'level' => $w['level'], 'status' => 1, 'is_delete' => 0]);
  132. if(!$wl){
  133. return 0;
  134. }
  135. $profit = floatval($order['pay_price'] * $wl['profit_rate'] / 100);
  136. return $profit;
  137. }
  138. }