123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace app\common\Service;
- use app\common\model\SkuSpec as SkuSpecModel;
- use think\Db;
- class SkuSpec
- {
- /**
- * 获取指定商品的SKU信息
- * @param int $goods_id 商品ID
- * @return array
- */
- public static function getGoodsSkuSpec($goods_id,$nType = 1)
- {
- $list = (new SkuSpecModel())
- ->field('MIN(`id`) AS `id`, MIN(`goods_id`) AS `goods_id`, `spec_id`')
- ->where('type', $nType)
- ->where('goods_id', $goods_id)
- ->with([
- 'Spec',
- 'SkuValue' => function ($query) use ($goods_id) {
- $query->where('goods_id', $goods_id)
- ->field('id,goods_id,spec_id,spec_value_id')
- ->with(['SpecValue']);
- }
- ])->group('spec_id')->select();
- $list = collection($list)->toArray();
- return $list;
- }
- /**
- * 批量获取SKU规格属性字符串
- * @param array $sku_ids SKU ID数组
- * @return array 返回 [sku_id => 'spec_name:spec_value,spec_name:spec_value'] 格式
- */
- public static function getSkuAttrs($sku_ids)
- {
- if (empty($sku_ids)) {
- return [];
- }
- // 通过shop_goods_sku表的spec_value_ids字段来查询规格信息
- $list = Db::name('shop_goods_sku')
- ->alias('sku')
- ->field('sku.id, GROUP_CONCAT(sp.name,":",sv.value ORDER BY sp.id asc) as sku_attr')
- ->join('shop_goods_sku_spec gss', "FIND_IN_SET(gss.id, sku.spec_value_ids)", 'LEFT')
- ->join('shop_spec sp', 'sp.id = gss.spec_id', 'LEFT')
- ->join('shop_spec_value sv', 'sv.id = gss.spec_value_id', 'LEFT')
- ->where('sku.id', 'in', $sku_ids)
- ->group('sku.id')
- ->select();
- $result = [];
- foreach ($list as $item) {
- $result[$item['id']] = $item['sku_attr'] ?: '';
- }
- return $result;
- }
- }
|