db->beginTransaction(); try{ $form = new self(); $mch = Mch::findOne($mch_id); $form->store_id = $mch->store_id; $form->user_id = $user_id; $form->mch_id = $mch_id; $form->amount = $amount; $form->before = $mch->account_money; if ($log_type == AccountLog::LOG_TYPE_INCOME) { $form->after = $form->before + $amount; } else { $form->after = $form->before - $amount; } $form->desc = $desc; $form->order_id = $order_id; $form->order_no = $order_no; $form->operator = ''; $form->operator_id = 0; $form->log_type = $log_type; $form->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $form->created_at = time(); if ($profit) { $form->profit = $profit; } if (!$form->save()) { \Yii::error([__METHOD__, $form->getErrors()]); throw new \Exception('入驻商佣金发放log保存失败' . array_shift($form->getFirstErrors())); } if ($log_type == AccountLog::LOG_TYPE_INCOME) { if($isTotal){ $mch->account_money_total += $amount; } $mch->account_money += $amount; } else { $mch->account_money -= $amount; if($mch->account_money < 0){ \Yii::error([__METHOD__, $mch->getErrors()]); throw new \Exception('入驻商佣金不足' . array_shift($mch->getFirstErrors())); } } $save = $mch->save(); if(!$save){ \Yii::error([__METHOD__, $mch->getErrors()]); throw new \Exception('入驻商佣金修改失败' . array_shift($mch->getFirstErrors())); } $t->commit(); return [ 'code' => 0, 'msg' => 'ok', ]; } catch (\Exception $ex) { $t->rollBack(); return [ 'code' => 1, 'msg' => '操作失败,' . $ex->getMessage(), ]; } } }