where(['store_id' => get_store_id(), 'is_delete' => 0]); if ($this->name) { $query->andWhere(['LIKE', 'name', $this->name]); } if ((int)$this->status !== -1 && $this->status !== null) { $query->andWhere(['status' => $this->status]); } if ($this->start_time) { $start_time = strtotime($this->start_time); $query->andWhere(['>=', 'created_at', $start_time]); } if ($this->end_time) { $end_time = strtotime($this->end_time); $query->andWhere(['<=', 'created_at', $end_time]); } $query->select('id, created_at, updated_at, name, cover_pic, url, status, sort')->orderBy('created_at desc'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = $item['updated_at'] ? date("Y-m-d H:i:s", $item['updated_at']) : ''; $item['status'] *= 1; $item['url'] = !empty($item['url']) ? json_decode($item['url'], true) : [ 'name' => '', 'link' => '', 'open_type' => '' ]; } 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 save () { $t = \Yii::$app->db->beginTransaction(); try { if (empty($this->cover_pic)) { throw new \Exception('图片地址不可为空'); } $banner_form = new PtActivityBanner(); if ($this->id) { $banner_form = PtActivityBanner::findOne($this->id); if (empty($banner_form)) { throw new \Exception('查找失败'); } } if (!empty($this->url)) { $url = $this->url; } else { $url = [ 'name' => '', 'link' => '', 'open_type' => '' ]; } $banner_form->name = $this->name; $banner_form->cover_pic = $this->cover_pic; $banner_form->url = json_encode($url); $banner_form->sort = $this->sort; $banner_form->status = $this->status ?: 0; $banner_form->store_id = get_store_id(); if (!$banner_form->save()) { throw new \Exception(json_encode($banner_form->errors)); } $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])) { PtActivityBanner::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } if ((int)$this->status === 2) { PtActivityBanner::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } } else { $rules = PtActivityBanner::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() ]; } } }