0] ]; } public function search () { try { $query = SeckillActivity::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]); if ((int)$this->status === 1) {//未开始 $query->andWhere(['>' , 'start_time', time()]); } if ((int)$this->status === 2) {//进行中 $query->andWhere(['AND', ['<' , 'start_time', time()], ['>' , 'end_time', time()]]); } if ((int)$this->status === 3) { //已结束 $query->andWhere(['<' , 'end_time', time()]); } if (!empty($this->name)) { //名称 $query->andWhere(['LIKE' , 'name', $this->name]); } $query->andWhere(['is_platform' => $this->is_platform]); if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选 $query->andWhere(['OR', ['AND', ['<=' , 'start_time', strtotime($this->start_time)], ['>=' , 'end_time',strtotime($this->end_time)] ], ['AND', ['<=' , 'start_time', strtotime($this->start_time)], ['<=' , 'end_time',strtotime($this->end_time)], ['>=' , 'end_time',strtotime($this->start_time)] ], ['AND', ['>=' , 'start_time', strtotime($this->start_time)], ['<=' , 'end_time',strtotime($this->end_time)] ], ['AND', ['>=' , 'start_time', strtotime($this->start_time)], ['>=' , 'end_time',strtotime($this->end_time)], ['<=' , 'start_time',strtotime($this->end_time)] ], ]); } $query->select('id, start_time, end_time, created_at, updated_at, name, is_platform_audit'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['is_platform_audit'] = (int)$item['is_platform_audit']; //获取活动状态 if ($item['start_time'] > time()) { $item['status'] = 1; } if ($item['start_time'] < time() && $item['end_time'] > time()) { $item['status'] = 2; } if ($item['end_time'] < time()) { $item['status'] = 3; } //格式化时间 $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['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); $item['order_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('order_id')->count(); $item['self_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('user_id')->count();; $item['order_price'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->sum('sale_price') ?: '0.00'; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getGoods() { try { // 如果是团购活动请求商品 要排除一下已经添加过的商品 if($this->group_activity == 1){ $activities = MdGroupActivities::find()->where(['store_id'=>get_store_id(),'is_delete'=>0])->andWhere(['>','end_time',time()])->select('id')->column(); $notGoodsId = MdGroupActivitiesGoods::find()->where(['activities_id'=>$activities,'is_delete'=>0])->select('goods_id')->column(); } $query = Goods::find()->alias('g')->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'status' => 1, 'product_type' => 0]); if(!empty($notGoodsId)){ $query->andWhere(['not in','g.id',$notGoodsId]); } if ($this->name) { $query->andWhere(['LIKE', 'g.name', $this->name]); } if ($this->cat_id) { $query->leftJoin(['gc' => GoodsCat::tableName()], 'gc.goods_id = g.id') ->leftJoin(['c' => Cat::tableName()], 'c.id = gc.cat_id') ->andWhere(['gc.cat_id' => $this->cat_id]); } $query->select('g.id, g.name, g.cover_pic, g.goods_num, g.price, g.attr, g.use_attr'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['attr'] = json_decode($item['attr'], true); if ($item['attr']) { foreach ($item['attr'] as &$value) { $value['pt_price'] = ""; } } $item['cat'] = GoodsCat::find()->alias('gc')->where(['gc.goods_id' => $item['id'], 'gc.is_delete' => 0]) ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id = c.id')->select('name')->asArray()->all(); } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getInfo() { try { $activity = SeckillActivity::find()->where(['id' => $this->id, 'is_delete' => 0]) ->select('id, name, start_time, end_time, self_limit_num ,order_limit_num ,is_use_coupon, is_platform')->one(); if ($activity) { $activity_goods = SeckillActivityGoods::find()->alias('sag')->leftJoin(['g' => Goods::tableName()], 'g.id = sag.goods_id') ->where(['sag.activity_id' => $activity->id, 'sag.is_delete' => 0]) ->select('g.id, g.name, g.cover_pic, sag.attr, g.use_attr, g.price, g.goods_num, sag.virtual_sales, sag.virtual_num, sag.seckill_num, sag.sale_num, sag.seckill_price') ->asArray()->all(); foreach ($activity_goods as &$activity_good) { $activity_good['attr'] = json_decode($activity_good['attr'], true); } $activity['start_time'] = date("Y-m-d H:i:s", $activity['start_time']); $activity['end_time'] = date("Y-m-d H:i:s", $activity['end_time']); } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'activity_goods' => $activity_goods ?? [], 'activity' => $activity ?: [] ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() . $e->getFile() . $e->getLine() ]; } } public function save () { $t = \Yii::$app->db->beginTransaction(); try { if (!$this->name || !$this->start_time || !$this->end_time || !$this->goods) { throw new \Exception("请将参数填充完整"); } $activity = SeckillActivity::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]); if (empty($activity)) { $activity = new SeckillActivity(); $activity->store_id = get_store_id(); } // else { // SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $activity->id]); // } $activity->name = $this->name; $activity->start_time = strtotime($this->start_time); $activity->end_time = strtotime($this->end_time); $activity->self_limit_num = (int)$this->self_limit_num; $activity->order_limit_num = (int)$this->order_limit_num; $activity->is_use_coupon = $this->is_use_coupon ?? 1; $activity->is_platform = (int)$this->is_platform; if (!$activity->save()) { throw new \Exception(json_encode($activity->errors)); } $goods = $this->goods; SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $activity->id, 'is_delete' => 0]); $open = false; foreach ($goods as $item) { $Goods = Goods::find()->where(['id' => $item['id'], 'store_id' => get_store_id(), 'is_delete' => 0, 'status' => 1])->select('id, use_attr, name, attr')->one(); if ($Goods) { $old_goods = SeckillActivityGoods::findOne(['activity_id' => $activity->id, 'goods_id' => $item['id']]); $sale_num = 0; if ($old_goods) { $sale_num = $old_goods->sale_num; } else { $open = true; } //验证是否存在其他时间段(未开始 / 进行中)相同的活动产品 $is_exist_goods = SeckillActivityGoods::find()->alias('sag')->where(['sag.goods_id' => $item['id'], 'sag.store_id' => get_store_id()]) ->leftJoin(['sg' => SeckillActivity::tableName()], 'sag.activity_id = sg.id') ->andWhere(['<>', 'sg.id', $activity->id]) ->andWhere(['OR', ['AND', ['<=' , 'start_time', strtotime($this->start_time)], ['>=' , 'end_time',strtotime($this->end_time)] ], ['AND', ['<=' , 'start_time', strtotime($this->start_time)], ['<=' , 'end_time',strtotime($this->end_time)], ['>=' , 'end_time',strtotime($this->start_time)] ], ['AND', ['>=' , 'start_time', strtotime($this->start_time)], ['<=' , 'end_time',strtotime($this->end_time)] ], ['AND', ['>=' , 'start_time', strtotime($this->start_time)], ['>=' , 'end_time',strtotime($this->end_time)], ['<=' , 'start_time',strtotime($this->end_time)] ], ])->andWhere(['sg.is_delete' => 0, 'sag.is_delete' => 0])->select('sag.id, sag.activity_id')->one(); if ($is_exist_goods) { throw new \Exception("部分商品已经在其他未开始/进行中的活动中"); } // //验证相同时间段是否存在其他活动产品(新人专享) // $is_exist_goods = ActivityNewUserGoods::find()->alias('pag')->where(['pag.goods_id' => $item['id'], 'pag.store_id' => get_store_id()]) // ->leftJoin(['pg' => ActivityNewUser::tableName()], 'pag.activity_id = pg.id') // ->andWhere(['OR', // ['AND', // ['<=' , 'pg.start_time', strtotime($this->start_time)], // ['>=' , 'pg.end_time',strtotime($this->end_time)] // ], // ['AND', // ['<=' , 'pg.start_time', strtotime($this->start_time)], // ['<=' , 'pg.end_time',strtotime($this->end_time)], // ['>=' , 'pg.end_time',strtotime($this->start_time)] // ], // ['AND', // ['>=' , 'pg.start_time', strtotime($this->start_time)], // ['<=' , 'pg.end_time',strtotime($this->end_time)] // ], // ['AND', // ['>=' , 'pg.start_time', strtotime($this->start_time)], // ['>=' , 'pg.end_time',strtotime($this->end_time)], // ['<=' , 'pg.start_time',strtotime($this->end_time)] // ], // ])->andWhere(['pg.is_delete' => 0, 'pag.is_delete' => 0])->select('pag.id, pag.activity_id')->one(); // if ($is_exist_goods) { // throw new \Exception("部分商品已经在其他未开始/进行中的新人专享活动中"); // } $activity_goods = new SeckillActivityGoods(); $activity_goods->goods_id = $item['id']; $activity_goods->virtual_sales = (int)$item['virtual_sales']; $activity_goods->virtual_num = (int)$item['virtual_num']; $activity_goods->store_id = get_store_id(); $activity_goods->use_attr = (int)$Goods->use_attr; $activity_goods->sale_num = $sale_num; if ((int)$Goods->use_attr === 1 && empty($item['attr'])) { throw new \Exception("保存失败, 商品" . $Goods->name . '没有规格参数'); } if (is_string($item['attr'])) { $item['attr'] = json_decode($item['attr'], true); } if ((int)$Goods->use_attr === 1 && $item['attr']) { $num = 0; foreach ($item['attr'] as $value) { if (!isset($value['attr_list']) || !isset($value['seckill_num']) || !isset($value['seckill_price'])) { throw new \Exception("保存失败, 商品" . $Goods->name . '规格参数错误'); } $num += $value['seckill_num']; } $activity_goods->seckill_num = (int)$num; $activity_goods->attr = json_encode($item['attr']); } else { $attr = json_decode($Goods->attr, true); $attr[0]['seckill_num'] = $activity_goods->seckill_num = (int)$item['seckill_num']; $attr[0]['seckill_price'] = $activity_goods->seckill_price = (float)$item['seckill_price']; $activity_goods->attr = json_encode($attr); } $activity_goods->activity_id = $activity->id; if (!$activity_goods->save()) { throw new \Exception(json_encode($activity_goods->errors)); } if ($old_goods && ($old_goods->use_attr != $activity_goods->use_attr || $old_goods->attr != $activity_goods->attr || $old_goods->virtual_sales != $activity_goods->virtual_sales || $old_goods->virtual_num != $activity_goods->virtual_num || $old_goods->sale_num != $activity_goods->sale_num || $old_goods->seckill_num != $activity_goods->seckill_num || $old_goods->seckill_price != $activity_goods->seckill_price ) ) { $open = true; } } else { throw new \Exception("商品未找到或已下架"); } } if ($open) { $activity->is_platform_audit = 0; if (!$activity->save()) { throw new \Exception(json_encode($activity->errors, JSON_UNESCAPED_UNICODE)); } } //觉得没啥用,直接保存后删除 SeckillActivityGoods::deleteAll(['activity_id' => $activity->id, 'is_delete' => 1]); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setStatus () { try { if ($this->ids) { $ids = explode(',', $this->ids); if (in_array($this->status, [0, 1])) { SeckillActivity::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } if ((int)$this->status === 2) { SeckillActivity::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); SeckillActivityGoods::updateAll(['is_delete' => 1], ['activity_id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } if (count($ids) === 1) { (new DiyCommon)->JobBehaviors(get_store_id(), StoreSyncExtLog::TYPE_SECKILL, $ids); } } else { $rules = SeckillActivity::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]); if (empty($rules)) { throw new \Exception("规则不存在"); } if (in_array($this->status, [0, 1])) { $rules->status = $this->status; } if ((int)$this->status === 2) { $rules->is_delete = 1; } if (!$rules->save()) { throw new \Exception(json_encode($rules->errors)); } } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 审核列表 */ public function auditList() { try { $query = SeckillActivity::find()->alias('a')->where(['a.is_delete' => 0, 'a.is_platform' => 1, 's.is_delete' => 0]); $query->leftJoin(['s' => Store::tableName()], 's.id = a.store_id'); if (intval($this->status) === 1) {//未开始 $query->andWhere(['AND', ['>' , 'a.start_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 2) {//进行中 $query->andWhere(['AND', ['<' , 'a.start_time', time()], ['>' , 'a.end_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 3) { //已结束 $query->andWhere(['AND', ['<' , 'a.end_time', time()], ['a.status' => 1]]); } if (intval($this->status) === 4) { //已终止 $query->andWhere(['a.status' => 0]); } if (!empty($this->name)) { //名称 $query->andWhere(['LIKE' , 'a.name', $this->name]); } if (!empty($this->store_name)) { //名称 $query->andWhere(['LIKE' , 's.name', $this->store_name]); } if ($this->is_platform_audit > -1 && $this->is_platform_audit !== null) { $query->andWhere(['a.is_platform_audit' => $this->is_platform_audit]); } //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动 if (\Yii::$app->prod_is_dandianpu()) { $store_id = Option::find()->where(['group' => 'store', 'name' => 'self_mini'])->select('store_id')->column(); $query->andWhere(['NOT IN', 's.id', $store_id]); } else { $query->andWhere(['s.business_model' => 2]); } if (!empty($this->start_time) && !empty($this->end_time)) { //时间筛选 $query->andWhere(['OR', ['AND', ['<=' , 'a.start_time', strtotime($this->start_time)], ['>=' , 'a.end_time',strtotime($this->end_time)] ], ['AND', ['<=' , 'a.start_time', strtotime($this->start_time)], ['<=' , 'a.end_time',strtotime($this->end_time)], ['>=' , 'a.end_time',strtotime($this->start_time)] ], ['AND', ['>=' , 'a.start_time', strtotime($this->start_time)], ['<=' , 'a.end_time',strtotime($this->end_time)] ], ['AND', ['>=' , 'a.start_time', strtotime($this->start_time)], ['>=' , 'a.end_time',strtotime($this->end_time)], ['<=' , 'a.start_time',strtotime($this->end_time)] ], ]); } $query->select('a.id, a.start_time, a.end_time, a.created_at, a.updated_at, a.name, a.is_platform_audit, s.name store_name, s.logo, s.id store_id, a.status'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['logo'] = $item['logo'] ?: Option::get(OptionSetting::STORE_LOGO, $item['store_id'], 'store', '')['value']; //获取活动状态 if (intval($item['status']) === 1) { if ($item['start_time'] > time()) { $item['status'] = 1; } if ($item['start_time'] < time() && $item['end_time'] > time()) { $item['status'] = 2; } if ($item['end_time'] < time()) { $item['status'] = 3; } } else { $item['status'] = 4; } $item['is_platform_audit'] = (int)$item['is_platform_audit']; //格式化时间 $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['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); $item['order_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('order_id')->count(); $item['self_num'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->groupBy('user_id')->count();; $item['order_price'] = SeckillActivityOrderLog::find()->where(['store_id' => get_store_id(), 'activity_id' => $item['id']])->sum('sale_price') ?: '0.00'; } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $pagination['list'], 'pageNo' => $pagination['pageNo'], 'totalCount' => $pagination['totalCount'], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 处理审核 */ public function auditHandle() { try { $id = $this->id; $status = $this->status; $cut_activity = SeckillActivity::findOne(['id' => $id, 'is_platform' => 1, 'is_delete' => 0]); if (!$cut_activity) { throw new \Exception('活动不存在'); } if (in_array($status, [1, 2])) { if (intval($cut_activity->is_platform_audit) !== 0) { throw new \Exception('活动已经审核'); } $cut_activity->is_platform_audit = $status; } if (intval($status) === 3) { $cut_activity->is_delete = 1; } if (in_array($status, [4, 5])) { $cut_activity->status = intval($status - 4); } if (!$cut_activity->save()) { throw new \Exception(json_encode($cut_activity->errors, JSON_UNESCAPED_UNICODE)); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }