GoodsLists.php 3.9 KB

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