$id, 'status' => [ScratchEnum::DISABLED, ScratchEnum::ENABLED]]); } else { $coupon = $this; } if (!$coupon || !$coupon->id) { throw new \ErrorException('错误的优惠券信息'); } if ($coupon->total_count == -1) { return $coupon; } if ($type === 'add') { $coupon->total_count += $num; } elseif ($type === 'sub') { if ($coupon->total_count < $num) { throw new \ErrorException('优惠券库存不足'); } $coupon->total_count -= $num; } else { throw new \ErrorException('错误的$type'); } if ($coupon->save()) { return $coupon; } else { throw new \ErrorException($coupon->getErrorMessage()); } } /** * @param $id * @param string $select * @return array|\yii\db\ActiveRecord|null * @throws \ErrorException */ public static function getData($id, $select = '*') { try { $data = self::find() ->where([ 'id' => $id, 'status' => [ScratchEnum::ENABLED, ScratchEnum::DISABLED], ]) ->select($select) ->asArray() ->one(); return $data; } catch (\Exception $exception) { throw new \ErrorException($exception->getMessage()); } } }