<?php
namespace Common\Model;
use Think\Model;
class FaceModel extends Model
{
	/**
	 * 添加顾客
	 * @param 	$data 参数
	 * @return  int 顾客id
	 */
    protected $tableName = 'member_customer';
	public function add_customer($data){
        if(!empty($data)){
            $deviceId=$data['deviceId'];
            $result=$this->get_shop_info($data['deviceId']);
            if($result['status']==1){
                $shop_id=$result['data']['id'];
                $add_data['customer_count']=1;
                $add_data['wdzpicurl']=$data['wdzpicurl'];
                $add_data['addtime']=$add_data['uptime']=NOW_TIME;
                $add_data['shop_id']=$shop_id;
                $customer_id=M('MemberCustomer')->add($add_data);
                if(!empty($customer_id)){
                    $add_data=array();
                    $add_data['customer_id']=$customer_id;
                    $add_data['wdzpicurl']=$data['wdzpicurl'];
                    $add_data['shop_id']=$shop_id;
                    $this->add_member_customer_visitor($add_data);
                    return $customer_id;
                }
            } 
        }
    }
    /**
     * 
     * @param  $deviceId 设备id
     * @return array
     */
    public function get_shop_info($deviceId){
    	$return=array('status'=>0,'data'=>'','info'=>'');
    	if(!empty($deviceId)){
            $shop_id=idtoname($deviceId,'ShopsFacedevice','shop_id','device_id',false);
            if($shop_id){
                $map=array();
                $map['id']=$shop_id;
                $shop_info=M('Shops')->where($map)->field('id,name,status')->find();
                if($shop_info){
                    // file_put_contents('uploads/PAY-fa'.I('get.method').date("YmdHis").'.txt',var_export($shop_id,true));
                    $return['status']=1;
                    $return['data']=$shop_info;
                    return $return;
                }else{
                    $return['info']="未找到仓库信息";
                    return $return;
                }
            }else{
    			$return['info']="设备未绑定";
    			return $return;
    		}
    	}else{
    		$return['info']="未获取到设备id";
    		return $return;
    	}
    }
    /**
     * 更新顾客
     */
    public function up_customer($data){
    	if(!empty($data)){
    		$result=$this->get_shop_info($data['deviceId']);
    		if($result['status']==1){
                // $map=array();
                // $map['old_ids']=array('like','%,'.$data['userid'].',%');
                // $new_id=M('MemberCustomer')->where($map)->getField('id');
                // if($new_id){
                //     $data['userid']=$new_id;
                // }
    			$map=$save_data=array();
	    		$map['id']=$data['userid'];
	    		$save_data['uptime']=NOW_TIME;
	    		$save_data['shop_id']=$data['shop_id']=$result['data']['id'];
	    		$customer_count=idtoname($data['userid'],'MemberCustomer','customer_count','id',false);
	    		$save_data['customer_count']=$customer_count+1;
	    		$save_data['wdzpicurl']=$data['wdzpicurl'];
                $save_data['membertype']=$data['memberType'];
    			M('MemberCustomer')->where($map)->save($save_data);
                $data['customer_id']=$data['userid'];
    			$this->add_member_customer_visitor($data);
    		}
    	}
    }
    /**
     * 添加顾客访问记录
     */
    public function add_member_customer_visitor($data){
    	if(!empty($data)){
    		$add_data=array();
	    	$add_data['addtime']=NOW_TIME;
	    	$add_data['wdzpicurl']=$data['wdzpicurl'];
            $add_data['shop_id']=$data['shop_id'];
	    	if($data['customer_id']){
	    		$add_data['customer_id']=$data['customer_id'];
	    	}
	    	M('MemberCustomerVisitor')->add($add_data);
    	}
    }
    /**
     * 人脸识别页面获取会员顾客信息
     * @param  $shop_id
     * @return array
     */
    public function get_member_customer($param){
        $list='';
        if($param['shop_id']){
            $map=array();
            $map['shop_id']=$param['shop_id'];
            $map['membertype']=array('neq',2);
            // if($param['customer_ids']){
            //     $customer_id_arr=explode(',',$param['customer_ids']);
            //     $map['id']=array('notin',$customer_id_arr);
            // }
            // $pagesize=10;
            // $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
            // $list=page_list('MemberCustomer',$map,'uptime desc',true,$pagesize,'',1);
            $list=M('MemberCustomer')->where($map)->order('uptime desc')->select();
            if($list){
                foreach ($list as $key => $value) {
                    // $customer_id_arr[$key]=$value['id'];
                    $list[$key]['uptime']=time_format($value['uptime']);
                    if($value['uid']>0){
                        $list[$key]['rank_id']=idtoname($value['uid'],"Member","rank_id","id",false);
                        $list[$key]['username']=idtoname($value['uid'],"Member","username",'id',false);
                        $map=array();
                        $map['type'] = array('in','1,6,13,14');
                        $map['pay_status'] = 3;
                        $map['uid']=$value['uid'];
                        $map['status']=3;
                        $order_amount = M('OrderInfo')->where($map)->field("count(id) as num ,sum(order_amount) as money")->find();
                        $list[$key]['lj_xf']=$order_amount['money']?$order_amount['money']:0;//累计消费
                    }
                }
                // $customer_ids=implode(',',$customer_id_arr);
                $map['uid']=array('gt',0);
                $default_uid=M('MemberCustomer')->where($map)->order('uptime desc')->getField('uid');
                $default_right=$this->get_member_customer_right($default_uid);
                $return['list']=$list;
                // $return['data']['customer_ids']=$customer_ids;
                $return['default_right']['member_info']=$default_right;
            }
        }
        return $return;
    }
    public function get_member_customer_right($uid){
        $map=array();
        $map['id']=$uid;
        $member_info=M('Member')->where($map)->find();
        if($member_info){
            $customer_id=idtoname($uid,'MemberCustomer','id','uid',false);
            $member_info['uptime']=time_format(idtoname($uid,'MemberCustomer','uptime','uid',false));
            $member_info['wdzpicurl']=idtoname($uid,'MemberCustomer','wdzpicurl','uid',false);
            $map=array();
            $map['customer_id']=$customer_id;
            $last_id=M('MemberCustomerVisitor')->where($map)->order('addtime desc')->getField('id');
            $map['id']=array('lt',$last_id);
            $member_info['wdzpicurl_before']=M('MemberCustomerVisitor')->where($map)->order('addtime desc')->getField('wdzpicurl');
            return $member_info;
        }
    }
    public function change_membertype($param){
        $return=array('status'=>3,'msg'=>'');
        if($param['customer_id'] && $param['shop_id']){
            $map=array();
            $map['id']=$param['customer_id'];
            $customer_info=M('MemberCustomer')->where($map)->find();
            $group_id=idtoname($param['shop_id'],"shops",'group_id','id',false);
            if($customer_info){
                if($customer_info['membertype']==2){
                    $return['msg']="已经是店员";
                }else{
                    $updata=array();
                    $updata['departno']=$group_id;
                    $updata['userid']=$param['customer_id'];
                    $updata['thirdpicurl']=$customer_info['wdzpicurl'];
                    $updata['memberType']=2;
                    $face = new \Common\Vendor\Face();
                    $result=$face->updateUser($updata);
                    if($result['stat']['code']==0){
                        $updata=array();
                        $updata['membertype']=2;
                        $res=M('MemberCustomer')->where($map)->save($updata);
                        if($res){
                            $return['status']=1;
                            $return['msg']="更改成功";
                        }else{
                            $return['status']=3;
                            $return['msg']="更改失败";
                        }
                    }else{
                        $return['msg']=$result['result'];
                    }
                }
            }else{
                $return['msg']="参数错误";
            }
        }else{
            $return['msg']="参数错误";
        }
        return $return;
    }
}
?>