1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace app\common\Service\Goods;
- use app\common\model\Goods;
- use app\common\Enum\GoodsEnum;
- class GoodService
- {
- /**
- * 根据分类ID获取商品列表
- * @param int $categoryId 分类ID
- * @param int $page 页码
- * @param int $pageSize 每页条数
- * @return \think\Paginator
- */
- public static function getCategoryGoods($arrCategoryId = [], $page = 0, $pageSize = 10){
-
- $query = Goods::where(function ($query) use ($arrCategoryId) {
- // 所有子分类使用 find_in_set or 匹配,亲测速度并不慢
- foreach ($arrCategoryId as $key => $category_id) {
- $query->whereOrRaw("find_in_set($category_id, category_ids)");
- }
- })->where('status',GoodsEnum::STATUS_ON_SALE)->order('weigh desc,id desc');
- $arrfield = ['id','title','image','category_ids','price','sales','views','description','lineation_price','is_hot','createtime'];
- // 判断是否分页
- if($page > 0){
- $nStart = $page * $pageSize;
- $goodsList = $query->field($arrfield)
- ->limit($nStart, $pageSize)
- ->select();
- }else{
- $goodsList = $query->field($arrfield)
- ->select();
- }
- $arrGoods = collection($goodsList)->toArray();
- return $arrGoods;
- }
- }
|