<?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['mchOrderNo'])->first();
            if(empty($order)){
                throw new \Exception('订单号不存在!');
            }
            if($order->status != 2){
                throw new \Exception('当前订单已处理!');
            }
            if(Arr::get($param,'status') == 2){
                $status  =   3;
            }else{
                if (empty($order)) {
                    throw new \Exception('暂无订单');
                }
                if ($order->status != 2) {
                    throw new \Exception('【' . $order->order_no . '】已被审核!');
                }
//
//                if ($order->type == 1) {
//                    $mold = moldType1;
//                    $moldfield = moldTypefild1;
//                } elseif ($order->type == 2) {
//                    $mold = moldType2;
//                    $moldfield = moldTypefild2;
//                } elseif ($order->type == 3) {
//                    $mold = moldType3;
//                    $moldfield = moldTypefild3;
//                } elseif ($order->type == 4) {
//                    $mold = moldType4;
//                    $moldfield = moldTypefild4;
//                } elseif ($order->type == 4) {
//                    $mold = moldType4;
//                    $moldfield = moldTypefild4;
//                }
                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";
    }


}
