MCase.Class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * 案例管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/03/28
  7. * Time: 16:07
  8. */
  9. namespace JinDouYun\Model\Manage;
  10. use Exception;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Manage\DCase;
  15. use JinDouYun\Dao\Manage\DCategory;
  16. class MCase
  17. {
  18. private $objDCase;
  19. private $objDCategory;
  20. public function __construct()
  21. {
  22. $this->objDCase = new DCase('manage');
  23. $this->objDCategory = new DCategory('manage');
  24. }
  25. /**
  26. * 案例添加
  27. * @param $params
  28. * @return ResultWrapper
  29. */
  30. public function addCase($params)
  31. {
  32. $dbResult = $this->objDCase->insert($params);
  33. if($dbResult === false){
  34. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  35. }
  36. return ResultWrapper::success($dbResult);
  37. }
  38. /**
  39. * 案例列表
  40. * @param $params
  41. * @return ResultWrapper
  42. */
  43. public function getAllCase($params)
  44. {
  45. $dbResult = $this->objDCase->select([], '*', 'id desc', $params['limit'], $params['offset']);
  46. if($dbResult === false){
  47. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  48. }
  49. if(empty($dbResult)){
  50. return ResultWrapper::success($dbResult);
  51. }
  52. $caseData = $dbResult;
  53. unset($dbResult);
  54. $total = $this->objDCase->count();
  55. $dbResult = $this->objDCategory->select(['type' => StatusCode::$standard, 'enableStatus' => StatusCode::$standard], 'id,categoryName', 'sort desc');
  56. if($dbResult === false){
  57. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  58. }
  59. if(empty($dbResult)){
  60. return ResultWrapper::success($dbResult);
  61. }
  62. $format = [];
  63. foreach($dbResult as $value){
  64. $format[$value['id']] = $value['categoryName'];
  65. }
  66. $returnData = [];
  67. $returnData['total'] = $total ? $total : 0;
  68. foreach($caseData as $value){
  69. $data = $value;
  70. $data['categoryName'] = isset($format[$value['caseCategoryId']]) ? $format[$value['caseCategoryId']] : '';
  71. $returnData['data'][] = $data;
  72. }
  73. return ResultWrapper::success($returnData);
  74. }
  75. /**
  76. * 增加点击量
  77. * @param $id
  78. * @return ResultWrapper
  79. */
  80. public function addCaseClicks($id)
  81. {
  82. $dbResult = $this->objDCase->set_inc('clicks', ['id' => $id]);
  83. if($dbResult === false){
  84. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  85. }
  86. return ResultWrapper::success($dbResult);
  87. }
  88. /**
  89. * 案例详情
  90. * @param $id
  91. * @return ResultWrapper
  92. */
  93. public function caseInfo($id)
  94. {
  95. $dbResult = $this->objDCase->get(['id' => $id]);
  96. if(!empty($dbResult['content'])){
  97. $dbResult['content'] = html_entity_decode($dbResult['content']);
  98. }
  99. $dbResult['categoryName'] = '';
  100. $categoryResult = $this->objDCategory->get(['id' => $dbResult['caseCategoryId'], 'enableStatus' => StatusCode::$standard]);
  101. if($categoryResult === false){
  102. return ResultWrapper::fail($this->objDCategory->error(), ErrorCode::$dberror);
  103. }
  104. $dbResult['categoryName'] = $categoryResult;
  105. if($dbResult === false){
  106. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  107. }
  108. return ResultWrapper::success($dbResult);
  109. }
  110. /**
  111. * 案例编辑
  112. * @param $params
  113. * @param $id
  114. * @return ResultWrapper
  115. */
  116. public function updateCase($params, $id)
  117. {
  118. $dbResult = $this->objDCase->update($params, ['id' => $id]);
  119. if($dbResult === false){
  120. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  121. }
  122. return ResultWrapper::success($dbResult);
  123. }
  124. /**
  125. * 案例删除
  126. * @param $id
  127. * @return ResultWrapper
  128. */
  129. public function delCase($id)
  130. {
  131. $dbResult = $this->objDCase->delete(['id' => $id]);
  132. if($dbResult === false){
  133. return ResultWrapper::fail($this->objDCase->error(), ErrorCode::$dberror);
  134. }
  135. return ResultWrapper::success($dbResult);
  136. }
  137. }