<?php

namespace wstmart\admin\controller;

use wstmart\admin\model\Platform as M;

/**

 * ============================================================================

 * 基础控业务处理

 */


class Platform extends Base{

	//列表

	public function index(){

		$where=[];

		$map=[];

		$page_map=[];

		//接受时间

		if (Request()->isPost()) {

			$start_date=input('start');

			$end_date=input('end');

			$start=strtotime($start_date);

			$end=strtotime($end_date);

			if($start && $end){

				$where['createTime'] =array('between',[$start,$end]);

				$map['createTime'] =array('between',[$start_date,$end_date]);

				$page_map['create_time']=array('between',[$start,$end]);

			}elseif($start){

				$where['createTime'] =array('egt',$start);

				$map['createTime'] =array('egt',$start_date);

				$page_map['create_time']=array('egt',$start);

			}elseif($end){

				$where['createTime'] =array('elt',$end);

				$page_map['create_time']=array('elt',$end);

				$map['createTime'] =array('egt',$end_date);

			}

		}

		//判断时间周期内的注册数量

		$where['loginSrc']=0;

		$result['pcNum'] = db('login_src')->where($where)->count();

		$where['loginSrc']=1;

		$result['webappNum'] = db('login_src')->where($where)->count();

		$where['loginSrc']=2 ;

		$result['appNum'] = db('login_src')->where($where)->count();

		//查询出所有新注册用户

		unset($where['loginSrc']);	

		//dump($ids);

		//下过单的用户

		if(Request()->isAjax()){

			$userIds=db('login_src')->where($where)->field('userId')->select();

			$ids=[];

			foreach ($userIds as &$value) {

				$ids[]=$value['userId'];

			}

			$map['userId']=array('in',$ids);

			$page_map['userId']=array('in',$ids);

		}		

		$map['isPay']=1;

		$result['validUser']=db('orders')->where($map)->group('userId')->count('userId');

		//总单数

		$orderNum=db('orders')->where($map)->count('orderId');

		//总浏览		

		$viewNum=db('page_view')->where($page_map)->count('id');

		// dump($orderNum);

		// dump($viewNum);

		//转换率

		if($orderNum==0 || $viewNum==0){

			$result['orderRade']="0".'%';

		}else{

			$result['orderRade']=(round($orderNum/$viewNum,2)*100).'%';

		}

		//dump($result);

		if (Request()->isAjax()) {

			exit(jsonReturn('',1,$result));

		}

		$this->assign('object',$result);

		//dump($result);

		return $this->fetch('platform');

	}

	// //获取分页

	// public function platformByPage(){

	// 	$m=new M();

	// 	$result=$m->platformByPage();

	// 	$result['status'] = 1;

	// 	return WSTGrid($result);

	// }
	//查看券值记录

	public function viewData(){
		if(Request()->isAjax()){
			$dataType = input('post.dataType');
			$start_date = input('start');
			$end_date = input('end');
			$start = strtotime($start_date);
			$end = strtotime($end_date);
			$m = Model('common/Table');
			$m->setTable('user_vouchers_notice');
			$result['num'] = 0;
			$where = [];
			$map = [];
			if($start && $end){
				$where['createTime'] =array('between',[$start,$end]);
				$map['createTime'] =array('between',[$start_date,$end_date]);
			}elseif($start){
				$where['createTime'] =array('egt',$start);
				$map['createTime'] =array('egt',$start_date);
			}elseif($end){
				$where['createTime'] =array('elt',$end);
				$map['createTime'] =array('egt',$end_date);
			}
			switch ($dataType) {
				case 1://预获产品券		
					$result['num'] = $m->getSum(array_merge($where,['moneyType'=>1]),'expectedProductNum');
					$result['num'] -= $m->getSum(array_merge($where,['moneyType'=>0]),'expectedProductNum');
					break;
				case 2://预获优惠券
					$result['num'] = $m->getSum(array_merge($where,['moneyType'=>1]),'expectedCouponsNum');
					$result['num'] -= $m->getSum(array_merge($where,['moneyType'=>0]),'expectedCouponsNum');
					break;
				case 3://产品券
				case 4://优惠券
				case 5://旺旺券
					$dataType-=2;
					$m->setTable('log_moneys');
					$result['num'] = $m->getSum(array_merge($map,['dataFlag'=>1,'moneyType'=>1,'moneyName'=>$dataType]),'money');
					$result['num'] -= $m->getSum(array_merge($map,['dataFlag'=>1,'moneyType'=>0,'moneyName'=>$dataType]),'money');
					break;
				default:				
					break;
			}
			exit(jsonReturn('',1,$result));
		}
		return $this->fetch('view_data');
	}
}