1], ['status', 'in', 'range' => [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN]], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'status' => '状态', 'sort' => '排序', 'brand_cat_name' => '品牌分类名称', 'start_time' => '开始时间', 'end_time' => '结束时间', ]; } //品牌分类列表 public function list() { try { $store_id = $this->store_id; $status = $this->status; $brand_cat_name = $this->brand_cat_name; $start_time = $this->start_time; $end_time = $this->end_time; $query = GoodsBrandCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]); if (isset($status) && in_array($status, [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN])) { $query->andWhere(['status' => $status]); } if (!empty($brand_cat_name)) { $query->andWhere(['LIKE', 'brand_cat_name', $brand_cat_name]); } if (!empty($start_time)) { $start_time = strtotime($start_time); $query->andWhere(['>=', 'created_at', $start_time]); } if (!empty($end_time)) { $end_time = strtotime($end_time); $query->andWhere(['<=', 'created_at', $end_time]); } $query->select('id, brand_cat_name, sort, status, created_at')->orderBy('sort DESC'); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['status'] = intval($item['status']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $list ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //保存品牌分类 public function save() { try { if (!$this->validate()) { throw new \Exception($this->getErrorSummary(false)[0]); } $id = $this->id; $brand_cat_name = $this->brand_cat_name; $sort = $this->sort; $status = intval($this->status); if (empty($brand_cat_name)) { throw new \Exception('品牌分类名称不能为空'); } $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]) ?: new GoodsBrandCat(); $GoodsBrandCat->brand_cat_name = $brand_cat_name; $GoodsBrandCat->sort = $sort; $GoodsBrandCat->status = $status; $GoodsBrandCat->store_id = $this->store_id; if (!$GoodsBrandCat->save()) { throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors))); } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改状态 public function setStatus() { try { if (!$this->validate()) { throw new \Exception($this->getErrorSummary(false)[0]); } $ids = explode(',', $this->ids); $status = intval($this->status); if (empty($ids)) { throw new \Exception('品牌分类不存在'); } foreach ($ids as $id) { $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]); if (!$GoodsBrandCat) { throw new \Exception('品牌分类不存在'); } $GoodsBrandCat->status = $status; if (!$GoodsBrandCat->save()) { throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors))); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //删除品牌分类 public function delete() { try { $ids = explode(',', $this->ids); if (empty($ids)) { throw new \Exception('品牌分类不存在'); } foreach ($ids as $id) { $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]); if (!$GoodsBrandCat) { throw new \Exception('品牌分类不存在'); } $GoodsBrandCat->is_delete = 1; if (!$GoodsBrandCat->save()) { throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors))); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }