ActivityOrderRebateSelfLevel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "{{%activity_rebate_order_self_level}}".
  13. *
  14. * @property integer $id
  15. */
  16. class ActivityOrderRebateSelfLevel extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%activity_rebate_order_self_level}}';
  24. }
  25. public function behaviors()
  26. {
  27. return [
  28. [
  29. 'class' => TimestampBehavior::class,
  30. ]
  31. ];
  32. }
  33. public function beforeSave($insert) {
  34. if(parent::beforeSave($insert)){
  35. $query = self::find()->where(['level' => $this->level, 'status' => 1, 'store_id' => $this->store_id,'is_delete' => 0]);
  36. $has = $query->andWhere([
  37. '!=', 'id', (int)$this->id
  38. ])->one();
  39. if($has){
  40. $this->addError('level', '操作失败,等级已存在,等级id:' . $has->id);
  41. return false;
  42. }
  43. $al1 = self::find()->where(['store_id' => $this->store_id, 'status' => 1,'is_delete' => 0])->andWhere(['<', 'level', $this->level])->andWhere(['!=', 'level', $this->getOldAttribute('level')])->orderBy('level DESC')->limit(1)->one();
  44. if($al1 && $this->money <= $al1->money && $this->child_num <= $al1->child_num && $this->child_level <= $al1->child_level){
  45. $this->addError('level', '操作失败,等级升级规则错误(金额、下级数量,下级等级,不能低于上一个等级),对比等级:' . $al1->level);
  46. return false;
  47. }
  48. $al2 = self::find()->where(['store_id' => $this->store_id, 'status' => 1,'is_delete' => 0])->andWhere(['>', 'level', $this->level])->andWhere(['!=', 'level', $this->getOldAttribute('level')])->orderBy('level ASC')->limit(1)->one();
  49. if($al2 && $this->money >= $al2->money && $this->child_num >= $al2->child_num && $this->child_level >= $al2->child_level){
  50. $this->addError('level', '操作失败,等级升级规则错误(金额、下级数量,下级等级,不能高于下一个等级),对比等级:' . $al2->level);
  51. return false;
  52. }
  53. if(!$insert && (
  54. ($this->getOldAttribute('level') != $this->level)
  55. || ($this->getOldAttribute('status') != $this->status && $this->status == 0)
  56. || ($this->getOldAttribute('is_delete') != $this->is_delete && $this->is_delete == 1)
  57. )){
  58. $count = ActivityOrderRebateSelfUser::find()->where(['store_id' => $this->store_id, 'level' => $this->getOldAttribute('level')])->count();
  59. if($count > 0) {
  60. $this->addError('level', '操作失败,当前等级下有会员,禁止修改等级,会员数量' . $count);
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. return false;
  67. }
  68. }