123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\model;
- use cores\BaseModel;
- use app\common\library\helper;
- use think\model\relation\HasOne;
- use app\common\enum\goods\SpecType as SpecTypeEnum;
- /**
- * 商品SKU模型
- * Class GoodsSku
- * @package app\common\model
- */
- class GoodsSku extends BaseModel
- {
- // 定义表名
- protected $name = 'goods_sku';
- // 定义主键
- protected $pk = 'id';
- /**
- * 关联模型:规格图片
- * @return HasOne
- */
- public function image(): HasOne
- {
- return $this->hasOne('UploadFile', 'file_id', 'image_id');
- }
- /**
- * 获取器:规格值ID集
- * @param $value
- * @return array|mixed
- */
- public function getSpecValueIdsAttr($value)
- {
- return helper::jsonDecode($value);
- }
- /**
- * 获取器:规格属性
- * @param $value
- * @return array|mixed
- */
- public function getGoodsPropsAttr($value)
- {
- return helper::jsonDecode($value);
- }
- /**
- * 设置器:规格值ID集
- * @param $value
- * @return string
- */
- public function setSpecValueIdsAttr($value): string
- {
- return helper::jsonEncode($value);
- }
- /**
- * 设置器:规格属性
- * @param $value
- * @return string
- */
- public function setGoodsPropsAttr($value): string
- {
- return helper::jsonEncode($value);
- }
- /**
- * 获取sku信息详情
- * @param int $goodsId
- * @param string $goodsSkuId
- * @return static|array|null
- */
- public static function detail(int $goodsId, string $goodsSkuId)
- {
- return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId], ['image']);
- }
- /**
- * 获取商品SKU列表
- * @param int $goodsId 商品ID
- * @param bool $withImage 是否携带sku封面图
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getSkuList(int $goodsId, bool $withImage = false): \think\Collection
- {
- return (new static)->with($withImage ? ['image'] : [])
- ->where('goods_id', '=', $goodsId)
- ->select();
- }
- /**
- * 生成skuList数据(写入goods_sku_id)
- * @param array $newSpecList
- * @param array $skuList
- * @return array
- */
- public static function getNewSkuList(array $newSpecList, array $skuList): array
- {
- foreach ($skuList as &$skuItem) {
- $skuItem['specValueIds'] = static::getSpecValueIds($newSpecList, $skuItem['skuKeys']);
- $skuItem['goodsProps'] = static::getGoodsProps($newSpecList, $skuItem['skuKeys']);
- $skuItem['goods_sku_id'] = implode('_', $skuItem['specValueIds']);
- }
- return $skuList;
- }
- /**
- * 根据$skuKeys生成规格值id集
- * @param array $newSpecList
- * @param array $skuKeys
- * @return array
- */
- private static function getSpecValueIds(array $newSpecList, array $skuKeys): array
- {
- $goodsSkuIdArr = [];
- foreach ($skuKeys as $skuKey) {
- $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
- $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
- $goodsSkuIdArr[] = $specValueItem['spec_value_id'];
- }
- return $goodsSkuIdArr;
- }
- /**
- * 根据$skuKeys生成规格属性记录
- * @param array $newSpecList
- * @param array $skuKeys
- * @return array
- */
- private static function getGoodsProps(array $newSpecList, array $skuKeys): array
- {
- $goodsPropsArr = [];
- foreach ($skuKeys as $skuKey) {
- $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
- $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
- $goodsPropsArr[] = [
- 'group' => ['name' => $groupItem['spec_name'], 'id' => $groupItem['spec_id']],
- 'value' => ['name' => $specValueItem['spec_value'], 'id' => $specValueItem['spec_value_id']]
- ];
- }
- return $goodsPropsArr;
- }
- /**
- * 新增商品sku记录
- * @param int $goodsId
- * @param array $newSkuList
- * @param int $specType
- * @param int|null $storeId 商城ID
- * @return array|bool|false
- */
- public static function add(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $newSkuList = [], int $storeId = null)
- {
- // 单规格模式
- if ($specType === SpecTypeEnum::SINGLE) {
- return (new static)->save(array_merge($newSkuList, [
- 'goods_id' => $goodsId,
- 'goods_sku_id' => 0,
- 'store_id' => $storeId ?: self::$storeId
- ]));
- } // 多规格模式
- elseif ($specType === SpecTypeEnum::MULTI) {
- // 批量写入商品sku记录
- return static::increasedFroMulti($goodsId, $newSkuList, $storeId);
- }
- return false;
- }
- /**
- * 批量写入商品sku记录
- * @param int $goodsId
- * @param array $skuList
- * @param int|null $storeId 商城ID
- * @return array|false
- */
- private static function increasedFroMulti(int $goodsId, array $skuList, int $storeId = null)
- {
- $dataset = [];
- foreach ($skuList as $skuItem) {
- $dataset[] = array_merge($skuItem, [
- 'id' => null, // 此处的id必须是数据库自增
- 'goods_sku_id' => $skuItem['goods_sku_id'],
- 'goods_price' => $skuItem['goods_price'] ?: 0.01,
- 'line_price' => $skuItem['line_price'] ?: 0.00,
- 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
- 'stock_num' => $skuItem['stock_num'] ?: 0,
- 'goods_weight' => $skuItem['goods_weight'] ?: 0,
- 'goods_props' => $skuItem['goodsProps'],
- 'spec_value_ids' => $skuItem['specValueIds'],
- 'goods_id' => $goodsId,
- 'store_id' => $storeId ?: self::$storeId
- ]);
- }
- return (new static)->addAll($dataset);
- }
- /**
- * 获取库存总数量 (根据sku列表数据)
- * @param array $skuList
- * @return float|int
- */
- public static function getStockTotal(array $skuList)
- {
- return (int)helper::getArrayColumnSum($skuList, 'stock_num');
- }
- /**
- * 获取商品价格高低区间 (根据sku列表数据)
- * @param array $skuList
- * @return array
- */
- public static function getGoodsPrices(array $skuList): array
- {
- $goodsPriceArr = helper::getArrayColumn($skuList, 'goods_price');
- return empty($goodsPriceArr) ? [0, 0] : [min($goodsPriceArr), max($goodsPriceArr)];
- }
- /**
- * 获取划线价格高低区间 (根据sku列表数据)
- * @param array $skuList
- * @return array
- */
- public static function getLinePrices(array $skuList): array
- {
- $linePriceArr = helper::getArrayColumn($skuList, 'line_price');
- return empty($linePriceArr) ? [0, 0] : [min($linePriceArr), max($linePriceArr)];
- }
- }
|