GoodsCategoryLists.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Log::write('用户'.$this->userId.'可查看的分类类型:'.implode(',', $category_types));
  33. $where['category_type'] = ['in', $category_types];
  34. }
  35. }
  36. return $where;
  37. }
  38. /**
  39. * @notes 获取列表
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @author whitef
  45. * @date 2024/07/07 18:23
  46. */
  47. public function lists(): array
  48. {
  49. $lists = GoodsCategory::where($this->searchWhere)
  50. ->where($this->queryWhere())
  51. ->field(['id', 'pid' ,'picture', 'name'])
  52. ->order(['weigh' => 'desc'])
  53. ->select()
  54. ->toArray();
  55. return linear_to_tree($lists, 'children', 'id', 'pid');
  56. }
  57. /**
  58. * @notes 获取数量
  59. * @return int
  60. * @author whitef
  61. * @date 2024/07/07 18:23
  62. */
  63. public function count(): int
  64. {
  65. return GoodsCategory::where($this->searchWhere)->where($this->queryWhere())->count();
  66. }
  67. }