MCategory.Class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * 官网频道管理模块
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2020/2/11
  7. * Time: 下午3:38
  8. */
  9. namespace JinDouYun\Model\Manage;
  10. use Mall\Framework\Core\StatusCode;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Manage\DCategory;
  14. use JinDouYun\Model\Manage\MArticle;
  15. class MCategory
  16. {
  17. protected $objDCategory;
  18. public function __construct()
  19. {
  20. $this->objDCategory = new DCategory();
  21. }
  22. // 添加频道
  23. public function addCategory($params)
  24. {
  25. $dbResult = $this->objDCategory->insert($params);
  26. if($dbResult === false){
  27. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  28. }else{
  29. return ResultWrapper::success($dbResult);
  30. }
  31. }
  32. // 修改频道启用/停用状态
  33. public function updateEnbaleStatus($params)
  34. {
  35. $dbResult = $this->objDCategory->update(['id'=>$params['id'],'enableStatus'=>$params['enableStatus']], $params['id']);
  36. if($dbResult === false){
  37. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  38. }else{
  39. return ResultWrapper::success($dbResult);
  40. }
  41. }
  42. //频道删除
  43. public function delCategory($id)
  44. {
  45. $dbResult = $this->objDCategory->delete(['id' => $id]);
  46. if($dbResult === false){
  47. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  48. }
  49. return ResultWrapper::success($dbResult);
  50. }
  51. // 获取所有频道数据
  52. public function getAllCategory($type)
  53. {
  54. $dbResult = $this->objDCategory->select(['enableStatus'=>StatusCode::$standard, 'type' => $type], '*', 'sort desc');
  55. if($dbResult === false){
  56. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  57. }
  58. if(empty($dbResult)){
  59. return ResultWrapper::fail('数据空了', ErrorCode::$contentNotExists);
  60. }
  61. return ResultWrapper::success($dbResult);
  62. }
  63. //获取频道和文章标题
  64. public function getCategoryAndArticle($params)
  65. {
  66. $sql = 'select id,categoryName from category where enableStatus = '.StatusCode::$standard.' and `type` = '.StatusCode::$delete.' order by id asc limit '.$params['categoryLimit'];
  67. $categoryData = $this->objDCategory->query($sql);
  68. if($categoryData === false){
  69. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  70. }
  71. if(empty($categoryData)){
  72. return ResultWrapper::success($categoryData);
  73. }
  74. $categoryIds = [];
  75. foreach($categoryData as $value){
  76. $categoryIds[] = $value['id'];
  77. }
  78. unset($sql);
  79. $sql = 'select * from article where categoryId in ('.implode(',', $categoryIds).') and deleteStatus = '.StatusCode::$standard. ' order by createTime desc';
  80. $dbResult = $this->objDCategory->query($sql);
  81. if($dbResult === false){
  82. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  83. }
  84. if(empty($dbResult)){
  85. return ResultWrapper::success($categoryData);
  86. }
  87. $formatArticle = [];
  88. foreach($dbResult as $value){
  89. if(isset($formatArticle[$value['categoryId']])){
  90. $count = count($formatArticle[$value['categoryId']]);
  91. }else{
  92. $count = 0;
  93. }
  94. if($count < $params['articleLimit']){
  95. $formatArticle[$value['categoryId']][] = $value;
  96. }
  97. }
  98. $returnData = [];
  99. foreach($categoryData as $value){
  100. $article = [];
  101. if(isset($formatArticle[$value['id']])){
  102. $article = $formatArticle[$value['id']];
  103. }
  104. $returnData[] = [
  105. 'id' => $value['id'],
  106. 'categoryName' => $value['categoryName'],
  107. 'article' => $article,
  108. ];
  109. }
  110. return ResultWrapper::success($returnData);
  111. }
  112. // 获取指定频道信息
  113. public function getCategoryInfo($params)
  114. {
  115. $dbResult = $this->objDCategory->get($params);
  116. if ($dbResult === false) {
  117. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  118. } else {
  119. return ResultWrapper::success($dbResult);
  120. }
  121. }
  122. // 修改频道信息
  123. public function editCategory($params)
  124. {
  125. if (empty($params['id'])) {
  126. return ResultWrapper::fail('没有指定要修改的频道id', ErrorCode::$paramError);
  127. }
  128. $dbResult = $this->objDCategory->update($params, $params['id']);
  129. if ($dbResult === false) {
  130. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  131. } else {
  132. return ResultWrapper::success($dbResult);
  133. }
  134. }
  135. }