'end_time', 'operator' => '<' ], [ 'id', 'required', 'on' => [ 'item', 'status' ] ], ['ids','string','on'=>'del'], ['ids','required','on'=>'del'], [ [ 'name', 'start_time', 'end_time', 'type', 'rules', 'goods' ], 'required', 'on' => ['create'] ], ]; } /** * 获取活动列表 * @return array * @author: hankaige * @Time: 2024/3/19 13:48 */ public function list(): array { $query = MdGroupActivities::find()->where([ 'store_id' => $this->store_id, 'is_delete' => 0 ]); if (!empty($this->start_time)) { $query->andWhere([ '<=', 'start_time', strtotime($this->start_time) ]); } if (!empty($this->end_time)) { $query->andWhere([ '>=', 'end_time', strtotime($this->end_time) ]); } if (!empty($this->name)) { $query->andWhere([ 'like', 'name', $this->name ]); } switch ($this->status) { case 1: $query->andWhere([ '>=', 'start_time', time() ]); break; case 2: $query->andWhere([ 'and', [ '<=', 'start_time', time() ], [ '>=', 'end_time', time() ] ]); break; case 3: $query->andWhere([ '<=', 'end_time', time() ]); break; default: break; } $result = pagination_make($query, FALSE, 'created_at DESC'); foreach ($result['list'] as &$item) { $temp = $item; $item = ArrayHelper::toArray($item); $item['start_time'] = date('Y-m-d H:i:s', $item['start_time']); $item['end_time'] = date('Y-m-d H:i:s', $item['end_time']); // 商品数量 $item['goods_number'] = count($temp->goods); // 支付金额 $item['order_price'] = Order::find()->where(['md_group_activities_id'=>$temp['id'],'is_pay'=>1])->andWhere(['!=','trade_status',1])->sum('pay_price') ?? 0; // 订单数量 $item['order_number'] = Order::find()->where(['md_group_activities_id'=>$temp['id'],'is_pay'=>1])->andWhere(['!=','trade_status',1])->count() ?? 0; $item['activity_status'] = $temp->activityStatus; } return [ 'code' => 0, 'data' => $result ]; } /** * 创建一个活动 * @return array * @author: hankaige * @Time: 2024/3/19 14:52 */ public function createItem(): array { // 验证数据 if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0] ]; } $t = \Yii::$app->db->beginTransaction(); try { if ($this->id == 0) { $model = new MdGroupActivities(); } else { $model = MdGroupActivities::findOne($this->id); } $model->name = $this->name; $model->store_id = $this->store_id; $model->start_time = strtotime($this->start_time); $model->end_time = strtotime($this->end_time); $model->type = $this->type; $model->rules = $this->rules; $model->status = $this->status; // 存在已添加过的消息队列 就先删除消息队列 if($model->jobs_id){ \Yii::$app->queue->remove($model->jobs_id); } // 重新添加消息队列 延迟时间为 活动结束时间 - 当前时间 $model->jobs_id = queue_push(new MdGroupActivitiesJob(['activity_id' => $model->id]), $this->end_time - time()); if (!$model->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $model->getErrors() ]; } // 先删除之前的活动商品记录 MdGroupActivitiesGoods::updateAll(['is_delete' => 1], [ 'activities_id' => $model->id, 'is_delete' => 0 ]); // 处理之前存在的活动商品 $goodsArray = []; $field = [ 'activities_id', 'goods_id', 'use_attr', 'attr', 'price', 'sale_num', 'created_at', 'updated_at', 'is_delete' ]; foreach ($this->goods as $item) { $goods = Goods::find()->where([ 'id' => $item['id'], 'is_delete' => 0, 'status' => 1 ])->one(); if ((int)$goods->use_attr === 1 && $item['attr']) { foreach ($item['attr'] as $value) { if (!isset($value['attr_list']) || !isset($value['group_price'])) { throw new \Exception("保存失败, 商品" . $goods->name . '规格参数错误'); } if ($value['group_price'] > $value['price']) { throw new \Exception("保存失败, 商品" . $goods->name . '团购价需小于售价'); } } $activityPrice = $item['attr'][0]['group_price']; $activityAttr = json_encode($item['attr']); } else { $attr = json_decode($goods->attr, TRUE); $attr[0]['group_price'] = $activityPrice = (float)$item['group_price']; $activityAttr = json_encode($attr); if ($activityPrice > $goods->price) { throw new \Exception("保存失败, 商品" . $goods->name . '团购价需小于售价'); } } $goodsItem = [ $model->id, $item['id'], $item['use_attr'], $activityAttr, $activityPrice, $item['sale_num'], time(), 0, 0 ]; $goodsArray[] = $goodsItem; } // 执行批量插入 $res = \Yii::$app->db->createCommand()->batchInsert(MdGroupActivitiesGoods::tableName(), $field, $goodsArray)->execute(); if ($res <= 0) { $t->rollBack(); return [ 'code' => 1, 'msg' => '插入活动商品失败' ]; } $t->commit(); return [ 'code' => 0, 'msg' => '活动创建成功' ]; } catch (\Throwable $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 获取一个活动详情 * @return array * @author: hankaige * @Time: 2024/3/19 14:56 */ public function item(): array { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0] ]; } $model = MdGroupActivities::find()->where([ 'id' => $this->id, 'is_delete' => 0 ])->one(); if (empty($model)) { $model = new MdGroupActivities(); } else { $model['start_time'] = date('Y-m-d H:i:s', $model['start_time']); $model['end_time'] = date('Y-m-d H:i:s', $model['end_time']); } $activityGoods = []; if (!empty($model->goods)) { foreach ($model->goods as $value) { // 如果没有设置规格图片 规格字段没有图片的时候 $activityGoods[] = [ 'id' => $value->goods_id, 'name' => $value->goods->name, 'cover_pic' => $value->goods->cover_pic, 'attr' => Json::decode($value->attr, TRUE), 'use_attr' => (string)$value->goods->use_attr, 'price' => $value->goods->price, 'goods_num' => $value->goods->goods_num, 'sale_num' => $value->sale_num, 'group_price' => $value->price ]; } } $model = ArrayHelper::toArray($model); $model['goods'] = $activityGoods; return [ 'code' => 0, 'data' => $model ]; } /** * 删除活动 * @return array * @author: hankaige * @Time: 2024/3/19 15:19 */ public function del(): array { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(FALSE)[0] ]; } $list = MdGroupActivities::find()->where([ 'id' => explode(',',$this->ids), 'is_delete' => 0 ])->all(); if (count($list) <= 0) { return [ 'code' => 1, 'msg' => '选择要删除的活动' ]; } $error = 0; foreach ($list as $item) { if (time() < $item->start_time || time() > $item->end_time) { $item->is_delete = 1; $item->save(); // 删除活动商品 MdGroupActivitiesGoods::updateAll(['is_delete' => 1], ['activities_id' => $item->id]); } else { $error++; } } if ($error > 0) { return [ 'code' => 1, 'msg' => '存在' . $error . '个进进行中的活动', ]; } else { return [ 'code' => 0, 'msg' => '操作成功' ]; } } // diy 获取活动列表 public function diyGetActivitiesList() { $list = MdGroupActivities::find()->where(['store_id'=>$this->store_id,'is_delete'=>0])->andWhere(['and',['<=','start_time',time()],['>=','end_time',time()]])->all(); foreach($list as &$item){ $goodsItem = $this->getGoodsList($item); $item = ArrayHelper::toArray($item); $item['goods_list'] = $goodsItem; } return ['code'=>0,'data'=>[ 'list'=>$list ]]; } // 通过获取获取活动商品信息 public function getGoodsListByActivity() { if(empty($this->id)){ return ['code'=>1,'msg'=>'活动ID不能为空']; } $activity = MdGroupActivities::findOne($this->id); $goodslist = $this->getGoodsList($activity); return [ 'code'=>0, 'data' => [ 'data' => $goodslist ] ]; } protected function getGoodsList($activity) { $goodsItem = []; foreach($activity->goods as $value){ $delivery_rules = DeliveryRules::find()->where(['id' => $value->goods->delivery_rules_id, 'is_delete' => 0, 'store_id' => get_store_id()])->select('type, times, days')->asArray()->one(); if ($delivery_rules) { $delivery_rules['times'] = (int)$delivery_rules['type'] === 1 ? '' : date("m月d日 H:i:s", $delivery_rules['times']); } $goodsItem[] = [ 'cover_pic' => $value->goods->cover_pic, 'name' => $value->goods->name, 'original_price' => $value->goods->original_price, 'price' => $value->price, 'virtual_sales' => $value->sale_num, 'delivery_rules' => $delivery_rules ?: null ]; } return $goodsItem; } }