TimestampBehavior::class, ] ]; } public function beforeSave($insert) { if(parent::beforeSave($insert)){ $query = self::find()->alias('a')->leftJoin(['ag' => ActivityOrderRebateSelfGoods::tableName()], 'a.id = ag.act_id'); $goods_ids = explode(',', $this->goods_ids); $this->goods_ids = implode(',', array_unique($goods_ids)); $query->andWhere([ 'a.store_id' => $this->store_id, 'ag.goods_id' => $goods_ids, 'a.is_delete' => 0, 'a.status' => 1, ]); $query->andWhere([ 'and', ['<', 'a.start_time', $this->end_time], ['>', 'a.end_time', $this->start_time], ]); $has = $query->andWhere([ '!=', 'a.id', (int)$this->id ])->one(); if($has){ $hasGoodIds = explode(',', $has->goods_ids); $this->addError('goods_id', '操作失败,商品已存在活动信息,活动id:' . $has->id . ' 商品id:' . implode(',', array_intersect($goods_ids, $hasGoodIds))); return false; } return true; } return false; } public static function activityAt($store_id = 0, $goods_id = 0, $time = null) { if($time === null){ $time = time(); } $query = self::find()->alias('a')->leftJoin(['ag' => ActivityOrderRebateSelfGoods::tableName()], 'a.id = ag.act_id'); $query->andWhere([ 'a.store_id' => $store_id, 'ag.goods_id' => $goods_id, 'a.is_delete' => 0, 'a.status' => 1, ]); $query->andWhere([ 'and', ['<', 'a.start_time', $time], ['>', 'a.end_time', $time], ]); $info = $query->one(); return $info; } //店铺进行中活动 public static function activityAtList($store_id = 0, $asArray = false) { $query = self::find(); $query->andWhere([ 'is_delete' => 0, 'status' => 1, ]); if($store_id > -1){ $query->andWhere(['store_id' => $store_id]); } $query->andWhere([ 'and', ['<', 'start_time', time()], ['>', 'end_time', time()], ]); $asArray && $query->asArray(); $list = $query->all(); return $list; } }