<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class ReturnmanageController extends Controller {
	/**
	 * @return 回水管理
	 */
	function index(Req $req) {
		$request=array();
		$request['account_name']=isset($req->account_name)?trim($req->account_name):null;
		$request['gametype'] = isset($req->gametype) ? trim($req->gametype) : null;
		$request['star_time'] = isset($req->star_time) ? trim($req->star_time) : trans('status.default_time.seven_day');
		$request['end_time'] = isset($req->end_time) ? trim($req->end_time) : trans('status.default_time.etime');
		$request['return_point'] = isset($req->return_point) ? trim($req->return_point) : 0.1;
		$request['total_money'] = isset($req->total_money) ? trim($req->total_money) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/money/returnInfo');
		$dt->enableCheckBox();
		$dt->setPriKey('account_name');
		$dt->setLang('money');
		$dt->addColsFields('account_name', array('templet' => '#userdetail', 'width' => 270));
		$dt->addColsFields('game_name', array('sort' => false, 'width' => 270,'templet'=>'#gameType'));
		$dt->addColsFields('total_money', array('width' => 270,'templet' => '#money'));
		//$dt->addColsFields('return_water', array('sort' => false, 'width' => 270));
		return view('admin.returnmanage/index', $dt->render($request));
	}

	/**
	 * 单用户回水明细
	 * [detail description]
	 * @return [type] [description]
	 */
	function userReturndetail() {
		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$game_name = Request::has('game_name') ? Request::get('game_name') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/money/userreturnList?account_name=' . $account_name . '&game_name=' . $game_name);
		$dt->setLang('money');
		$dt->setPriKey('order_id');
		$dt->addColsFields('id');
		$dt->addColsFields('account_name');
		$dt->addColsFields('game_name', array('sort' => false));
		$dt->addColsFields('money_time');
		$dt->addColsFields('money');
		$dt->addColsFields('prize_money');
		$dt->addColsFields('get_money');
		$dt->addColsFields('returnstatus', array('sort' => false));
		return view('admin.view', $dt->render());
	}
	//一件回水
	function delete(Req $req) {
		$get_data = $req->data;
        $star_time=$req->star_time?$req->star_time.' 00:00:00':'';
        $end_time=$req->end_time?$req->end_time.' 23:59:59':'';
        $game_name=$req->game?trim($req->game):'';
//		if(empty($s_time)||empty($e_time)){
//            return responseToJson(-5051572522);
//        }
        /**时间条件***/
        $timearea=array();
        if (!empty($star_time)) {
            $star_time = date('Y-m-d H:i:s', strtotime($star_time));
            $timearea[] = array('money_time', '>=', $star_time);
        }
        if (!empty($end_time)) {
            $end_time = date('Y-m-d H:i:s', strtotime($end_time));
            $timearea[] = array('money_time', '<=', $end_time);
        }
        /**数据检验***/
		if (!is_array($get_data)) {
			return responseToJson(-2001);
		}
		$account_id_array=array();
		foreach ($get_data as $v) {
			if ($v['total_money']<= 0) {
				return responseToJson(-5030021122);
			}
            //获取用户id数组
            $account_id_array[]=$v['account_identity'];
		}

		DB::beginTransaction();//开启事务
		$money_buy = new \App\Models\MoneyBuy;

		//更改结算状态
		foreach ($get_data as $v) {
			$res = $money_buy->updateWater($v['account_name'],$timearea,$game_name);
			if ($res < 0) {
				break;
			}
		}
		if ($res < 0) {
            DB::rollback();
            return responseToJson($res);
		}
		//插入回水记录
		$data_back_array = array(); //回水数据数组
		$data_min_array = array(); //详情数据数组
		//获取用户信息
		$account_detailed = new \App\Models\Account_detailed;

        //获取用户当前余额
        $account_cash_array=$account_detailed->getCashArray($account_id_array);

		$rate = (!empty($req->return_point))?$req->return_point:0.1;
		$rate = ($rate>=1)?$rate/100:$rate;
		//组装数据
		foreach ($get_data as $k => $v) {
			$order_id = OrderID();
			$money = abs($rate * $v['total_money']);
			$data_back_array[$k]['account_name'] = $v['account_name'];
			$data_back_array[$k]['account_identity'] = $v['account_identity'];
			$data_back_array[$k]['order_id'] = $order_id;
			$data_back_array[$k]['money'] = $money;
			$data_back_array[$k]['total_money'] = $v['total_money'];
			$data_back_array[$k]['sysetem_user'] = session('adminInfo.admin_name');
			$data_back_array[$k]['processing_time'] = date('Y-m-d H:i:s');
			$data_back_array[$k]['ratio'] = $rate;
			$data_back_array[$k]['info_identity'] = UUID();

			$data_min_array[$k]['account_name'] = $v['account_name'];
			$data_min_array[$k]['account_identity'] = $v['account_identity'];
			$data_min_array[$k]['trade_id'] = $order_id;
			$data_min_array[$k]['money'] = $money;
			$data_min_array[$k]['sysetem_user'] = session('adminInfo.admin_name');
			$data_min_array[$k]['info_identity'] = UUID();
			$data_min_array[$k]['money_time'] = date('Y-m-d H:i:s');
			$data_min_array[$k]['money_type'] = 1;
			$data_min_array[$k]['money_cash'] = $account_cash_array[$v['account_identity']]+$money;
			$data_min_array[$k]['trade_type'] = 8;
			$data_min_array[$k]['trade_desc'] = sprintf(trans('trade.admin_hui_money'), session('adminInfo.admin_name'), $v['account_name'], $money, $order_id);
		}

		//插入回水记录
		$return_db = new \App\Models\Money_return;
		$res_return = $return_db->returnMoney($data_back_array);
		if ($res_return < 0) {
			DB::rollback();
			return responseToJson($res_return);
		}

		//插入详情记录
		$db_money_de = new \App\Models\Money_details;
		$res_detail = $db_money_de->returnData($data_min_array);
		if ($res_detail < 0) {
			DB::rollback();
			return responseToJson($res_detail);
		}

		//更新用户余额
		foreach ($get_data as $k => $v) {
			$money = abs($rate * $v['total_money']);
			$res_money = $account_detailed->addMoney($v['account_identity'], $money);
			if ($res_money < 0) {
				break;
			}
		}
     
		if ($res_money < 0) {
			DB::rollback();
			return responseToJson($res_money);
		}
		//组装
        $account_str=array();
        $length=count($get_data);
        $limit=0;
        if($length>20){
            $limit=ceil($length/20);
        }
        $i=0;
		foreach ($get_data as $k=>$v){
		    if($k<20*($i+1)){
		        if(!isset($account_str[$i])){
                    $account_str[$i]='';
                }
                $account_str[$i].=$v['account_name'].',';
            }else{
                $i++;
            }
        }
        $log=array();
         foreach ($account_str as $k=>$v){
             $log[]=array(
                 session('adminInfo.admin_name'),
                 $v
             );
         }
         foreach ($log as $v){
            $res=OperationLog(session('adminInfo.admin_id'),'return_money',$v);
            if($res<0) {
                break;
            }
         }
        if($res<0){
            DB::rollback();
            return responseToJson($res);
        }
        DB::commit();
		return responseToJson(1);
	}

}

?>