1], ['status', 'in', 'range' => [UserLabel::STATUS_CLOSE, UserLabel::STATUS_OPEN]], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'store_id' => 'Store ID', 'status' => '状态', 'sort' => '排序', 'label_name' => '标签名称', 'start_time' => '开始时间', 'end_time' => '结束时间', ]; } //用户标签列表 public function list() { try { $store_id = $this->store_id; $status = $this->status; $label_name = $this->label_name; $start_time = $this->start_time; $end_time = $this->end_time; $query = UserLabel::find()->where(['store_id' => $store_id, 'is_delete' => 0]); if (isset($status) && in_array($status, [UserLabel::STATUS_CLOSE, UserLabel::STATUS_OPEN])) { $query->andWhere(['status' => $status]); } if (!empty($label_name)) { $query->andWhere(['LIKE', 'label_name', $label_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, label_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; $label_name = $this->label_name; $sort = $this->sort; $status = intval($this->status); if (empty($label_name)) { throw new \Exception('标签名称不能为空'); } $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]) ?: new UserLabel(); $userLabel->label_name = $label_name; $userLabel->sort = $sort; $userLabel->status = $status; $userLabel->store_id = $this->store_id; if (!$userLabel->save()) { throw new \Exception(implode(';', array_values($userLabel->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('用户标签不存在1'); } foreach ($ids as $id) { $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]); if (!$userLabel) { throw new \Exception('用户标签不存在2'); } $userLabel->status = $status; if (!$userLabel->save()) { throw new \Exception(implode(';', array_values($userLabel->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) { $userLabel = UserLabel::findOne(['id' => $id, 'is_delete' => 0]); if (!$userLabel) { throw new \Exception('用户标签不存在'); } $userLabel->is_delete = 1; if (!$userLabel->save()) { throw new \Exception(implode(';', array_values($userLabel->firstErrors))); } } return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }