db->beginTransaction(); try { // 根据流水号获取提现记录 $withdrawLog = StoreWithdrawLog::find()->where(['id' => $data['withdrawal_source_id'], 'status' => 1])->one(); if (!$withdrawLog) { throw new \Exception("提现记录不存在"); } $withdrawLog->withdraw_status = 2; if (!$withdrawLog->save()) { throw new \Exception("提现状态修改失败"); } $mch = Store::find()->where([ 'id' => $withdrawLog->mch_id, 'mall_id' => $withdrawLog->mall_id, 'user_id' => $withdrawLog->user_id, 'status' => 1 ])->one(); if ($mch) { $mch->balance += $withdrawLog->num; if (!$mch->save()) { throw new \Exception("商户余额修改失败"); } \Yii::$app->custom->logs("商户提现回退:" . $withdrawLog->user_id, '回退金额:' . $withdrawLog->num); } $trans->commit(); return true; } catch (\Exception $e) { $trans->rollBack(); \Yii::$app->custom->logs("退款失败", $e->getMessage()); return false; } } /** * 打款成功 * @return void * @throws \Exception */ public static function remit_success($cash, $msg = '打款成功') { $content = []; if ($cash->content) { $content = json_decode($cash->content, true); } $cash->withdraw_status = 1; $content['remit_at'] = date('Y-m-d H:i:s', time()); $content['remit_content'] = $msg; $cash->content = json_encode($content); if (!$cash->save()) { throw new \Exception("保存提现数据失败"); } // 添加以支出金额 $storeInfo = Store::findOne(['user_id' => $cash->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); $storeInfo->total_expenses += $cash->num; if (!$storeInfo->save()) { throw new \Exception("保存提现数据失败"); } } public static function remit_fail($cash, $msg = '打款失败') { $content = empty($cash->content) ? [] : json_decode($cash->content, true); $content['remit_content'] = $msg; $res = StoreWithdrawLog::updateAll([ 'withdraw_status' => 0, 'content' => json_encode($content), 'updated_at' => time() ], ['id' => $cash->id]); } }