public function moveSurplus($number, $remark)
{
$number = (int) $number;
$surplusOriginal = $this->surplus;
$this->surplus += $number;
if ($this->surplus < 0) {
$this->surplus = 0;
}
if ($surplusOriginal == $this->surplus) {
return true;
}
$transaction = Yii::$app->db->beginTransaction();
try {
if (!$this->save(false)) {
throw new \Exception('商品错误!');
}
$goodsSurplus = new GoodsSurplus();
$goodsSurplus->goods_id = $this->id;
$goodsSurplus->surplus_before = $surplusOriginal;
$goodsSurplus->amount = $number;
$goodsSurplus->surplus_after = $this->surplus;
$goodsSurplus->remark = $remark;
if (!$goodsSurplus->save(false)) {
throw new \Exception('商品库存记录失败!');
}
$transaction->commit();
return true;
} catch (\Exception $e) {
$transaction->rollBack();
return false;
}
}