| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreWithdrawLog;
- use common\enums\StatusEnum;
- /**
- * 提现事务服务
- */
- class WithdrawTransferService
- {
- /**
- * @param array $data
- * @return bool
- */
- public function refund(array $data): bool
- {
- $trans = \Yii::$app->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]);
- }
- }
|