MusicianWithdrawService.php 6.08 KB
<?php

namespace App\Services;


use App\Helper\ErrorCode;
use App\Helper\RedisClient;
use App\Helper\Response;
use App\Models\Legal\StakeholderBalance;
use App\Models\Legal\StakeholderBalanceByPayer;
use App\Models\Musician\CashOutOrder;
use Illuminate\Support\Facades\DB;

/**
 * Class MusicianAgreementService
 * @package App\Services
 */
class MusicianWithdrawService extends Service
{
    /**
     * 冻结资金
     * @return \Illuminate\Http\JsonResponse
     */
    public function prepare()
    {
        $order_id = $this->request->input('order_id');

        if (!RedisClient::instance()->set(str_replace('#order#', $order_id, config('cachekey.withdraw.prepare')), true, ["nx", "ex" => 3])) {
            return $this->error(ErrorCode::REPEAT_SUBMIT);
        }

        $order = CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_DEFAULT, 'status'=>CashOutOrder::STATUS_WAIT])->first();

        //订单非法
        if (empty($order)) return $this->error(ErrorCode::ORDER_ILLEGAl);
        if (empty($order->balance_detail)) return $this->error(ErrorCode::ORDER_WITHDRAW_ERROR);
        if (empty($order->cash_out_money)) return $this->error(ErrorCode::ORDER_MONEY_ERROR);

        Db::beginTransaction();
        try {
            //付款方集合
            foreach ($order->balance_detail['detail'] as $item) {

                //付款方账户
                StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
                    ->where('balance', '>=', $item['balance'])
                    ->update([
                        'balance'=>Db::raw("balance - {$item['balance']}"),
                        'freeze'=>Db::raw("freeze + {$item['balance']}"),
                    ]);

                //总账户
                StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('balance', '>=', $item['balance'])
                    ->update([
                        'balance'=>Db::raw("balance - {$item['balance']}"),
                        'freeze'=>Db::raw("freeze + {$item['balance']}"),
                    ]);
            }

            CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_DEFAULT, 'status'=>CashOutOrder::STATUS_WAIT])->update([
                'withdraw_state' => CashOutOrder::WITHDRAW_STATE_WAIT,
            ]);

            DB::commit();

            return Response::success(['order_id'=>$order->order_id]);

        } catch (\Exception $e) {
            DB::rollBack();
            return Response::error();
        }

    }

    /**
     * 后台提现
     * @return \Illuminate\Http\JsonResponse
     */
    public function withdraw()
    {
        $order_id = $this->request->input('order_id');

        if (!RedisClient::instance()->set(str_replace('#order#', $order_id, config('cachekey.withdraw.audit')), true, ["nx", "ex" => 3])) {
            return Response::error(ErrorCode::REPEAT_SUBMIT);
        }

        $order = CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_WAIT, 'status'=>$this->request->input('status')])->first();

        //订单非法
        if (empty($order)) return Response::error(ErrorCode::ORDER_ILLEGAl);
        if (empty($order->balance_detail)) return Response::error(ErrorCode::ORDER_WITHDRAW_ERROR);

        Db::beginTransaction();

        try {

            switch (intval($this->request->input('status'))) {
                case 1:
                    //审核通过
                    $this->withdrawAuditSuccess($order);
                    break;
                case 2:
                    //审核失败
                    $this->withdrawAuditFail($order);
                    break;
            }

            CashOutOrder::query()->where(['order_id'=>$order_id, 'withdraw_state'=>CashOutOrder::WITHDRAW_STATE_WAIT, 'status'=>$this->request->input('status')])->update([
                'withdraw_state'=>$this->request->input('status')
            ]);

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

        } catch (\Exception $e) {
            Db::rollBack();
            return Response::error(ErrorCode::WITHDRAW_HANDLE_ERROR);
        }
    }

    /**
     * 提现审核成功
     * @param CashOutOrder $order
     */
    private function withdrawAuditSuccess(CashOutOrder $order)
    {
        foreach ($order->balance_detail['detail'] as $item) {

            //付款方维度
            StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
                ->where('freeze', '>=', $item['balance'])
                ->update([
                    'freeze'=>Db::raw("freeze - {$item['balance']}"),
                    'pay_out'=>Db::raw("pay_out + {$item['balance']}"),
                ]);

            //修改总的资金账户
            StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('freeze', '>=', $item['balance'])
                ->update([
                    'freeze'=>Db::raw("freeze - {$item['balance']}"),
                    'pay_out'=>Db::raw("pay_out + {$item['balance']}"),
                ]);

        }
    }

    /**
     * 提现审核失败
     * @param CashOutOrder $order
     */
    private function withdrawAuditFail(CashOutOrder $order)
    {

        foreach ($order->balance_detail['detail'] as $item) {

            StakeholderBalanceByPayer::query()->where(['stakeholder_id'=>$item['stakeholderId'], 'subject_no'=>$item['no']])
                ->where('freeze', '>=', $item['balance'])
                ->update([
                    'freeze'=>Db::raw("freeze - {$item['balance']}"),
                    'balance'=>Db::raw("balance + {$item['balance']}"),
                ]);

            StakeholderBalance::query()->where(['stakeholder_id'=>$item['stakeholderId']])->where('freeze', '>=', $item['balance'])
                ->update([
                    'freeze'=>Db::raw("freeze - {$item['balance']}"),
                    'balance'=>Db::raw("balance + {$item['balance']}"),
                ]);

        }
    }
}