MMaterial.Class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * 素材Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2020/03/31
  7. * Time: 15:44
  8. */
  9. namespace JinDouYun\Model\Material;
  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\Material\DMaterialCategory;
  15. use JinDouYun\Dao\Material\DMaterialContent;
  16. class MMaterial
  17. {
  18. private $objDMaterialCategory;
  19. private $objDMaterialContent;
  20. private $userCenterId;
  21. private $enterpriseId;
  22. public function __construct($enterpriseId, $userCenterId)
  23. {
  24. $this->enterpriseId = $enterpriseId;
  25. $this->userCenterId = $userCenterId;
  26. $this->objDMaterialCategory = new DMaterialCategory('default');
  27. $this->objDMaterialContent = new DMaterialContent('default');
  28. $this->objDMaterialContent->setTable($this->objDMaterialContent->get_Table().'_'.$enterpriseId);
  29. }
  30. /**
  31. * 素材内容添加
  32. * @param $params
  33. * @return ResultWrapper
  34. * @throws Exception
  35. */
  36. public function addMaterialContent($params)
  37. {
  38. $insertData['shopId'] = isset($params['shopId']) ? $params['shopId'] : 0;
  39. $insertData['categoryId'] = $params['categoryId'];
  40. $insertData['name'] = $params['name'];
  41. $insertData['content'] = $params['content'];
  42. $insertData['deleteStatus'] = StatusCode::$standard;
  43. $insertData['createTime'] = time();
  44. $insertData['updateTime'] = time();
  45. $dbResult = $this->objDMaterialContent->insert($insertData);
  46. if($dbResult === false){
  47. return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
  48. }
  49. return ResultWrapper::success($dbResult);
  50. }
  51. /**
  52. * 素材分类添加
  53. * @param $params
  54. * @return ResultWrapper
  55. * @throws Exception
  56. */
  57. public function addMaterialCategory($params)
  58. {
  59. $insertData['shopId'] = isset($params['shopId']) ? $params['shopId'] : 0;
  60. $insertData['enterpriseId'] = $this->enterpriseId;
  61. $insertData['pid'] = $params['pid'];
  62. $insertData['title'] = $params['title'];
  63. $insertData['deleteStatus'] = StatusCode::$standard;
  64. $insertData['createTime'] = time();
  65. $insertData['updateTime'] = time();
  66. $dbResult = $this->objDMaterialCategory->insert($insertData);
  67. if($dbResult === false){
  68. return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
  69. }
  70. return ResultWrapper::success($dbResult);
  71. }
  72. /**
  73. * 素材内容修改
  74. * @param $params
  75. * @return ResultWrapper
  76. * @throws Exception
  77. */
  78. public function updateMaterialContent($params)
  79. {
  80. $dbResult = $this->objDMaterialContent->update(['categoryId' => $params['categoryId'], 'updateTime' => time()], ['id' => $params['id']]);
  81. if($dbResult === false){
  82. return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
  83. }
  84. return ResultWrapper::success($dbResult);
  85. }
  86. /**
  87. * 素材分类修改
  88. * @param $params
  89. * @return ResultWrapper
  90. * @throws Exception
  91. */
  92. public function updateMaterialCategory($params)
  93. {
  94. $where = ['id' => $params['id']];
  95. unset($params['id']);
  96. $dbResult = $this->objDMaterialCategory->update($params, $where);
  97. if($dbResult === false){
  98. return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
  99. }
  100. return ResultWrapper::success($dbResult);
  101. }
  102. /**
  103. * 素材内容删除
  104. * @param $params
  105. * @return ResultWrapper
  106. */
  107. public function delMaterialContent($params)
  108. {
  109. $where = ['id' => $params['id']];
  110. $dbResult = $this->objDMaterialContent->update(['deleteStatus' => StatusCode::$delete,'updateTime' => time(), 'extend' => json_encode(['userCenterId' => $this->userCenterId])], $where);
  111. if($dbResult === false){
  112. return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
  113. }
  114. return ResultWrapper::success($dbResult);
  115. }
  116. /**
  117. * 素材分类删除
  118. * @param $params
  119. * @return ResultWrapper
  120. */
  121. public function delMaterialCategory($params)
  122. {
  123. $where = ['id' => $params['id']];
  124. //判断是否有内容
  125. $dbResult = $this->objDMaterialContent->select(['categoryId' => $where['id'], 'deleteStatus' => StatusCode::$standard]);
  126. if($dbResult === false){
  127. return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
  128. }
  129. if($dbResult){
  130. return ResultWrapper::fail('请先删除分组里内容', ErrorCode::$paramError);
  131. }
  132. $dbResult = $this->objDMaterialCategory->update(['deleteStatus' => StatusCode::$delete], $where);
  133. if($dbResult === false){
  134. return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
  135. }
  136. return ResultWrapper::success($dbResult);
  137. }
  138. /**
  139. * 素材分类列表
  140. * @return ResultWrapper
  141. */
  142. public function getAllMaterialCategory($params = [])
  143. {
  144. $where = [
  145. 'pid' => 0,
  146. 'enterpriseId' => $this->enterpriseId,
  147. 'deleteStatus' => StatusCode::$standard,
  148. 'shopId' => isset($params['shopId']) ? $params['shopId'] : 0,
  149. ];
  150. $dbResult = $this->objDMaterialCategory->select($where, '*', 'id desc');
  151. if ($dbResult === false) {
  152. return ResultWrapper::fail($this->objDMaterialCategory->error(), ErrorCode::$dberror);
  153. }
  154. $defaultCategory = [
  155. 'id' => 0,
  156. 'enterpriseId' => $this->enterpriseId,
  157. 'pid' => 0,
  158. 'title' => '默认分组',
  159. 'deleteStatus' => StatusCode::$standard,
  160. 'createTime' => 0,
  161. 'updateTime' => 0,
  162. ];
  163. array_unshift($dbResult, $defaultCategory);
  164. return ResultWrapper::success($dbResult);
  165. }
  166. /**
  167. * 素材内容列表
  168. * @param $params
  169. * @return ResultWrapper
  170. */
  171. public function getAllMaterialContent($params)
  172. {
  173. $limit = $params['limit'];
  174. unset($params['limit']);
  175. $offset = $params['offset'];
  176. unset($params['offset']);
  177. $where = ' categoryId = '.$params['categoryId']. ' and deleteStatus = '.StatusCode::$standard;
  178. !empty($params['name']) && $where .= " and name like '%".$params['name']."%'";
  179. $where .= ' and shopId = '.(isset($params['shopId']) ? $params['shopId'] : 0);
  180. $dbResult = $this->objDMaterialContent->select($where, '*', 'id desc', $limit, $offset);
  181. if ($dbResult === false) {
  182. return ResultWrapper::fail($this->objDMaterialContent->error(), ErrorCode::$dberror);
  183. }
  184. $total = $this->objDMaterialContent->count($where);
  185. $return = [
  186. 'data' => $dbResult,
  187. 'total' => ($total) ? intval($total) : 0,
  188. ];
  189. return ResultWrapper::success($return);
  190. }
  191. }