store_id)){ $this->store_id = get_store_id(); } } public function search () { try { $query = ActivityNewUser::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]); } if (!empty($this->start_time)) { $query->andWhere(['>' , 'end_time', strtotime($this->start_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<' , 'start_time', strtotime($this->end_time)]); } $query->andWhere(['is_platform' => $this->is_platform]); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['publish'] = $item['status']; //获取活动状态 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']); } 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 { $query = Goods::find()->where(['is_delete' => 0, 'status' => 1, 'product_type' => 0, 'store_id' => $this->store_id]); if ($this->goods_name) { $query->andWhere(['LIKE' , 'name', $this->goods_name]); } $query->select('id, name, cover_pic, goods_num, price, attr, use_attr'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['attr'] = json_decode($item['attr'], true); $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 getCoupons() { try { $query = Coupon::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]); if ($this->coupon_name) { $query->andWhere(['like', 'name', $this->coupon_name]); } $list = $query->asArray()->all(); foreach($list as &$item){ $item['name'] = 'id:' . $item['id'] . ' ' . $item['name']; } return [ 'code' => 0, 'msg' => "获取成功", 'data' => $list, 's' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getInfo() { try { $activity = ActivityNewUser::find()->where(['id' => $this->id])->one(); if ($activity) { $activity_goods = Goods::find()->where(['in', 'id', explode(',', $activity->goods_ids)])->andWhere(['is_delete' => 0])->asArray()->all(); $activity_coupons = Coupon::find()->where(['in', 'id', explode(',', $activity->coupon_ids)])->andWhere(['is_delete' => 0])->asArray()->all(); $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']); $goods_ext = ActivityNewUserGoods::findAll(['activity_id' => $activity->id, 'is_delete' => 0]); } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'activity_goods' => $activity_goods ?? [], 'goods_ext' => $goods_ext ?? [], 'activity_coupons' => $activity_coupons ?? [], '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) { throw new \Exception("请将参数填充完整"); } // $has = ActivityNewUser::find()->where([ // 'and', // ['is_delete' => 0, 'store_id' => $this->store_id, 'status' => 1], // ['<', 'start_time', strtotime($this->end_time)], // ['>', 'end_time', strtotime($this->start_time)], // ['!=', 'id', intval($this->id)], // ['is_platform' => intval($this->is_platform)], // ])->one(); // if($has){ // throw new \Exception("时间内已有活动:" . $has['name']); // } $activity = ActivityNewUser::findOne($this->id); if (empty($activity)) { $activity = new ActivityNewUser(); $activity->store_id = $this->store_id; } $activity->name = $this->name; $activity->start_time = strtotime($this->start_time); $activity->end_time = strtotime($this->end_time); $activity->goods_ids = $this->goods_ids; $activity->coupon_ids = $this->coupon_ids; $activity->buy_limit = $this->buy_limit; $activity->is_platform = (int)$this->is_platform; if (!$activity->save()) { throw new \Exception(json_encode($activity->errors)); } if(!empty($this->goods_ext)){ foreach($this->goods_ext as &$item){ $item['activity_id'] = $activity->id; $item['store_id'] = $this->store_id; } $result = ActivityNewUserGoods::saveList($this->goods_ext, $activity->id, $is_platform_audit); if ($result['code'] !== 0) { throw new \Exception($result['msg']); } if ($is_platform_audit) { $activity->is_platform_audit = 0; $activity->save(); } } $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])) { ActivityNewUser::updateAll(['status' => $this->status], ['and', ['in', 'id', $ids], ['store_id' => get_store_id()]]); } if ((int)$this->status === 2) { ActivityNewUser::updateAll(['is_delete' => 1], ['and', ['in', 'id', $ids], ['store_id' => get_store_id()]]); } } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 审核列表 */ public function auditList() { try { $query = ActivityNewUser::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' , 'name', $this->name]); } if (!empty($this->store_name)) { //名称 $query->andWhere(['LIKE' , 's.name', $this->store_name]); } //只有平台运营的商城或者单店铺的无独立小程序才展示商盟的营销活动 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)] ], ]); } if ($this->is_platform_audit > -1 && $this->is_platform_audit !== null) { $query->andWhere(['a.is_platform_audit' => $this->is_platform_audit]); } $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['publish'] = $item['status']; //格式化时间 $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']); } 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 = ActivityNewUser::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() ]; } } }