GoodsCategoryLists.php 2.0 KB

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