WithdrawService.php 5.02 KB
<?php

namespace App\Services;

use App\ApiInterface\WithdrawInterface;
use App\Helper\ErrorCode;
use App\Helper\Response;
use App\Models\Legal\StakeholderBalanceByPayer;
use App\Models\Legal\StakeholderIncomeSyncApp;
use App\Models\Legal\StakeholderWithdrawAppLogs;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * Class MusicianAgreementService
 * @package App\Services
 */
class WithdrawService extends Service implements WithdrawInterface
{
    /**
     * 提现申请
     * @param StakeholderIncomeSyncApp $app
     */
    public function advance(StakeholderIncomeSyncApp $app)
    {
        $condition = $this->compose($app);

        DB::beginTransaction();

        try {

            //修改状态-》提现中
            StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>1])->update([
                'withdraw_status'=>2
            ]);

            //修改资金账户
            StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('balance', '>=', $app->total_money)->update([
                'freeze' => DB::raw("freeze  + {$app->total_money}"),
                'balance'=> DB::raw("balance - {$app->total_money}"),
            ]);

            //提现申请日志
            StakeholderWithdrawAppLogs::query()->create([
                'serial_no'  => $app->serial_no,
                'identifier' => $this->request->get('identifier')->identifier,
                'type'       => __FUNCTION__,
                'withdraw_money'    => $app->total_money,
            ]);

            DB::commit();
            return Response::success();

        } catch (\Exception $e) {
            DB::rollBack();
            Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
            return Response::error(ErrorCode::WITHDRAW_APPLY_FAIL);
        }

    }

    /**
     * 提现成功
     * @param StakeholderIncomeSyncApp $app
     */
    public function success(StakeholderIncomeSyncApp $app)
    {
        $condition = $this->compose($app);

        DB::beginTransaction();

        try {

            //修改状态-》提现中 -> 提现完成
            StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>2])->update([
                'withdraw_status'=>3
            ]);

            //修改资金账户
            StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('freeze', '>=', $app->total_money)->update([
                'freeze' => DB::raw("freeze  - {$app->total_money}"),
                'pay_out'=> DB::raw("pay_out + {$app->total_money}"),
            ]);

            //提现申请日志
            StakeholderWithdrawAppLogs::query()->create([
                'serial_no'  => $app->serial_no,
                'identifier' => $this->request->get('identifier')->identifier,
                'type'       => __FUNCTION__,
                'withdraw_money'    => $app->total_money,
            ]);

            DB::commit();
            return Response::success();

        } catch (\Exception $e) {
            DB::rollBack();
            Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
            return Response::error(ErrorCode::WITHDRAW_APPLY_FAIL);
        }


    }

    /**
     * 提现失败
     * @param StakeholderIncomeSyncApp $app
     */
    public function fail(StakeholderIncomeSyncApp $app)
    {
        $condition = $this->compose($app);

        DB::beginTransaction();

        try {

            //修改状态-》提现中 -》 已确认
            StakeholderIncomeSyncApp::query()->where(['serial_no'=>$app->serial_no, 'identifier'=>$this->request->get('identifier')->identifier, 'sync_status'=>1, 'withdraw_status'=>2])->update([
                'withdraw_status'=>1
            ]);

            //修改资金账户
            StakeholderBalanceByPayer::query()->where(['condition'=>$condition])->where('freeze', '>=', $app->total_money)->update([
                'freeze' => DB::raw("freeze  - {$app->total_money}"),
                'balance'=> DB::raw("balance + {$app->total_money}"),
            ]);

            //提现申请日志
            StakeholderWithdrawAppLogs::query()->create([
                'serial_no'  => $app->serial_no,
                'identifier' => $this->request->get('identifier')->identifier,
                'type'       => __FUNCTION__,
                'withdraw_money'    => $app->total_money,
            ]);

            DB::commit();
            return Response::success();

        } catch (\Exception $e) {
            DB::rollBack();
            Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]);
            return Response::error();
        }
    }

    /**
     * @param StakeholderIncomeSyncApp $app
     * @return string
     */
    private function compose(StakeholderIncomeSyncApp $app)
    {
        return $app->stakeholder_id .'|'. $app->subject_no;
    }
}