0], [['purchase_limit_num'],'default', 'value' => 0] ]; } // 关联模型 public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id']); } public function getDepositPresaleActive() { return $this->hasOne(DepositPresaleActive::class, ['id' => 'deposit_presale_active_id']); } public function getGoodsAttr() { return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']); } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'deposit_presale_active_id' => 'qimall_addons_deposit_presale_active.id', 'goods_id' => '商品id;对应goods主表', 'attr_id' => '商品规格id;对应goods_attr.id;', 'deposit_presale_price' => '预售价格', 'deposit_price' => '定金', 'stock' => '定金预售库存', 'purchase_limit_num' => '限购数量', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Update At', ]; } public static function setData(array $params) { try { $presale_price = $params['deposit_presale_goods']['presale_price']; $deposit_price = $params['deposit_presale_goods']['deposit_price']; if ($params['deposit_presale_goods']['is_attr'] == 1) { $goods_id = 0; foreach ($params['form']['select_attr_groups'] as $item) { $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]); if (!$model) { $model = new self(); } $model->loadDefaultValues(); if(!$goods_id) $goods_id = $item['goods_id']; $insert_data = [ 'mall_id' => $params['mall_id'], 'attr_id' => $item['id'], 'deposit_presale_price' => $item['deposit_presale_price'], 'deposit_price' => $item['deposit_price'], 'deposit_presale_active_id' => $params['deposit_presale_active_id'], 'goods_id' => $item['goods_id'] ?? $goods_id, 'stock' => $item['deposit_stock'], 'purchase_limit_num' => $item['purchase_limit_num'] ]; if (!$model->load($insert_data, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); } } else { foreach ($params['form']['attr'] as $item) { $param = $item; $param['mall_id'] = $params['mall_id']; $param['attr_id'] = $item['id']; $param['deposit_presale_price'] = $presale_price; $param['deposit_price'] = $deposit_price; $param['deposit_presale_active_id'] = $params['deposit_presale_active_id']; $param['purchase_limit_num'] = $params['deposit_presale_goods']['purchase_limit_num']; $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]); if (!$model) { $model = new self(); } $model->loadDefaultValues(); if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage()); if (empty($params['form']['is_attr'])) $model->stock = $params['deposit_presale_goods']['goods_stock']; if (!$model->save()) throw new \Exception($model->getErrorMessage()); } } return true; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * @desc:扣减规格库存 * @Author: hua * @Date: 2021/11/9 * @Time: 11:46 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $goods_attr_id * @param $num +增加 -减少 * @return bool * @throws Exception */ public static function handleStock($mall_id, $deposit_presale_active_id, $goods_attr_id, $num) { try { $model = self::findOne(['mall_id' => $mall_id, 'deposit_presale_active_id' => $deposit_presale_active_id, 'attr_id' => $goods_attr_id, 'status' => StatusEnum::ENABLED]); if (empty($model)) throw new \Exception('规格不存在'); if (!is_int($num)) throw new \Exception('库存数量必须为整数'); if ($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足'); $model->stock += $num; $res = $model->save(); if ($res === false) throw new \Exception($model->getErrorMessage()); return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } }