<?php

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class DepositController extends ThinkController {
    const model_name = 'Deposit';

    public function lists($p = 1){
        if(isset($_REQUEST['user_account'])){
            $map['user_account']=array('like','%'.trim($_REQUEST['user_account']).'%');
            unset($_REQUEST['user_account']);
        }
        if(isset($_REQUEST['pay_order_number'])){
            $map['pay_order_number']=array('like','%'.trim($_REQUEST['pay_order_number']).'%');
            unset($_REQUEST['pay_order_number']);
        }
        if(isset($_REQUEST['pay_ip'])){
            $map['pay_ip']=array('like','%'.trim($_REQUEST['pay_ip']).'%');
            unset($_REQUEST['pay_ip']);
        }

//        $promoteRoot = getPowerPromoteIds();
//        $data_empower_type = session('user_auth')['data_empower_type'];
//
//        if ($promoteRoot) {
//            $map['promote_id'] =array('in',$promoteRoot);
//        } else if(!$promoteRoot&&$data_empower_type!=1){
//            $map['id'] = array('lt',1);
//        }
        setPowerPromoteIds($map,'promote_id');

        if(!isset($_REQUEST['promote_id'])){

        }else if(isset($_REQUEST['promote_id']) && $_REQUEST['promote_id']==0){
            $map['promote_id']=array('elt',0);
        }elseif(isset($_REQUEST['promote_name'])&&$_REQUEST['promote_id']==-1){
            $map['promote_id']=get_promote_id($_REQUEST['promote_name']);
        }else{
            $map['promote_id']=$_REQUEST['promote_id'];
        }
        if(isset($_REQUEST['time-start'])&&isset($_REQUEST['time-end'])){
            $map['payed_time'] =array('BETWEEN',array(strtotime($_REQUEST['time-start']),strtotime($_REQUEST['time-end'])+24*60*60-1));
            unset($_REQUEST['time-start']);unset($_REQUEST['time-end']);
        }elseif(isset($_REQUEST['time-start'])){
            $map['payed_time'] = ['GT',strtotime(I('time-start'))];
            unset($_REQUEST['time-start']);
        }elseif(isset($_REQUEST['time-end'])){
            $map['payed_time'] = ['LT',strtotime(I('time-end'))+86399];
            unset($_REQUEST['time-end']);
        }
        if(isset($_REQUEST['start'])&&isset($_REQUEST['end'])){
            $map['payed_time'] =array('BETWEEN',array(strtotime($_REQUEST['start']),strtotime($_REQUEST['end'])+24*60*60-1));
            unset($_REQUEST['start']);unset($_REQUEST['end']);
        }elseif(isset($_REQUEST['start'])){
            $map['payed_time'] = ['GT',strtotime(I('start'))];
            unset($_REQUEST['start']);
        }elseif(isset($_REQUEST['end'])){
            $map['payed_time'] = ['LT',strtotime(I('end'))+86399];
            unset($_REQUEST['end']);
        }
        if(isset($_REQUEST['pay_way'])) {
            if ($_REQUEST['pay_way'] == "2") {
                $map['pay_way'] = ['in', '2,3,4'];
            } else {
                $map['pay_way'] = $_REQUEST['pay_way'];
            }
            unset($_REQUEST['pay_way']);
        }

        if ($_REQUEST['merchant_id'] && $_REQUEST['merchant_id'] != 0) {
            $map['merchant_id'] = $_REQUEST['merchant_id'];
            unset($_REQUEST['merchant_id']);
        }

        if(isset($_REQUEST['pay_status'])){
            $map['pay_status']=$_REQUEST['pay_status'];
            unset($_REQUEST['pay_status']);
        }
        if($_REQUEST['data_order']!=''){
            $data_order=reset(explode(',',$_REQUEST['data_order']));
            $data_order_type=end(explode(',',$_REQUEST['data_order']));
            $this->assign('userarpu_order',$data_order);
            $this->assign('userarpu_order_type',$data_order_type);
            $sort = $data_order==3?'desc':'asc';
            $map['order'] = $data_order_type.' '.$sort;
        }
        D(self::model_name)->addSubsiteWhere($map);
        $map1=$map;
        $map1['pay_status']=1;

        $this->checkListOrCountAuthRestMap($map,[]);
        

        $total=null_to_0(D(self::model_name)->where($map1)->sum('pay_amount'));

        $otherMap = ["pay_status"=>1];
        D(self::model_name)->addSubsiteWhere($otherMap);

        $ttotal=null_to_0(D(self::model_name)->where('payed_time'.total(1))->where($otherMap)->sum('pay_amount'));
        $ytotal=null_to_0(D(self::model_name)->where('payed_time'.total(5))->where($otherMap)->sum('pay_amount'));
        $this->assign('total',$total);
        $this->assign('ttotal',$ttotal);
        $this->assign('ytotal',$ytotal);

        $this->m_title = '平台币充值';
        $this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Deposit/lists','status'=>1])->find());

        $this->assign("is_admin",is_administrator());
        $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
        $this->assign('show_data_power', $show_data_power);
        parent::lists(self::model_name, $_GET["p"], $map);
    }

    public function lists_secord() {

        if(isset($_REQUEST['user_account'])){
            $map['user_account']=array('like','%'.trim($_REQUEST['user_account']).'%');
            unset($_REQUEST['user_account']);
        }
        if(isset($_REQUEST['order_number'])){
            $map['order_number']=array('like','%'.trim($_REQUEST['order_number']).'%');
            unset($_REQUEST['order_number']);
        }
        if(isset($_REQUEST['pay_ip'])){
            $map['pay_ip']=array('like','%'.trim($_REQUEST['pay_ip']).'%');
            unset($_REQUEST['pay_ip']);
        }

        setPowerPromoteIds($map,'promote_id');

        if(!isset($_REQUEST['promote_id'])){

        }else if(isset($_REQUEST['promote_id']) && $_REQUEST['promote_id']==0){
            $map['promote_id']=array('elt',0);
        }elseif(isset($_REQUEST['promote_name'])&&$_REQUEST['promote_id']==-1){
            $map['promote_id']=get_promote_id($_REQUEST['promote_name']);
        }else{
            $map['promote_id']=$_REQUEST['promote_id'];
        }
        if(isset($_REQUEST['time-start'])&&isset($_REQUEST['time-end'])){
            $map['create_time'] =array('BETWEEN',array(strtotime($_REQUEST['time-start']),strtotime($_REQUEST['time-end'])+24*60*60-1));
            unset($_REQUEST['time-start']);unset($_REQUEST['time-end']);
        }elseif(isset($_REQUEST['time-start'])){
            $map['create_time'] = ['GT',strtotime(I('time-start'))];
            unset($_REQUEST['time-start']);
        }elseif(isset($_REQUEST['time-end'])){
            $map['create_time'] = ['LT',strtotime(I('time-end'))+86399];
            unset($_REQUEST['time-end']);
        }
        if(isset($_REQUEST['start'])&&isset($_REQUEST['end'])){
            $map['create_time'] =array('BETWEEN',array(strtotime($_REQUEST['start']),strtotime($_REQUEST['end'])+24*60*60-1));
            unset($_REQUEST['start']);unset($_REQUEST['end']);
        }elseif(isset($_REQUEST['start'])){
            $map['create_time'] = ['GT',strtotime(I('start'))];
            unset($_REQUEST['start']);
        }elseif(isset($_REQUEST['end'])){
            $map['create_time'] = ['LT',strtotime(I('end'))+86399];
            unset($_REQUEST['end']);
        }
        if(isset($_REQUEST['pay_way'])) {
            if ($_REQUEST['pay_way'] == "2") {
                $map['pay_way'] = ['in', '2,3,4'];
            } else {
                $map['pay_way'] = $_REQUEST['pay_way'];
            }
            unset($_REQUEST['pay_way']);
        }


        if(isset($_REQUEST['pay_status'])){
            $map['pay_status']=$_REQUEST['pay_status'];
            unset($_REQUEST['pay_status']);
        }
        if($_REQUEST['data_order']!=''){
            $data_order=reset(explode(',',$_REQUEST['data_order']));
            $data_order_type=end(explode(',',$_REQUEST['data_order']));
            $this->assign('userarpu_order',$data_order);
            $this->assign('userarpu_order_type',$data_order_type);
            $sort = $data_order==3?'desc':'asc';
            $map['order'] = $data_order_type.' '.$sort;
        }
        if ($_REQUEST['merchant_id']) {
            $map['merchant_id'] = $_REQUEST['merchant_id'];
            unset($_REQUEST['merchant_id']);
        }

        $map1=$map;
        $map1['pay_status']=1;

        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
        $map1['order_status'] = 1;
        unset($map1['pay_status']);
        if ($map['pay_status']) {
            $map['order_status']=$map['pay_status'];
        }

        $total=null_to_0(D('coin_pay_order')->where($map1)->sum('pay_amount'));
        $ttotal=null_to_0(D('coin_pay_order')->where('create_time'.total(1))->where(array('pay_status'=>1))->sum('pay_amount'));
        $ytotal=null_to_0(D('coin_pay_order')->where('create_time'.total(5))->where(array('pay_status'=>1))->sum('pay_amount'));
        $this->assign('total',$total);
        $this->assign('ttotal',$ttotal);
        $this->assign('ytotal',$ytotal);

        $data = D('coin_pay_order')
            /* 查询指定字段,不指定则查询所有字段 */
            ->field('*,order_status as pay_status,order_number as pay_order_number,merchant_id')
            // 查询条件
            ->where($map)
            /* 默认通过id逆序排列 */
            ->order("id desc")
            /* 数据分页 */
            ->page($page, $row)
            /* 执行查询 */
            ->select();

        $this->checkListOrCountAuthRestMap($map,[]);

        /* 查询记录总数 */
        $count = D('coin_pay_order')->where($map)->count();

        $page = set_pagination($count,$row);
        if($page) {$this->assign('_page', $page);}
        $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
        $this->assign('show_data_power', $show_data_power);
        $this->assign('list_data', $data);
        $this->meta_title = '平台币充值列表';
        $this->display();die();
    }



    public function send_lists(){
        if(IS_POST){
            $type = $_REQUEST['type'];
            $firstpay = A('Send','Event');
            \Think\Log::actionLog('Deposit/send_lists','Deposit',1);
            switch ($type) {
                case 1:
                    $firstpay->add1();
                    break;
                case 2:
                    $firstpay->add2();
                    break;
                case 3:
                    $firstpay->add3();
                    break;
            }
        }
        else{
            $this->meta_title ="后台发放(玩家)";


            $this->m_title = '平台币发放(玩家)';
            $this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Deposit/send_lists','status'=>1])->find());

            $this->display();
        }
    }

    /**
     * 后台充值平台币(玩家)
     */
    public function lists_sen(){

        if(isset($_REQUEST['user_account'])){
            $map['user_account']=array('like','%'.trim($_REQUEST['user_account']).'%');
            unset($_REQUEST['user_account']);
        }
        if(isset($_REQUEST['time-start'])&&isset($_REQUEST['time-end'])){
            $map['create_time'] =array('BETWEEN',array(strtotime($_REQUEST['time-start']),strtotime($_REQUEST['time-end'])+24*60*60-1));
            unset($_REQUEST['time-start']);unset($_REQUEST['time-end']);
        }elseif(isset($_REQUEST['time-start'])){
            $map['create_time'] = ['GT',strtotime(I('time-start'))];
            unset($_REQUEST['time-start']);
        }elseif(isset($_REQUEST['time-end'])){
            $map['create_time'] = ['LT',strtotime(I('time-end'))+86399];
            unset($_REQUEST['time-end']);
        }
        if(isset($_REQUEST['start'])&&isset($_REQUEST['end'])){
            $map['create_time'] =array('BETWEEN',array(strtotime($_REQUEST['start']),strtotime($_REQUEST['end'])+24*60*60-1));
            unset($_REQUEST['start']);unset($_REQUEST['end']);
        }elseif(isset($_REQUEST['start'])){
            $map['create_time'] = ['GT',strtotime(I('start'))];
            unset($_REQUEST['start']);
        }elseif(isset($_REQUEST['end'])){
            $map['create_time'] = ['LT',strtotime(I('end'))+86399];
            unset($_REQUEST['end']);
        }

        $map['op_account']=session('user_auth')['username'];

        if(isset($_REQUEST['op_account'])){
            if($_REQUEST['op_account']=='全部'){
                unset($_REQUEST['op_account']);
            }else{
                $map['op_account']=$_REQUEST['op_account'];
                unset($_REQUEST['op_account']);
            }
        }
        if(isset($_REQUEST['op_id'])){
            $map['op_id']=$_REQUEST['op_id'];
            unset($_REQUEST['op_id']);
        }

        if(is_numeric($_REQUEST['status']) && ($_REQUEST['status']==0 || $_REQUEST['status']==1)) {
            $map['status'] = $_REQUEST['status'];
            unset($_REQUEST['status']);
        }

        $map1=$map;
        $map1['status']=1;
        $total=null_to_0(M('provide_user','tab_')->where($map1)->sum('amount'));
        $ttotal=null_to_0(M('provide_user','tab_')->where('create_time'.total(1))->where(array('status'=>1))->sum('amount'));
        $ytotal=null_to_0(M('provide_user','tab_')->where('create_time'.total(5))->where(array('status'=>1))->sum('amount'));
        // $this->assign('dtotal',$dtotal);
        $this->assign('total',$total);
        $this->assign('ttotal',$ttotal);
        $this->assign('ytotal',$ytotal);

        $model = array(
            'm_name' => 'provide_user',
            'map'    => $map,
            'fields' => 'tab_provide_user.*,u.puid',
            'title'  => "后台发放(玩家)",
            'join'   => 'left join tab_user as u on u.id = tab_provide_user.user_id',
            'template_list' =>'lists_sen',
            'order'=>'tab_provide_user.id desc',
        );
        $user = A('User','Event');

        $this->m_title = '平台币发放(玩家)';
        $this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Deposit/send_lists','status'=>1])->find());


        $user->user_join_($model,$_GET['p']);
    }


    /**
     * 批量充值
     * @param $ids
     */
    public function batch($ids=''){
        if ( empty($ids) ) {
            $this->error('请选择要操作的数据!');
        }
        $list=M("provide_user","tab_");
        $map['id']=array("in",$ids);
        $map['status']=0;
        $pro=$list->where($map)->select();
        $userModel = M("User","tab_");
        for ($i=0; $i <count($pro) ; $i++) {
            $maps['id']=$pro[$i]['user_id'];
            $puid = $userModel->field('puid')->where($maps)->find();
            if(!empty($puid) && $puid['puid']>0) {
                $maps['id']=$puid['puid'];
            }
            $user=$userModel->where($maps)->setInc("balance",$pro[$i]['amount']);
            $list->where($map)->setField("status",1);
        }
        \Think\Log::actionLog('Deposit/batch','Deposit',1);
        $this->success("充值成功",U("lists_sen",array('type'=>2)));
    }

    public function delprovide($ids=null){
        if ( empty($ids) ) {
            $this->error('请选择要操作的数据!');
        }
        $list=M("provide_user","tab_");
        $map['id']=array("in",$ids);
        $map['status']=0;
        $delete=$list->where($map)->delete();
        if($delete){
            \Think\Log::actionLog('Deposit/batch','Deposit',1);
            $this->success("批量删除成功!",U("lists_sen",array('type'=>2)));
        }else{
            $this->error("批量删除失败!",U("lists_sen",array('type'=>2)));
        }
    }


}