GoodsLists.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\lists;
  15. use app\common\model\goods\Goods;
  16. use app\common\lists\ListsSearchInterface;
  17. use app\common\model\goods_category\GoodsCategory;
  18. use app\common\service\FileService;
  19. /**
  20. * Goods列表
  21. * Class GoodsLists
  22. * @package app\adminapi\listsgoods
  23. */
  24. class GoodsLists extends BaseApiDataLists implements ListsSearchInterface
  25. {
  26. /**
  27. * @notes 设置搜索条件
  28. * @return \string[][]
  29. * @author likeadmin
  30. * @date 2024/07/07 18:37
  31. */
  32. public function setSearch(): array
  33. {
  34. return [
  35. '=' => ['goods_status'],
  36. '%like%' => ['goods_name','goods_brand'],
  37. 'between' => ['service_total', 'service_fee'],
  38. ];
  39. }
  40. public function queryWhere()
  41. {
  42. $where = [];
  43. $where['user_id'] = 0;//常规商品
  44. if (!empty($this->params['goods_category_id'])) {
  45. $goodsCategoryId = end($this->params['goods_category_id']);
  46. $goodsCategoryData = GoodsCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
  47. ->select()->toArray();
  48. $ids =get_tree_ids($goodsCategoryData,$goodsCategoryId);
  49. $ids[] = $goodsCategoryId;
  50. $where[] = ['goods_category_id','in' ,$ids];
  51. }
  52. return $where;
  53. }
  54. /**
  55. * @notes 获取列表
  56. * @return array
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @author likeadmin
  61. * @date 2024/07/07 18:37
  62. */
  63. public function lists(): array
  64. {
  65. $lists = Goods::order(['id' => 'desc'])
  66. ->where($this->searchWhere)
  67. ->where($this->queryWhere())
  68. ->visible([
  69. 'id','goods_image','goods_video','goods_category_ids',
  70. 'goods_number','good_unit','base_service_fee',
  71. 'service_total','service_fee','service_image','fee_schedule','warranty_period'
  72. ])
  73. ->limit($this->limitOffset, $this->limitLength)
  74. ->select()
  75. ->toArray();
  76. $goodsCategoryObj = GoodsCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
  77. ->select();
  78. foreach ($lists as &$item) {
  79. $item['goods_category_ids'] = array_map("intval",$item['goods_category_ids']);
  80. $item['goods_category_ids_str'] = implode(' / ',$goodsCategoryObj->whereIn('id', $item['goods_category_ids'])->column('name'));
  81. $item['goods_image'] = $item['goods_image'] ? FileService::getFileUrl($item['goods_image']):null;
  82. $item['goods_video'] = $item['goods_video'] ? FileService::getFileUrl($item['goods_video']):null;
  83. }
  84. return $lists;
  85. }
  86. /**
  87. * @notes 获取数量
  88. * @return int
  89. * @author likeadmin
  90. * @date 2024/07/07 18:37
  91. */
  92. public function count(): int
  93. {
  94. return Goods::where($this->searchWhere)->where($this->queryWhere())->count();
  95. }
  96. }