123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace app\common\Service\Goods;
- use app\common\model\Goods;
- use app\common\Enum\GoodsEnum;
- use app\common\model\GoodsSkuSpec;
- 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;
- }
- // 查询商品关联规格
- public static function getGoodsCustomizedSpec($goods_id = 0, $type = 0)
- {
- $goodsSkuSpec = GoodsSkuSpec::where('id', $goods_id)
- ->where('type', $type)
- ->find();
-
- return $goodsSkuSpec;
- }
- }
|