GoodService.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace app\common\Service\Goods;
  3. use app\common\model\Goods;
  4. use app\common\Enum\GoodsEnum;
  5. class GoodService
  6. {
  7. /**
  8. * 根据分类ID获取商品列表
  9. * @param int $categoryId 分类ID
  10. * @param int $page 页码
  11. * @param int $pageSize 每页条数
  12. * @return \think\Paginator
  13. */
  14. public static function getCategoryGoods($arrCategoryId = [], $page = 0, $pageSize = 10){
  15. $query = Goods::where(function ($query) use ($arrCategoryId) {
  16. // 所有子分类使用 find_in_set or 匹配,亲测速度并不慢
  17. foreach ($arrCategoryId as $key => $category_id) {
  18. $query->whereOrRaw("find_in_set($category_id, category_ids)");
  19. }
  20. })->where('status',GoodsEnum::STATUS_ON_SALE)->order('weigh desc,id desc');
  21. $arrfield = ['id','title','image','category_ids','price','sales','views','description','lineation_price','is_hot','createtime'];
  22. // 判断是否分页
  23. if($page > 0){
  24. $nStart = $page * $pageSize;
  25. $goodsList = $query->field($arrfield)
  26. ->limit($nStart, $pageSize)
  27. ->select();
  28. }else{
  29. $goodsList = $query->field($arrfield)
  30. ->select();
  31. }
  32. $arrGoods = collection($goodsList)->toArray();
  33. return $arrGoods;
  34. }
  35. }