<?php
// +----------------------------------------------------------------------
// | 徐州梦创信息科技有限公司—专业的游戏运营,推广解决方案.
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.vlcms.com  All rights reserved.
// +----------------------------------------------------------------------
// | Author: kefu@vlcms.com QQ:97471547
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class PromoteCoinModel extends Model{

    /**
     * 构造函数
     * @param string $name 模型名称
     * @param string $tablePrefix 表前缀
     * @param mixed $connection 数据库连接信息
     */
	public function __construct($name = '', $tablePrefix = '', $connection = '')
	{
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }

    /* 自动验证规则 */
    protected $_validate = array(
        array('num', [1,999999], '发放数量错误', self::MUST_VALIDATE, 'between', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT,'function'),
        array('op_id',UID,self::MODEL_INSERT),
    );


    //获取列表
    public function get_lists($where){
        return $this->where($where)->select();
    }

    /**
     * 平台币记录
     * @param $promote_id 渠道ID
     * @param $sid  来源ID
     * @param $num  数量
     * @param $type 类型 1:增加 2:减少
     */
    public function record($promote_id,$sid,$num,$type){
        $data['promote_id'] = $promote_id;
		$data['source_id'] = $sid;
        $data['num'] = $num;
        $data['type'] = $type;
        $data['promote_type'] = get_promote_level($promote_id);
        $data = $this->create($data);
        if(!$data){
            return $this->getError();
		}
        $res = $this->add($data);
        return $res;
	}
	
	public function addRecord($params)
	{
		$data = [];
		$data['promote_id'] = $params['promote_id'];
		$data['source_id'] = $params['source_id'];
		$data['game_id'] = $params['game_id'];
        $data['num'] = $params['num'];
        $data['type'] = $params['type'];
        $data['promote_type'] = get_promote_level($params['promote_id']);
        $data = $this->create($data);
        if(!$data){
            return $this->getError();
        }
		$status = $this->add($data);
		if ($status) {
			$id = M()->getLastInsID();
        	return $id;
		} else {
			return 0;
		}
	}
		
		
		/*
		 * 平台币后台发放记录(只查自己的发放)
		 * @param		array		$map		条件数组
		 * @param		integer	$page		页数
		 * @param		integer	$row		每页条数
		 * @param		string	$field	字段
		 * @author 鹿文学
		 */
		public function grant($map=array(),$page=1,$row=10,$field='') {
			
			$map['type'] = 1;
			
			$map['banlan_type'] = 1;
			
			$map['source_id'] = 0;

			$map['promote_id'] = PID;
			
			$map['status'] = 1;
			
			$field = !empty($field)?$field:'id,"--" as order_number,"自己" as recharge_id,num,create_time,1 as source,"--" as way';
			
			$list = $this->field($field)->where($map)->order('create_time desc,id desc')->page($page,$row)->select();
			
			$count = $this->where($map)->count();
			
			return ['count'=>$count,'list'=>$list];
			
		}
		
		/*
		 * 所有平台币后台发放记录(只查自己的发放)
		 * @author 鹿文学
		 */
		public function grant_all() {
			$map['type'] = 1;
			
			$map['banlan_type'] = 1;
			
			$map['source_id'] = 0;

			$map['promote_id'] = PID;
			
			$map['status'] = 1;
			
			$field = 'id,"--" as order_number,"自己" as recharge_id,num,create_time,1 as source,"--" as way';
			
			$list = $this->field($field)->where($map)->order('create_time desc,id desc')->select();
			
			return $list;
			
		}
		
		/*
		 * 获取平台币记录(包含充值记录、后台发放记录)
		 * @param		integer	$page		页数
		 * @param		integer	$row		每页条数
		 * @author 鹿文学
		 */
		public function all_coin($page,$row) {
			
			$bmap=[];
			
			if(!empty($_REQUEST['order_number'])){
				$bmap['pay_order_number'] = array('like','%'.$_REQUEST['order_number'].'%');
				$pcount = 0;
			} else{
				
				$pmap['type'] = 1;
				
				$pmap['banlan_type'] = 1;
				
				$pmap['status'] = 1;
				
				$pmap['source_id'] = 0;
				
				$pmap['promote_id'] = PID;

				
				if(empty($_REQUEST['account']) || stripos(PROMOTE_ACCOUNT,$_REQUEST['account'])!== false || strpos('自己',$_REQUEST['account']) !== false) {
					
				$pids = $this->field('id,1 as type,create_time')->where($pmap)->order('create_time desc,id desc')->select();
				
				$pcount = $this->where($pmap)->count();
				
				}
				
				
			}
			
			
			
			$bmap['pay_status'] = 1;
			
			$bmap['status'] = 1;
			
			$pid = $child = $this->get_sub_all_promote_list();
			$pid[] = PID;
			if(!empty($_REQUEST['account'])) {			
if(strpos('自己',$_REQUEST['account']) !== false) {
	
				$bmap['_complex'] = array(
					'recharge_id'=>PID,
					'promote_id'=>PID,
				);

} else {
				$bmap['_complex'] = array(
					'_complex'=>array('promote_account'=>['like','%'.$_REQUEST['account'].'%'],'recharge_id'=>PID),
					'_string'=>'recharge_account like \'%'.$_REQUEST['account'].'%\' and promote_id='.PID,
					'_logic'=>'or'
				);
	
}
				
			}	else {
				
				$bmap['_complex'] = array(
					'recharge_id'=>PID,
					'promote_id'=>PID,
					'_logic'=>'or'
				);
				
			}
			
			$bids = D('Balance')->field('id,2 as type,create_time')->where($bmap)->order('create_time desc')->select();
			
			$bcount = D('Balance')->where($bmap)->count();
			
			$count = $pcount + $bcount;
			
			if(!empty($pids) && !empty($bids)) {
				$ids = array_merge($pids,$bids);
			} elseif(!empty($pids) && empty($bids)) {
				$ids = $pids;
			} elseif(empty($pids) && !empty($bids)) {
				$ids = $bids;
			} else {
				return ['count'=>0,'list'=>[]];
			}
			
			
			$ids = array_chunk(my_sort($ids,'create_time',SORT_DESC),$row);
			
			$mapids = $ids[$page-1];
			
			$pmapids = [];
			
			$bmapids = [];
			
			foreach($mapids as $k => $v) {
				if($v['type']==1) {
					$pmapids[] = $v['id'];					
				} elseif($v['type']==2) {
					$bmapids[] = $v['id'];
				}				
			}
			
			$pdata = [];
			if(count($pmapids) > 0 && empty($_REQUEST['order_number']))
			$pdata = $this->field('id,"--" as order_number,"自己" as recharge_id,num,create_time,1 as source,"--" as way')
					->where(['id'=>['in', $pmapids]])->select();
			
			$bdata = [];
			if(count($bmapids) > 0)
			$bdata = D('Balance')->field('id,pay_order_number as order_number,recharge_id,promote_id,create_time,money,case recharge_type when 1 then "支付宝" when 2 then "微信" else "--" end as way')
					->where(['id'=>['in', $bmapids]])->select();
			
			$data = [];
			
			foreach($mapids as $ok => $ov) {
				if($ov['type']==2) {
					foreach($bdata as $k => $v) {
						if($v['id'] == $ov['id']) {
							$v2=array(
								'id'=>$v['id'],
								'order_number'=>$v['order_number'],
								'create_time'=>$v['create_time'],
								'way'=>$v['way'],
							);
							if($v['recharge_id']==$v['promote_id']) {
								$v2['recharge_id'] = '自己';
								$v2['num'] = $v['money'];
								$v2['source'] = 2;
							}elseif($v['recharge_id']==PID && $v['promote_id']!=PID) {
								$v2['recharge_id'] = '自己';
								$v2['num'] =  $v['money'];
								$v2['source'] = 3;
							}elseif($v['promote_id']==PID && !in_array($v['recharge_id'],$pid)) {
								$v2['recharge_id'] = $v['recharge_id'];
								$v2['num'] = $v['money'];
								$v2['source'] = 4;
							}elseif($v['promote_id']==PID && in_array($v['recharge_id'],$child)) {
								$v2['recharge_id'] = $v['recharge_id'];
								$v2['num'] = $v['money'];
								$v2['source'] = 5;
							}
							
							$data[$ok] = $v2;						
						}
					}					
				}
				if($ov['type']==1) {
					foreach($pdata as $k => $v) {
						if($v['id'] == $ov['id']) {
							$data[$ok] = $v;
						}
					}
					
				}
			}
			
		
			
			return ['count'=>$count,'list'=>$data];
			
		}
		
		
		
		/*
		 * 所有获取平台币记录(包含充值记录、后台发放记录)
		 * @author 鹿文学
		 */
		public function all_coin_all() {
			
			$bmap=[];
			
			if(!empty($_REQUEST['order_number'])){
				$bmap['pay_order_number'] = array('like','%'.$_REQUEST['order_number'].'%');

			} else{
				
				$pmap['type'] = 1;
				
				$pmap['banlan_type'] = 1;
				
				$pmap['status'] = 1;
				
				$pmap['source_id'] = 0;
				
				$pmap['promote_id'] = PID;
				
if(empty($_REQUEST['account']) || stripos(PROMOTE_ACCOUNT,$_REQUEST['account'])!== false || strpos('自己',$_REQUEST['account']) !== false) {
	
				$pdata= $this->field('id,"--" as order_number,"自己" as recharge_id,num,create_time,1 as source,"--" as way')
						->where($pmap)->order('create_time desc')->select();
}			
				
			}
			
			
			
			$bmap['pay_status'] = 1;
			
			$bmap['status'] = 1;
			
			$pid = $child = $this->get_sub_all_promote_list();
			$pid[] = PID;
			if(!empty($_REQUEST['account'])) {			
if(strpos('自己',$_REQUEST['account']) !== false) {
	
				$bmap['_complex'] = array(
					'recharge_id'=>PID,
					'promote_id'=>PID,
				);

} else {
				$bmap['_complex'] = array(
					'_complex'=>array('promote_account'=>['like','%'.$_REQUEST['account'].'%'],'recharge_id'=>PID),
					'_string'=>'recharge_account like \'%'.$_REQUEST['account'].'%\' and promote_id='.PID,
					'_logic'=>'or'
				);
}				
			}	else {
				
				$bmap['_complex'] = array(
					'recharge_id'=>PID,
					'promote_id'=>PID,
					'_logic'=>'or'
				);
				
			}
			
			$bdata = D('Balance')->field('id,pay_order_number as order_number,recharge_id,promote_id,create_time,money,case recharge_type when 1 then "支付宝" when 2 then "微信" else "--" end as way')
				->where($bmap)->order('create_time desc')->select();
			
			$data = [];
			
			foreach($bdata as $k => $v) {
				$v2=array(
					'id'=>$v['id'],
					'order_number'=>$v['order_number'],
					'create_time'=>$v['create_time'],
					'way'=>$v['way'],
				);
				if($v['recharge_id']==$v['promote_id']) {
					$v2['recharge_id'] = '自己';
					$v2['num'] = $v['money'];
					$v2['source'] = 2;
				}elseif($v['recharge_id']==PID && $v['promote_id']!=PID) {
					$v2['recharge_id'] = '自己';
					$v2['num'] = $v['money'];
					$v2['source'] = 3;
				}elseif($v['promote_id']==PID && !in_array($v['recharge_id'],$pid)) {
					$v2['recharge_id'] = $v['recharge_id'];
					$v2['num'] = $v['money'];
					$v2['source'] = 4;
				}elseif($v['promote_id']==PID && in_array($v['recharge_id'],$child)) {
					$v2['recharge_id'] = $v['recharge_id'];
					$v2['num'] = $v['money'];
					$v2['source'] = 5;
				}
				
				$bdata[$k] = $v2;												
			}					
			
			if(!empty($pdata) && !empty($bdata)) {
				$data = array_merge($pdata,$bdata);
			} elseif(!empty($pdata) && empty($bdata)) {
				$data = $pdata;
			} elseif(empty($pdata) && !empty($bdata)) {
				$data = $bdata;
			} else {
				return [];
			}
			
			return my_sort($data,'create_time',SORT_DESC);
			
		}
		
		/*
		 * 删除记录
		 * @author 鹿文学
		 */
		public function del($id=0) {
			
			if(is_numeric($id) && $id>0) {
				
				
				return $this->where(['id'=>$id])->setField('status',0);
				
			} elseif(is_array($id)) {
				
				return $this->where(['id'=>['in',$id]])->setField('status',0);
				
			} else {
				return 0;
			}
			
		}
		
		
		
		/*
		 * 获取子渠道
		 * @author 鹿文学
		 */
		public function get_sub_all_promote_list() {
			
			if(PRO_GRADE==1) {
				$map['parent_id'] = PID;
				$map['grand_id'] = PID;
				$map['_logic'] = 'or';
			} elseif(PRO_GRADE==2) {
				$map['parent_id'] = PID;
			} else {
				return [PID];
			}
			
			$result = M('Promote','tab_')->field('id')->where($map)->select();
			
			return is_array($result)?array_column($result,'id'):'';
			
		}
		

}