CategoryService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. $parentSuperPath = self::getCategoryPath($params['category_super_id']);
  130. $superPath = '#' . $params['category_super_id'] . '#';
  131. if ($parentSuperPath == $superPath) {
  132. $data['category_super_path'] = $superPath;
  133. } else {
  134. $data['category_super_path'] = $parentSuperPath . $superPath;
  135. }
  136. }
  137. if (!SysCategory::where('category_id', $id)->update($data)) {
  138. throw new \Exception('分类修改失败');
  139. }
  140. } catch (\Exception $e) {
  141. return json_fail('分类修改失败');
  142. }
  143. return json_success('分类修改成功');
  144. }
  145. /**
  146. * @Desc 修改分类状态
  147. * @Author Gorden
  148. * @Date 2024/2/22 11:49
  149. *
  150. * @param $id
  151. * @param $params
  152. * @return \support\Response
  153. */
  154. public static function updateStatus($id, $params)
  155. {
  156. try {
  157. SysCategory::where('category_id', $id)->update(['category_status' => $params['category_status']]);
  158. } catch (\Exception $e) {
  159. return json_fail('分类状态修改失败');
  160. }
  161. return json_success('分类状态修改成功');
  162. }
  163. /**
  164. * @Desc 删除分类
  165. * @Author Gorden
  166. * @Date 2024/2/22 11:56
  167. *
  168. * @param $id
  169. * @return \support\Response
  170. */
  171. public static function delCategory(Request $request)
  172. {
  173. $ids = $request->post('category_id');
  174. if (!$ids) {
  175. return json_fail("数据错误");
  176. }
  177. try {
  178. if (is_array($ids)) {
  179. SysCategory::whereIn('category_id', $ids)->delete();
  180. } else {
  181. SysCategory::where('category_id', $ids)->delete();
  182. }
  183. } catch (\Exception $e) {
  184. return json_fail('分类删除失败');
  185. }
  186. return json_success('分类删除成功');
  187. }
  188. /**
  189. * @Desc 获取path
  190. * @Author Gorden
  191. * @Date 2024/2/22 10:40
  192. *
  193. * @param $categoryId
  194. * @return int|mixed
  195. */
  196. public static function getCategoryPath($categoryId)
  197. {
  198. $category = SysCategory::where('category_id', $categoryId)->first();
  199. if (!$category) {
  200. throw new \Exception('分类不存在');
  201. }
  202. if (empty($category->category_super_path)) {
  203. return "#" . $categoryId . "#";
  204. }
  205. return $category->category_super_path;
  206. }
  207. /**
  208. * Notes: 获取树状分类信息
  209. * User: ZhouBenXu
  210. * DateTime: 2024/7/2 下午2:52
  211. * @param $parentId
  212. * @return \support\Response
  213. */
  214. public static function getCategoryTree($parentId)
  215. {
  216. // 查询当前父级ID下的所有分类
  217. $list = self::getAllCategories($parentId);
  218. $allCategories = self::getTree($list, 152);
  219. return json_success('', $allCategories);
  220. }
  221. /**
  222. * Notes: 获取指定分类下的所有分类
  223. * User: ZhouBenXu
  224. * DateTime: 2024/7/2 下午2:41
  225. * @param $parentId
  226. * @param $categories
  227. * @return array|mixed
  228. */
  229. public static function getAllCategories($parentId = 0, $categories = [])
  230. {
  231. // 查询当前父级ID下的所有分类
  232. $row = SysCategory::where('category_super_id', $parentId)->where('category_status', 'ACTIVED')->get()->toArray();
  233. if (!empty($row)) {
  234. foreach ($row as $key => $value) {
  235. // 当前分类加入数组
  236. $value['label'] = $value['category_name'];
  237. $value['value'] = $value['category_id'];
  238. $categories[] = $value;
  239. // 递归查询子分类
  240. $categories = self::getAllCategories($value['category_id'], $categories);
  241. }
  242. }
  243. return $categories;
  244. }
  245. /**
  246. * Notes: 树状结构
  247. * User: ZhouBenXu
  248. * DateTime: 2024/7/2 下午2:51
  249. * @param $list
  250. * @param $id
  251. * @return array
  252. */
  253. public static function getTree($list = [], $id = 0)
  254. {
  255. $tree = array();
  256. foreach ($list as $k => $v) {
  257. if ($v['category_super_id'] == $id) {
  258. if (!empty(self::getTree($list, $v['category_id']))) {
  259. $v['children'] = self::getTree($list, $v['category_id']);
  260. }
  261. $tree[] = $v;
  262. }
  263. }
  264. return $tree;
  265. }
  266. /**
  267. * @Desc 分类详情
  268. * @Author Gorden
  269. * @Date 2024/2/22 11:37
  270. *
  271. * @param $id
  272. * @return \support\Response
  273. */
  274. public static function getCategoryInfos($ids)
  275. {
  276. $category = SysCategory::whereIn('category_id', $ids)->select(['category_id', 'category_name', 'category_super_id'])->get()->toArray();
  277. if (!$category) {
  278. return json_fail('分类不存在');
  279. }
  280. return $category;
  281. }
  282. }