<?php
namespace Admin\Controller;
use Think\Model;

class DeductBindRecordController extends ThinkController {

	const model_name = 'DeductBindRecord';

	/**
	*扣除绑定平台币记录
	*@author 小纯洁
	*/
	public function recordLists($p=1)
	{
		$model_name=self::model_name;
		if(isset($_REQUEST['account'])){
			$extend['user_account'] = array('LIKE','%'.$_REQUEST['account'].'%');
			$map = $extend;
			unset($_REQUEST['account']);
		}

		$map['create_time'] = total(1,false);
        $sum['to_day'] = D($model_name)->where($map)->sum('quantity');
        $map['create_time'] = total(5,false);
        $sum['yst_day'] = D($model_name)->where($map)->sum('quantity');

        $sum['all_num'] = D($model_name)->where($extend)->sum('quantity');

        $this->assign('sum',$sum);

			$this->m_title = '绑币收回';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'DeductBindRecord/recordLists','status'=>1])->find());


		parent::order_lists($model_name,1,$extend);
	}

	/**
	*扣除绑定平台币
	*/
	public function deductBindBalance(){
		if(IS_POST){

			if(!isset($_REQUEST['user_id']) || $_REQUEST['user_id']<1){
				$this->error("请选择账号");
			}

			if(!isset($_REQUEST['game_id'])){
				$this->error("请选游戏");
			}

			if((!isset($_REQUEST['quantity']))){
				$this->error("请输入扣除金额");
			}
			/* if((int)(int)$_REQUEST['quantity'] ==0){
				$this->error("请输入大于零的正整数");
			} */
			$amount = $_REQUEST['quantity'];
			if($amount<=0 || !preg_match('/^[1-9](\d+)?$/',$amount)) {
				$this->error("请输入大于0的整数");
			}



			if(!D('Member')->check_sc_pwd($_REQUEST['second_pwd'])){
				$this->error("二级密码输入有误");
			}

			M()->startTrans();
			$puid = M('User','tab_')->field('puid,account')->where(['id'=>$_REQUEST['user_id']])->find();
			if(!empty($puid) && $puid['puid']>0) {
                $user_play_map['user_id'] = $puid['puid'];
            } else {
                $user_play_map['user_id'] = $_REQUEST['user_id'];
            }
			$user_play_map['game_id'] = $_REQUEST['game_id'];
			$d = M('UserPlay','tab_')->where($user_play_map)->find();

			if((int)$_REQUEST['quantity'] > (int)$d['bind_balance']){
				$this->error("扣除金额大于剩余金额");
			}

			$deduct = M("DeductBindRecord","tab_");
			$data["user_id"]      = $_REQUEST["user_id"];
			$data["user_account"] = $puid["account"];
			$data["game_id"]      = $_REQUEST["game_id"];
			$data["game_name"]    = get_game_name_by_id($_REQUEST['game_id']);
			$data["quantity"]     = $_REQUEST["quantity"];
			$data["execute_id"]   = UID;
			$data["execute_account"] =get_admin_nickname(UID);// $_SESSION['user_auth']['username'];
			$data["create_time"] = time();

			$result  = M('UserPlay','tab_')->where($user_play_map)->setDec('bind_balance',$data["quantity"]);
			$result1 = $deduct->data($data)->add();
			if($result !== false && $result1 !==false){
				M()->commit();$this->success("操作成功",U('DeductBindRecord/recordLists'));
			}else{
            	//事务回滚
            	M()->rollback();
            	$this->error("操作失败");
        	}
		}else{
			$this->meta_title = '收回绑币';
			$this->m_title = '绑币收回';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'DeductBindRecord/recordLists','status'=>1])->find());


			$this->display();
		}
	}


}