Worker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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}}".
  13. *
  14. * @property integer $id
  15. * @property integer $store_id
  16. * @property integer $cat_id
  17. * @property integer $user_id
  18. * @property integer $saas_user_id
  19. * @property integer $level
  20. * @property string $name
  21. * @property string $desc
  22. * @property string $tel
  23. * @property string $logo
  24. * @property integer $is_delete
  25. * @property integer $book_start_time
  26. * @property integer $book_end_time
  27. * @property integer $open_status
  28. * @property integer $status
  29. * @property string $reason
  30. * @property string $area
  31. * @property integer $created_at
  32. * @property integer $updated_at
  33. * @property string $lat
  34. * @property string $lng
  35. * @property string $form
  36. * @property integer $province_id
  37. * @property integer $city_id
  38. * @property integer $district_id
  39. */
  40. class Worker extends \yii\db\ActiveRecord
  41. {
  42. const STATUS_WAIT_AUDIT = 0; //未审核
  43. const STATUS_VALID = 1; //已审核
  44. const STATUS_REJECT = 2; //已拒绝
  45. const STATUS_CLOSE = 3; //禁用
  46. /**
  47. * @inheritdoc
  48. */
  49. public static function tableName()
  50. {
  51. return '{{%worker}}';
  52. }
  53. public function behaviors()
  54. {
  55. return [
  56. [
  57. 'class' => TimestampBehavior::class,
  58. ]
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function rules()
  65. {
  66. return [
  67. [['id', 'store_id', 'cat_id', 'user_id', 'saas_user_id', 'level', 'is_delete', 'open_status', 'status', 'province_id', 'city_id', 'district_id'], 'integer'],
  68. [['created_at', 'updated_at', 'lat', 'lng'], 'safe'],
  69. [['name', 'desc', 'tel', 'logo', 'reason', 'area', 'form'], 'string'],
  70. [['name'], 'required'],
  71. ['name', 'string', 'min' => 2, 'max' => 12],
  72. ['tel', 'string', 'min' => 11, 'max' => 11],
  73. ['open_status', 'integer', 'min' => 0, 'max' => 1],
  74. ['book_start_time', 'compare', 'compareAttribute' => 'book_end_time', 'operator' => '<='],
  75. ['book_end_time', 'compare', 'compareValue' => '2359', 'operator' => '<='],
  76. ];
  77. }
  78. public function beforeSave($insert)
  79. {
  80. if (parent::beforeSave($insert)) {
  81. if($this->user_id > 0){
  82. if($this->isNewRecord){
  83. $is = self::find()->where(['user_id' => $this->user_id, 'status' => self::STATUS_VALID])->one();
  84. }else{
  85. $is = self::find()->where(['user_id' => $this->user_id, 'status' => self::STATUS_VALID])->andWhere(['!=', 'id', $this->id])->one();
  86. }
  87. if($is){
  88. $this->addError('user_id', '操作失败,此用户[****'. mb_substr($is->name, -1) . $is->id .']已存在!');
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. return false;
  95. }
  96. public static function afterOrderSave($insert, $changedAttributes, $order) {
  97. try{
  98. if($order->order_type != Order::ORDER_TYPE_WORKER){
  99. return;
  100. }
  101. WorkerOrderExt::afterOrderSave($insert, $changedAttributes, $order);
  102. } catch (\Exception $ex) {
  103. //debug_log(['afterOrderSave', $insert, $changedAttributes, $ex], __CLASS__ . '.log');
  104. }
  105. }
  106. public function getLevelInfo()
  107. {
  108. return $this->hasOne(WorkerLevel::class, ['level' => 'level'])->andWhere(['store_id' => $this->store_id]);
  109. }
  110. public function getPics()
  111. {
  112. return $this->hasMany(WorkerPic::class, ['worker_id' => 'id'])->andWhere(['is_delete' => 0]);
  113. }
  114. public function getGoods()
  115. {
  116. return $this->hasMany(WorkerGoods::class, ['worker_id' => 'id'])->andWhere(['status' => 1]);
  117. }
  118. public function showBookTime()
  119. {
  120. $this->book_start_time = self::formatTime($this->book_start_time);
  121. $this->book_end_time = self::formatTime($this->book_end_time);
  122. }
  123. public static function formatTime($time){
  124. $time = str_pad($time, 4, 0, STR_PAD_LEFT);
  125. return substr($time, -4, 2) . ':' . substr($time, -2);
  126. }
  127. public static function getStatusName($status){
  128. $arr = [
  129. self::STATUS_WAIT_AUDIT => '未审核',
  130. self::STATUS_VALID => '已审核',
  131. self::STATUS_REJECT => '已拒绝',
  132. self::STATUS_CLOSE => '已禁用',
  133. ];
  134. return $arr[$status] ?? '';
  135. }
  136. }