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, status, created_at, updated_at, name')->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; } 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->name)) { throw new \Exception('分类名称不可为空'); } $pt_goods_cat = new PtActivityGoodsCat(); if ($this->id) { $pt_goods_cat = PtActivityGoodsCat::findOne($this->id); if (empty($pt_goods_cat)) { throw new \Exception('查找失败'); } } $pt_goods_cat->name = $this->name; $pt_goods_cat->status = $this->status ?: 1; $pt_goods_cat->store_id = get_store_id(); if (!$pt_goods_cat->save()) { throw new \Exception(json_encode($pt_goods_cat->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])) { PtActivityGoodsCat::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } if ((int)$this->status === 2) { PtActivityGoodsCat::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]); } } else { $rules = PtActivityGoodsCat::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() ]; } } }