123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace app\admin\service\sys_manage;
- use app\model\SysCategory;
- use support\Db;
- use support\Request;
- class CategoryService
- {
- /**
- * @Desc 分类列表
- * @Author Gorden
- * @Date 2024/2/22 11:28
- *
- * @param $page
- * @param $limit
- * @param $keywords
- * @return \support\Response
- */
- public static function categoryList($page, $limit, $keywords)
- {
- $list = SysCategory::select('*')
- ->when($keywords != '', function ($query) use ($keywords) {
- $query->where('category_name', 'like', '%' . $keywords . '%');
- })
- ->orderBy('category_addtimes', 'DESC')
- ->forPage($page, $limit)
- ->get()
- ->toArray();
- $count = SysCategory::when($keywords != '', function ($query) use ($keywords) {
- $query->where('category_name', 'like', '%' . $keywords . '%');
- })->count();
- return json_success('', compact('list', 'page', 'limit', 'count'));
- }
- /**
- * @Desc 分类详情
- * @Author Gorden
- * @Date 2024/2/22 11:37
- *
- * @param $id
- * @return \support\Response
- */
- public static function categoryInfo($id)
- {
- $category = SysCategory::where('category_id', $id)->first();
- if (!$category) {
- return json_fail('分类不存在');
- }
- return json_success('', $category->toArray());
- }
- /**
- * @Desc 添加分类
- * @Author Gorden
- * @Date 2024/2/22 10:25
- *
- * @param $params
- * @return \support\Response
- */
- public static function insertCategory($params)
- {
- DB::beginTransaction();
- try {
- $data = [
- 'category_name' => $params['category_name'],
- 'category_super_id' => $params['category_super_id'] ?? 0,
- 'category_status' => $params['category_status'],
- 'category_classify' => $params['category_classify'] ?? '',
- 'category_type' => $params['category_type'] ?? '',
- 'category_icon' => $params['category_icon'] ?? '',
- 'category_images' => $params['category_images'] ?? '',
- 'category_url' => $params['category_url'] ?? '',
- 'category_page' => $params['category_page'] ?? '',
- 'category_sort' => $params['category_sort'] ?? '',
- 'category_groupby' => $params['category_groupby'] ?? '',
- 'category_remark' => $params['category_remark'] ?? '',
- 'category_extend_json' => !empty($params['category_extend_json']) ? $params['category_extend_json'] : '{}',
- 'category_addtimes' => time()
- ];
- $categoryId = SysCategory::insertGetId($data);
- if (!$categoryId) {
- throw new \Exception('添加分类失败');
- }
- // 不是顶级分类,有path
- if ($params['category_super_id'] != 0) {
- // 回填path
- $categoryPath = self::getCategoryPath($params['category_super_id']);
- SysCategory::where('category_id', $categoryId)->update(['category_super_path' => $categoryPath . '#' . $params['category_super_id'] . '#']);
- }
- // 提交事务
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- dump($e->getMessage());
- return json_fail('添加分类失败');
- }
- return json_success('添加分类成功');
- }
- /**
- * @Desc 修改分类
- * @Author Gorden
- * @Date 2024/2/22 11:14
- *
- * @param $id
- * @param $params
- * @return \support\Response
- */
- public static function updateCategory($id, $params)
- {
- try {
- $category = SysCategory::select('category_super_id')->where('category_id', $id)->first();
- if (!$category) {
- throw new \Exception('分类不存在');
- }
- $data = [
- 'category_name' => $params['category_name'],
- 'category_classify' => $params['category_classify'] ?? '',
- 'category_type' => $params['category_type'] ?? '',
- 'category_icon' => $params['category_icon'] ?? '',
- 'category_images' => $params['category_images'] ?? '',
- 'category_url' => $params['category_url'] ?? '',
- 'category_page' => $params['category_page'] ?? '',
- 'category_sort' => $params['category_sort'] ?? '',
- 'category_groupby' => $params['category_groupby'] ?? '',
- 'category_remark' => $params['category_remark'] ?? '',
- 'category_status' => $params['category_status'] ?? '',
- 'category_extend_json' => !empty($params['category_extend_json']) ? $params['category_extend_json'] : '{}',
- 'category_addtimes' => time()
- ];
- if ($category->category_super_id != $params['category_super_id'] && $params['category_super_id'] != 0) {
- $data['category_super_id'] = $params['category_super_id'];
- $data['category_super_path'] = self::getCategoryPath($params['category_super_id']) . '#' . $params['category_super_id'] . '#';
- }
- if (!SysCategory::where('category_id', $id)->update($data)) {
- throw new \Exception('分类修改失败');
- }
- } catch (\Exception $e) {
- return json_fail('分类修改失败');
- }
- return json_success('分类修改成功');
- }
- /**
- * @Desc 修改分类状态
- * @Author Gorden
- * @Date 2024/2/22 11:49
- *
- * @param $id
- * @param $params
- * @return \support\Response
- */
- public static function updateStatus($id, $params)
- {
- try {
- SysCategory::where('category_id', $id)->update(['category_status' => $params['category_status']]);
- } catch (\Exception $e) {
- return json_fail('分类状态修改失败');
- }
- return json_success('分类状态修改成功');
- }
- /**
- * @Desc 删除分类
- * @Author Gorden
- * @Date 2024/2/22 11:56
- *
- * @param $id
- * @return \support\Response
- */
- public static function delCategory(Request $request)
- {
- $ids = $request->post('category_id');
- if (!$ids) {
- return json_fail("数据错误");
- }
- try {
- if (is_array($ids)) {
- SysCategory::whereIn('category_id', $ids)->delete();
- } else {
- SysCategory::where('category_id', $ids)->delete();
- }
- } catch (\Exception $e) {
- return json_fail('分类删除失败');
- }
- return json_success('分类删除成功');
- }
- /**
- * @Desc 获取path
- * @Author Gorden
- * @Date 2024/2/22 10:40
- *
- * @param $categoryId
- * @return int|mixed
- */
- public static function getCategoryPath($categoryId)
- {
- $category = SysCategory::where('category_id', $categoryId)->first();
- if (!$category) {
- throw new \Exception('分类不存在');
- }
- if (empty($category->category_super_path)) {
- return "#" . $categoryId . "#";
- }
- return $category->category_super_path;
- }
- }
|