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

namespace App\Model;
use Think\Model;
class MemberModel extends Model{
	
    public function member_list($keyword,$current_id){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
        if($keyword){$map['mobile|real_name|card_sn|username']=array('like','%'.$keyword.'%');}
        $map['status']=1;
		if($current_id == 1){//粉丝
			$map['is_follow']=1;
             // 1122 ggw 粉丝范围判断
            $shop_id=I('shop_id');
            if($shop_id){
                $auth_code = 'fans';
                $ret=D('Shop/Shops')->shop_option($shop_id,$auth_code);
                if($ret['status']==1){ //显示门店粉丝
                    $map['user_shop']=$shop_id;
                }
            }
            // 1122end
            
		}else{//会员
			$map['is_tmp']=0;
            $is_share_user=1;//0共享 1不共享
            $shop_id=I('shop_id');
            if($shop_id){//查看当前店铺是否共享会员
                $is_share_user=idtoname($shop_id,'Shops','is_share_user');
                if(empty($is_share_user)){//不共享会员]
                    $c_map=$customer=array();
                    $c_map['shop_id'] = $shop_id;
                    $count = M('ShopsCustomer')->cache(true)->where($c_map)->count();
                    if($count>0){
                        $customer = M('ShopsCustomer')->cache(true)->where($c_map)->getField('user_id',true);
                    }
                    if(empty($customer)){//无顾客
                        $map['user_shop']=$shop_id;
                    }else{
                        $where=array();
                        $where['user_shop']=$shop_id;
                        $where['id']  = array('in',$customer);
                        $where['_logic'] = 'or';
                        $map['_complex'] = $where;
                    }
                }
            }
            if(!empty($is_share_user)){//会员共享调用
                $smap=array();
                $smap['is_share_user']=0;
                $cachetime=60*60*24*7;
                $noshareshopids=M('Shops')->where($smap)->cache(true,$cachetime)->getField('id',true);
                if(!empty($noshareshopids)){
                    $map['user_shop'] = array('notin',$noshareshopids);
                }            
            }
		}
        $pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
        $info=page_list('Member',$map,'id desc','id,nickname,real_name,username,mobile,money,points,header_img,wdzpicurl,usepoints,rank_id',$pagesize,'',1);
        if($info['list']){
			foreach($info['list'] as $k=>$v){
				$info['list'][$k]['nickname'] = show_user_name($v['id']);
				$info['list'][$k]['rank_img'] = '../../image/v'.idtoname($v['rank_id'],'MemberRank','sort').'.png';
                
                if($info['list'][$k]['mobile'] || $info['list'][$k]['real_name'] || $info['list'][$k]['username'] || $info['list'][$k]['nickname']){
                    $is_hide_phone=idtoname($shop_id,'Shops','is_hide_phone');
                    if($is_hide_phone==1){
                        if($info['list'][$k]['nickname'] && $info['list'][$k]['nickname']==$info['list'][$k]['mobile'])$info['list'][$k]['nickname']=str_cutmobile($info['list'][$k]['nickname']);
                        if($info['list'][$k]['mobile'])$info['list'][$k]['mobile']=str_cutmobile($info['list'][$k]['mobile']);
                        if($info['list'][$k]['real_name'] && strlen($info['list'][$k]['real_name'])==11)$info['list'][$k]['real_name']=str_cutmobile($info['list'][$k]['real_name']);
                        if($info['list'][$k]['username'] && strlen($info['list'][$k]['username'])==11)$info['list'][$k]['username']=str_cutmobile($info['list'][$k]['username']);
                    }
                }
			}
			$return['status']=1; $return['data'] = $info['list'];
		}
        else{$return=array('status'=>2,'msg'=>'暂无数据');}
        return $return;
    }
    public function member_infor($uid,$type=0){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
        $map['id']=$uid;
        $info=M("Member")->where($map)->field('id,nickname,is_tmp,sex,mobile,money,points,real_name,usepoints,frozen_money,rank_id,birthday,reg_time,province,city_id,district,guide,header_img,wdzpicurl')->find();
        if($info){
            $return['status']=1;
			$map = array();$map['id'] = $info['rank_id'];
            $MemberRank=M('MemberRank')->where($map)->field('name,goods_price,sort')->find();
			$info['nickname'] = show_user_name($info['id']);
            $info['rank_name']=$MemberRank['name'];
            $info['goods_price']=$MemberRank['goods_price'];
			$sort=$MemberRank['sort'];
			$info['rank_img'] = '../../image/v'.$sort.'.png';
			if($info['goods_price'])$info['goods_price']=$info['goods_price']/10;
            $info['reg_time']=time_format($info['reg_time']);
            $info['daogou_name']=$info['guide']>0?idtoname($info['guide'],'ShopsAccount','name','id',false):'';
            if(empty($info['idcard']) || $info['idcard'] == 'null' || $info['idcard'] == null){$info['IDcard'] = '';$info['idcard'] = '';}
            //查询等级特权
			$map = array();
			$rank_limit = "'0,".$info['rank_id']."'";
			$map['rank_limit'] = array('IN',$rank_limit);
			$map['status'] = 1;
			if($type==1)$map['consumption_type'] = array('neq',2);
			$info['many_points'] = M('ManypointsRule')->where($map)->field(true)->select();
			if($info['many_points']){
				foreach($info['many_points'] as $k=>$v){
					if($v['consumption_type'] == 1 && floatval($v['give_num']) > 0){
						$info['many_points'][$k]['describe'] = '充值满'.$v['money_limit'].'，送'.$v['give_num'].'倍积分';
					}elseif($v['consumption_type'] == 2 && floatval($v['give_num']) > 0){
						$info['many_points'][$k]['describe'] = '消费满'.$v['money_limit'].'，送'.$v['give_num'].'倍积分';
					}elseif($v['consumption_type'] == 3 && floatval($v['give_num']) > 0){
						$info['many_points'][$k]['describe'] = '充值满'.$v['money_limit'].'，送'.$v['give_num'].'元现金';
					}
				}
			}unset($k,$v);
            $shop_id=I('shop_id');
			//查询优惠劵
			$map = array();
            $map['status'] = 0;
			$map['uid'] = $uid;
            if($shop_id)$map['_string'] = "shop_id='0' or shop_id like '%,".$shop_id.",%'";
            //$map['begin_timestamp']=array('elt',NOW_TIME);
			$map['end_timestamp'] = array('egt',NOW_TIME);
            //卡券
            $info['coupons'] = M('CouponsGet')->where($map)->field(true)->select();
			foreach($info['coupons'] as $k=>$v){
                $coupons_info= idtoname($v['coupons_id'],'Coupons','*');
                if(empty($coupons_info['title'])){
                    unset($info['coupons'][$k]);continue;
                }
				$info['coupons'][$k]['c_type'] =$coupons_info['type'];
				$info['coupons'][$k]['title'] = $coupons_info['title'];
                $info['coupons'][$k]['start_time'] = time_format($v['begin_timestamp'],'Y-m-d');
                $info['coupons'][$k]['end_time'] = time_format($v['end_timestamp'],'Y-m-d');
                if($info['coupons'][$k]['c_type'] == 1){
					$info['coupons'][$k]['start_time'] = time_format($coupons_info['begin_timestamp'],'Y-m-d');
					$info['coupons'][$k]['end_time'] = time_format($coupons_info['end_timestamp'],'Y-m-d');
				}elseif($info['coupons'][$k]['c_type'] == 2){
					$info['coupons'][$k]['fixed_term'] = $coupons_info['fixed_term'];
					$info['coupons'][$k]['fixed_begin_term'] = $coupons_info['fixed_begin_term'];
                    if($v['fixed_begin_term']<=0){
                        $info['coupons'][$k]['fixed_begin_term']="立即生效";
                    }else{
                        $info['coupons'][$k]['fixed_begin_term']=$v['fixed_begin_term']."天生效";
                    }
				}
                $info['coupons'][$k]['color'] = $coupons_info['color'];
                $info['coupons'][$k]['logo_url'] = $coupons_info['logo_url'];

                $rank_id = $coupons_info['rank_id'];
                if($rank_id=='CASH'){
                    $info['coupons'][$k]['discount_type']=1;
                }else{
                    $info['coupons'][$k]['discount_type']=2;
                    $info['coupons'][$k]['discount']=(100-floatval($v['discount']))/10;
                }
			}
            $map = array();
            $map['uid'] = $uid;
            $info['last_money']=M('order_info')->where($map)->order('addtime desc')->limit(1)->getField('order_amount');
            $info['last_money']=format_decimal($info['last_money']);
            $info['points']=intval($info['points']);
            if($info['province'])$info['provinces']=idtoname($info['province'],'Area','name');
            if($info['city_id'])$info['city']=idtoname($info['city_id'],'Area','name');
            if($info['district'])$info['districts']=idtoname($info['district'],'Area','name');
            
            if($shop_id && ($info['mobile'] || $info['real_name'] || $info['username'] || $info['nickname'])){
                $is_hide_phone=idtoname($shop_id,'Shops','is_hide_phone');
                if($is_hide_phone==1){
                    if($info['nickname'] && $info['nickname']==$info['mobile'])$info['nickname']=str_cutmobile($info['nickname']);
                    if($info['mobile'])$info['mobile']=str_cutmobile($info['mobile']);
                    if($info['real_name'] && strlen($info['real_name']==11))$info['real_name']=str_cutmobile($info['real_name']);
                    if($info['username'] && strlen($info['username']==11))$info['username']=str_cutmobile($info['username']);
                }
            }
            
            $return['data'] = $info;
        }else{$return=array('status'=>2,'msg'=>'暂无数据');}
        return $return;
    }
    public function get_grade($id){
        $return=array('status'=>3,'msg'=>'');
        $info=M("MemberRank")->field(true)->find($id);
		if($info['sort'] == 1){
			$name = '等级1(最低)';
		}elseif($info['sort'] == 10){
			$name = '等级10(最高)';
		}else{
			$name = '等级'.$info['sort'];
		}
		$info['goods_price'] = Intval($info['goods_price']);
		$info['rank_img'] = '../../image/v'.$info['sort'].'.png';
        $info['sort'] = array('id'=>$info['sort'],'name'=>$name);
		
        if($info){
            $return['status'] = 1;
            $return['data'] = $info;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    public function get_memberinfo($id,$shop_id=''){
        $return=array('status'=>3,'msg'=>'');
        $map = array();
        $map['id']=$id;
        $info=M("Member")->where($map)->field('id,username,nickname,sex,real_name,mobile,birthday,reg_time,card_sn,province,city_id,district,guide,IDcard,wdzpicurl')->find();
        if($info){
            $info['reg_time']=time_format($info['reg_time']);
            if($info['province'])$info['provinces']=idtoname($info['province'],'Area','name');
            if($info['city_id'])$info['city']=idtoname($info['city_id'],'Area','name');
            if($info['district'])$info['districts']=idtoname($info['district'],'Area','name');
            $info['region']=$info['provinces'].$info['city'].$info['districts'];
            if($info['guide'] && empty($shop_id)){
				$info['guide'] = array('id'=>$info['guide'],'name'=>idtoname($info['guide'],'ShopsAccount','name','id',false));
			}
            if(empty($info['idcard']) || $info['idcard'] == 'null' || $info['idcard'] == null){$info['IDcard'] = '';$info['idcard'] = '';}
            if(empty($shop_id))$shop_id=I('shop_id');
            if($shop_id){
                $dagou=$this->getdaogoulist($shop_id);
                $info['daogou']=$dagou['data'];
            }
            if($shop_id && ($info['mobile'] || $info['real_name'] || $info['username'] || $info['nickname'])){
                $is_hide_phone=idtoname($shop_id,'Shops','is_hide_phone');
                if($is_hide_phone==1){
                    if($info['nickname'] && $info['nickname']==$info['mobile'])$info['nickname']=str_cutmobile($info['nickname']);
                    if($info['mobile'])$info['mobile']=str_cutmobile($info['mobile']);
                    if($info['real_name'] && strlen($info['real_name'])==11)$info['real_name']=str_cutmobile($info['real_name']);
                    if($info['username'] && strlen($info['username'])==11)$info['username']=str_cutmobile($info['username']);
                }
            }
            
            $return['status'] = 1;
            $return['data'] = $info;            
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    public function del_grade($id){
        $return=array('status'=>3,'msg'=>'');
        if(empty($id)){
            $return['msg'] = '会员已绑定等级,不可删除';
            return $return;
        }
        $result=check_delete($id,1,'rank');
        if(!$result['status']){
            $return['msg']=$result['info'];
            return $return;
        }
        $ret=M('MemberRank')->delete($id);
        if($ret){
                $return['status']=1;
        }else{
            $return['msg']='删除失败';
        }
        return $return;
    }
	public function del_many_points($id){
        $return=array('status'=>3,'msg'=>'');
        if(empty($id)){
            $return['msg'] = '数据错误';
            return $return;
        }
        $ret=M('ManypointsRule')->delete($id);
        if($ret){
                $return['status']=1;
        }else{
            $return['msg']='删除失败';
        }
        return $return;
    }
	public function del_care($id){
        $return=array('status'=>3,'msg'=>'');
        if(empty($id)){
            $return['msg'] = '数据错误';
            return $return;
        }
        $ret=M('MemberCare')->delete($id);
        if($ret){
                $return['status']=1;
        }else{
            $return['msg']='删除失败';
        }
        return $return;
    }
    public function grade_list(){
        $return=array('status'=>1,'msg'=>'');
        $map = array();
        $map['status']=1;
        $grade_list=M("MemberRank")->where($map)->field('id,name,min_points,max_points,goods_price,rank_img')->select();
        if($grade_list){
			foreach($grade_list as $k=>$v){
				$grade_list[$k]['goods_price'] = $v['goods_price']/10;
			}
            $return['data']=$grade_list;
        }else{
            $return['status']=2;
            $return['msg']='未获取到数据';
        }
        return $return;
    }
    public function member_grade_add($postdata){
        $return=array('status'=>3,'msg'=>'');
        if(empty($postdata)){
            $return['msg']='数据错误';
            return $return;
        }
        $id = $postdata['id'];
        $model = M('MemberRank');
        $map = array();
        $map['sort']=$postdata['sort'];
        if($id>0){$map['id']=array('neq',$id);}
        $result = $model->where($map)->count();
        if (!empty($result)){
            $return['msg']='该等级已经存在';
            return $return;
        }
        $postdata['status'] = 1;
        if($id>0){
            $ret = $model->save($postdata);
            if($ret){
                $return['status'] = 1;
            }else{
                $return['msg']='修改失败';
            }
        }else{
            $id = $model->add($postdata);
            if($id){
                $return['status'] = 1;
            }else{
                $return['msg']='添加失败';
            }
        }
        return $return;
    }
    public function member_add($postdata){
        $return=array('status'=>3,'msg'=>'');
        $model = M('Member');
        // if($postdata['username'] == ''){
            // $return['msg']='用户名不能为空';
            // return $return;
        // }
        $con_where = array();
        $con_where['name'] = 'MEMBERATTRIBUTE';
        $con_have = M("Config")->where($con_where)->field("id,name,value")->find();
        if($con_have){
            $con_data = unserialize($con_have['value']);
            if($con_data['real_name']['is_shop']){
                if($postdata['real_name'] == ''){
                    $return['msg']='真实姓名不能为空';
                    return $return;
                }
            }
            if($con_data['birthday']['is_shop']){
                if($postdata['birthday'] == ''){
                    $return['msg']='生日不能为空';
                    return $return;
                }
            }
        }
        if($postdata['mobile'] == ''){
            $return['msg']='手机号不能为空';
            return $return;
        }
        // if($postdata['card_sn'] == ''){
            // $return['msg']='卡号不能为空';
            // $return $return;
        // }
		$map=array();
        $map['mobile']=$postdata['mobile'];
        
        if($postdata['id']){
            if(str_exists($postdata['mobile'],'****')){
                $member=idtoname($postdata['id'],'Member','mobile,real_name,username,nickname');
                $postdata['mobile']=$member['mobile'];
                if($postdata['real_name'] && str_exists($postdata['real_name'],'****'))$postdata['real_name']=$member['real_name'];
                if($postdata['username'] && str_exists($postdata['username'],'****'))$postdata['username']=$member['username'];
                if($postdata['nickname'] && str_exists($postdata['nickname'],'****'))$postdata['nickname']=$member['nickname'];
            }
            
            unset($postdata['reg_time']);
            unset($postdata['region']);
			$map['id'] = array('neq',$postdata['id']);
			$result = $model->where($map)->count();
            if (!empty($result)){
                $return['msg']='手机号'.$postdata['mobile'].'的会员已经存在';
                return $return;
            }
            $postdata['updatetime']=NOW_TIME;
            if($postdata['mobile']){
                $postdata['is_tmp'] = 0;
            }
            $postdata['name'] = json_decode(htmlspecialchars_decode($postdata['name']),true);
            if($postdata['name']){
                $where = $c_map = array();
                $c_map['name'] = 'ATTRIBUTE';
                $c_have = M("Config")->where($c_map)->getField("value");
                $c_list = json_decode($c_have,true);
                foreach ($postdata['name'] as $key => $value) {
                    if($value){
                        $where['aid'] = $key;
                        $where['uid'] = $postdata['id'];
                        $have = M("MemberAttribute")->where($where)->count();
                        if($c_list[$key]['type'] == 'date'){
                            $value = strtotime($value);
                        }
                        if($c_list[$key]['type'] == 'checkbox'){
                            $value= preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$value);
                            // $value = ltrim($value, ","); 
                        }
                        if($have){
                            $save_data = array();
                            $save_data['content'] = $value;
                            M("MemberAttribute")->where($where)->save($save_data);
                        }else{
                            $add_data = array();
                            $add_data['uid'] = $postdata['id'];
                            $add_data['aid'] = $key;
                            $add_data['content'] = $value;
                            M("MemberAttribute")->add($add_data);
                        }
                        if($c_list[$key]['type'] == 'date'){
                            $value = date('Y-m-d',$value);
                        }
                    }
                    // $data[] = array('id'=>$key,'name'=>$value);
                }unset($key,$value);
                unset($postdata['name']);
            }
            unset($postdata['nickname']);
            $result = $model->save($postdata);
			
			//绑定会员和顾客照片
			if(($postdata['wdzpicurl'] || $postdata['mc_id']) && $postdata['id']){
				$auth_code='main-face_recognition';
				$postdata['auth_code']=$postdata['face_auth_code'];
				$postdata['auth_appkey']=$postdata['face_auth_appkey'];
				unset($postdata['face_auth_code']);unset($postdata['face_auth_appkey']);
				$ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
				if($ret['status']==1) {
					$data = array();$mc_id = 0;
					$data['uid'] = $postdata['id'];
					if($postdata['mc_id']){
						$mc_id = $postdata['mc_id'];
					}
					$data['mc_id'] = $mc_id;
					$data['shop_id'] = $postdata['current_id'];
					$data['wdzpicurl'] = $postdata['wdzpicurl'];
					$this->save_member_customer($data);
				}
			}
            if($result){
                $return['status'] = 1;
                $return['msg']='修改成功';
            }else{
                $return['msg']='修改失败';
            }
        }else{
            $result = $model->where($map)->count();
            if (!empty($result)){
                $return['msg']='手机号'.$postdata['mobile'].'的会员已经存在';
                return $return;
            }
			$postdata['parent_id'] = 0;
			if(!empty($postdata['parent_mobile'])){
				if($postdata['parent_mobile'] == $postdata['mobile']){
					$return['msg']='推荐人手机号'.$postdata['parent_mobile'].'不能是自己手机号';
					return $return;
				}else{
					$map = array();$map['mobile'] = $postdata['parent_mobile'];
					$parent_id = $model->where($map)->getField('id');
					if (!$parent_id){
						$return['msg']='推荐人手机号'.$postdata['parent_mobile'].'不存在';
						return $return;
					}else{
						$postdata['parent_id'] = $parent_id;
					}
				}
			}
            $postdata['salt'] = randStr();
            $postdata['reg_time'] = $postdata['card_time'] = NOW_TIME;
            $postdata['reg_ip'] = get_client_ip(1);
            $postdata['rank_id'] = 1;
            $postdata['points'] = 0;
            $postdata['recharge_points'] = 0;
            $postdata['is_tmp'] = 0;
            $postdata['card_status'] = 1;
            $postdata['status'] = 1;
            $postdata['guide'] = $postdata['guide'];
            $postdata['user_shop'] = $postdata['shop_id'];
            unset($postdata['region']);
            C('TOKEN_ON',false);
            if($postdata['name']){
                $arr_name = $postdata['name'];
            }unset($postdata['name']);
            $model->create($postdata);
            $id = $model->add();
            if($id){
                $arr_name = json_decode(htmlspecialchars_decode($arr_name),true);
                if($arr_name){
                    $where = $c_map = array();
                    $c_map['name'] = 'ATTRIBUTE';
                    $c_have = M("Config")->where($c_map)->getField("value");
                    $c_list = json_decode($c_have,true);
                    foreach ($arr_name as $key => $value) {
                        if($value){
                            if($c_list[$key]['type'] == 'date'){
                                $value = strtotime($value);
                            }
                            if($c_list[$key]['type'] == 'checkbox'){
                                $value= preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$value);
                            }
                            $add_data = array();
                            $add_data['uid'] = $id;
                            $add_data['aid'] = $key;
                            $add_data['content'] = $value;
                            M("MemberAttribute")->add($add_data);
                            if($c_list[$key]['type'] == 'date'){
                                $value = date('Y-m-d',$value);
                            }
                        }
                    }unset($key,$value);
                }
				
				//绑定会员和顾客照片
				if(($postdata['wdzpicurl'] || $postdata['mc_id']) && $id){
					$auth_code='main-face_recognition';
					$postdata['auth_code']=$postdata['face_auth_code'];
					$postdata['auth_appkey']=$postdata['face_auth_appkey'];
					unset($postdata['face_auth_code']);unset($postdata['face_auth_appkey']);
					$ret=D("Pad/ShopsRoleAuth")->check_auth_code($auth_code,$postdata);
					if($ret['status']==1) {
						$data = array();$mc_id = 0;
						$data['uid'] = $id;
						if($postdata['mc_id']){
							$mc_id = $postdata['mc_id'];
						}
						$data['mc_id'] = $mc_id;
						$data['shop_id'] = $postdata['current_id'];
						$data['wdzpicurl'] = $postdata['wdzpicurl'];
						$this->save_member_customer($data);
					}
				}
				
                $return['status'] = 1;
                $return['msg']='办理成功';
            }else{
                $return['msg']='办理失败';
            }
        }
        return $return;
    }
	// 会员人脸绑定
	public function save_member_customer($post){
		$return=array('status'=>3,'msg'=>'');
		if(empty($post)){return false;}
		
		if($post['mc_id'] && $post['uid']){
			$mc_save = array();
			$mc_save['id'] = $post['mc_id'];
			$mc_save['uid'] = $post['uid'];
			$mc_save['membertype'] = 1;
			if($post['wdzpicurl']){
				$mc_save['wdzpicurl'] = $post['wdzpicurl'];
			}
			M('MemberCustomer')->save($mc_save);
		}else{
			if($post['wdzpicurl'] && $post['uid']){
				$data = $mc_data = $mcv_data = array();
				$data['shop_id'] = $post['shop_id'];
				$data['wdzpicurl'] = $post['wdzpicurl'];
				$data['addtime'] = NOW_TIME;
				$mc_data = $data;
				$mc_data['customer_count'] = 1;$mc_data['membertype'] = 1;
				$mc_data['uptime'] = NOW_TIME;$mc_data['uid'] = $post['uid'];
				$ret = M('MemberCustomer')->add($mc_data);//添加顾客表
				
				$mcv_data = $data;
				$mcv_data['customer_id'] = $ret;
				M('MemberCustomerVisitor')->add($mcv_data);//添加顾客记录表
				
				$add_data = $adddata = array();
				$add_data['userid']=$ret;
				$add_data['thirdpicurl']=$post['wdzpicurl'];
				$add_data['membertype']=1;
				$adddata[]=$add_data;
				$face = new \Common\Vendor\Face;
				$return_add=$face->addUser($adddata);//上传云
				if($return_add['stat']['code']!=0){
					$return['info']=$return_add['result'];
					return $return;
				}
			}else{
				return false;
			}
		}
	}
    public function integral_reward(){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
        $map['device']=7;
        $map['status']=1;
        $map['group']=array('eq',1);
		$map['name'] = array('neq','LOGIN_GAIN_SELL_INTEGRAL');
        $info = M('Config')->where($map)->field('id,name,title,group,extra,value,remark,type')->order('sort')->select();
        if($info){
            foreach ($info as $key => $value) {
                if($info[$key]['type']==4){
                    $info[$key]['extra']=strtoarray($value['extra']);
                }
            }
            $return['data']=$info;
            $return['status'] = 1;
        }else{
            $return['status']=2;
            $return['msg']='未获取到数据';
        }
        return $return;
    }
	public function get_mrlist(){
		$return=array('status'=>1,'msg'=>'');
		$map=array();
        $map['status']=1;
		$groupData = M('MemberRank')->where($map)->field('id,name')->select();
		$z_count = M('Member')->where($map)->count();
		// $newgroupData[0] = '全部等级('.$z_count.')';
		if($groupData){
			$newgroupData[0] = array('id'=>0,'name'=>'全部等级('.$z_count.')');
			foreach ($groupData as $k=>$v) {
				$map['rank_id'] = $v['id'];
				$num = M('Member')->where($map)->count();
				$newgroupData[$k+1]=array('id'=>$v['id'],'name'=>$v['name']."(".$num.")");
                // $newgroupData[$v['id']] = $v['name']."(".$num.")";
			}
		}
		$return['data'] = $newgroupData;
        return $return;
            
	}
	public function many_points_add($postdata){
		$return=array('status'=>3,'msg'=>'');
		$model = D('ManypointsRule');
		if(!$postdata){
			$return['msg']='数据错误';
			return $return;
		}
		if (!isset($postdata['name']) || ($postdata['name'] == '')) {
			$return['msg']='名称不能为空';
			return $return;
        }
		$map = array();
		if($postdata['id']){$map['id'] = array('neq',$postdata['id']);}
		$map['name'] = $postdata['name'];
		$count = $model->where($map)->count();
		if ($count > 0) {
			$return['msg']='该名称已经存在！';
			return $return;
		}
		if($postdata['consumption_type']==1 || $postdata['consumption_type']==2){
			$postdata['give_type']=2;
		}
		if($postdata['consumption_type']==3){
			$postdata['give_type']=1;
		}
		if($postdata['id']){
			$result = $model->save($postdata);
		}else{
			$postdata['begin_time'] = 0;
			$postdata['end_time'] = 0;
			$postdata['add_time'] = NOW_TIME;
			$postdata['status'] = 1;
			$result = $model->add($postdata);
		}
		if ($result) {
            $return['status'] = 1;
		} else {
			if($postdata['id']){
				$return['msg'] = '修改失败';
			}else{
				$return['msg'] = '添加失败';
			}
		}
		return $return;
	}
    public function save_reward($list){
        $return=array('status'=>3,'msg'=>'');
        if($list){
            foreach ($list as $key => $value) {
                $map=array();
                $map['id']=$value['id'];
                $info = M('Config')->where($map)->setField('value',$value['value']);
            }
        }
        $return['status'] = 1;
        return $return;
    }
	public function many_points(){
		$return=array('status'=>1,'msg'=>'');
        $map = array();
        // $map['status']=1;
        $many_points_list=M("ManypointsRule")->where($map)->field('id,name,consumption_type,rank_limit,money_limit,give_type,give_num')->order('id desc')->select();
		foreach($many_points_list as $k=>$v){
			if($v['rank_limit'] == 0){
				$many_points_list[$k]['rank_limit'] = '任意等级';
			}else{
				$many_points_list[$k]['rank_limit'] = idtoname($v['rank_limit'],'MemberRank','name','id',false);
			}
			
			if($v['consumption_type'] == 1){
				$many_points_list[$k]['consumption_type'] = '充值享积分特权';
				$many_points_list[$k]['give_num'] = $v['give_num'];
				$many_points_list[$k]['type'] = '充值';
				$many_points_list[$k]['remark'] = '  倍积分';
			}elseif($v['consumption_type'] == 2){
				$many_points_list[$k]['consumption_type'] = '消费享积分特权';
				$many_points_list[$k]['give_num'] = $v['give_num'];
				$many_points_list[$k]['type'] = '消费';
				$many_points_list[$k]['remark'] = '  倍积分';
			}elseif($v['consumption_type'] == 3){
				$many_points_list[$k]['consumption_type'] = '充值送现金特权';
				$many_points_list[$k]['give_num'] = $v['give_num'];
				$many_points_list[$k]['type'] = '充值';
				$many_points_list[$k]['remark'] = '  元现金';
			}
		}
        if($many_points_list){
            $return['data']=$many_points_list;
        }else{
            $return['status']=2;
            $return['msg']='未获取到数据';
        }
        return $return;
	}
	public function get_many_point_info($id){
        $return=array('status'=>3,'msg'=>'');
        $info=M("ManypointsRule")->field(true)->find($id);
		if($info['rank_limit'] == 0){
			$map=array();
			$map['status']=1;
			$z_count = M('Member')->where($map)->count();
			$info['rank_limit'] = array('id'=>0,'name'=>'全部等级('.$z_count.')');
		}else{
			$map['rank_id'] = $info['rank_limit'];
			$num = M('Member')->where($map)->count();
			$info['rank_limit']=array('id'=>$info['rank_limit'],'name'=>idtoname($info['rank_limit'],'MemberRank','name')."(".$num.")");
		}
        if($info){
            $return['status'] = 1;
            $return['data'] = $info;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    public function member_carelist($type=0){
        $return=array('status'=>3,'msg'=>'');
        $map=array();
		if($type==3)$map['type'] =3;
        $info=M('MemberCare')->where($map)->order('id desc')->select();
        if($info){
            foreach ($info as $key => $value) {
                if($value['type']==1){
                    $info[$key]['type']="节日";
                }elseif($value['type']==2){
                    $info[$key]['type']="生日";
                }
                if($value['status']==0){
                    $info[$key]['statuss']="未发送";
                }elseif($value['status']==1){
                    $info[$key]['statuss']="已发送";
                }
				$info[$key]['send_time'] = date("Y-m-d H:i",$value['send_time']);
            }
            $return['status'] = 1;
            $return['data'] = $info;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    public function get_careinfo(){
        $return=array('status'=>3,'msg'=>'');
        $info=array();
        //节日
		
        $zong = M('Member')->count();
        $member = M('Member')->where('is_tmp = 0')->count();
        $fans = M('Member')->where('is_tmp = 1')->count();
		
        $info['num']=array('0'=>array('id'=>0,'name'=>'全部用户('.$zong.')'),'1'=>array('id'=>1,'name'=>'会员('.$member.')'),'2'=>array('id'=>2,'name'=>'粉丝('.$fans.')'));
		$info['send_type']=array('0'=>array('id'=>1,'name'=>'赠送积分'),'1'=>array('id'=>2,'name'=>'赠送卡券'));
		
        $info['info']=$this->getGroup(1);
		// $info['FIXED_SIGNATURE'] = C('FIXED_SIGNATURE');
		$map = array();
		$map['name'] = 'FIXED_SIGNATURE';
		$info['FIXED_SIGNATURE'] = M('Config')->where($map)->getField('value');
		
		//短信查询
        $smsclass = new \Common\Vendor\Sms('Hanchaosms');
        $ret =$smsclass->balance();//条数
		$info['sms_num'] = $ret['data'];
		
        if($info){
            $return['status'] = 1;
            $return['data'] = $info;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    public function add_membercare($postdata){
        $return=array('status'=>3,'msg'=>'');
        if(empty($postdata['type'])){
            $this->error('请选择类型');exit;
        }
		//短信营销
		if($postdata['type'] == 3){
			$data['festival_name'] = $postdata['festival_name'];
            $data['type'] = $postdata['type'];
            $data['rank_limit'] = $postdata['rank_limit'];
            $data['content'] = $postdata['content'];
			$data['wx_message'] = 0;
			$data['sms_message'] = 1;
            $res = M('MemberCare')->add($data);
			if($res){
				if($postdata['is_send'] == 1)$return['data'] = $res;
                $return['status'] = 1;
                $return['msg'] = "添加成功";
            }else{
                $return=array('status'=>2,'msg'=>'添加失败');
            }
		}
        // 节日
        if($postdata['type'] == 1){
            if(!$postdata['send_type'] || !$postdata['festival_name']){
                // $this->error('请完整填写');exit;
                $return['msg']="请完整填写";
                return $return;
            }
            if($postdata['send_type'] == 1){
                if(!floatval($postdata['points'])){$return['msg']="请填写正确的积分";return $return;}
                unset($postdata['coupons_id']);                   
            }
            if($postdata['send_type'] == 2){
                if(!$postdata['coupons_id']){$this->error('请选择卡券');exit;}
                unset($postdata['points']);
                
                if($postdata['send_member'] == 1){
                    $m_where['wxid']=array('eq','');
                }elseif($postdata['send_member'] == 2){
                    $m_where['wxid']=array('neq','');
                }
                if($postdata['rank_limit'] != 0){
                    $m_where['rank_id']=array('eq',$postdata['rank_limit']);
                }
                $m_count = M('Member')->where($m_where)->count();
                $quantity = idtoname($postdata['coupons_id'],'Coupons','quantity','id',false);
                if($quantity<$m_count){
                    $this->error('卡券数量不足');exit;
                }
            }
            $data['festival_name'] = $postdata['festival_name'];
            $data['festival_time'] = strtotime($postdata['festival_time']);
            $data['type'] = $postdata['type'];
            $data['rank_limit'] = $postdata['rank_limit'];
            $data['send_member'] = $postdata['send_member'];
            $data['send_type'] = $postdata['send_type'];
            if(!$postdata['content']){$postdata['content']='有任何疑问，请致电客服。';}$data['content'] = $postdata['content'];
            if($postdata['points'])$data['points'] = $postdata['points'];
            if($postdata['coupons_id'])$data['coupons_id'] = $postdata['coupons_id'];
			if($postdata['wx_message'] == 1)$data['wx_message'] = 1;
			if($postdata['sms_message'] == 1)$data['sms_message'] = 1;
            // $data['send_time'] = strtotime($postdata['send_time']);
            $res = M('MemberCare')->add($data);
            if($res){
				if($postdata['is_send'] == 1)$return['data'] = $res;
                $return['status'] = 1;
                $return['msg'] = "添加成功";
            }else{
                $return=array('status'=>2,'msg'=>'添加失败');
            }
        }
        // 生日
        if($postdata['type'] == 2){
            if(!$postdata['send_birthday'] || !$postdata['send_type'] || !$postdata['festival_name']){
                $return['msg']="请完整填写";
                return $return;
            }
            if($postdata['send_type'] == 1){
                if(!floatval($postdata['points'])){$return['msg']="请填写正确的积分";return $return;}
                unset($postdata['coupons_id']);
            }
            if($postdata['send_type'] == 2){
                if(!$postdata['coupons_id']){$this->error('请选择卡券');exit;}
                unset($postdata['points']);
                if($postdata['send_birthday']>10){
                    $m_where['birthday'] = array('like','%-'.$postdata['send_birthday'].'-%');
                }else{
                    $m_where['birthday'] = array('like','%-0'.$postdata['send_birthday'].'-%');
                }
                $m_count = M('Member')->where($m_where)->count();
                $quantity = idtoname($postdata['coupons_id'],'Coupons','quantity','id',false);
                if($quantity<$m_count){
                    $this->error('卡券数量不足');exit;
                }
            }
            $data['festival_name'] = $postdata['festival_name'];
            $data['type'] = $postdata['type'];
            $data['send_birthday'] = $postdata['send_birthday'];
            $data['send_type'] = $postdata['send_type'];
            if($postdata['points'])$data['points'] = $postdata['points'];
            if($postdata['coupons_id'])$data['coupons_id'] = $postdata['coupons_id'];
            if(!$postdata['content']){$postdata['content']='有任何疑问，请致电客服。';}$data['content'] = $postdata['content'];
			if($postdata['wx_message'])$data['wx_message'] = 1;
			if($postdata['sms_message'])$data['sms_message'] = 1;
            $res = M('MemberCare')->add($data);
            if($res){
				if($postdata['is_send'] == 1)$return['data'] = $res;
                $return['status'] = 1;
                $return['msg'] = "添加成功";
            }else{
                $return=array('status'=>2,'msg'=>'添加失败');
            }
        }
        return $return;
    }
    public function check_care($id){
        $return=array('status'=>3,'msg'=>'');
        $map = array();

        $info['num']['zong'] = M('Member')->count();
        $info['num']['member'] = M('Member')->where('is_tmp = 0')->count();
        $info['num']['fans'] = M('Member')->where('is_tmp = 1')->count();
        $info['info']=$this->getGroup(1);

        $map['id'] = $id;
        $info['list'] = M('MemberCare')->where($map)->field(true)->find();
        if($info){
			if($info['list']['send_member'] == 1){
				$info['list']['send_member'] = '会员';
			}elseif($info['list']['send_member'] == 2){
				$info['list']['send_member'] = '粉丝';
			}else{
				$info['list']['send_member'] = '全部用户';
			}
			//等级限制
			if($info['list']['rank_limit'] == 0){
				$info['list']['rank_limit'] = '不限制';
			}else{
				$info['list']['rank_limit'] = idtoname($info['list']['rank_limit'],'MemberRank','name');
			}
			//赠送类型
			if($info['list']['send_type'] == 1){
				$info['list']['send_type'] = $info['list']['points'].'积分';
			}elseif($info['list']['send_type'] == 2){
				$info['list']['send_type'] = idtoname($info['list']['coupons_id'],'Coupons','title');
			}
            if($info['list']['festival_time']){
                $info['list']['festival_time']=time_format($info['list']['festival_time'],'Y-m-d');
            }
            $return['data']=$info;
            $return['status']=1;
        }else{
            $return=array('status'=>2,'msg'=>'暂无数据');
        }
        return $return;
    }
    /* 得到所属组 */
    private function getGroup($type = ''){
        //等级限制
        $groupData = M('MemberRank')->where('status=1')->field(true)->order('sort,id desc')->select();
        if ($type == 1) {
			$map = array();$map['status'] =1;
			$z_count = M('Member')->where($map)->count();
			$newgroupData[0] = array('id'=>0,'name'=>'全部等级('.$z_count.')');
            foreach ($groupData as $k=>$v) {
                $map['rank_id'] = $v['id'];
                $num = M('Member')->where($map)->count();
                // $newgroupData[$v['id']] = $v['name']."(".$num.")";
				$newgroupData[$k+1]=array('id'=>$v['id'],'name'=>$v['name']."(".$num.")");
            }$groupData=$newgroupData;
            
            //生日
            $arr = array('0'=>'01','1'=>'02','2'=>'03','3'=>'04','4'=>'05','5'=>'06','6'=>'07','7'=>'08','8'=>'09','9'=>'10','10'=>'11','11'=>'12');
            foreach($arr as $k=>$v){
                $maps['birthday'] = array('like','%-'.$v.'-%');
                $nums[$v] = M('Member')->where($maps)->count();
                $n = intval($v);
				$birthdays[$k]=array('id'=>$n,'name'=>$v.'月份生日的会员('.$nums[$v].')');
                // $birthdays[$n] = $v.'月份生日的会员('.$nums[$v].')';
            }
            $c_map['coupons_type'] = 1;
            $c_map['status'] = 0;
            $c_map['quantity'] = array('GT',0);
            //卡券
            $coupons = M('Coupons')->where($c_map)->field(true)->select();
            foreach ($coupons as $v) {
                // $newcoupons[$v['id']] = $v['title']."(".$v['quantity'].")";
				$newcoupons[$k]=array('id'=>$v['id'],'name'=>$v['title']."(".$v['quantity'].")");
            }$coupons=$newcoupons;
        }
        $return['groupData']=$groupData;
        $return['birthdays']=$birthdays;
        $return['coupons']=$coupons;
        return $return;
    }
	public function get_member($mobile){
		$map = array();
		$map['mobile'] = $mobile;
		$info = M('Member')->where($map)->find();
		if($info){
			$return['data']=$info;
            $return['status']=1;
		}else{
			$return=array('status'=>3,'msg'=>'没有该手机的会员');
		}
		return $return;
	}
	public function get_memberpack($postdata){
		$map = array();
		$map['id'] = $postdata['uid'];
		$info = M('Member')->where($map)->find();
		if($info){
			$sort=idtoname($info['rank_id'],'MemberRank','sort','id',false);
			$info['rank_img'] = '../../image/v'.$sort.'.png';
			if($postdata['package_id']){
				$map = array();
				$map['id'] = $postdata['package_id'];
				$package_info = D('Goods')->get_package($postdata['package_id']);
				$info['package_info'] = $package_info['data'];
			}
			$return['data']=$info;
            $return['status']=1;
		}else{
			$return=array('status'=>3,'msg'=>'没有该手机的会员');
		}
		return $return;
	}
	public function sub_recharge($postdata){
		$return=array('status'=>3,'msg'=>'');
		if(!$postdata['uid']){
			$return['msg']='参数错误';
			return $return;
		}
		if($postdata['money']){
			account_change($postdata['uid'],$postdata['money'],1,12,'店铺充值');
			account_change($postdata['uid'],$postdata['money'],7,12,'累计充值');
			
			$order_data['order_sn'] = createOrderNo();
			$order_data['type'] = 2;
			$order_data['uid'] = $postdata['uid'];
			$order_data['shop_id'] = $shop_id;
			$order_data['shipping_status'] = 1;
			$order_data['payment_id'] = $postdata['payment_id'];
			$order_data['pay_status'] = 3;
			$order_data['pay_time'] = NOW_TIME;
			$order_data['goods_amount'] = 0;
			$order_data['goods_allmoney'] = 0;
			$order_data['order_amount'] = $postdata['money'];
			$order_data['order_money'] = 0;
			$order_data['pay_amount'] = $postdata['money'];
			$order_data['addtime'] = NOW_TIME;
			$order_data['confirm_time'] = NOW_TIME;
			$order_data['status'] = 3;
			M('OrderInfo')->add($order_data);
			shop_give_double($postdata['uid'],$postdata['money']);
		}else{
			$return['msg'] = '金额错误';
			return $return;
		}
		$return['status'] = 1;
		$return['msg'] = '充值成功';
		return $return;
	}
	public function message_list($paramdata){
        $map=array();
		if($paramdata['current_id'] == 1){
			$pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
			$list=page_list('WxMsg',$map,'sendtime desc','id,openid,content,sendtime,addtime,uid,is_success',$pagesize,'',1);
		}else{
			$map['send_type'] = 0;
			$pagesize=empty($pagesize)?C('WEB_ARTICLE_PAGE'):$pagesize;
			$list=page_list('SmsLogs',$map,'sendtime desc','id,to,content,sendtime,addtime,uid,is_success',$pagesize,'',1);
		}
		if($list['list']){
			foreach($list['list'] as $k=>$v){
				$list['list'][$k]['sendtime']=time_format($v['addtime'],'Y-m-d H:m:s');
				$list['list'][$k]['real_name']=idtoname($v['uid'],'Member','real_name','id',false);
			}
		}
		$list['list']['Fixed_signature'] = C('FIXED_SIGNATURE');
        return $list['list'];

    }
    // 获取导购员
    public function getdaogoulist($shop_id){
        $where=$map=array();
        $map['shop_id']=$shop_id;
        $map['status']=1;
        $ids = M('ShopsAccount')->where($map)->getField('id',true);
        $where['role_id']=2;
        if($ids){
            $where['account_id']=array('in',$ids);
            $daogou_arr = M('ShopsRoleAccount')->where($where)->getField('account_id',true);
        }else{
            $daogou_arr='';
        }
        $newdaogou_arr=array();
        if($daogou_arr){
            foreach ($daogou_arr as $v) {
                $acc_name = idtoname($v,'ShopsAccount','name','id',false);
                if($acc_name){
                    $newdaogou_arr[$v] = $acc_name;
                }
            }
			$return['status']=1;
			$return['data']=$newdaogou_arr;
        }else{
			$return['status']=3;
			$return['msg']='导购员为空';
        }
        return $return;
    }
	public function member_bind($post){
		$return = array();$return['status'] = 3;$return['msg'] = '';
		if(empty($post['mc_id'])){
			$return['msg'] = '参数错误';
			return $return;
		}
		$uid = 0;
		if($post['mobile']){
			$map = array();$map['mobile'] = $post['mobile'];$map['is_tmp'] = 0;
			$uid = M('Member')->where($map)->getField('id');
		}
		if($uid <= 0){
			$return['msg'] = '该手机号不是会员';
			return $return;
		}else{
			$mc_save = array();
			$mc_save['id'] = $post['mc_id'];
			$mc_save['uid'] = $uid;
			$mc_save['membertype'] = 1;
			$ret = M('MemberCustomer')->save($mc_save);
			if($ret){
				$return['status'] = 1;
				$return['msg'] = '绑定成功';
			}else{
				$return['msg'] = '绑定失败';
			}
			return $return;
		}
	}
	public function member_bind_add($postdata){
        $return=array('status'=>3,'msg'=>'');
        $model = M('Member');
        $con_where = array();
        $con_where['name'] = 'MEMBERATTRIBUTE';
        $con_have = M("Config")->where($con_where)->field("id,name,value")->find();
        if($con_have){
            $con_data = unserialize($con_have['value']);
            if($con_data['real_name']['is_shop']){
                if($postdata['real_name'] == ''){
                    $return['msg']='真实姓名不能为空';
                    return $return;
                }
            }
            if($con_data['birthday']['is_shop']){
                if($postdata['birthday'] == ''){
                    $return['msg']='生日不能为空';
                    return $return;
                }
            }
        }
        if(empty($postdata['mobile'])){
            $return['msg']='手机号不能为空';
            return $return;
        }
		$map=array();
        $map['mobile']=$postdata['mobile'];
		$result = $model->where($map)->count();
		if (!empty($result)){
			$return['msg']='手机号'.$postdata['mobile'].'的会员已经存在';
			return $return;
		}
		$postdata['parent_id'] = 0;
		if(!empty($postdata['parent_mobile'])){
			if($postdata['parent_mobile'] == $postdata['mobile']){
				$return['msg']='推荐人手机号'.$postdata['parent_mobile'].'不能是自己手机号';
				return $return;
			}else{
				$map = array();$map['mobile'] = $postdata['parent_mobile'];
				$parent_id = $model->where($map)->getField('id');
				if (!$parent_id){
					$return['msg']='推荐人手机号'.$postdata['parent_mobile'].'不存在';
					return $return;
				}else{
					$postdata['parent_id'] = $parent_id;
				}
			}
		}
		$postdata['salt'] = randStr();
		$postdata['reg_time'] = $postdata['card_time'] = NOW_TIME;
		$postdata['reg_ip'] = get_client_ip(1);
		$postdata['rank_id'] = 1;
		$postdata['points'] = 0;
		$postdata['recharge_points'] = 0;
		$postdata['is_tmp'] = 0;
		$postdata['card_status'] = 1;
		$postdata['status'] = 1;
		$postdata['guide'] = $postdata['guide'];
		unset($postdata['region']);
		C('TOKEN_ON',false);
		if($postdata['name']){
			$arr_name = $postdata['name'];
		}unset($postdata['name']);
		$model->create($postdata);
		$id = $model->add();
		if($id){
			$arr_name = json_decode(htmlspecialchars_decode($arr_name),true);
			if($arr_name){
				$where = $c_map = array();
				$c_map['name'] = 'ATTRIBUTE';
				$c_have = M("Config")->where($c_map)->getField("value");
				$c_list = json_decode($c_have,true);
				foreach ($arr_name as $key => $value) {
					if($value){
						if($c_list[$key]['type'] == 'date'){
							$value = strtotime($value);
						}
						if($c_list[$key]['type'] == 'checkbox'){
							$value= preg_replace("/(\n)|(\s)|(\t)|(\')|(')|(，)/" ,',' ,$value);
						}
						$add_data = array();
						$add_data['uid'] = $id;
						$add_data['aid'] = $key;
						$add_data['content'] = $value;
						M("MemberAttribute")->add($add_data);
						if($c_list[$key]['type'] == 'date'){
							$value = date('Y-m-d',$value);
						}
					}
				}unset($key,$value);
			}
			
			//绑定会员和顾客照片
			if(($postdata['wdzpicurl'] || $postdata['mc_id']) && $id){
				$data = array();$mc_id = 0;
				$data['uid'] = $id;
				if($postdata['mc_id']){
					$mc_id = $postdata['mc_id'];
				}
				$data['mc_id'] = $mc_id;
				$data['shop_id'] = $postdata['current_id'];
				$data['wdzpicurl'] = $postdata['wdzpicurl'];
				$this->save_member_customer($data);
			}
			
			$return['status'] = 1;
			$return['msg']='添加成功';
		}else{
			$return['msg']='添加失败';
		}
        return $return;
    }
    
    /**
     * 管理员或店铺登录验证
     * @param array $data 登录数据
     * @return array
     */
    public function dologin($data) {//登录 
        $return = array('status'=>3,'msg'=>'参数错误');
        $username = $data['username'];
        $password = $data['password'];
        if(empty($username) || empty($password))return $return;
        
         //总管理员登录验证
        $map_or=$map=array();
        $map_or['mobile']=$username;
        $map_or['username']= $username;
        $map_or['_logic']='OR';
        $map['_complex'] = $map_or;
        $map["status"] =1;
        $is_admin = M('Admin')->where($map)->count();        
        $map = $memberinfo = $result = $siteresult = array();        
        if(empty($is_admin)){
            //门店管理员及门店收银员登录
            $data['login_name'] = $username;
            //去分站获取验证管理员数据
            $map = $shopinfo = array();
            $map_or=array();
            $map_or['mobile']=$username;
            $map_or['login_name']= $username;
            $map_or['_logic']='OR';
            $map['_complex'] = $map_or;
            $map['status'] = 1;
            $shopinfo = M('ShopsAccount')->where($map)->field(true)->find();
            $postpassword = creatpwd($data['password'],'HANCHAO'.$shopinfo['salt']);
            if($postpassword==$shopinfo['password']){
                $return['msg'] = "请求成功!";
                $return['status'] = 1;
                $return['data']['domain'] = $shopinfo['id'];
                $return['data']['appid'] = $shopinfo['id'];
                //返回门店权限和认证信息
                $return['data']['auth_model'] = '';
                $return['data']['mid'] = $shopinfo['id'];
                $return['data']['is_certified'] =1;//是否认证
                $return['data']['parent_id'] = 0;//推荐人
                $return['data']['root_id'] ='';
                $return['data']['root_token'] = '';
                $return['data']['shop_num'] = 1;
                $return['data']['shop_max_num'] = 999999999;
                $return['data']['root_name'] =$username;//记录主管理账号，便于员工登录
                $return['data']['c_identity'] = 1;
                
                
                $return['data']['sid'] = $shopinfo['id'];
                $return['data']['token'] = $shopinfo['password'];
                $return['data']['current_shop_id'] = $shopinfo['shop_id'];
                $map=array('id'=>$shopinfo['shop_id']);
                $return['data']['open_tone'] = M('Shops')->where($map)->getField('is_open_tone');
                $return['data']['is_root'] = 0;
                $savedata = array();
                $savedata['login_count'] = array('exp', 'login_count+1');
                $savedata['last_login'] = NOW_TIME;
                $savedata['last_ip'] = get_client_ip(1);
                M('ShopsAccount')->where(array('id'=>$shopinfo['id']))->save($savedata);
                $this->set_push_param($data['registration_id'],$data['registration_type'],$return['data']['sid'],$data['device']);//推送
            }else{  
                $return['msg']='密码错误!';
                if(empty($shopinfo)){$return['msg']='账号不存在!';}
                return $return;
            }            
        }else{
            //分站总管理员登录
            $map_or=array();
            $map_or['mobile']=$username;
            $map_or['username']= $username;
            $map_or['_logic']='OR';
            $map['_complex'] = $map_or;
            $map["status"] = array('gt', 0);
            $memberinfo = M('Admin')->where($map)->field(true)->find();
            if($memberinfo){
                $postpassword = creatpwd($password,$memberinfo['salt']);
                if($postpassword==$memberinfo['password']){
                    $userdata=$map=array();
                    $map['id']=$memberinfo['id'];
                    $userdata['login_count'] = array('exp', 'login_count+1');
                    $userdata['last_login_time'] = NOW_TIME;
                    $userdata['last_login_ip'] = get_client_ip(1);
                    M('Admin')->save($userdata);
                    
                    $return['msg'] = "请求成功!";
                    $return['data']['domain'] = $memberinfo['id'];
                    $return['data']['appid'] = $memberinfo['id'];
                    //返回门店权限和认证信息
                    $return['data']['auth_model'] = '';
                    $return['data']['mid'] = $memberinfo['id'];
                    $return['data']['is_certified'] =1;//是否认证
                    $return['data']['parent_id'] = 0;//推荐人
                    //先返回超级管理员信息兼容注册和登录
                    $return['data']['is_root'] = 1;
                    $return['data']['root_id'] = $memberinfo['id'];
                    $return['data']['root_token'] = $postpassword;
                    
                    
                    $map = array();
                    $map['status'] = 1;
                    $return['data']['shop_num'] = M('Shops')->where($map)->count();
                    if($return['data']['shop_num']<1){//门店数量为0，跳转到新增门店
                        $return['msg']='请先去创建门店!';
                        $return['jumpurl']='creat_shop';
                        return $return;
                    }
                    $current_shop_id=M('Shops')->where($map)->order('id asc')->getField('id');
                    $map = array();
                    $map['status'] = 1;
                    $map['shop_id'] = $current_shop_id;
                    $sitesaccount = M('ShopsAccount')->where($map)->order('id asc')->field('id,password')->find();
                    $return['data']['current_shop_id'] = $current_shop_id;
                    $return['data']['sid'] = $sitesaccount['id'];
                    $return['data']['token'] = $sitesaccount['password'];
                    //查询门店创建门店数量上限
                    $return['data']['shop_max_num'] = 999999999;
                    $return['data']['root_name'] =$username;//记录主管理账号，便于员工登录
                    $return['data']['c_identity'] = 1;
                    $return['status'] = 1;
                    return $return;                    
                }else{
                    $return['msg']='密码错误!';
                }
            }else{
                $return['msg']='用户不存在,或者被删除!';
            }
        }
        return $return;
    }
}
?>