BookingOrderExt.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\utils\Notice\NoticeSend;
  9. use Yii;
  10. use yii\behaviors\TimestampBehavior;
  11. use yii\db\ActiveRecord;
  12. /**
  13. * This is the model class for table "{{%booking_order_ext}}".
  14. *
  15. * @property integer $id
  16. */
  17. class BookingOrderExt extends \yii\db\ActiveRecord
  18. {
  19. const IGNORE_ORDER_IDS = 'IGNORE_ORDER_IDS';
  20. const STATUS_EXT_WAIT_PAY = 5; //待支付
  21. const STATUS_EXT_WAIT_SYS_CONFIRM = 10; //待系统确认
  22. const STATUS_EXT_HAS_SYS_CONFIRM = 20; //系统确认/待开始
  23. const STATUS_EXT_START = 50; //进行中
  24. const STATUS_EXT_CANCEL = 70; //取消
  25. const STATUS_EXT_FINISH = 100; //完成
  26. /**
  27. * @inheritdoc
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%booking_order_ext}}';
  32. }
  33. public function behaviors()
  34. {
  35. return [];
  36. }
  37. public static function afterOrderSave($insert, $changedAttributes, $order) {
  38. try{
  39. if($order->order_type != 2){
  40. return;
  41. }
  42. self::statusAfterOrderSave($insert, $changedAttributes, $order);
  43. } catch (\Exception $ex) {
  44. //debug_log([__METHOD__, $insert, $changedAttributes, $ex->getMessage()], __CLASS__ . '.log');
  45. }
  46. }
  47. public static function statusAfterOrderSave($insert, $changedAttributes, $order) {
  48. //debug_log([__METHOD__, $insert, isset($changedAttributes['is_pay']), $order->is_pay, $changedAttributes, $order], __CLASS__ . '.log');
  49. if($insert){
  50. $self = new self();
  51. $self->order_id = $order->id;
  52. $self->store_id = $order->store_id;
  53. $save = $self->save();
  54. if(!$save){
  55. //debug_log([__METHOD__, $self->getErrors()], __CLASS__ . '.log');
  56. }
  57. }
  58. if(isset($changedAttributes['is_pay']) && ($order->is_pay == 1)){
  59. //debug_log([__METHOD__, 'is_pay', $order->is_pay, $order->pay_price, $order->id], __CLASS__ . '.log');
  60. // if($order->pay_price == 0){
  61. // self::updateAll(['status_ext' => self::STATUS_EXT_HAS_SYS_CONFIRM], ['order_id' => $order->id, 'status_ext' => self::STATUS_EXT_WAIT_PAY]);
  62. // }else{
  63. self::updateAll(['status_ext' => self::STATUS_EXT_WAIT_SYS_CONFIRM], ['order_id' => $order->id, 'status_ext' => self::STATUS_EXT_WAIT_PAY]);
  64. // }
  65. }
  66. if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CANCEL)){
  67. self::updateAll(['status_ext' => self::STATUS_EXT_CANCEL], ['order_id' => $order->id]);
  68. }
  69. if(isset($changedAttributes['trade_status']) && ($order->trade_status == Order::ORDER_FLOW_CONFIRM)){
  70. self::updateAll(['status_ext' => self::STATUS_EXT_FINISH], ['order_id' => $order->id]);
  71. }
  72. if(isset($changedAttributes['clerk_id']) && ($order->clerk_id > 0)){
  73. $user = User::findOne($order->clerk_id);
  74. $saasUser = SaasUser::findOne(['mobile' => $user['binding']]);
  75. if($user && $saasUser){
  76. $md_staff = MdStaff::findOne(['saas_user_id' => $saasUser['id'], 'store_id' => $order->store_id]);
  77. if($md_staff){
  78. self::updateAll(['worker_id' => $md_staff['id'], 'worker_name' => $md_staff['name'], 'time_has_bind' => time()], ['order_id' => $order->id]);
  79. }
  80. }
  81. }
  82. // //debug_log(['statusAfterOrderSave', $insert, $changedAttributes, $addr], __CLASS__ . '.log');
  83. }
  84. public static function afterOrderDetailSave($insert, $changedAttributes, $orderDetail) {
  85. try{
  86. $attr = json_decode($orderDetail['attr'], true);
  87. self::updateAll(['booking_time_start' => $attr['start_date'], 'booking_time_end' => $attr['end_date']], ['order_id' => $orderDetail->order_id]);
  88. } catch (\Exception $ex) {
  89. //debug_log([__METHOD__, $insert, $changedAttributes, $ex->getMessage()], __CLASS__ . '.log');
  90. }
  91. }
  92. public function setWorker($worker_id, $worker_name) {
  93. $this->worker_id = $worker_id;
  94. $this->worker_name = $worker_name;
  95. $this->time_has_bind = time();
  96. NoticeSend::bookExamine($this->order_id, 1);
  97. return $this;
  98. }
  99. public static function getWorkerOrderGroupStatus($worker_id) {
  100. $order_count = self::find()->where(['worker_id' => $worker_id])
  101. ->andWhere(['!=', 'status_ext', self::STATUS_EXT_CANCEL])
  102. ->select('count(1) cc, status_ext')
  103. ->groupBy('status_ext')->asArray()->all();
  104. return $order_count;
  105. }
  106. }