CategoryService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. $parentSuperPath = self::getCategoryPath($params['category_super_id']);
  85. $superPath = '#' . $params['category_super_id'] . '#';
  86. if ($parentSuperPath == $superPath) {
  87. $categorySuperPath = $superPath;
  88. } else {
  89. $categorySuperPath = $parentSuperPath . $superPath;
  90. }
  91. SysCategory::where('category_id', $categoryId)->update(['category_super_path' => $categorySuperPath]);
  92. }
  93. // 提交事务
  94. DB::commit();
  95. } catch (\Exception $e) {
  96. DB::rollBack();
  97. dump($e->getMessage());
  98. return json_fail('添加分类失败');
  99. }
  100. return json_success('添加分类成功');
  101. }
  102. /**
  103. * @Desc 修改分类
  104. * @Author Gorden
  105. * @Date 2024/2/22 11:14
  106. *
  107. * @param $id
  108. * @param $params
  109. * @return \support\Response
  110. */
  111. public static function updateCategory($id, $params)
  112. {
  113. try {
  114. $category = SysCategory::select('category_super_id')->where('category_id', $id)->first();
  115. if (!$category) {
  116. throw new \Exception('分类不存在');
  117. }
  118. $data = [
  119. 'category_name' => $params['category_name'],
  120. 'category_classify' => $params['category_classify'] ?? '',
  121. 'category_type' => $params['category_type'] ?? '',
  122. 'category_icon' => $params['category_icon'] ?? '',
  123. 'category_images' => $params['category_images'] ?? '',
  124. 'category_url' => $params['category_url'] ?? '',
  125. 'category_page' => $params['category_page'] ?? '',
  126. 'category_sort' => $params['category_sort'] ?? '',
  127. 'category_groupby' => $params['category_groupby'] ?? '',
  128. 'category_remark' => $params['category_remark'] ?? '',
  129. 'category_status' => $params['category_status'] ?? '',
  130. 'category_extend_json' => !empty($params['category_extend_json']) ? $params['category_extend_json'] : '{}',
  131. 'category_addtimes' => time()
  132. ];
  133. if ($category->category_super_id != $params['category_super_id'] && $params['category_super_id'] != 0) {
  134. $data['category_super_id'] = $params['category_super_id'];
  135. $parentSuperPath = self::getCategoryPath($params['category_super_id']);
  136. $superPath = '#' . $params['category_super_id'] . '#';
  137. if ($parentSuperPath == $superPath) {
  138. $data['category_super_path'] = $superPath;
  139. } else {
  140. $data['category_super_path'] = $parentSuperPath . $superPath;
  141. }
  142. }
  143. if (!SysCategory::where('category_id', $id)->update($data)) {
  144. throw new \Exception('分类修改失败');
  145. }
  146. } catch (\Exception $e) {
  147. return json_fail('分类修改失败');
  148. }
  149. return json_success('分类修改成功');
  150. }
  151. /**
  152. * @Desc 修改分类状态
  153. * @Author Gorden
  154. * @Date 2024/2/22 11:49
  155. *
  156. * @param $id
  157. * @param $params
  158. * @return \support\Response
  159. */
  160. public static function updateStatus($id, $params)
  161. {
  162. try {
  163. SysCategory::where('category_id', $id)->update(['category_status' => $params['category_status']]);
  164. } catch (\Exception $e) {
  165. return json_fail('分类状态修改失败');
  166. }
  167. return json_success('分类状态修改成功');
  168. }
  169. /**
  170. * @Desc 删除分类
  171. * @Author Gorden
  172. * @Date 2024/2/22 11:56
  173. *
  174. * @param $id
  175. * @return \support\Response
  176. */
  177. public static function delCategory(Request $request)
  178. {
  179. $ids = $request->post('category_id');
  180. if (!$ids) {
  181. return json_fail("数据错误");
  182. }
  183. try {
  184. if (is_array($ids)) {
  185. SysCategory::whereIn('category_id', $ids)->delete();
  186. } else {
  187. SysCategory::where('category_id', $ids)->delete();
  188. }
  189. } catch (\Exception $e) {
  190. return json_fail('分类删除失败');
  191. }
  192. return json_success('分类删除成功');
  193. }
  194. /**
  195. * @Desc 获取path
  196. * @Author Gorden
  197. * @Date 2024/2/22 10:40
  198. *
  199. * @param $categoryId
  200. * @return int|mixed
  201. */
  202. public static function getCategoryPath($categoryId)
  203. {
  204. $category = SysCategory::where('category_id', $categoryId)->first();
  205. if (!$category) {
  206. throw new \Exception('分类不存在');
  207. }
  208. if (empty($category->category_super_path)) {
  209. return "#" . $categoryId . "#";
  210. }
  211. return $category->category_super_path;
  212. }
  213. /**
  214. * Notes: 获取树状分类信息
  215. * User: ZhouBenXu
  216. * DateTime: 2024/7/2 下午2:52
  217. * @param $parentId
  218. * @return \support\Response
  219. */
  220. public static function getCategoryTree($parentId)
  221. {
  222. // 查询当前父级ID下的所有分类
  223. $list = self::getAllCategories($parentId);
  224. $allCategories = self::getTree($list, 152);
  225. return json_success('', $allCategories);
  226. }
  227. /**
  228. * Notes: 获取指定分类下的所有分类
  229. * User: ZhouBenXu
  230. * DateTime: 2024/7/2 下午2:41
  231. * @param $parentId
  232. * @param $categories
  233. * @return array|mixed
  234. */
  235. public static function getAllCategories($parentId = 0, $categories = [])
  236. {
  237. // 查询当前父级ID下的所有分类
  238. $row = SysCategory::where('category_super_id', $parentId)->where('category_status', 'ACTIVED')->get()->toArray();
  239. if (!empty($row)) {
  240. foreach ($row as $key => $value) {
  241. // 当前分类加入数组
  242. $value['label'] = $value['category_name'];
  243. $value['value'] = $value['category_id'];
  244. $categories[] = $value;
  245. // 递归查询子分类
  246. $categories = self::getAllCategories($value['category_id'], $categories);
  247. }
  248. }
  249. return $categories;
  250. }
  251. /**
  252. * Notes: 树状结构
  253. * User: ZhouBenXu
  254. * DateTime: 2024/7/2 下午2:51
  255. * @param $list
  256. * @param $id
  257. * @return array
  258. */
  259. public static function getTree($list = [], $id = 0)
  260. {
  261. $tree = array();
  262. foreach ($list as $k => $v) {
  263. if ($v['category_super_id'] == $id) {
  264. if (!empty(self::getTree($list, $v['category_id']))) {
  265. $v['children'] = self::getTree($list, $v['category_id']);
  266. }
  267. $tree[] = $v;
  268. }
  269. }
  270. return $tree;
  271. }
  272. /**
  273. * @Desc 分类详情
  274. * @Author Gorden
  275. * @Date 2024/2/22 11:37
  276. *
  277. * @param $id
  278. * @return \support\Response
  279. */
  280. public static function getCategoryInfos($ids)
  281. {
  282. $category = SysCategory::whereIn('category_id', $ids)->select(['category_id', 'category_name', 'category_super_id'])->get()->toArray();
  283. if (!$category) {
  284. return json_fail('分类不存在');
  285. }
  286. return $category;
  287. }
  288. /**
  289. * @Desc 获取分类及父级
  290. * @Author Gorden
  291. * @Date 2024/12/10 15:54
  292. *
  293. * @param $category_id
  294. * @return array
  295. */
  296. public static function getCategoryAndSupper($category_id = [])
  297. {
  298. $categoryNames = [];
  299. foreach ($category_id as $id) {
  300. $category = SysCategory::where('category_id', $id)->first();
  301. if (!empty($category) && !empty($category->category_super_path)) {
  302. $superIds = array_filter(array_unique(explode('#', $category->category_super_path)));
  303. $superCategory = SysCategory::whereIn('category_id', $superIds)->pluck('category_name', 'category_id')->toArray();
  304. foreach ($superIds as $superId) {
  305. if (key_exists($superId, $superCategory)) {
  306. if (isset($categoryNames[$id])) {
  307. $categoryNames[$id] = $categoryNames[$id] . $superCategory[$superId] . ' / ';
  308. }else{
  309. $categoryNames[$id] = $superCategory[$superId] . ' / ';
  310. }
  311. }
  312. }
  313. $categoryNames[$id] = $categoryNames[$id] . $category->category_name;
  314. }
  315. }
  316. return $categoryNames;
  317. }
  318. }