GoodsLists.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if (!empty($this->params['goods_category_id'])) {
  44. $goodsCategoryId = end($this->params['goods_category_id']);
  45. $goodsCategoryData = GoodsCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
  46. ->select()->toArray();
  47. $ids =get_tree_ids($goodsCategoryData,$goodsCategoryId);
  48. $ids[] = $goodsCategoryId;
  49. $where[] = ['goods_category_id','in' ,$ids];
  50. }
  51. return $where;
  52. }
  53. /**
  54. * @notes 获取列表
  55. * @return array
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @author likeadmin
  60. * @date 2024/07/07 18:37
  61. */
  62. public function lists(): array
  63. {
  64. $lists = Goods::order(['id' => 'desc'])
  65. ->where($this->searchWhere)
  66. ->where($this->queryWhere())
  67. ->visible([
  68. 'id','goods_image','goods_video','goods_category_ids',
  69. 'goods_number','good_unit','base_service_fee',
  70. 'service_total','service_fee','service_image','fee_schedule','warranty_period'
  71. ])
  72. ->limit($this->limitOffset, $this->limitLength)
  73. ->select()
  74. ->toArray();
  75. $goodsCategoryObj = GoodsCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
  76. ->select();
  77. foreach ($lists as &$item) {
  78. $item['goods_category_ids'] = array_map("intval",$item['goods_category_ids']);
  79. $item['goods_category_ids_str'] = implode(' / ',$goodsCategoryObj->whereIn('id', $item['goods_category_ids'])->column('name'));
  80. $item['goods_image'] = $item['goods_image'] ? FileService::getFileUrl($item['goods_image']):null;
  81. $item['goods_video'] = $item['goods_video'] ? FileService::getFileUrl($item['goods_video']):null;
  82. }
  83. return $lists;
  84. }
  85. /**
  86. * @notes 获取数量
  87. * @return int
  88. * @author likeadmin
  89. * @date 2024/07/07 18:37
  90. */
  91. public function count(): int
  92. {
  93. return Goods::where($this->searchWhere)->where($this->queryWhere())->count();
  94. }
  95. }