<?php

namespace app\controller;
use app\business\StreamBusiness;
use Illuminate\Support\Arr;
use support\Db;
use support\Log;
use support\Request;

class IssueController
{
    public function index(Request $request)
    {
        $param=$request->all();
        Log::channel('issue')->info('接口回调',$param);
        Db::beginTransaction();
        try {
            $order=Db::table('wa_withdraw')->where('order_no',$param['merchantUniqueOrderId'])->first();
            if(empty($order)){
                throw new \Exception('订单号不存在!');
            }
            if($order->status != 2){
                throw new \Exception('当前订单已处理!');
            }
            if(Arr::get($param,'status') == '100'){
                $status  =   3;
            }else{
                if (empty($order)) {
                    throw new \Exception('暂无订单');
                }
                if ($order->status != 2) {
                    throw new \Exception('【' . $order->order_no . '】已被审核!');
                }

                StreamBusiness::addStream($order->user_id,  $order->money, streamType6, $order->type, moldTypefild($order->type),$order->id);
                $status = 4;
            }
            Db::table('wa_withdraw')->where('id',$order->id)->update(['status'=>$status]);

            Db::commit();
        }catch (\Throwable $exception){
            Db::rollBack();
            Log::channel('issue')->error('接口回调'.$exception->getMessage(),$param);
        }

        return "success";
    }


    public function indextwo(Request $request)
    {
        $param=$request->all();
        Log::channel('issue')->info('接口回调',$param);
        Db::beginTransaction();
        try {
            $order=Db::table('wa_withdraw')->where('order_no',$param['merchantUniqueOrderId'])->first();
            if(empty($order)){
                throw new \Exception('订单号不存在!');
            }
            if($order->status != 2){
                throw new \Exception('当前订单已处理!');
            }
            if(Arr::get($param,'status') == '100'){
                $status  =   3;
            }else{
                if (empty($order)) {
                    throw new \Exception('暂无订单');
                }
                if ($order->status != 2) {
                    throw new \Exception('【' . $order->order_no . '】已被审核!');
                }
                StreamBusiness::addStream($order->user_id, $order->money, streamType6, $order->type, moldTypefild($order->type), $order->id);
                $status = 4;
            }
            Db::table('wa_withdraw')->where('id',$order->id)->update(['status'=>$status]);

            Db::commit();
        }catch (\Throwable $exception){
            Db::rollBack();
            Log::channel('issue')->error('接口回调'.$exception->getMessage(),$param);
        }

        return "success";
    }

    public function issue_three(Request $request)
    {
        $param=$request->all();
        Log::channel('issue')->info('接口回调',$param);
        Db::beginTransaction();
        try {
            $order=Db::table('wa_withdraw')->where('order_no',$param['tradeid'])->first();
            if(empty($order)){
                throw new \Exception('订单号不存在!');
            }
            if($order->status != 2){
                throw new \Exception('当前订单已处理!');
            }
            if(Arr::get($param,'status') == 88){
                $status  =   3;
            }else{
                if (empty($order)) {
                    throw new \Exception('暂无订单');
                }
                if ($order->status != 2) {
                    throw new \Exception('【' . $order->order_no . '】已被审核!');
                }

                StreamBusiness::addStream($order->user_id,  $order->money, streamType6, $order->type, moldTypefild($order->type),$order->id);
                $status = 4;
            }
            Db::table('wa_withdraw')->where('id',$order->id)->update(['status'=>$status]);

            Db::commit();
        }catch (\Throwable $exception){
            Db::rollBack();
            Log::channel('issue')->error('接口回调'.$exception->getMessage(),$param);
        }

        return "success";
    }

}
