FirmGoodLists.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @author 林海涛
  4. * @date 2024/7/15 下午3:41
  5. */
  6. namespace app\api\lists;
  7. use app\common\model\goods\Goods;
  8. use app\common\lists\ListsSearchInterface;
  9. use app\common\model\goods_category\GoodsCategory;
  10. use app\common\service\FileService;
  11. use think\db\Query;
  12. /**
  13. * Goods列表
  14. * Class GoodsLists
  15. * @package app\adminapi\listsgoods
  16. */
  17. class FirmGoodLists extends BaseApiDataLists implements ListsSearchInterface
  18. {
  19. /**
  20. * @notes 设置搜索条件
  21. * @return \string[][]
  22. * @author likeadmin
  23. * @date 2024/07/07 18:37
  24. */
  25. public function setSearch(): array
  26. {
  27. return [
  28. '=' => ['goods_status'],
  29. '%like%' => ['goods_name', 'goods_brand'],
  30. 'between' => ['service_total', 'service_fee'],
  31. ];
  32. }
  33. public function queryWhere()
  34. {
  35. $where = [];
  36. $where[] = ['user_id','=',$this->userId];
  37. $where[] = ['platform_value','=',0];
  38. if (!empty($this->params['goods_category_id'])) {
  39. $goodsCategoryId = end($this->params['goods_category_id']);
  40. $goodsCategoryData = GoodsCategory::where(['status' => 1])->order(['pid' => 'asc', 'weigh' => 'desc', 'id' => 'desc'])
  41. ->select()->toArray();
  42. $ids = get_tree_ids($goodsCategoryData, $goodsCategoryId);
  43. $ids[] = $goodsCategoryId;
  44. $where[] = ['goods_category_id', 'in', $ids];
  45. }
  46. // 创建时间
  47. if (!empty($this->params['create_time'])) {
  48. $time = [strtotime($this->params['create_time'][0]), strtotime($this->params['create_time'][1])];
  49. $where[] = ['create_time', 'between', $time];
  50. }
  51. //更新时间
  52. if (!empty($this->params['update_time'])) {
  53. $time = [strtotime($this->params['update_time'][0]), strtotime($this->params['update_time'][1])];
  54. $where[] = ['update_time', 'between', $time];
  55. }
  56. return $where;
  57. }
  58. /**
  59. * @notes 获取列表
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @author likeadmin
  65. * @date 2024/07/07 18:37
  66. */
  67. public function lists(): array
  68. {
  69. $lists = Goods::order(['id' => 'desc'])
  70. ->with(['goods_category' => function (Query $query) {
  71. $query->visible(['name']);
  72. }])
  73. ->where($this->searchWhere)
  74. ->where($this->queryWhere())
  75. ->limit($this->limitOffset, $this->limitLength)
  76. ->select()
  77. ->toArray();
  78. $goodsCategoryObj = GoodsCategory::where(['status' => 1])->order(['pid' => 'asc', 'weigh' => 'desc', 'id' => 'desc'])
  79. ->select();
  80. foreach ($lists as &$item) {
  81. $item['goods_category_ids'] = array_map("intval", $item['goods_category_ids']);
  82. $item['goods_category_ids_str'] = implode(' / ', $goodsCategoryObj->whereIn('id', $item['goods_category_ids'])->column('name'));
  83. $item['goods_image'] = $item['goods_image'] ? FileService::getFileUrl($item['goods_image']) : null;
  84. $item['goods_video'] = $item['goods_video'] ? FileService::getFileUrl($item['goods_video']) : null;
  85. }
  86. return $lists;
  87. }
  88. /**
  89. * @notes 获取数量
  90. * @return int
  91. * @author likeadmin
  92. * @date 2024/07/07 18:37
  93. */
  94. public function count(): int
  95. {
  96. return Goods::where($this->searchWhere)->where($this->queryWhere())->count();
  97. }
  98. }