CategoryService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\admin\service\sys_manage;
  3. use app\model\SysCategory;
  4. use support\Db;
  5. use support\Request;
  6. class CategoryService
  7. {
  8. /**
  9. * @Desc 分类列表
  10. * @Author Gorden
  11. * @Date 2024/2/22 11:28
  12. *
  13. * @param $page
  14. * @param $limit
  15. * @param $keywords
  16. * @return \support\Response
  17. */
  18. public static function categoryList($page, $limit, $keywords)
  19. {
  20. $list = SysCategory::select('*')
  21. ->when($keywords != '', function ($query) use ($keywords) {
  22. $query->where('category_name', 'like', '%' . $keywords . '%');
  23. })
  24. ->orderBy('category_addtimes', 'DESC')
  25. ->forPage($page, $limit)
  26. ->get()
  27. ->toArray();
  28. $count = SysCategory::when($keywords != '', function ($query) use ($keywords) {
  29. $query->where('category_name', 'like', '%' . $keywords . '%');
  30. })->count();
  31. return json_success('', compact('list', 'page', 'limit', 'count'));
  32. }
  33. /**
  34. * @Desc 分类详情
  35. * @Author Gorden
  36. * @Date 2024/2/22 11:37
  37. *
  38. * @param $id
  39. * @return \support\Response
  40. */
  41. public static function categoryInfo($id)
  42. {
  43. $category = SysCategory::where('category_id', $id)->first();
  44. if (!$category) {
  45. return json_fail('分类不存在');
  46. }
  47. return json_success('', $category->toArray());
  48. }
  49. /**
  50. * @Desc 添加分类
  51. * @Author Gorden
  52. * @Date 2024/2/22 10:25
  53. *
  54. * @param $params
  55. * @return \support\Response
  56. */
  57. public static function insertCategory($params)
  58. {
  59. DB::beginTransaction();
  60. try {
  61. $data = [
  62. 'category_name' => $params['category_name'],
  63. 'category_super_id' => $params['category_super_id'] ?? 0,
  64. 'category_status' => $params['category_status'],
  65. 'category_classify' => $params['category_classify'] ?? '',
  66. 'category_type' => $params['category_type'] ?? '',
  67. 'category_icon' => $params['category_icon'] ?? '',
  68. 'category_images' => $params['category_images'] ?? '',
  69. 'category_url' => $params['category_url'] ?? '',
  70. 'category_page' => $params['category_page'] ?? '',
  71. 'category_sort' => $params['category_sort'] ?? '',
  72. 'category_groupby' => $params['category_groupby'] ?? '',
  73. 'category_remark' => $params['category_remark'] ?? '',
  74. 'category_extend_json' => !empty($params['category_extend_json']) ? $params['category_extend_json'] : '{}',
  75. 'category_addtimes' => time()
  76. ];
  77. $categoryId = SysCategory::insertGetId($data);
  78. if (!$categoryId) {
  79. throw new \Exception('添加分类失败');
  80. }
  81. // 不是顶级分类,有path
  82. if ($params['category_super_id'] != 0) {
  83. // 回填path
  84. $categoryPath = self::getCategoryPath($params['category_super_id']);
  85. SysCategory::where('category_id', $categoryId)->update(['category_super_path' => $categoryPath . '#' . $params['category_super_id'] . '#']);
  86. }
  87. // 提交事务
  88. DB::commit();
  89. } catch (\Exception $e) {
  90. DB::rollBack();
  91. dump($e->getMessage());
  92. return json_fail('添加分类失败');
  93. }
  94. return json_success('添加分类成功');
  95. }
  96. /**
  97. * @Desc 修改分类
  98. * @Author Gorden
  99. * @Date 2024/2/22 11:14
  100. *
  101. * @param $id
  102. * @param $params
  103. * @return \support\Response
  104. */
  105. public static function updateCategory($id, $params)
  106. {
  107. try {
  108. $category = SysCategory::select('category_super_id')->where('category_id', $id)->first();
  109. if (!$category) {
  110. throw new \Exception('分类不存在');
  111. }
  112. $data = [
  113. 'category_name' => $params['category_name'],
  114. 'category_classify' => $params['category_classify'] ?? '',
  115. 'category_type' => $params['category_type'] ?? '',
  116. 'category_icon' => $params['category_icon'] ?? '',
  117. 'category_images' => $params['category_images'] ?? '',
  118. 'category_url' => $params['category_url'] ?? '',
  119. 'category_page' => $params['category_page'] ?? '',
  120. 'category_sort' => $params['category_sort'] ?? '',
  121. 'category_groupby' => $params['category_groupby'] ?? '',
  122. 'category_remark' => $params['category_remark'] ?? '',
  123. 'category_status' => $params['category_status'] ?? '',
  124. 'category_extend_json' => !empty($params['category_extend_json']) ? $params['category_extend_json'] : '{}',
  125. 'category_addtimes' => time()
  126. ];
  127. if ($category->category_super_id != $params['category_super_id'] && $params['category_super_id'] != 0) {
  128. $data['category_super_id'] = $params['category_super_id'];
  129. $data['category_super_path'] = self::getCategoryPath($params['category_super_id']) . '#' . $params['category_super_id'] . '#';
  130. }
  131. if (!SysCategory::where('category_id', $id)->update($data)) {
  132. throw new \Exception('分类修改失败');
  133. }
  134. } catch (\Exception $e) {
  135. return json_fail('分类修改失败');
  136. }
  137. return json_success('分类修改成功');
  138. }
  139. /**
  140. * @Desc 修改分类状态
  141. * @Author Gorden
  142. * @Date 2024/2/22 11:49
  143. *
  144. * @param $id
  145. * @param $params
  146. * @return \support\Response
  147. */
  148. public static function updateStatus($id, $params)
  149. {
  150. try {
  151. SysCategory::where('category_id', $id)->update(['category_status' => $params['category_status']]);
  152. } catch (\Exception $e) {
  153. return json_fail('分类状态修改失败');
  154. }
  155. return json_success('分类状态修改成功');
  156. }
  157. /**
  158. * @Desc 删除分类
  159. * @Author Gorden
  160. * @Date 2024/2/22 11:56
  161. *
  162. * @param $id
  163. * @return \support\Response
  164. */
  165. public static function delCategory(Request $request)
  166. {
  167. $ids = $request->post('category_id');
  168. if (!$ids) {
  169. return json_fail("数据错误");
  170. }
  171. try {
  172. if (is_array($ids)) {
  173. SysCategory::whereIn('category_id', $ids)->delete();
  174. } else {
  175. SysCategory::where('category_id', $ids)->delete();
  176. }
  177. } catch (\Exception $e) {
  178. return json_fail('分类删除失败');
  179. }
  180. return json_success('分类删除成功');
  181. }
  182. /**
  183. * @Desc 获取path
  184. * @Author Gorden
  185. * @Date 2024/2/22 10:40
  186. *
  187. * @param $categoryId
  188. * @return int|mixed
  189. */
  190. public static function getCategoryPath($categoryId)
  191. {
  192. $category = SysCategory::where('category_id', $categoryId)->first();
  193. if (!$category) {
  194. throw new \Exception('分类不存在');
  195. }
  196. if (empty($category->category_super_path)) {
  197. return "#" . $categoryId . "#";
  198. }
  199. return $category->category_super_path;
  200. }
  201. }