GoodsCategoryLists.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\workerapi\lists;
  3. use app\common\lists\ListsSearchInterface;
  4. use app\common\model\goods_category\GoodsCategory;
  5. use app\common\model\master_worker\MasterWorker;
  6. /**
  7. * GoodsCategory列表
  8. * Class GoodsCategoryLists
  9. * @package app\api\lists\goods_category
  10. */
  11. class GoodsCategoryLists extends BaseWorkerDataLists implements ListsSearchInterface
  12. {
  13. /**
  14. * @notes 设置搜索条件
  15. * @return \string[][]
  16. * @author likeadmin
  17. * @date 2024/07/07 18:23
  18. */
  19. public function setSearch(): array
  20. {
  21. return [
  22. '=' => ['category_type', 'name', 'is_goods', 'status'],
  23. ];
  24. }
  25. public function queryWhere(){
  26. $where = [];
  27. if(!empty($this->userId)){
  28. $ids = explode(',', MasterWorker::where('id', $this->userId)->value('category_ids'));
  29. if(!empty($ids)){
  30. $category_types = GoodsCategory::where('id', 'in', $ids)->group('category_type')->column('category_type');
  31. $where['category_type'] = ['in', $category_types];
  32. }
  33. }
  34. return $where;
  35. }
  36. /**
  37. * @notes 获取列表
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @author whitef
  43. * @date 2024/07/07 18:23
  44. */
  45. public function lists(): array
  46. {
  47. $lists = GoodsCategory::where($this->searchWhere)
  48. ->where($this->queryWhere())
  49. ->field(['id', 'pid' ,'picture', 'name'])
  50. ->order(['weigh' => 'desc'])
  51. ->select()
  52. ->toArray();
  53. return linear_to_tree($lists, 'children', 'id', 'pid');
  54. }
  55. /**
  56. * @notes 获取数量
  57. * @return int
  58. * @author whitef
  59. * @date 2024/07/07 18:23
  60. */
  61. public function count(): int
  62. {
  63. return GoodsCategory::where($this->searchWhere)->where($this->queryWhere())->count();
  64. }
  65. }