Category.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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. use cores\BaseModel;
  14. use think\model\relation\HasOne;
  15. /**
  16. * 商品分类模型
  17. * Class Category
  18. * @package app\common\model
  19. */
  20. class Category extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'category';
  24. // 定义主键
  25. protected $pk = 'category_id';
  26. /**
  27. * 分类图片
  28. * @return HasOne
  29. */
  30. public function image(): HasOne
  31. {
  32. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  33. }
  34. /**
  35. * 分类详情
  36. * @param int|array $where
  37. * @param array $with
  38. * @return static|array|null
  39. */
  40. public static function detail($where, array $with = [])
  41. {
  42. return static::get($where, $with);
  43. }
  44. /**
  45. * 获取列表记录
  46. * @param array $param
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getList(array $param = []): array
  53. {
  54. $list = $this->getAll($param);
  55. return $this->getTreeData($list);
  56. }
  57. /**
  58. * 获取所有记录
  59. * @param array $param
  60. * @return \think\Collection
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. protected function getAll(array $param = []): \think\Collection
  66. {
  67. // 默认查询参数
  68. $params = $this->setQueryDefaultValue($param, [
  69. 'status' => -1 // 状态(1显示 0隐藏 -1全部)
  70. ]);
  71. // 设置检索条件
  72. $filter = [];
  73. $params['status'] > -1 && $filter[] = ['status', '=', $params['status']];
  74. // 查询列表数据
  75. return $this->with(['image'])
  76. ->where($filter)
  77. ->order(['sort', 'create_time'])
  78. ->select();
  79. }
  80. /**
  81. * 获取树状列表
  82. * @param $list
  83. * @param int $parentId
  84. * @return array
  85. */
  86. private function getTreeData($list, int $parentId = 0): array
  87. {
  88. $data = [];
  89. foreach ($list as $key => $item) {
  90. if ($item['parent_id'] == $parentId) {
  91. $children = $this->getTreeData($list, $item['category_id']);
  92. !empty($children) && $item['children'] = $children;
  93. $data[] = $item;
  94. unset($list[$key]);
  95. }
  96. }
  97. return $data;
  98. }
  99. /**
  100. * 过滤不存在的分类ID集
  101. * @param array $categoryIds
  102. * @param int|null $storeId
  103. * @return array
  104. */
  105. public static function filterCategoryIds(array $categoryIds, int $storeId = null): array
  106. {
  107. return (new static)->where('category_id', 'in', $categoryIds)
  108. ->where('store_id', '=', $storeId ?: self::$storeId)
  109. ->column('category_id');
  110. }
  111. }