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

namespace Home\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class BalanceModel 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(
        
    );

    /* 自动完成规则 */
    protected $_auto = array(
        
    );

		/*
		 * 平台币充值记录
		 * @param		array		$map		条件数组
		 * @param		integer	$page		页数
		 * @param		integer	$row		每页条数
		 * @param   integer	$flag		标志(1:自主充值,2:渠道充值,3:充值渠道,4:充值子渠道)
		 * @author 鹿文学
		 */
    public function recharge($map,$page,$row,$flag='') {
			
			$map['pay_status'] = 1;
			$map['status'] = 1;
			
			empty($_REQUEST['order_number']) || $map['pay_order_number'] = array('like','%'.$_REQUEST['order_number'].'%');
			
			$field = 'id,pay_order_number as order_number,create_time,case recharge_type when 1 then "支付宝" when 2 then "微信" else "--" end as way';
			
			switch($flag) {
				case 1:{/* 自主充值 */
					$map['promote_id'] = $map['recharge_id'] = PID;
					$field .= ',"自己" as recharge_id,money as num,2 as source';
				};break;
				case 2:{/* 渠道充值 */
					if(!empty($_REQUEST['account'])) {						
						$map['promote_account']=['like','%'.$_REQUEST['account'].'%'];						
					}
					$map['promote_id'] = array('neq',PID);
					$map['recharge_id'] = PID;
					$field .= ',"自己" as recharge_id,money as num,3 as source';
				};break;
				case 3:{/* 充值渠道 */
					if(!empty($_REQUEST['account'])) {						
						$map['recharge_account']=['like','%'.$_REQUEST['account'].'%'];						
					}
					$child = $this->get_sub_all_promote_list();
					$child[]=PID;
					$map['recharge_id'] = array('not in',$child);
					$map['promote_id'] = PID;
					$field .= ',recharge_id,money as num,4 as source';
				};break;
				case 4:{/* 充值子渠道 */
					if(!empty($_REQUEST['account'])) {						
						$map['recharge_account']=['like','%'.$_REQUEST['account'].'%'];
					}
					
					$map['recharge_id'] = array('in',$this->get_sub_all_promote_list());
					$map['promote_id'] = PID;
					$field .= ',recharge_id,money as num,5 as source';
				};break;
				default:
						$field .= 'money as num';
			}
			
			$list = $this->field($field)->where($map)->order('create_time desc')->page($page,$row)->select();
			
			$count = $this->where($map)->count();
			
			return ['count'=>$count,'list'=>$list];
			
		}
		
		/*
		 * 所有平台币充值记录
		 * @param   integer	$flag		标志(1:自主充值,2:渠道充值,3:充值渠道,4:充值子渠道)
		 * @author 鹿文学
		 */
    public function recharge_all($flag='') {
			
			$map['pay_status'] = 1;
			$map['status'] = 1;
			
			empty($_REQUEST['order_number']) || $map['pay_order_number'] = array('like','%'.$_REQUEST['order_number'].'%');
			
			$field = 'id,pay_order_number as order_number,create_time,case recharge_type when 1 then "支付宝" when 2 then "微信" else "--" end as way';
			
			switch($flag) {
				case 1:{/* 自主充值 */
					$map['promote_id'] = $map['recharge_id'] = PID;
					$field .= ',"自己" as recharge_id,money as num,2 as source';
				};break;
				case 2:{/* 渠道充值 */
					if(!empty($_REQUEST['account'])) {						
						$map['promote_account']=['like','%'.$_REQUEST['account'].'%'];						
					}
					$map['promote_id'] = array('neq',PID);
					$map['recharge_id'] = PID;
					$field .= ',"自己" as recharge_id,money as num,3 as source';
				};break;
				case 3:{/* 充值渠道 */
					if(!empty($_REQUEST['account'])) {						
						$map['recharge_account']=['like','%'.$_REQUEST['account'].'%'];						
					}
					$child = $this->get_sub_all_promote_list();
					$child[]=PID;
					$map['recharge_id'] = array('not in',$child);
					$map['promote_id'] = PID;
					$field .= ',recharge_id,money as num,4 as source';
				};break;
				case 4:{/* 充值子渠道 */
					if(!empty($_REQUEST['account'])) {						
						$map['recharge_account']=['like','%'.$_REQUEST['account'].'%'];
					}
					
					$map['recharge_id'] = array('in',$this->get_sub_all_promote_list());
					$map['promote_id'] = PID;
					$field .= ',recharge_id,money as num,5 as source';
				};break;
				default:
						$field .= 'money as num';
			}
			
			$list = $this->field($field)->where($map)->order('create_time desc')->select();
			
			return $list;
			
		}
		
		/*
		 * 删除记录
		 * @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'):'';
			
		}

}