GoodService.php 1.6 KB

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