hasOne(Goods::class, ['id' => 'goods_id']); } public function getGroupBuyActive() { return $this->hasOne(GroupBuyActive::class, ['id' => 'group_buy_active_id']); } public function getGoodsAttr() { return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']); } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'group_buy_active_id' => 'qimall_addons_group_buy_active.id', 'goods_id' => '商品id;对应goods主表', 'attr_id' => '商品规格id;对应goods_attr.id;', 'group_buy_price' => '价格', 'stock' => '拼团库存', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Update At', ]; } public static function setData(array $params) { try { foreach ($params['form']['attr'] as $item){ $param = $item; $param['mall_id'] = $params['mall_id']; $param['attr_id'] = $item['id']; $param['group_buy_active_id'] = $params['group_buy_active_id']; $model = new self(); $model->loadDefaultValues(); if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage()); if(empty($params['form']['is_attr'])) $model->stock = $params['group_buy_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,$group_buy_active_id,$goods_attr_id,$num){ try{ $model = self::findOne(['mall_id'=>$mall_id,'group_buy_active_id'=>$group_buy_active_id,'attr_id'=>$goods_attr_id]); 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; } } }