validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $t = \Yii::$app->db->beginTransaction(); $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, // 'status' => Notice::STATUS_TRASH, // 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->is_delete = 1; //删除RichText RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id); if(!$model->save()){ $t->rollBack(); return $this->getModelError($model); } $t->commit(); } } else { $t = \Yii::$app->db->beginTransaction(); $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'status' => Notice::STATUS_TRASH, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->is_delete = 1; //删除RichText RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id); if(!$model->save()){ $t->rollBack(); return $this->getModelError($model); } $t->commit(); } return $this->apiReturnSuccess("删除成功"); } public function publish() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'status' => Notice::STATUS_DRAFT, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->status = Notice::STATUS_PUBLISHED; if(empty($model->published_at)){ $model->published_at = time(); } if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'status' => Notice::STATUS_DRAFT, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->status = Notice::STATUS_PUBLISHED; if(empty($model->published_at)){ $model->published_at = time(); } if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("发布成功"); } public function trash() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'status' => [Notice::STATUS_DRAFT, Notice::STATUS_PUBLISHED], 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->status = Notice::STATUS_TRASH; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'status' => [Notice::STATUS_DRAFT, Notice::STATUS_PUBLISHED], 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->status = Notice::STATUS_TRASH; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("操作成功"); } public function cancel_trash() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'status' => Notice::STATUS_TRASH, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->status = Notice::STATUS_DRAFT; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'status' => Notice::STATUS_TRASH, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->status = Notice::STATUS_DRAFT; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("操作成功"); } public function top() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'is_top' => 0, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->is_top = 1; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'is_top' => 0, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->is_top = 1; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("设置成功"); } public function cancel_top() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'is_top' => 1, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->is_top = 0; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'is_top' => 1, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->is_top = 0; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("设置成功"); } public function show_index() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'is_index' => 0, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->is_index = 1; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'is_index' => 0, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->is_index = 1; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("设置成功"); } public function hide_index() { if(!$this->validate()){ return $this->getModelError(); } if(is_array($this->notice_id)){ foreach ($this->notice_id as $notice_id){ $model = Notice::findOne([ 'id' => $notice_id, 'is_delete' => 0, 'is_index' => 1, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null) continue; $model->is_index = 0; if(!$model->save()){ return $this->getModelError($model); } } } else { $model = Notice::findOne([ 'id' => $this->notice_id, 'is_delete' => 0, 'is_index' => 1, 'cx_mch_id' => $this->cx_mch_id ]); if($model == null){ return $this->apiReturnError('公告不存在或已经删除'); } $model->is_index = 0; if(!$model->save()){ return $this->getModelError($model); } } return $this->apiReturnSuccess("设置成功"); } public function detail() { if(!$this->validate()){ return $this->getModelError(); } $form = new CommonNoticeListForm(); $form->scenario = 'detail'; $form->notice_id = $this->notice_id; $form->cx_mch_id = $this->cx_mch_id; $form->status = Notice::STATUS_PUBLISHED; $res = $form->search(1); if($res['code'] != 0) return $res; if(count($res['data']) == 0) return $this->apiReturnError('公告详情不存在'); Notice::setViewedNum($this->notice_id, $this->cx_mch_id); $data = $res['data'][0]; return $this->apiReturnSuccess('ok',$data); } }