objDCategory = new DCategory(); } // 添加频道 public function addCategory($params) { $dbResult = $this->objDCategory->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } // 修改频道启用/停用状态 public function updateEnbaleStatus($params) { $dbResult = $this->objDCategory->update(['id'=>$params['id'],'enableStatus'=>$params['enableStatus']], $params['id']); if($dbResult === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } //频道删除 public function delCategory($id) { $dbResult = $this->objDCategory->delete(['id' => $id]); if($dbResult === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } // 获取所有频道数据 public function getAllCategory($type) { $dbResult = $this->objDCategory->select(['enableStatus'=>StatusCode::$standard, 'type' => $type], '*', 'sort desc'); if($dbResult === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail('数据空了', ErrorCode::$contentNotExists); } return ResultWrapper::success($dbResult); } //获取频道和文章标题 public function getCategoryAndArticle($params) { $sql = 'select id,categoryName from category where enableStatus = '.StatusCode::$standard.' and `type` = '.StatusCode::$delete.' order by id asc limit '.$params['categoryLimit']; $categoryData = $this->objDCategory->query($sql); if($categoryData === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } if(empty($categoryData)){ return ResultWrapper::success($categoryData); } $categoryIds = []; foreach($categoryData as $value){ $categoryIds[] = $value['id']; } unset($sql); $sql = 'select * from article where categoryId in ('.implode(',', $categoryIds).') and deleteStatus = '.StatusCode::$standard. ' order by createTime desc'; $dbResult = $this->objDCategory->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::success($categoryData); } $formatArticle = []; foreach($dbResult as $value){ if(isset($formatArticle[$value['categoryId']])){ $count = count($formatArticle[$value['categoryId']]); }else{ $count = 0; } if($count < $params['articleLimit']){ $formatArticle[$value['categoryId']][] = $value; } } $returnData = []; foreach($categoryData as $value){ $article = []; if(isset($formatArticle[$value['id']])){ $article = $formatArticle[$value['id']]; } $returnData[] = [ 'id' => $value['id'], 'categoryName' => $value['categoryName'], 'article' => $article, ]; } return ResultWrapper::success($returnData); } // 获取指定频道信息 public function getCategoryInfo($params) { $dbResult = $this->objDCategory->get($params); if ($dbResult === false) { return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } else { return ResultWrapper::success($dbResult); } } // 修改频道信息 public function editCategory($params) { if (empty($params['id'])) { return ResultWrapper::fail('没有指定要修改的频道id', ErrorCode::$paramError); } $dbResult = $this->objDCategory->update($params, $params['id']); if ($dbResult === false) { return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror); } else { return ResultWrapper::success($dbResult); } } }