作者 赵彬吉

update

@@ -26,6 +26,9 @@ class CategoryController extends BaseController @@ -26,6 +26,9 @@ class CategoryController extends BaseController
26 } 26 }
27 $sort = ['id' => 'desc']; 27 $sort = ['id' => 'desc'];
28 $data = $logic->getList($map, $sort, ['id', 'pid', 'title', 'image', 'keywords', 'describe', 'status','created_at'],0); 28 $data = $logic->getList($map, $sort, ['id', 'pid', 'title', 'image', 'keywords', 'describe', 'status','created_at'],0);
  29 + foreach ($data as &$v){
  30 + $v['product_num'] = $logic->getProductNum($v['id']);
  31 + }
29 return $this->success(Arr::listToTree($data)); 32 return $this->success(Arr::listToTree($data));
30 } 33 }
31 34
@@ -54,4 +54,16 @@ class CategoryLogic extends BaseLogic @@ -54,4 +54,16 @@ class CategoryLogic extends BaseLogic
54 } 54 }
55 return parent::delete($ids); 55 return parent::delete($ids);
56 } 56 }
  57 +
  58 + /**
  59 + * 关联产品数量
  60 + * @param $cate_id
  61 + * @return mixed
  62 + * @author zbj
  63 + * @date 2023/4/28
  64 + */
  65 + public function getProductNum($cate_id){
  66 + $ids = $this->model->getChildIdsArr($cate_id);
  67 + return CategoryRelated::whereIn('cate_id', $ids)->count();
  68 + }
57 } 69 }
@@ -14,6 +14,12 @@ class Category extends Base @@ -14,6 +14,12 @@ class Category extends Base
14 //设置关联表名 14 //设置关联表名
15 protected $table = 'gl_product_category'; 15 protected $table = 'gl_product_category';
16 16
  17 + /**
  18 + * 子分类
  19 + * @var array
  20 + */
  21 + protected $child_ids_arr = [];
  22 +
17 public function getImageAttribute($value) 23 public function getImageAttribute($value)
18 { 24 {
19 return Upload::path2url($value); 25 return Upload::path2url($value);
@@ -23,4 +29,37 @@ class Category extends Base @@ -23,4 +29,37 @@ class Category extends Base
23 { 29 {
24 $this->attributes['image'] = Upload::url2path($value); 30 $this->attributes['image'] = Upload::url2path($value);
25 } 31 }
  32 +
  33 +
  34 + /**
  35 + * 获取指定分类的所有子分类IDS(包括自己)
  36 + * @param $id
  37 + * @return array
  38 + * @author zbj
  39 + * @date 2023/4/28
  40 + */
  41 + public function getChildIdsArr($id)
  42 + {
  43 + $this->child_ids_arr = [$id];
  44 + return $this->getChildrenIdArr($id);
  45 + }
  46 +
  47 + /**
  48 + * 递归获取指定分类的所有子孙
  49 + * @param $id
  50 + * @return array
  51 + * @author zbj
  52 + * @date 2023/4/28
  53 + */
  54 + protected function getChildrenIdArr($id)
  55 + {
  56 + $list = parent::where("pid", $id)->pluck('pid', 'id');
  57 + if ($list) {
  58 + foreach ($list as $id => $pid) {
  59 + $this->child_ids_arr[] = $id;
  60 + $this->getChildrenIdArr($id);
  61 + }
  62 + }
  63 + return $this->child_ids_arr;
  64 + }
26 } 65 }