ActivityOrderRebateSelf.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_n}}".
  13. *
  14. * @property integer $id
  15. */
  16. class ActivityOrderRebateSelf extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%activity_rebate_order_self}}';
  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()->alias('a')->leftJoin(['ag' => ActivityOrderRebateSelfGoods::tableName()], 'a.id = ag.act_id');
  36. $goods_ids = explode(',', $this->goods_ids);
  37. $this->goods_ids = implode(',', array_unique($goods_ids));
  38. $query->andWhere([
  39. 'a.store_id' => $this->store_id,
  40. 'ag.goods_id' => $goods_ids,
  41. 'a.is_delete' => 0,
  42. 'a.status' => 1,
  43. ]);
  44. $query->andWhere([
  45. 'and',
  46. ['<', 'a.start_time', $this->end_time],
  47. ['>', 'a.end_time', $this->start_time],
  48. ]);
  49. $has = $query->andWhere([
  50. '!=', 'a.id', (int)$this->id
  51. ])->one();
  52. if($has){
  53. $hasGoodIds = explode(',', $has->goods_ids);
  54. $this->addError('goods_id', '操作失败,商品已存在活动信息,活动id:' . $has->id . ' 商品id:' . implode(',', array_intersect($goods_ids, $hasGoodIds)));
  55. return false;
  56. }
  57. return true;
  58. }
  59. return false;
  60. }
  61. public static function activityAt($store_id = 0, $goods_id = 0, $time = null) {
  62. if($time === null){
  63. $time = time();
  64. }
  65. $query = self::find()->alias('a')->leftJoin(['ag' => ActivityOrderRebateSelfGoods::tableName()], 'a.id = ag.act_id');
  66. $query->andWhere([
  67. 'a.store_id' => $store_id,
  68. 'ag.goods_id' => $goods_id,
  69. 'a.is_delete' => 0,
  70. 'a.status' => 1,
  71. ]);
  72. $query->andWhere([
  73. 'and',
  74. ['<', 'a.start_time', $time],
  75. ['>', 'a.end_time', $time],
  76. ]);
  77. $info = $query->one();
  78. return $info;
  79. }
  80. //店铺进行中活动
  81. public static function activityAtList($store_id = 0, $asArray = false) {
  82. $query = self::find();
  83. $query->andWhere([
  84. 'is_delete' => 0,
  85. 'status' => 1,
  86. ]);
  87. if($store_id > -1){
  88. $query->andWhere(['store_id' => $store_id]);
  89. }
  90. $query->andWhere([
  91. 'and',
  92. ['<', 'start_time', time()],
  93. ['>', 'end_time', time()],
  94. ]);
  95. $asArray && $query->asArray();
  96. $list = $query->all();
  97. return $list;
  98. }
  99. }