<?php
/** 店铺登录
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Pad\Model;
use Think\Model;
class FaceModel extends Model{
    protected $tableName = 'member_customer';

    public function member_customer_list($param){
    	$return=array('status'=>3);
    	if($param['shop_id']){
    		$map['shop_id']=$param['shop_id'];
	        $map['membertype']=array('neq',2);
	        if(!empty($param['pagesize']))$pagesize=$param['pagesize'];
	        else $pagesize=6;
	        if($param['p']){
	        	$p=$param['p'];
	        }else{
	        	$p=1;
	        }
	    	$list=page_list('MemberCustomer',$map,'uptime desc',true,$pagesize,'',$p);
	    	if($list['list']){
	    		foreach ($list['list'] as $key => $value) {
		            $list['list'][$key]['uptime_format']=time_format($value['uptime']);
		            if($value['uid']>0){
		                $rank_id=idtoname($value['uid'],"Member","rank_id","id",false);
		                $list['list'][$key]['rank_img']='../../image/v'.$rank_id.'.png';
		                $list['list'][$key]['nickname']=show_user_name($value['uid']);
		                $list['list'][$key]['lj_xf']=floatval(idtoname($value['uid'],"Member","points","id",false));
		                // $consume=$this->cunsume($value['uid'],$value['addtime']);
		                // if($consume['status']==1){
		                	// $list['list'][$key]['lj_xf']=$consume['data']['lj_xf'];
		                // }
		            }
	        	}
	        	$return['status']=1;
	        	$return['list']=$list['list'];
	    	}else{
	    		$return['status']=2;
	    		$return['msg']="暂无数据";
	    	}
    	}else{
    		$return['msg']="参数错误！";
    	}
    	return $return;
    }
    public function cunsume($uid,$reg_time){
    	$return=array('status'=>3,'msg'=>'','data'=>'');
    	if(!empty($uid) && !empty($reg_time)){
    		$map=array();
			$map['type'] = array('in','1,6,13,14');
	        $map['pay_status'] = 3;
	        $map['uid']=$uid;
	        $map['status']=3;
	        $order_amount = M('OrderInfo')->where($map)->field("count(id) as num ,sum(order_money) as money")->find();
	        $lj_xf=$order_amount['money']?$order_amount['money']:0;//累计消费
	        $av_money=floatval($order_amount['money'])/intval($order_amount['num']);//平均每单
	        $av_money_format=round($av_money,2);
	        //该会员 入驻天数
	        $day = intval((NOW_TIME-$reg_time)/24/3600);
	        $av_day = number_format($order_amount['money']/$day,2);//平均每天消费
	        $where=array();
	        $where['status']=1;
	        $where['uid']=$uid;
	        $coupon = M('CouponsGet')->where($where)->count();//共使用券
	        $return['data']['lj_xf']=$lj_xf;
	        $return['data']['av_day']=$av_day;
	        $return['data']['av_money_format']=$av_money_format;
	        $return['data']['coupon']=$coupon;
	        $return['status']=1;
    	}else{
    		$return['status']=3;
    		$return['msg']="参数错误！";
    	}
    	return $return;
	}
	public function label_list(){
		$return=array('status'=>2,'data'=>'','msg'=>'');
		$map = array();$map['id'] = $uid;
		$label_ids = M('Member')->where($map)->getField('label_ids');
		if($label_ids){
			foreach ($label_ids as $key => $value) {
				$label_name[$key]=idtoname($value,'Type','name','id',false);
			}
			$label_name_implode=implode(',',array_filter($label_name));
			$return['status']=1;
			$return['data']=$label_name_implode;
		}else{
			$return['msg']='暂无数据';
		}
		return $return;
	}
	/**
	 * *上次购买情况
	 */
	public function last_buy($uid){
		$return=array('status'=>3,'msg'=>'','data'=>'');
		if(empty($uid)){
			$return['status']=3;
			$return['msg']="参数错误";
		}else{
			$map_og=$map=array();
			$map['uid']=$uid;
			$map['type']=array('in','6,13,14');
			$map['status']=array('in','1,2,3,6,8');
			$map['is_delete']=0;
			$order_info=M('OrderInfo')->where($map)->order('id desc')->find();
			if($order_info){
				$map_og['order_id']=$order_info['id'];
				$order_goods=M('OrderGoods')->where($map_og)->getField('goods_name',true);
				$goods_name=implode(',', $order_goods);
				$return['data']['addtime']=time_format($order_info['addtime']);
				$return['data']['order_amount']=$order_info['order_amount'];
				$return['data']['shop_name']=idtoname($order_info['shop_id'],'Shops','name','id',false);
				$return['data']['goods_name']=$goods_name;
				$return['data']['type']=$order_info['type'];
				$return['data']['address']=idtoname($order_info['province'],'Area','name').idtoname($order_info['city'],'Area','name').idtoname($order_info['district'],'Area','name').$order_info['address'];
				$return['status']=1;
			}else{
				$return['status']=2;
				$return['msg']="暂无数据";
			}
		}
		return $return;
	}
	public function member_customer_info($param){
		$return=array('status'=>3,'data'=>'');
		if($param['customer_id']){
			$map=array();
	        $map['customer_id']=$param['customer_id'];
	        $customer_info=M('MemberCustomerVisitor')->where($map)->order('addtime desc')->find();
	        if($customer_info){
	        	$customer_info['customer_count']=idtoname($param['customer_id'],'MemberCustomer','customer_count','id',false);
	        	$customer_info['addtime_format']=time_format($customer_info['addtime']);
	        	$uid=idtoname($param['customer_id'],'MemberCustomer','uid','id',false);
	        	if($uid>0){
	        		$member_info=idtoname($uid,'Member','username,nickname,real_name,mobile,rank_id,money,usepoints,reg_time,sex,birthday,IDcard,province,city_id,district,is_tmp','id',false);
	        		$customer_info['uid']=$uid;
	        		$customer_info['is_tmp']=$member_info['is_tmp'];
	        		$customer_info['nickname']=show_user_name($uid);
	        		$customer_info['rank_id']=$member_info['rank_id'];
	        		$customer_info['rank_name']=idtoname($member_info['rank_id'],'MemberRank','name','id',false);
	        		$customer_info['rank_img']='../../image/v'.$member_info['rank_id'].'.png';
	        		$customer_info['money']=$member_info['money'];
	        		$customer_info['usepoints']=$member_info['usepoints'];
	        		$customer_info['sex']=$member_info['sex'];
	        		$customer_info['birthday']=$member_info['birthday'];
	        		$customer_info['area']=idtoname($member_info['province'],'Area','name').idtoname($member_info['city_id'],'Area','name').idtoname($member_info['district'],'Area','name');
	        		$cunsume=$this->cunsume($uid,$member_info['reg_time']);
					$return['data']['cunsume']=$cunsume['data'];
	        		$last_buy=$this->last_buy($uid);
					$return['data']['last_buy']=$last_buy['data'];
	        		// $label_list=$this->label_list();
	        		$top_goods_number=$this->top_goods_number($uid);
					$return['data']['top_goods_number']=$top_goods_number['data'];
					$return['data']['cate_count']=$top_goods_number['cate_count'];
					$return['data']['count_pay']=$top_goods_number['count_pay'];
					$return['data']['brand_count']=$top_goods_number['brand_count'];
	        	}
	            $map=array();
	            $map['customer_id']=$param['customer_id'];
	            $last_id=M('MemberCustomerVisitor')->where($map)->order('addtime desc')->getField('id');
	            $map['id']=array('lt',$last_id);
	            $customer_info['wdzpicurl_before']=M('MemberCustomerVisitor')->where($map)->order('addtime desc')->getField('wdzpicurl');
	            $map=array();
	            $map['shop_id']=idtoname($param['customer_id'],'MemberCustomer','shop_id','id',false);
	            $ip=M('ShopsFacedevice')->where($map)->order('id asc')->getField('ip');
	            if($ip){
	            	$video_source='rtsp://'.$ip;
	            }else{
	            	$video_source='';
	            }
	            $return['status']=1;
	            $return['data']['customer_info']=$customer_info;
	            $return['data']['video_source']=$video_source;
	            // $return['data']['label_list']=$label_list['data'];
	        }else{
	        	$return['status']=2;
	        	$return['msg']="暂无数据";
	        }
		}else{
			$return['msg']="参数错误";
		}
		return $return;
	}
	public function top_goods_number($uid,$size=10){
		$return=array('status'=>3,'msg'=>'','data'=>'');
		if(!empty($uid)){
			$ec = new \Common\Vendor\Echarts();
			// 商品分类分析
			$result=$this->catelist($uid);
			$cate_count=array();
			foreach ($result['data'] as $key => $value) {
				if($value['value'] > 0){
					$cate_count[]=array('分类'=>$value['name'],'数量'=>$value['value']);
				}
			}
			$return['cate_count']=array('columns'=>array('分类','数量'),'rows'=>$cate_count);
			
			
			$now_time=strtotime(date('Y-m-d',NOW_TIME));
			// 消费时段统计
			$end_time=strtotime($now_time)+86399;
			$map=array();
			$map['type'] = array('in','1,6,13,14');
			$map['pay_status'] = 3;
			$map['uid']=$uid;
			$countz = M('OrderInfo')->where($map)->count();
			$count = M('OrderInfo')->where($map)->field("count(id) num,addtime as l_month")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%k')")->select();
			foreach($count as $value){
				$counts[date('G',$value['l_month'])] = $value['num'];
			}
			$count1=$count2=$count3=$count4=$count5=$count6=0;
			for($i=0;$i<24;$i++){
				if($counts[$i]){
					if($i<4 && $i>=0){
						$count1+=$counts[$i];
					}
					if($i<8 && $i>=3){
						$count2+=$counts[$i];
					}
					if($i<12 && $i>=8){
						$count3+=$counts[$i];
					}
					if($i<16 && $i>=12){
						$count4+=$counts[$i];
					}
					if($i<20 && $i>=16){
						$count5+=$counts[$i];
					}
					if($i<24 && $i>=20){
						$count6+=$counts[$i];
					}
				}
			}
			if($countz>0){
				$time=array('00:00-03:59','04:00-07:59','08:00-11:59','12:00-15:59','16:00-19:59','20:00-23:59');
				$datas=array(
							array("value"=>$count1,"name"=>"00:00-03:59"),
							array("value"=>$count2,"name"=>"04:00-07:59"),
							array("value"=>$count3,"name"=>"08:00-11:59"),
							array("value"=>$count4,"name"=>"12:00-15:59"),
							array("value"=>$count5,"name"=>"16:00-19:59"),
							array("value"=>$count6,"name"=>"20:00-23:59"),
						);
				if($count1==0){unset($datas[0]);unset($time[0]);}
				if($count2==0){unset($datas[1]);unset($time[1]);}if($count3==0){unset($datas[2]);unset($time[2]);}
				if($count4==0){unset($datas[3]);unset($time[3]);}if($count5==0){unset($datas[4]);unset($time[4]);}if($count6==0){unset($datas[5]);unset($time[5]);}
				$datas=array_merge($datas);$time=array_merge($time);
			}else{
				$time=array();
				$datas=array();
			}
			$count_pay=array();
			foreach ($datas as $key => $value) {
				if($value['value'] > 0){
					$count_pay[]=array('时间段'=>$value['name'],'数量'=>$value['value']);
				}
			}
			$return['count_pay']=array('columns'=>array('时间段','数量'),'rows'=>$count_pay);

			$map=array();
			$map['uid']=$uid;
			$map['type'] = array('in','6,13,14');
			$map['pay_status'] = 3;
			$map['status'] = 3;
			$order_ids = M('OrderInfo')->where($map)->getField('id',true);
			// 品牌排行
			if($order_ids){
				$map = array();
				$map['order_id'] = array('in',$order_ids);
				$goods_ids=M('OrderGoods')->where($map)->getField('goods_id',true);
				$b_map=array();
				if($goods_ids){	
					$b_map['g.id']=array('in',$goods_ids);
					$b_map['og.order_id'] = array('in',$order_ids);
					$b_map['g.brand_id'] = array('gt',0);
					$brand_ids=M('Goods')->alias('g')->JOIN('__ORDER_GOODS__ og on og.goods_id = g.id')->where($b_map)->group('g.brand_id')->field('g.id,g.brand_id,sum(og.goods_number) as num')->order('sum(og.goods_number) desc')->limit(10)->select();
				}
			}
			$brand_name=$brand_data=array();
			if($brand_ids){
				foreach ($brand_ids as $kb => $vb) {
					$bname=idtoname($vb['brand_id'],'Brand','brand_name','id',false);
					$brand_name[]=$bname;
					$brand_data[]=array('value'=>intval($vb['num']),'name'=>$bname);
				}
			}
			$brand_count=array();
			foreach ($brand_data as $key => $value) {
				if($value['value'] > 0){
					$brand_count[]=array('品牌'=>$value['name'],'数量'=>$value['value']);
				}
			}
			$return['brand_count']=array('columns'=>array('品牌','数量'),'rows'=>$brand_count);
		
			
			$map=array();
			$map['uid']=$uid;
			$map['type'] = array('in','6,13,14');
			$map['pay_status'] = 3;
			$map['status'] = 3;
			$order_ids = M('OrderInfo')->where($map)->getField('id',true);
			// 单品购买排行
			$goods_all=0;
			if(!empty($order_ids)){
				$map = array();
				$map['order_id'] = array('in',$order_ids);
				$goods_all=M('OrderGoods')->where($map)->sum('goods_number');
				$map['group'] = 'goods_id';
				$order = 'sum(goods_number) desc';
				$field = 'sum(goods_number) as num,goods_id,goods_sn,goods_name';
				// $size = '10';
				$list = page_list('OrderGoods',$map,$order,$field,$size);
				if(!empty($list['list'])){
					foreach ($list['list'] as $key => $value) {
						$map=array();
						$map['goods_id']=$value['goods_id'];
						$map['order_id']=array('in',$order_ids);
						$order_count=M('OrderGoods')->where($map)->count();
						$list['list'][$key]['order_count']=intval($order_count);
					}
				}
				$return['status']=1;
				$return['data']=$list['list'];
			}else{
				$return['status']=2;
				$return['msg']="暂无数据";
			}
		}else{
			$return['msg']="参数错误";
		}
		return $return;
	}
	public function catelist($uid){
		$return=$cate_count=$data=$og_map=$map=$map_c=$g_map=$catename=array();
        $map_c['status']=1;
        $map_c['pid']=0;
        $carids = M('PurchaseCategory')->where($map_c)->field('id,name')->select();
        unset($map_c['pid']);
        if($carids){
        	$map['type'] = array('in','1,6,13,14');
	        $map['pay_status'] = 3;
	        $map['uid']=$uid;
	        $orderid=M('OrderInfo')->where($map)->getField("id",true);
	        if($orderid){
	            foreach($carids as $k=>$v){
	            	$catename[]=$v['name'];
	                $map_c['pid']=$v['id'];
	                $child_catid = M('PurchaseCategory')->where($map_c)->getField('id',true);
	                if($child_catid){
	                	$child_catid[]=$v['id'];
	                	$g_map['cat_id']=array('in',$child_catid);
	                }else{
	                	$g_map['cat_id']=$v['id'];
	                }
	                $goods_ids = M('Goods')->where($g_map)->getField('id',true);
	                if($goods_ids){
		                $og_map['goods_id']=array('in',$goods_ids);
		                $og_map['order_id']=array('in',$orderid);
		                $goods_num=M('OrderGoods')->where($og_map)->sum('goods_number');
		                $goodsnum[]=intval($goods_num);
	                }else{
	                	$goodsnum[]=0;
	                }
	            }
	        }
        }
        $allnum=array_sum($goodsnum);
        if(count($catename) > 0){
        	foreach ($catename as $key => $value) {
        		$data[]=array("value"=>$goodsnum[$key],"name"=>$value);
        	}
        }
        $return['data']=$data;
		$return['catename']=$catename;
		return $return;
	}
}
?>