关于数据库中优惠券使用数量和库存加减的问题
这两处代码为什么只有一个地方用到了 where('id',$this->id)
呢
/**
* 新增、减少优惠券数量
*/
public function changeUsed($increase = true)
{
// 传入 true 代表新增使用量。否则代表减少使用量
if ($increase) {
// 与检查 SKU 库存类似,这里需要检查当前用量是否已经超过用量
return $this->where('id', $this->id)->where('used', '<', $this->total)->increment('used');
} else {
return $this->decrement('used');
}
}
/**
* 减库存
*/
public function decreaseStock($amount)
{
if ($amount < 0) {
throw new InternalException('减库存不可小于0');
}
return $this->where('id', $this->id)->where('stock', '>=', $amount)->decrement('stock', $amount);
}
/**
* 加库存
*/
public function addStock($amount)
{
if ($amount < 0) {
throw new InternalException('加库存不可小于0');
}
$this->increment('stock', $amount);
}
推荐文章: