// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\model; use app\common\model\GoodsSku as GoodsSkuModel; use app\common\model\Goods as GoodsModel; use app\common\enum\goods\SpecType as SpecTypeEnum; /** * 商品规格模型 * Class GoodsSku * @package app\store\model */ class GoodsSku extends GoodsSkuModel { /** * 更新商品sku记录 * @param int $goodsId * @param int $specType * @param array $skuList * @return array|bool|false */ public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = []) { // echo "
"; // print_r($skuList); // exit(); //单规格修改还按照之前的逻辑 if ($specType == SpecTypeEnum::SINGLE) { // 删除所有的sku记录 static::deleteAll(['goods_id' => $goodsId]); // 新增商品sku记录 return static::add($goodsId, $specType, $skuList); } else { $goodsIds = array_column($skuList, "goods_id"); if (!in_array($goodsId, $goodsIds)) { return "当前商品没有被选择到sku里面"; } $goodsList = GoodsModel::whereIn('goods_id', $goodsIds)->select(); foreach ($goodsList as $value) { //非当前商品中,有多规格商品 if ($value['goods_id'] != $goodsId && $value['spec_type'] == 20) { return '当前商品中有多规格商品'; } } //设置spu_id为当前商品id的商品的spu_id=0; by liuqing GoodsModel::where('spu_id', $goodsId)->update(['spu_id' => 0]); GoodsSku::where('spu_id', $goodsId)->update(['spu_id' => 0, 'goods_props' => "", 'spec_value_ids' => '']); //更新sku列表里面的商品id的spu_id GoodsModel::whereIn('goods_id', $goodsIds)->update(['spu_id' => $goodsId]); foreach ($skuList as $skuItem) { $temp = [ 'goods_props' => json_encode($skuItem['goodsProps'], JSON_UNESCAPED_UNICODE), 'spec_value_ids' => json_encode($skuItem['specValueIds'], JSON_UNESCAPED_UNICODE), 'spu_id' => $goodsId ]; if ($skuItem['goods_id'] == $goodsId) { $temp['goods_price'] = $skuItem['goods_price'] ?? 0.01; $temp['cost_price'] = $skuItem['cost_price'] ?? 0.00; $temp['goods_sku_no'] = $skuItem['goods_sku_no'] ?? ''; $temp['stock_num'] = $skuItem['stock_num'] ?? 0; $temp['goods_weight'] = $skuItem['goods_weight'] ?? 0; } GoodsSku::where('goods_id', $skuItem['goods_id'])->update($temp); } return true; } } }