Category.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model;
  13. /**
  14. * 商品分类模型
  15. * Class Category
  16. * @package app\common\model
  17. */
  18. class Category extends BaseModel
  19. {
  20. // 定义表名
  21. protected $name = 'category';
  22. // 定义主键
  23. protected $pk = 'category_id';
  24. /**
  25. * 分类图片
  26. * @return \think\model\relation\HasOne
  27. */
  28. public function image()
  29. {
  30. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  31. }
  32. public function goods()
  33. {
  34. return $this->hasMany(GoodsCategoryRel::class, 'category_id', 'category_id');
  35. }
  36. /**
  37. * 分类详情
  38. * @param int|array $where
  39. * @param array $with
  40. * @return array|null|static
  41. */
  42. public static function detail($where, array $with = [])
  43. {
  44. return static::get($where, $with);
  45. }
  46. /**
  47. * 获取列表记录
  48. * @param array $param
  49. * @return array
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function getList(array $param = [])
  55. {
  56. $list = $this->getAll($param);
  57. return $this->getTreeData($list);
  58. }
  59. /**
  60. * 获取所有记录
  61. * @param array $param
  62. * @return \think\Collection
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. protected function getAll(array $param = [])
  68. {
  69. // 默认查询参数
  70. $params = $this->setQueryDefaultValue($param, [
  71. 'status' => -1 // 状态(1显示 0隐藏 -1全部)
  72. ]);
  73. // 设置检索条件
  74. $filter = [];
  75. $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
  76. // 查询列表数据
  77. $list = $this->with(['image'])
  78. ->withCount(['goods'])
  79. ->where($filter)
  80. ->order(['sort', 'create_time'])
  81. ->select();
  82. if (!empty($list)) {
  83. foreach($list as $item) {
  84. $item->goods_count = GoodsCategoryRel::alias('gcr')
  85. ->leftJoin('goods', 'goods.goods_id=gcr.goods_id')
  86. ->where('goods.is_delete', 0)
  87. ->where('goods.status', 10)
  88. ->where('gcr.category_id', $item->category_id)
  89. ->count();
  90. }
  91. }
  92. return $list;
  93. }
  94. /**
  95. * 获取树状列表
  96. * @param $list
  97. * @param int $parentId
  98. * @return array
  99. */
  100. private function getTreeData($list, int $parentId = 0)
  101. {
  102. $data = [];
  103. foreach ($list as $key => $item) {
  104. if ($item['parent_id'] == $parentId) {
  105. $children = $this->getTreeData($list, $item['category_id']);
  106. !empty($children) && $item['children'] = $children;
  107. $data[] = $item;
  108. unset($list[$key]);
  109. }
  110. }
  111. return $data;
  112. }
  113. }