<?php
/**
 * 真人转账审核
 * User: qin
 * Date: 2019/2/18
 * Time: 14:30
 */
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Request;
use Illuminate\Support\Facades\DB;

class TransferVerifyController extends Controller
{
	/**
     * 审核用户转账申请接口
     */
    public function transferCredit()
    {
        $username = isset($_POST['username']) ? $_POST['username'] : '';
        $game_type = isset($_POST['game_type']) ? $_POST['game_type'] : '';
    	$localuser = isset($_POST['localuser']) ? $_POST['localuser'] : '';
        $credit = isset($_POST['credit']) ? $_POST['credit'] : '';
        $type = isset($_POST['type']) ? $_POST['type'] : '';
        $billno = isset($_POST['billno']) ? $_POST['billno'] : '';
        $stype = isset($_POST['stype']) ? $_POST['stype'] : '';
        $remarks = isset($_POST['remarks']) ? $_POST['remarks'] : '';
        $zngame = isset($_POST['zngame']) ? $_POST['zngame'] : '';

        if(empty($game_type))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入游戏英文名称'));
        }
        else if(empty($localuser))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入用户名'));
        }
        else if(empty($credit))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入转账金额'));
        }
        else if(empty($type))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入转账类型'));
        }
        else if(empty($billno))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入订单号'));
        }
        else if(empty($stype))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入审核类型'));
        }
        else if(empty($remarks))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入审核理由'));
        }else if(empty($zngame))
        {
            echo json_encode(array('status' => -1, 'data' => '', 'msg' => '请传入游戏中文名称'));
        }
        else
        {
        	$ostr = '\\App\\Lib\\Biz\\Game\\Egame\\'.ucfirst($game_type);
            $obj = new $ostr();
            $user = new \App\Models\Account();
            $userobj = new \App\Models\Account_detailed();
            $moneydetails = new \App\Models\Money_details();
            $moneyrecharge = new \App\Models\Money_recharge();
            $re = new \App\Models\Oggame_transfer_record();
            //查询用户信息
            $un = $user->getinfo($localuser,3);
            //查询用户可操作余额
            $userinfo = $userobj->getInfoBy($un['identity']);
            $set = new \App\Models\Rgame_setting();

            $where = array();
            $where[] = array('game_type','=',$game_type);
            $set_obj  = $set->getGame($where);
            
            if($stype ==1){

                $logtwo = array(session ('adminInfo.admin_name'),$type.'-'.$stype,$localuser,$game_type,$zngame);
                OperationLog (session ('adminInfo.admin_id'), 'transferCredit', $logtwo);
                //查询用户当前最新余额
                $nc = $userobj->getInfoBy($un['identity']);
                if($type == 1)
                {
                    $res_to=$obj->transferCreditNew($localuser,$username,$credit,1);

                    if($res_to['status'] !=1){
                        $logtwo = array(session ('adminInfo.admin_name'),$type.'-'.$stype,$localuser,$game_type,$zngame);
                        OperationLog (session ('adminInfo.admin_id'), 'transferCredit', $logtwo);
                        echo json_encode(array('status' => 0, 'data' => '', 'msg' => $res_to['msg']));
                    }else{
                        $msg = '[审核通过]从余额转入到'.$zngame.'，金额为：'.$credit.' 元。订单号为：'.$billno.'';
                        $ud['trade_desc'] = $msg;
                        $moneydetails->updateInfo($ud,$billno,3);

                        //更改用户转账申请状态
                        $w[] = array('order_no','=',$billno);
                        $w[] = array('game_type','=',$game_type);
                        $w[] = array('local_user','=',$localuser);
                        $d['type'] = 1;
                        $d['exec_time'] = time();
                        $d['exec_result'] = '[转账成功]从余额转入到游戏(手机版)';
                        $d['remarks'] = $remarks;
                        $re->setTransferCredit($w,$d);

                        echo json_encode(array('status' => 1, 'data' => '', 'msg' => '转账成功', 'exec_result' => $d['exec_result']));
                    }
                    
                }
                else if($type == 2)
                {
                    $add = $userobj->addMoney($userinfo['account_identity'],$credit);
                    //写入资金操作记录
                    $msg = '[审核成功]从'.$zngame.'转入到余额，金额为：'.$credit.' 元。订单号为：'.$billno.'';
                    $log=array(
                        'info_identity'=>UUID (),
                        'trade_id'=>date('YmdHis', time()).rand(111111,999999),
                        'account_name'=>$localuser,
                        'account_identity'=>$userinfo['account_identity'],
                        'money'=>$credit,
                        'money_time'=>date('Y-m-d H:i:s'),
                        'money_type'=>1,
                        'reason'=>$remarks,
                        'trade_type'=>19,
                        'trade_desc'=>$msg,
                        'sysetem_user'=>session ('adminInfo.admin_name'),
                        'money_cash'=>floatval($userinfo['cash'])+floatval($credit),
                        'status'=>1,
                    );
                    $moneydetails->insert($log);
                    // $ud['trade_desc'] = $msg;
                    // $ud['money_cash'] = floatval($userinfo['cash'])+floatval($credit);
                    // $moneydetails->updateInfo($ud,$billno,3);


                    //更新充值记录状态
                    $da['info_identity'] = UUID ();
                    $da['order_id'] = date('YmdHis', time()).rand(111111,999999);
                    $da['apply_time'] = date('Y-m-d H:i:s');
                    $da['complete_time'] = date('Y-m-d H:i:s');
                    $da['money'] = $credit;
                    $da['account_name'] = $localuser;
                    $da['account_identity'] = $userinfo['account_identity'];
                    $da['status'] = 1;
                    $da['recharge_type'] = '额度转换';
                    $da['sysetem_user'] = session ('adminInfo.admin_name');
                    $da['reason'] = '审核通过';
                    $da['money_cash'] = floatval($userinfo['cash'])+floatval($credit);
                    $da['remark'] = '审核通过加款';
                    $moneyrecharge->insert($da);

                    //更改用户转账申请状态
                    $w[] = array('order_no','=',$billno);
                    $w[] = array('game_type','=',$game_type);
                    $w[] = array('local_user','=',$localuser);
                    $d['type'] = 1;
                    $d['exec_time'] = time();
                    $d['exec_result'] = '[转账成功]从游戏转入到余额(手机版)';
                    $d['remarks'] = $remarks;

                    $re->setTransferCredit($w,$d);

                    echo json_encode(array('status' => 1, 'data' => '', 'msg' => '转账成功', 'exec_result' => $d['exec_result']));
                }

            }else{

                if($type == 1)
                {
                    //返回到用户余额
                    $userobj->addMoney($userinfo['account_identity'],$credit);

                    $msg = '[审核未通过]从余额转入到'.$zngame.'，金额为：'.$credit.' 元。订单号为：'.$billno.'';
                    $log=array(
                        'info_identity'=>UUID (),
                        'trade_id'=>date('YmdHis', time()).rand(111111,999999),
                        'account_name'=>$localuser,
                        'account_identity'=>$userinfo['account_identity'],
                        'money'=>$credit,
                        'money_time'=>date('Y-m-d H:i:s'),
                        'money_type'=>1,
                        'trade_type'=>19,
                        'trade_desc'=>$msg,
                        'sysetem_user'=>session ('adminInfo.admin_name'),
                        'money_cash'=>floatval($userinfo['cash'])+floatval($credit),
                        'status'=>1,
                    );
                    $moneydetails->insert($log);
                    // $ud['trade_desc'] = $msg;
                    // $ud['money_cash'] = floatval($userinfo['cash'])+floatval($credit);
                    // $moneydetails->updateInfo($ud,$billno,3);

                    $da['info_identity'] = UUID ();
                    $da['order_id'] = date('YmdHis', time()).rand(111111,999999);
                    $da['apply_time'] = date('Y-m-d H:i:s');
                    $da['complete_time'] = date('Y-m-d H:i:s');
                    $da['money'] = $credit;
                    $da['account_name'] = $localuser;
                    $da['account_identity'] = $userinfo['account_identity'];
                    $da['status'] = 1;
                    $da['recharge_type'] = '额度转换';
                    $da['sysetem_user'] = session ('adminInfo.admin_name');
                    $da['reason'] = '审核未通过';
                    $da['money_cash'] = floatval($userinfo['cash'])+floatval($credit);
                    $da['remark'] = '审核未通过退款';
                    //var_dump($da);exit;
                    $moneyrecharge->insert($da);

                    //更改用户转账申请状态
                    $w[] = array('order_no','=',$billno);
                    $w[] = array('game_type','=',$game_type);
                    $w[] = array('local_user','=',$localuser);

                    $d['type'] = 4;
                    $d['exec_time'] = time();
                    $d['exec_result'] =  '[审核未通过]从余额转入到游戏(手机版)';
                    $d['remarks'] = $remarks;

                    $re->setTransferCredit($w,$d);

                    echo json_encode(array('status' => 1, 'data' => '', 'msg' => '申请未通过', 'exec_result' => $d['exec_result']));
                    exit;
                }
                else if($type == 2)
                {
                    //更改用户转账申请状态
                    $w[] = array('order_no','=',$billno);
                    $w[] = array('game_type','=',$game_type);
                    $w[] = array('local_user','=',$localuser);

                    $d['type'] = 4;
                    $d['exec_time'] = time();
                    $d['exec_result'] = '[审核未通过]从游戏转入到余额(手机版)';
                    $d['remarks'] = $remarks;

                    $re->setTransferCredit($w,$d);

                    //返还到用户游戏余额
                    $res_to=$obj->transferCreditNew($localuser,$username,$credit,1);
                    if($res_to['status'] !=1){
                        $logtwo = array(session ('adminInfo.admin_name'),$type.'-'.$stype,$localuser,$game_type,$zngame);
                        OperationLog (session ('adminInfo.admin_id'), 'transferCredit', $logtwo);
                    }
                    echo json_encode(array('status' => 1, 'data' => '', 'msg' => '申请未通过', 'exec_result' => $d['exec_result']));
                    exit;
                }
            }
        }
    }
}

?>