<?php
/**
 * 店铺模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@qq.com>
 * @Author chang <13331202938@126.com>
 */
namespace Common\Model;
use Think\Model;
class ShopsModel extends Model{
	/**
     * 获取店铺公共信息
     * @param int $shop_id
     * @return array
     */
	public function get_common($param){
		$return=array();
		$return['status']=1;
		$shopid=$param['id'];
		$return=$this->get_shop($shopid,$param['mid']);
		
		if(!$return['status'])return $return;
		$return['shop_id']=$shopid;
		//店铺的分类
		$cates=$this->get_shop_category($shopid,$param);
		$return['cates']=$cates;
		$cates_name=idtoname($param['shop_cid'],'ShopsCategory');
		$return['cates_name']=$cates_name?$cates_name:'分类';
		//店铺设置
		$return['data']['setting']=$this->shops_setting($shopid,$param['request_type']);
		if(ACTION_NAME=='index'&&$return['data']['setting']['mode']==1)$return['data']['setting']['left']=1;
		$return['act_name']=ACTION_NAME;
		//热销排行
		$hot_list=$this->get_shop_goods($shopid,2);
		$return['hot_list']=$hot_list['list'];
		//导航
		$map=array();
		$map['shop_id']=$shopid;
		$map['status']=1;
		$map['pid']=0;
		$return['nav']=M('Nav')->where($map)->field('id,name,url')->select();
		return $return;
	}
	/**
     * 店铺商品列表
     * @param int $shop_id
     * @return array
     */
	public function get_lists($shopid,$param=array()){
		$return=array();
		$return['request']=$param;
		//品牌
		$map=array();
		$map['shop_id']=$shopid;
		$map['status']=1;
		$map['is_delete']=0;
		$map['publish_wait']=1;
		$map['brand_id']=array('gt',0);
		$brandids=M('Goods')->where($map)->field('brand_id')->group('brand_id')->select();
		$all=array('brand_id'=>0,'name'=>'全部');
		if(!$param['brand_id'])$all['check']=1;
		array_unshift($brandids,$all);
		foreach($brandids as $k=>$v){
			if($param['brand_id']==$v['brand_id']){$brandids[$k]['check']=1;}
			$param_b=array();
			$param_b['id']=$shopid;
			$param_b['brand_id']=$v['brand_id'];
			if($param['shop_cid'])$param_b['shop_cid']=$param['shop_cid'];
			if($param['keyword'])$param_b['keyword']=$param['keyword'];
			if($v['brand_id']>0)$brandids[$k]['name']=idtoname($v['brand_id'],'Brand','brand_name');
			$brandids[$k]['url']=U(MODULE_NAME.'/Shop/lists',$param_b);
		}
		$return['brands']=$brandids;
		//价格
		$pricelists=$this->get_price_map($shopid,$param['price']);
		$return['pricelist']=$pricelists['pricelist'];
		//排序
		$order_list=D('Common/Brand')->get_order_list($param,MODULE_NAME.'/Shop/lists',2);
		$return['order_list']=$order_list['order_list'];
		//标签
		
		$map=array();
		$map['shop_id']=$shopid;
		$map['status'] = 1;
        $map['publish_wait'] = 1;
        $map['is_delete'] = 0;
		$shop_goods_ids=M('Goods')->where($map)->getField('id',true);
		$map_label=array();
		$map_label['goods_id']=array('in',$shop_goods_ids);
		$labels=M('ProjectLabelLink')->where($map_label)->field('label_id')->group('label_id')->select();
		$label_ids=array();
		foreach($labels as $k=>$v){
			$param_l=I('param.');
			$labels[$k]['name']=idtoname($v['label_id'],'ProjectLabel','label_name');
			$param_l['label'.$k]=$v['label_id'];
			$labels[$k]['url']=U(MODULE_NAME.'/Shop/lists',$param_l);
			if($param['label'.$k]==$v['label_id']){
				$labels[$k]['check']=1;
				unset($param_l['label'.$k]);
				$labels[$k]['url']=U(MODULE_NAME.'/Shop/lists',$param_l);
			}
			if($param['label'.$k]>0)$label_ids[]=$param['label'.$k];
		}
		$return['labels']=$labels;
		//筛选
		$search_map=array();
		$search_map['shop_id']=$shopid;
		$keyword=trim($param['keyword']);
		if($keyword)$search_map['goods_name']=array('like','%'.$keyword.'%');
		if($param['brand_id']>0){
			$search_map['brand_id']=$param['brand_id'];
		}
			
		if($param['price_id']>0){
			$price_map=$price[$param['price_id']];
			//if($price_map['max']>0&&$price_map['min']>0)$search_map['shop_price']=array(array('elt',$price_map['max']),array('gt',$price_map['min']));
			if($price_map['max']>0)$search_map['shop_price']=array(array('elt',$price_map['max']),array('gt',$price_map['min']));
			if($price_map['max']==0&&$price_map['min']>0)$search_map['shop_price']=array('gt',$price_map['min']);
		}
		if(!empty($label_ids)){
			$map=array();
			//$map['shop_id']=$shopid;
			$map['label_id']=array('in',$label_ids);
			$goods_ids=M('ProjectLabelLink')->where($map)->getField('goods_id',true);
			$search_map['id']=array('in',$goods_ids);
		}
		if(!empty($pricelists['shop_price']))$search_map['shop_price']=$pricelists['shop_price'];
		$page=$param['page']?$param['page']:1;
		$list=D('Common/Goods')->get_goods_list(0,$search_map,'',$order_list['order'],$page,8);
		$return['glist']=$list;
		return $return;
	}
    /**
     * 获取店铺信息
     * @param int $id
     * @return array
     */
    public function get_shop($id=0,$mid=0){
        $return=array();
        $return['status']=0;
        if(empty($id)){
            $return['msg']='参数错误！';
            return $return;
        }
        $shop_map['id'] = $id;
        $shop=M('Shops')->where($shop_map)->field(true)->find();
        if(empty($shop)){
            $return['msg']='该店铺不存在！';
            return $return;
        }
        if(empty($shop['status'])){
            $return['msg']='该店铺已闭店！';
            return $return;
        }
        $data=$this->format_shop_item($shop);
        $return['status']=1;
        $return['data']=$data;
        return $return;
    }

    /**
     * 格式化店铺信息
     * @param $data
     * @return array
     */
    public function format_shop_item($data){
		$return=$data;
		$catids=explode(',',$data['cate_ids']);
		$catstr='';
		$catlong='';
        global $GoodsCategory;
		foreach($catids as $k=>$v){
			if($k<3)$catstr.=idtoname($v,'GoodsCategory').' ';
			$catlong.=idtoname($v,'GoodsCategory').' ';
		}
		$return['catlong']=$catlong;
		$return['catids']=$catstr;
        $return['city_format']=idtoname($data['city_id'],'Area');
        $return['district_format']=idtoname($data['district'],'Area');
        $return['town_format']=idtoname($data['town'],'Area');
        $return['community_format']=idtoname($data['community'],'Area');
        $return['updatetime_format']=date_format($data['updatetime'],'Y-m-d');
        //等级续费到期和保证金扣完将店铺打回普通等级
        $return['rank']=($data['end_time']>NOW_TIME)?0:$data['rank'];
        $return['rank_format']=empty($return['rank'])?'普通店铺':idtoname($return['rank'],'ProjectLabel','label_name');
        $return['cat_format']=idtoname($return['catids'],'GoodsCategory');
        // $return['commont_info']=$this->get_shop_commont($data['id']);//店铺的评论信息
        $return['average']=0;foreach($return['commont_info'] as $k=>$v){$return['average']+=$v['score'];}//店铺的平均分
        $return['cates']=$this->get_shop_category($data['id']);//店铺的分类
        $return['goods_count']=$this->get_goods_count($data['id']);//商品个数
        $return['good_rate']=empty($data['dp_count'])?'100%':(round(($data['dp_count_3']/$data['dp_count'])*100,2).'%');
        $return['middle_rate']=empty($data['dp_count'])?'0%':(round(($data['dp_count_2']/$data['dp_count'])*100,2).'%');
		$return['bad_rate']=empty($data['dp_count'])?'0%':(round(($data['dp_count_1']/$data['dp_count'])*100,2).'%');
        return $return;
    }
    public function get_goods_count($shopid=0){
        $return=0;
        $map=array();
        $map['shop_id']=$shopid;
        $map['status']=1;
        $map['is_delete'] = 0;
        $return=M('ShopsGoods')->where($map)->count();
        return $return;
    }

	/**
     * 店铺的分类,缓存
     * @param $shopid
     * @return array
     */
    public function get_shop_category($shopid,$param=array()){
		$key=$shopid.'_ShopsCategory';
		$cates=S($key);
		if($cates){
			$cates=$cates;
		}else{
			$map=array();
			$map['shop_id']=$shopid;
			$map['status']=1;
			$map['pid']=0;
			$shopcate=M('ShopsCategory')->where($map)->field(true)->select();
			foreach($shopcate as $k=>$v){
				$map['pid']=$v['id'];
				$shopcate[$k]['son_cates']=M('ShopsCategory')->where($map)->field(true)->select();
                foreach($shopcate[$k]['son_cates'] as $ke=>$va){
                    $shopcate[$k]['son_cateids'][$ke]=$va['id'];
                }
			}
			S($key,$shopcate);
			$cates=$shopcate;
		}
		if(!empty($param)&&!empty($cates)){
			foreach($cates as $k=>$v){
				$param_c=array();
				$param_c['id']=$shopid;
				$param_c['shop_cid']=$v['id'];
				$cates[$k]['url']=U(MODULE_NAME.'/Shop/lists',$param_c);
				if($param['shop_cid']==$v['id'])$cates[$k]['show']=1;
				foreach($v['son_cates'] as $kk=>$vv){
					if($param['shop_cid']==$vv['id']){$cates[$k]['show']=1;}
					$param_r=array();
					$param_r['id']=$shopid;
					$param_r['shop_cid']=$vv['id'];
					if($param['keyword'])$param_r['keyword']=$param['keyword'];
					$cates[$k]['son_cates'][$kk]['url']=U(MODULE_NAME.'/Shop/lists',$param_r);
				}
			}
			if(!$param['shop_cid'])$cates[0]['show']=1;
		}
		return $cates;
    }
	
	 /**
     * 店铺商品列表
     * @param $shopid
	 * @param $type,1热销，2新品,3筛选
     * @return array
     */
	 public function get_shop_goods($shopid,$page_size=0,$page=1,$type=1){
		$map=array();
		$map['shop_id']=$shopid;
		switch($type){
			case 1:$order='sell_count desc,is_hot desc';break;
			case 2:$order='addtime desc';break;
			default :$order='is_new desc';
		}
        $list=D('Common/Goods')->get_goods_list(0,$map,'',$order,$page,$page_size);
		return $list;
	 }
	 
	/**
     * 店铺外卖订单(已完成订单包括服务订单)
     * @param $postdata
	 * @order_status $type int  订单状态  1：全部订单，2：待付款，3：待发货，4：待收货，5：待评论
	 * @is_num $is_num true  只取数量
	 * @page_num $page_num int  分页数量
	 * @field  string  要查询的订单字段
     * @return mixed
     */
    public function shop_all_order($postdata,$type=1,$is_num=0,$page_num=10,$field=true){
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$map['shop_id']=$shop_id;
		$map['is_delete']=0;
		$map['type'] = 14;
		if($type == 1){
			$map['type'] = array('IN','12,14');
		}
		
		$map['goods_allmoney']=array('gt',0);
		if($type == 'no_pay' || $type == 2){
			$map['pay_status']=1;
			$map['status']=array('lt',3);
		}elseif($type == 'no_ship' || $type == 3){
			$map['pay_status']=3;
			$map['shipping_status']=array('in','1,5');
			$map['status']=array('lt',3);
		}elseif($type == 'yes_ship' || $type == 4){//未完成订单（发货了，未收货）
			$map['pay_status']=3;
			$map['shipping_status']=3;
			$map['status']=array('lt',3);
		}elseif($type == 'no_comment' || $type == 5){
			$map['status']=3;
			$map['is_comment']=0;
		}elseif($type == 'no_group' || $type == 6){
			$map['pay_status']=3;
			$map['shipping_status']=5;
		}elseif($type == 'service_order' || $type == 7){
			$map['pay_status']=3;
			$map['shipping_status']=3;
			$map['status']=array('lt',3);
			$map['type'] = 11;
		}elseif($type == 'yes_order' || $type == 8){//已完成订单
			$map['pay_status']=3;
			$map['shipping_status']=4;
			$map['type'] = array('IN','12,14');
		}elseif($type == 'tui_order' || $type == 9){//退款、退货订单
			$map['pay_status']=3;
			$map['status']=array('in','6,7,8,9');
			$map['type'] = array('IN','11,14');
		}elseif($type == 'service_order_hx' || $type == 10){//已完成订单
			$map['pay_status']=3;
			$map['shipping_status']=4;
			$map['type'] = 11;
			// $map['type'] = array('IN','11,12');
		}
		
		if($is_num){
			$all_order['num']=M('OrderInfo')->where($map)->count();
			return $all_order;
		}
		
		//搜索条件
		if(!empty($postdata['time_form']))$map['addtime'][]=array('egt',strtotime($postdata['time_form']));
		if(!empty($postdata['time_to']))$map['addtime'][]=array('elt',strtotime($postdata['time_to']));
		if(!empty($postdata['order_sn']))$map['order_sn']=$postdata['order_sn'];
		if(!empty($postdata['buyer_name'])){
			$search_map['username']=array('like','%'.$postdata['buyer_name'].'%');
			$search_user=M('Member')->where($search_map)->getField('id',true);
			if($search_user){$map['uid']=array('in',$search_user);}else{$map['uid']='';}
		}
		$all_order=page_list('OrderInfo',$map,'id desc',$field,$page_num,'',1);
		//格式化订单数据
		if($all_order){
			foreach($all_order['list'] as $k=>$v){
				$goods_info['all_num']=$goods_info['num']=0;
				$all_order['list'][$k]=$v;
				$all_order['list'][$k]['goods_amount']=$v['goods_amount'];
				$goods_map['order_id']=$v['id'];
				$goods_info['list']=M('OrderGoods')->where($goods_map)->getField('id,goods_id,goods_name,goods_attr,goods_price,goods_number',true);
				$goods_info['num']=M('OrderGoods')->where($goods_map)->count();
				foreach($goods_info['list'] as $ke=>$vo){
                    $goodsinfo=idtoname($vo['goods_id'],'Goods','img,goods_weight');
					$goods_info['list'][$ke]=$vo;
					$goods_info['all_num']+=$vo['goods_number'];
					$goods_info['list'][$ke]['goods_price']=$vo['goods_price'];
					$goods_info['list'][$ke]['img']=$goodsinfo['img'];
                    $goods_info['list'][$ke]['goods_attr_format'] = unserialize($vo['goods_attr']);
                    $goods_info['list'][$ke]['goods_weight'] = $goodsinfo['goods_weight'];
				}
				$all_order['list'][$k]['goods']=$goods_info;
			}
		}
        return $all_order;
    }
	/**
     * 店铺外卖订单
     * @param $postdata
	 * @order_status $type int  订单状态  1：全部订单，2：待付款，3：待发货，4：待收货，5：待评论
	 * @is_num $is_num true  只取数量
	 * @page_num $page_num int  分页数量
	 * @field  string  要查询的订单字段
     * @return mixed
     */
    public function shop_cashier_order($postdata,$type=1,$is_num=0,$page_num=10,$field=true){
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$map['shop_id']=$shop_id;
		$map['is_delete']=0;
		if($type == 'no_pay' || $type == 2){
			$map['pay_status']=1;
			$map['status']=array('lt',3);
		}elseif($type == 'no_ship' || $type == 3){
			$map['pay_status']=3;
			$map['shipping_status']=1;
			$map['status']=array('lt',3);
		}elseif($type == 'yes_ship' || $type == 4){
			$map['pay_status']=3;
			$map['shipping_status']=3;
			$map['status']=array('lt',3);
		}elseif($type == 'no_comment' || $type == 5){
			$map['status']=3;
			$map['is_comment']=0;
		}elseif($type == 'no_group' || $type == 6){
			$map['pay_status']=3;
			$map['shipping_status']=5;
		}
		$map['type'] = 13;
		if($is_num){
			$all_order['num']=M('OrderInfo')->where($map)->count();
			return $all_order;
		}
		
		//搜索条件
		if(!empty($postdata['time_form']))$map['addtime'][]=array('egt',strtotime($postdata['time_form']));
		if(!empty($postdata['time_to']))$map['addtime'][]=array('elt',strtotime($postdata['time_to']));
		if(!empty($postdata['order_sn']))$map['order_sn']=$postdata['order_sn'];
		if(!empty($postdata['buyer_name'])){
			$search_map['mobile|username']=array('like','%'.$postdata['buyer_name'].'%');
			$search_user=M('Member')->where($search_map)->getField('id',true);
			if($search_user)$map['uid']=array('in',$search_user);
		}
		$all_order=page_list('OrderInfo',$map,'id desc',$field,$page_num,'',1);
		//格式化订单数据
		if($all_order){
			foreach($all_order['list'] as $k=>$v){
				$goods_info['all_num']=$goods_info['num']=0;
				$all_order['list'][$k]=$v;
				$all_order['list'][$k]['goods_amount']=$v['goods_amount'];
				$goods_map['order_id']=$v['id'];
				$goods_info['list']=M('OrderGoods')->where($goods_map)->getField('id,goods_id,goods_name,goods_attr,goods_price,goods_number',true);
				$goods_info['num']=M('OrderGoods')->where($goods_map)->count();
				foreach($goods_info['list'] as $ke=>$vo){
                    $goodsinfo=idtoname($vo['goods_id'],'Goods','img,goods_weight');
					$goods_info['list'][$ke]=$vo;
					$goods_info['all_num']+=$vo['goods_number'];
					$goods_info['list'][$ke]['goods_price']=$vo['goods_price'];
					$goods_info['list'][$ke]['img']=$goodsinfo['img'];
                    $goods_info['list'][$ke]['goods_attr_format'] = unserialize($vo['goods_attr']);
                    $goods_info['list'][$ke]['goods_weight'] = $goodsinfo['goods_weight'];
				}
				$all_order['list'][$k]['goods']=$goods_info;
			}
		}
        return $all_order;
    }
	/**
     * 店铺服务订单
     * @param $postdata
	 * @order_status $type int  订单状态  1：全部订单，2：待付款，3：待发货，4：待收货，5：待评论
	 * @is_num $is_num true  只取数量
	 * @page_num $page_num int  分页数量
	 * @field  string  要查询的订单字段
     * @return mixed
     */
    public function shop_service_order($postdata,$type=1,$is_num=0,$page_num=10,$field=true){
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$map['shop_id']=$shop_id;
		$map['is_delete']=0;
		// $map['type']=array('in','1,6');
		if($type == 'no_pay' || $type == 2){
			$map['pay_status']=1;
			$map['status']=array('lt',3);
		}elseif($type == 'no_ship' || $type == 3){
			$map['pay_status']=3;
			$map['shipping_status']=1;
			$map['status']=array('lt',3);
		}elseif($type == 'yes_ship' || $type == 4){
			$map['pay_status']=3;
			$map['shipping_status']=3;
			$map['status']=array('lt',3);
		}elseif($type == 'no_comment' || $type == 5){
			$map['status']=3;
			$map['is_comment']=0;
		}elseif($type == 'no_group' || $type == 6){
			$map['pay_status']=3;
			$map['shipping_status']=5;
		}
		$map['type'] = 11;
		if($is_num){
			$all_order['num']=M('OrderInfo')->where($map)->count();
			return $all_order;
		}
		//搜索条件
		if(!empty($postdata['time_form']))$map['addtime'][]=array('egt',strtotime($postdata['time_form']));
		if(!empty($postdata['time_to']))$map['addtime'][]=array('elt',strtotime($postdata['time_to']));
		if(!empty($postdata['order_sn']))$map['order_sn']=$postdata['order_sn'];
		if(!empty($postdata['buyer_name'])){
			$search_map['username']=array('like','%'.$postdata['buyer_name'].'%');
			$search_user=M('Member')->where($search_map)->getField('id',true);
			if($search_user)$map['uid']=array('in',$search_user);
		}
		$all_order=page_list('OrderInfo',$map,'id desc',$field,$page_num,'',1);
		
		//格式化订单数据
		if($all_order){
			foreach($all_order['list'] as $k=>$v){
				$goods_info['all_num']=$goods_info['num']=0;
				$all_order['list'][$k]=$v;
				$all_order['list'][$k]['goods_amount']=$v['goods_amount'];
				$goods_map['order_id']=$v['id'];
				$goods_info['list']=M('OrderGoods')->where($goods_map)->getField('id,goods_id,goods_name,goods_attr,goods_price,goods_number',true);
				$goods_info['num']=M('OrderGoods')->where($goods_map)->count();
				foreach($goods_info['list'] as $ke=>$vo){
                    $goodsinfo=idtoname($vo['goods_id'],'Goods','img,goods_weight');
					$goods_info['list'][$ke]=$vo;
					$goods_info['all_num']+=$vo['goods_number'];
					$goods_info['list'][$ke]['goods_price']=$vo['goods_price'];
					$goods_info['list'][$ke]['img']=$goodsinfo['img'];
                    $goods_info['list'][$ke]['goods_attr_format'] = unserialize($vo['goods_attr']);
                    $goods_info['list'][$ke]['goods_weight'] = $goodsinfo['goods_weight'];
				}
				$all_order['list'][$k]['goods']=$goods_info;
			}
		}
        return $all_order;
    }
	
	public function collect_shop($id){
        $return = $where = $map_co = array();
        $member_info = checkuserauthtype();
        if (empty($member_info)) {
            $return['msg'] = '请登录';
            $return['status'] = 0;
            $return['url'] = getjumpurl('JUMP_1');
        } else {
            $collect = M('ShopsCollect');
            $map_co['shop_id'] = $id;
            $map_co['uid'] = $member_info['id'];
            $favinfo = $collect->where($map_co)->count();
            if ($favinfo) {
                $favinfo = $collect->where($map_co)->delete();
                $return['status'] = 2;
                $return['msg'] = '取消收藏成功!';
            } else {
                $postdata['shop_id'] = $id;
                $postdata['uid'] = $member_info['id'];
                $postdata['addtime'] = NOW_TIME;
                $postdata['status'] = 1;
                $result = $collect->add($postdata);
                if ($result) {
                    $return['status'] = 1;
                    $return['msg'] = '收藏成功';
                } else {
                    $return['status'] = 3;
                    $return['msg'] = '收藏失败!';
                }
            }
        }
        return $return;
	}
	/**
	  *价格分级
	  */
	private function get_grade_price($shop_id){
		if(empty($shop_id)) return false;
		$map=array();
		$map['g.status']=1;
		$map['g.publish_wait']=1;
		$map['g.is_delete']=0;
		$map['g.shop_id']=$shop_id;
		$row = M('Goods')->alias('g')->field('min(g.shop_price) AS min, max(g.shop_price) AS max')->where($map)->find();
		// 取得价格分级最小单位级数，比如，千元商品最小以100为级数
		$price_grade = 0.0001;
		for($i=-2; $i<= log10($row['max']); $i++){$price_grade *= 10;}      //100
		//跨度
		$dx = ceil(($row['max'] - $row['min']) / 4 / $price_grade) * $price_grade;
		if($dx == 0){$dx = $price_grade;}   
		for($i = 1; $row['min'] > $dx * $i; $i ++);
		for($j = 1; $row['min'] > $dx * ($i-1) + $price_grade * $j; $j++);
		$row['min'] = $dx * ($i-1) + $price_grade * ($j - 1);
		for(; $row['max'] >= $dx * $i; $i ++);
		$row['max'] = $dx * ($i) + $price_grade * ($j - 1);
		$price_grades = M('Goods')->alias('g')->field('(FLOOR((g.shop_price - '.$row['min'].') / '.$dx.')) AS sn, COUNT(*) AS goods_num')->where($map)->group('sn')->select();
		foreach ($price_grades as $key=>$val){
			$temp_key = $key + 1;
			$price_grades[$temp_key]['goods_num'] = $val['goods_num'];
			$price_grades[$temp_key]['start'] = $row['min'] + round($dx * $val['sn']);
			$price_grades[$temp_key]['end'] = $row['min'] + round($dx * ($val['sn'] + 1));
			$price_grades[$temp_key]['price_range'] = $price_grades[$temp_key]['start'] . '&nbsp;-&nbsp;' . $price_grades[$temp_key]['end'];
			$price_grades[$temp_key]['formated_start'] = number_format($price_grades[$temp_key]['start'] ,2, '.', '');
			$price_grades[$temp_key]['formated_end'] = number_format($price_grades[$temp_key]['end'],2, '.', '');
			$price_grades[$temp_key]['url'] = U('Cate/Index', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_grades[$temp_key]['start'], 'price_max'=> $price_grades[$temp_key]['end'], 'filter_attr'=>$filter_attr_str));
			/* 判断价格区间是否被选中 */
			if (isset($_REQUEST['price_min']) && $price_grades[$temp_key]['start'] == $price_min && $price_grades[$temp_key]['end'] == $price_max){
				$price_grades[$temp_key]['selected'] = 1;
			}else{
				$price_grades[$temp_key]['selected'] = 0;
			}
			$pricelist[$temp_key] = $price_grades[$temp_key]['price_range'];
		}
		ksort($pricelist);
		return $pricelist;
	}
	/**
	  * 价格分级条件
	  */
	private function get_price_map($shop_id,$price){
		if(empty($shop_id)) return $return;
		$pricelist = $this->get_grade_price($shop_id);
		$return['pricelist'] = $pricelist;
		if (!empty($price) && !empty($pricelist)) {
				$pricedata = $pricelist[$price];
				if (!empty($pricedata)) {
					if (str_exists($pricedata, '-')) {
						$pricearr = explode('-', str_replace('&nbsp;', '', $pricedata));
						$return['shop_price'][] = array('EGT', $pricearr[0]);
						$return['shop_price'][] = array('elt', $pricearr[1]);
					} else if (str_exists($pricedata, '以上')) {
						$return['shop_price'] = array('EGT', str_replace('以上', '', $pricedata));
					}
				}
		}
		return $return;
	}
	/**
      *门店地址列表
      *@return array
      */
    public function get_shops_list($map=array(),$join='',$order='',$p=1,$pagesize=0){
		$map['status'] = 1;
        $order='id desc';
        $pagesize = empty($pagesize) ? C('WEB_SHOPS_PAGE') : $pagesize;
        $p = empty($p) ? 1 : $p;
		
        $shops_list = page_list('Shops',$map,$order,'shop_id,address',$pagesize,'',$p);
        foreach($shops_list['list'] as $key=>$val){
            $shops_list['list'][$key] = $this->format_shops_item($val);
        }
        return $shops_list;
    }
	/**
     * 格式化地址数据
     * @param array $data
     * @return array
     */
    public function format_shops_item($data=array()){
        $result=$data;
        $result['username']=idtoname($data['uid'],"Member",'username');
        $regions['province']=idtoname($data['province'],"Area",'name');
        $regions['city']=idtoname($data['city'],"Area",'name');
        $regions['district']=idtoname($data['district'],"Area",'name');
        $result['region']=$regions['province'].$regions['city'].$regions['district'];
        $result['best_time']=time_format($data['best_time'],'Y-m-d');
        $result['updatetime']=time_format($data['updatetime'],'Y-m-d');
        return $result;
    }
	/**
     * 会员的收货地址离门店的距离（必须要判断好收货地址方可使用该方法）
     * @param $location_id
     * @param $address_arr
     */
    public function get_member_account_distance($locationids,$address_arr){
		
        if(count($locationids)<0)return false;
        $module = 'ShopsAccount';$field = true;
        $map = $data = array();$return = array('status'=>0,'msg'=>'请重新获取定位信息');
		
        if($address_arr){
            $xpoint = $address_arr['xpoint'];
            $ypoint = $address_arr['ypoint'];
        }else {
            $geo = session('siteinfo');
            $xpoint = $geo['temp_lng'];
            $ypoint = $geo['temp_lat'];
        }
        if($xpoint>0){
            $pi = PI();  //圆周率
            $r = 6378137;  //地球平均半径(米)
            $result['append_field'] = array("(ACOS(SIN(($ypoint * $pi)/180)*SIN((a.ypoint*$pi)/180 )+COS(($ypoint*$pi)/180 )* COS((a.ypoint*$pi)/180)*COS(($xpoint*$pi)/180-(a.xpoint*$pi)/180))*$r)"=>"distance",'g.*');
            $result['order'] = array("distance"=>"asc");
        }
        if($result['append_field'])$field = array();
        $order = $result['order'];
        $field = $result['append_field'];
        if(empty($field))return $return;
        $map['a.shop_id'] = array('in',$locationids);
        $map['a.status'] = 1;
        // $result = page_list($module,$map,$order,$field);
		$result = page_list($module,$map);
        if($result['list']){
            foreach($result['list'] as $k=>$v){
                $data[$v['id']] = $v;
            }
        }
        return $data;
    }
	
	/**
       *修改会员信息，登录密码，支付密码，卡支付密码
       *@return array
       */
	public function shop_infor($data){
		$return = $savedata = $saveacc = array();
        $return['status']=0;
        $shopsinfo=checkshopauthtype();

	    $s_map['id']=$shopsinfo['shop_id'];
        if($shopsinfo['pid'] == 0){
	        $shopinfo=M('Shops')->where($s_map)->field(true)->find();
        }else{
        	$map['id']=$shopsinfo['id'];
        	$map['shop_id']=$shopsinfo['shop_id'];
	        $shopinfo=M('ShopsAccount')->where($map)->field(true)->find();
        }
		if($data) $regular= new \Common\Vendor\Regular();
        if($data['infos']=='pwd') {
            if(!$regular->isEmpty($data['password']) || !$regular->isEmpty($data['newpassword']) || !$regular->isEmpty($data['repassword'])){
                $return['msg']="必填项不能为空";
				return $return;
			}
            $password=creatpwd($data['password'],'HANCHAO'.$shopinfo['salt']);
            if ($password != $shopinfo['password']){
                $return['msg']='原密码输入错误';
                return $return;
            }
            if($data['newpassword'] == $data['password']){
                    $return['msg']='新密码和旧密码一样，请重新输入';
                    return $return;
             }
            if(!$regular->passWord(6,16,$data['newpassword'])){
                 $return['msg']='密码长度应为6-16位，可包含数字、字母、特殊符号';
                return $return;
            }
            if ($data['newpassword'] != $data['repassword']) {
                    $return['msg']='两次输入密码不一致';
                    return $return;
            }
            $savedata['password'] = $saveacc['password'] = creatpwd($data['newpassword'],'HANCHAO'.$shopinfo['salt']);
        }
        // if($shopsinfo['pid'] == 0 && $data['infos']=='paypwd' || $data['infos']=='info'){
	        if($data['infos']=='paypwd'){
	        	if($shopsinfo['pid']!=0){ $return['msg']="支付密码只可主账号修改"; return $return;}

	        	if($shopinfo['paypassword']){
	        		$paypassword=creatpwd($data['paypassword'],'HANCHAO'.$shopinfo['salt']);
		            if ($paypassword != $shopinfo['paypassword'] && !empty($data['paypassword'])){
		                $return['msg']='原密码输入错误';
		                return $return;
		            }
	        	}
	            
	            if(empty($data['newpaypassword'])){
	                $return['msg']="必填项不能为空";
	                return $return;
	            }
	            if(!$regular->passWord(6,16,$data['newpaypassword'])){
	                $return['msg']='密码长度应为6-16位，可包含数字、字母、特殊符号';
	                return $return;
	            }
	            if ($data['newpaypassword'] != $data['repaypassword']) {
	                $return['msg']='两次输入密码不一致';
	                return $return;
	            }
	            if($data['newpaypassword'] == $data['paypassword']){
					$return['msg']='新密码和旧密码一样，请重新输入';
					return $return;
	            }
	            $savedata['paypassword'] = creatpwd($data['newpaypassword'],'HANCHAO'.$shopinfo['salt']);
	           
	        }
	        if($data['infos']=='info'){
	        	$saveacc['name'] = $data['name'];
	        	$saveacc['login_name'] = $data['login_name'];
	        	$saveacc['qq'] = $data['qq'];
	        	$saveacc['weixin'] = $data['weixin'];
	        	$saveacc['shops_call'] = $data['shops_call'];
	        	$savedata = $saveacc;
	        	$saveacc['mobile'] = $data['contacts_phone'];
	        	$savedata['phone'] = $data['contacts_phone'];
	        	$savedata['contacts'] = $data['contacts'];
	        	$savedata['business_week_start'] = $data['business_week_start'];
	        	$savedata['business_week_end'] = $data['business_week_end'];
	        	$savedata['business_hours_start'] = $data['business_hours_start'];
	        	$savedata['business_hours_end'] = $data['business_hours_end'];
	        	$savedata['address'] = $data['address'];
	        	$savedata['thumb'] = $data['thumb'];
	        	$savedata['picture'] = $data['picture'];
	        	$savedata['machine_code'] = $data['machine_code'];
	        }
		// }else{
		// 	$return['msg']='没有修改支付密码和门店信息的权限';
		// }
		
		$savedata['updatetime']=$saveacc['updatetime']=NOW_TIME;

		if($shopsinfo['pid'] == 0){
			$result=M('Shops')->where($s_map)->save($savedata);
		}
        
        $map_acc['id'] = $shopsinfo['id'];
		$map_acc['shop_id'] = $shopsinfo['shop_id'];
		$resultacc=M('ShopsAccount')->where($map_acc)->save($saveacc);
		if($shopsinfo['pid'] == 0){
			if($result && $resultacc){
				if($data['infos']=='pwd'){
	                $data=D('Common/ShopLogin')->logout();
	                $return['msg']='修改成功，请重新登录';
	            }else{
					$return['url']=U('mobile/shop_center/index');
	                $return['msg']='修改成功';
	            }
	            $return['status']=1;
	        } else {
	            $return['msg']='修改失败';
	        }
		}else{
			if($resultacc){
				if($data['infos']=='pwd'){
	                $data=D('Common/ShopLogin')->logout();
	                $return['msg']='修改成功，请重新登录';
	            }else{
					$return['url']=U('mobile/shop_center/index');
	                $return['msg']='修改成功';
	            }
	            $return['status']=1;
	        } else {
	            $return['msg']='修改失败';
	        }
		}
        
        return $return;
	}
    /**
     * 店铺是否收藏
     * @param $shop_id
     * @return int
     */
    public function is_shops_collect($shop_id){
        $return=0;
        $member_info=checkuserauthtype();
        if($member_info['id']){
            $maps['shop_id']=$shop_id;
            $maps['uid']=$member_info['id'];
            $is_collect=M('ShopsCollect')->where($maps)->count();
            if($is_collect)$return=1;
        }
        return $return;
    }
	/**
     * 套餐核销
     * @param $postdata
     * @return array
     */
	public function sub_package($postdata){
		$return['status'] = 0;
		if(!empty($postdata['mobile'])){
				$whereor['card_sn'] = $postdata['mobile'];
				$whereor['mobile']= $postdata['mobile'];
				$whereor['_logic'] = 'or';
				$m['_complex'] = $whereor;
		}else{
			$return['info'] = '请填写卡号或手机号';
			return $return;
		}
        $userinfo = M("Member")->where($m)->field('id,paypassword,salt')->find();
		$pay_pwd = $postdata['paypassword'];
		if($pay_pwd=='' || creatpwd($pay_pwd,'HANCHAO'.$userinfo['salt']) != $userinfo['paypassword']){
                $return['info'] = '支付密码错误';
                return $return;
        }
		$use_package = array_filter($postdata['sub_package']);
		if(!$use_package){
			$return['info'] = '请选择核销的服务';
            return $return;
		}
		$p_map['id'] = array('in',implode(',',array_keys($use_package)));
		$p_map['uid'] = $userinfo['id'];
		$p_map['endtime'] = array("gt",NOW_TIME);
		$package_info = M('MemberPackage')->field('id,service_num,use_num')->where($p_map)->select();
		if(count($package_info) != count($use_package)){
			$return['info'] = '服务信息错误';
			return $return;
		}
		foreach($package_info as $k=>$v){
			$ids[] = $v['id'];
		}unset($k,$v);
		foreach($use_package as $k=>$v){
			if(in_array($k,$ids)){
				$service_info = idtoname($k,'MemberPackage','*');
				$s_num = $service_info['service_num'] - $service_info['use_num'];
				$s_name = idtoname($k,'Goods','goods_name');
				if($v>$s_num){
					$return['info'] = $s_name.'剩余服务次数不足';
					return $return;
				}
			}else{
				$return['info'] = '服务信息错误';
				return $return;
			}
		}unset($k,$v);
		foreach($use_package as $k=>$v){
			$order_true = 1;
			$true = 0;
			$m_package = M('MemberPackage')->where('id='.$k)->find();
			$save = array();
			$save['id'] = $k;
			$save['use_num'] = $m_package['use_num'] + $v;
			$save['use_time'] = NOW_TIME;
			$res = M('MemberPackage')->save($save);
			$add = array();
			$shopsinfo=session('shops_info');
			$add['uid'] = $userinfo['id'];
			$add['shop_id'] = MID;
			$add['shop_login_name'] = $shopsinfo['login_name'];
			$add['package_id'] = $m_package['package_id'];
			$add['goods_id'] = $m_package['goods_id'];
			$add['order_id'] = $m_package['order_id'];
			$add['num'] = $v;
			$add['sum_num'] = $m_package['service_num'];
			$add['sheng_num'] = $m_package['service_num']-$v;
			$add['addtime'] = NOW_TIME;
			$res_hx = M('PackageHx')->add($add);
			$order_info=idtoname($m_package['order_id'],'OrderInfo','*');
			$money = idtoname(MID,'Shops','deduct_price','id',false);
			$goods_name = idtoname($m_package['goods_id'],'Goods','goods_name');
			// shop_balance_log(MID,-$m_package['supply_price'],2,3,'套餐订单'.$order_info['order_sn'].'内'.$goods_name.'服务核销,因扣除'.$money.'店铺待扣服务费,未结算总额减少', $order_info);
			// if(C('IS_BALANCE')){//自动结算
				// shop_balance_log(MID,$m_package['supply_price'],1,3,'套餐订单'.$order_info['order_sn'].'内'.$goods_name.'服务核销,因扣除'.$money.'店铺待扣服务费,营业总额增加', $order_info);
			// }else{
				// shop_balance_log(MID,$m_package['supply_price'],3,3,'套餐订单'.$order_info['order_sn'].'内'.$goods_name.'服务核销,因扣除'.$money.'店铺待扣服务费,待结算总额增加', $order_info);
			// }
			// $map['order_id'] = $m_package['order_id'];
			// $o_info = M('MemberPackage')->where($map)->select();
			// foreach($o_info as $ke=>$ve){
				// if($ve['service_num'] != $ve['use_num']){
					// $order_true = 2;
				// }
			// }
			// if($order_true != 2){
				// $o_where['id'] = $m_package['order_id'];
				// $o_save['status'] = 3;
				// $o_save['shipping_status'] = 4;
				// M('OrderInfo')->where($o_where)->save($o_save);
				// $order_money = idtoname($m_package['order_id'],'OrderInfo','order_amount','id',false);
				// account_change($userinfo['id'],$order_money,3,4,"变更累计消费");
			// }
			
			
		}
		$return['status'] = 1;
		$return['info'] = '核销成功';
		return $return;
	}
	//套餐查询
	public function selectpack($postdata){
			$return['status'] = 0;
			$where = array();
			if(!empty($postdata['card_sn'])){
				$whereor['card_sn'] = $postdata['card_sn'];
				$whereor['mobile']= $postdata['card_sn'];
				$whereor['_logic'] = 'or';
				$where['_complex'] = $whereor;
			}else{
				$return['info'] = '请填写卡号或手机号';
				return $return;
			}
			$user_info = M("Member")->where($where)->find();
			if(!$user_info['id']){
				$return['info'] = '没有此会员';
				return $return;
			}else{
				$map = array();
				$map['uid'] = $user_info['id']; 
				$map['endtime'] = array("gt",NOW_TIME);
				$map['package_id'] = array("neq",0);
				if($postdata['order_id'])$map['order_id'] = $postdata['order_id'];
				$member_package = M("MemberPackage")->where($map)->select();
				// return $member_package;
				$bb = array();
				foreach($member_package as $key=>$value){
					if($value['service_num'] > $value['use_num']){
						$map = array();
						$map['id'] = $value['package_id'];
						$goods = M("Goods")->where($map)->find();
						$goods_arr = array();
						$goods_arr = explode(",",$goods['shop_ids']);
						if(in_array(MID,$goods_arr) || $goods['shop_ids'] == 0){
							$bb[$value['order_id']] = $value['package_id'];
						}
					}
					
				}unset($key,$value);
				
				$str = "";
				$count = 0;
				foreach($bb as $k=>$v){
					$packname = M("Goods")->where("id = ".$v)->getField("goods_name");
					$map = array();
					$map['uid'] = $user_info['id']; 
					$map['endtime'] = array("gt",NOW_TIME);
					$map['package_id'] = $v;
					$map['order_id'] = $k;
					$memberpackage = M("MemberPackage")->where($map)->select();
					$count = count($memberpackage);
					$name = "";
					if($memberpackage){
						// $str .= "<tr> <td rowspan='".$count."'>".$packname."</td>";
						foreach($memberpackage as $key=>$value){
							$name = M("Goods")->where("id = ".$value['goods_id'])->getField("goods_name");
							$wei = intval($value['service_num']) - intval($value['use_num']);
							$str .= "<tr><td>".$name."</td>
									<td>".$value['use_num']."</td>
									<td>".$wei."</td>
									<td>
										<span onclick='jian(".$value[id].")'>-</span>
										<a id='num".$value[id]."'>0</a>
										<span onclick='add(".$value[id].")'>+</span>
										<input type='hidden' id='wei_num".$value[id]."' value='".$wei."'>
										<input type='hidden' name='sub_package[".$value[id]."]' id='sub_package_".$value[id]."' value=''>
									</td>";
							$str .= "</tr>";
						}unset($key,$value);
					}
				}unset($k,$v);
				$data = "<select onchange='change_tc(this.value)'><option selected='selected' value='0'>请选择套餐</option>";
				foreach($bb as $k=>$v){
					$data.="<option value=".$k.">".idtoname($v,'Goods','goods_name')."</option>";
				}		
				$data.=	"</select>";
				$return['status'] = 1;
				$return['info'] = $str;
				$return['data'] = $data;
				return $return;
			}
	}
	public function select_hx($postdata){
		$result = array();$result['status'] = 0;
		$shopsinfo=checkshopauthtype();
		
		$service_no = $postdata['service_no'];
		$map = array();$map['service_sn'] = $service_no;
		$service_info = M('MemberPackage')->where($map)->find();
		if(empty($service_info)){
			$result['info'] = '服务核销码错误';
			return $result;
		}
		if($service_info['use_num'] >= 1 || $service_info['status'] == 1){
			$result['info'] = '该服务核销码已被核销';
			return $result;
		}
		if($service_info['status'] == 2){
			$result['info'] = '该订单已取消，不能在核销';
			return $result;
		}
		
		$where = array();
		$where['id'] = $service_info['order_id'];
		$order_info=M('OrderInfo')->where($where)->field(true)->find();
		if($order_info['status'] == 4){
			$savedata = array();$savedata['id'] = $service_info['id'];
			$savedata['status'] = 2;
			M('MemberPackage')->save($savedata);
			
			$result['info'] = '该订单已取消，不能在核销';
			return $result;
		}
		
		// $shop_id = ','.MID.',';
		$where = array();
		$where['id'] = $service_info['goods_id'];
		$where['goods_type'] = 4;
		// $where['_string'] = "shop_ids = ".MID." OR shop_ids='0'";
		$where['_string'] = "shop_ids = ".$shopsinfo['shop_id']." OR shop_ids='0'";
		$count = M('Goods')->where($where)->count();
		if(!$count){
			$result['info'] = '您仓库没有核销该核销码的权利';
			return $result;
		}
		if($service_info){
			$data['name'] = idtoname($service_info['goods_id'],'Goods','goods_name');
			$data['goods_sn'] = idtoname($service_info['goods_id'],'Goods','goods_sn');
			$data['order_sn'] = idtoname($service_info['order_id'],'OrderInfo','order_sn');
			$data['num'] = $service_info['service_num'];
			$result['status'] = 1;
			$result['data'] = $data;
		}
		return $result;
	}
	public function service_hx($postdata){
		$result = array();$result['status'] = 0;
		$shopsinfo=checkshopauthtype();
		
		$service_no = $postdata['service_no'];
		$map = array();$map['service_sn'] = $service_no;
		$service_info = M('MemberPackage')->where($map)->field(true)->find();
		if(empty($service_info)){
			$result['info'] = '服务核销码错误';
			return $result;
		}
		if($service_info['use_num'] >= 1 || $service_info['status'] == 1){
			$result['info'] = '该服务核销码已被使用';
			return $result;
		}
		if($service_info['status'] == 2){
			$result['info'] = '该服务核销码已经退货';
			return $result;
		}
		
		$where = array();
		$where['id'] = $service_info['order_id'];
		$order_info=M('OrderInfo')->where($where)->field(true)->find();
		if($order_info['status'] == 4){
			$savedata = array();$savedata['id'] = $service_info['id'];
			$savedata['status'] = 2;
			M('MemberPackage')->save($savedata);
			
			$result['info'] = '该订单已取消，不能在核销';
			return $result;
		}
		if($order_info['pay_status'] != 3){
			$result['info'] = '该订单没有完全支付，不能核销';
			return $result;
		}
		
		// $shop_id = ','.MID.',';
		$where = array();
		$where['id'] = $service_info['goods_id'];
		$where['goods_type'] = 4;
		// $where['_string'] = "shop_ids = ".MID." OR shop_ids='0'";
		$where['_string'] = "shop_ids = ".$shopsinfo['shop_id']." OR shop_ids='0'";
		$count = M('Goods')->where($where)->count();
		if(!$count){
			$result['info'] = '您仓库没有核销该核销码的权利';
			return $result;
		}
		
		//修改服务次数
		$savedata = array();
		$savedata['id'] = $service_info['id'];
		$savedata['status'] = 1;
		$savedata['use_num'] = $service_info['use_num'] + 1;
		$savedata['use_shop'] = $postdata['shop_id'];
		$savedata['hx_name'] = $postdata['hx_name'];
		$savedata['use_time'] = NOW_TIME;
		$service_ret = M('MemberPackage')->save($savedata);
		
		//修改订单店铺
		if(!empty($service_info['order_id'])){
			$g_map['id']=$service_info['order_id'];
			// $save['shop_id']=MID;
			$save['shop_id']=$shopsinfo['shop_id'];
			$res=M('OrderInfo')->where($g_map)->save($save);
		}
		
		//处理订单
		$orderinfo['order_id'] = $service_info['order_id'];
		$orderinfo['admin_id'] = $service_info['uid'];
		$orderinfo['type'] = 'Member';
		
		// $price_info=get_balance_price($service_info['order_id']);//获取订单的结算价
		// shop_balance_log(MID, $price_info['price'], 6,3,'服务' . $order_info['order_sn'] . '付款,销售总额增加', $order_info);
		// shop_balance_log($shopsinfo['shop_id'], $price_info['price'], 6,3,'服务' . $order_info['order_sn'] . '付款,销售总额增加', $order_info);
		$order_ret = D('Common/OrderInfo')->confirm_order($orderinfo);
		if($order_ret['status'] == 1){
			$result['status'] = 1;
			$result['info'] = '核销成功';
		}
		return $result;
	}
	/**
      * 服务核销记录
	  * shop_id 门店id
      * @return array
      */
	public function check_service_jilu($shop_id){
		$return['status'] = 0;
		if(!$shop_id){$result['msg'] = '参数错误！';}
		$map = array();$map['use_shop'] = $shop_id;
		$map['status'] = 1;$map['use_time'] = array('gt',0);
		// $list = M('MemberPackage')->where($map)->field(true)->find();
		$service_list = page_list('MemberPackage',$map,'id desc',true,20,'',1);
		if(empty($service_list['list'])){
			$return['msg']='没有更多数据了!';return $return;
		}else{
			$list = $service_list['list'];
			foreach($list as $k=>$v){
				$list[$k] = $v;
				$list[$k]['hx_sn']=$v['service_sn'];
				$list[$k]['hx_time']=time_format($v['use_time']);
				$map = array();$map['id'] = $v['goods_id'];
				$list[$k]['name'] = M('Goods')->where($map)->getField('goods_name');
				$list[$k]['user_name'] = show_user_name($v['uid']);
			}
			
			if($list){
				$return['status']=1;
				$return['data'] = $list;
			}else{
				$return['msg']='没有更多数据了!';
			}
			return $return;
		}
	}
	/**
      *门店会员开卡
      *@return array
      */
	public function update_card($postdata){
		$return['status'] = 0;
		$res = M('Config')->field('value')->where("name='WX_CARD'")->find();
        $val = unserialize($res['value']);
        if($val['open_card'] == 0){
        	$return['info']='会员卡自动激活!';
			return $return;
        }elseif ($val['open_card'] == 1) {
        	$return['info']='会员卡手动激活!';
			return $return;
        }
		if(!$postdata['phone']){
			$return['info']='请输入手机号!';
			return $return;
		}
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$s_map['id'] = $shop_id;
		$shopinfo = M('Shops')->where($s_map)->field(true)->find();
		// if (empty($shopinfo['paypassword'])){
                // $return['info']='您店铺还没有设置支付密码';
                // return $return;
        // }
		// $paypassword=creatpwd($postdata['paypassword'],'HANCHAO'.$shopinfo['salt']);
		// if(empty($postdata['paypassword']) || $paypassword !=$shopinfo['paypassword']){
			// $return['info']='支付密码错误';
            // return $return;
		// }
		if($postdata['money'] && !$postdata['payment_id']){
			$return['info']='请选择支付方式!';
			return $return;
		}
		$member_info = M('Member')->where('mobile=' . $postdata['phone'])->field(true)->find();
		if($member_info['card_sn']){
			$return['info']='该会员已经开卡!';
			return $return;
		}
		//验证会员卡号
		if($postdata['card_sn']){
			$card_map['card_sn'] = array('eq',$postdata['card_sn']);
			$cardsncount = M('Member')->where($card_map)->count();
			if ($cardsncount > 0) {
				$return['info']='卡号已经存在，请重新填写!';
				return $return;
			}
		}else{
			$return['info']='请输入卡号!';
			return $return;
		}
		$today = strtotime(date("Y-m-d"),time());
		$end = $today+60*60*24;
		$o_map['type'] = 2;
		$o_map['shop_id'] = $shop_id;
		$o_map['pay_time'] = array('between',array($today,$end));
		$sum_money = M('OrderInfo')->where($o_map)->sum('order_amount');//今日充值总数目
		$shop_quota = C('SHOP_RECHARGE_QUOTA');//平台规定每日充值额度
		$surplus_money = $shop_quota - $sum_money;
		if($surplus_money<0)$surplus_money = 0;
		if($surplus_money<$postdata['money']){
			$return['info'] = '充值失败，请联系管理员';
			return $return;
		}else{
			$shop_type = idtoname($shop_id,'Shops','shop_type','id',false);
			$data['card_sn'] = $postdata['card_sn'];
			$data['user_shop'] = $shop_id;
			$data['card_time'] = $postdata['card_time'];
			$data['card_status'] = 1;
			$m_map['mobile'] = array('eq',$postdata['phone']);
			$res = M('Member')->where($m_map)->save($data);
			if($shop_type == 1){
				if($postdata['money']){
					account_change($member_info['id'],$postdata['money'],1,12,'店铺充值');
					// shop_balance_log($shop_id,$postdata['money'],1,6,'给'.$member_info['username'].'充值,扣除'.$postdata['money']);
					account_change($member_info['id'],$postdata['money'],7,12,'累计充值');
					$grade_choices = C('GRADE_CHOICES');//会员升级选择
					if(C('IS_MEMBER_UPGRADE')){
						if($grade_choices == 2){//累计充值升级
							// user_ranks($member_info['user_id']);
						}
					}
					$order_data['order_sn'] = createOrderNo();
					$order_data['type'] = 2;
					$order_data['uid'] = $member_info['id'];
					$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;
					$order_data['distribute'] = $shopsinfo['login_name'];
					M('OrderInfo')->add($order_data);
					shop_give_double($member_info['id'],$postdata['money']);
				}
			}else{
				$total_revenues = idtoname($shop_id,'Shops','total_revenues','id',false);
				if($total_revenues>=$postdata['money']){
					if($postdata['money']){
					account_change($member_info['id'],$postdata['money'],1,12,'店铺充值');
					// shop_balance_log($shop_id,-$postdata['money'],1,6,'给'.$member_info['username'].'充值,扣除'.$postdata['money']);
					account_change($member_info['id'],$postdata['money'],7,12,'累计充值');
					$grade_choices = C('GRADE_CHOICES');//会员升级选择
						if(C('IS_MEMBER_UPGRADE')){
							if($grade_choices == 2){//累计充值升级
								user_ranks($member_info['user_id']);
							}
						}
					}
					$order_data['order_sn'] = createOrderNo();
					$order_data['type'] = 2;
					$order_data['uid'] = $member_info['id'];
					$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;
					$order_data['distribute'] = $shopsinfo['login_name'];
					M('OrderInfo')->add($order_data);
					shop_give_double($member_info['id'],$postdata['money']);
				}else{
					$return['info'] = '店铺余额不足';
					return $return;
				}
				
			}
		}
		if($res){
			$return['status'] = 1;
			$return['info'] = '开卡成功';
			// 开启微信会员卡open
			$Weixin = new \Common\Vendor\WeixinCard();
	        $Weixin->membercard($postdata['phone'],1,'初始积分同步');
	        // 微信会员卡end
			//开卡赠积分
			if($member_info['code']){
				$usepoints=C('CARD_INTEGRAL');
				if($usepoints>0){
					account_change($member_info['id'],$usepoints,4,5,'领卡赠积分');
				}
			}
		}else{
			$return['info'] = '开卡失败';
		}
		return $return;
	}
	/**
      *门店会员补卡
      *@return array
      */
	public function update_fill($postdata){
		$return['status'] = 0;
		if(!$postdata['phone']){
			$return['info']='请输入手机号!';
			return $return;
		}
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$p_map['mobile'] = $postdata['phone'];
		$member_info = M('Member')->where($p_map)->field(true)->find();
		if(empty($member_info['card_sn'])){
			$return['info']='该会员还未开卡!';
			return $return;
		}
		if(empty($member_info['paypassword'])){
			$return['info']='该会员还未设置支付密码!';
			return $return;
		}
		$paypassword=creatpwd($postdata['paypassword'],'HANCHAO'.$member_info['salt']);
		if(empty($postdata['paypassword']) || $paypassword !=$member_info['paypassword']){
			$return['info']='支付密码错误';
            return $return;
		}
		//验证会员卡号
		if($postdata['card_sn']){
			$c_map['card_sn'] = $postdata['card_sn'];
			$cardsncount = M('Member')->where($c_map)->count();
			if ($cardsncount > 0) {
				$return['info']='新卡号已经存在，请重新填写!';
				return $return;
			}
		}else{
			$return['info']='请输入新卡号!';
			return $return;
		}
		$data['card_sn'] = $postdata['card_sn'];
		$data['user_shop'] = $shop_id;
		$data['card_time'] = $postdata['card_time'];
		$data['card_status'] = 1;
		$s_where['mobile'] = $postdata['phone'];
		$res = M('Member')->where($s_where)->save($data);
		if($res){
			$return['status'] = 1;
			$return['info'] = '补卡成功';
		}else{
			$return['info'] = '补卡失败';
		}
		return $return;
	}
	/**
      *门店会员充值
      *@return array
      */
	public function sub_recharge($postdata){
		$return['status'] = 0;
		if(!$postdata['keyword']){
			$return['info']='请输入手机号或者卡号!';
			return $return;
		}
		$shopsinfo=session('shops_info');
		$shop_id=$shopsinfo['shop_id'];
		$mobile = $postdata['keyword'];
		$whereor['mobile']=array('eq',$mobile);
        $whereor['card_sn']=array('eq',$mobile);
        $whereor['_logic'] = 'or';
        $map['_complex'] = $whereor;
		$member_info = M('Member')->where($map)->field(true)->find();
		// if(empty($member_info['card_sn'])){
		// 	$return['info']='该会员还未开卡!';
		// 	return $return;
		// }
		$today = strtotime(date("Y-m-d"),time());
		$end = $today+60*60*24;
		$o_map['type'] = 2;
		$o_map['shop_id'] = $shop_id;
		$o_map['pay_time'] = array('between',array($today,$end));
		$sum_money = M('OrderInfo')->where($o_map)->sum('order_amount');//今日充值总数目
		$shop_quota = C('SHOP_RECHARGE_QUOTA');//平台规定每日充值额度
		$surplus_money = $shop_quota - $sum_money;
		if($surplus_money<0)$surplus_money = 0;
		if($surplus_money<$postdata['money']){
			$return['info'] = '充值失败，请联系管理员';
			return $return;
		}else{
			$shop_type = idtoname($shop_id,'Shops','shop_type','id',false);
			$data['card_sn'] = $postdata['card_sn'];
			$data['user_shop'] = $shop_id;
			$data['card_time'] = $postdata['card_time'];
			$data['card_status'] = 1;
			$m_map['mobile'] = array('eq',$postdata['phone']);
			$res = M('Member')->where($m_map)->save($data);
			if($shop_type == 1){
				if($postdata['money']){
					account_change($member_info['id'],$postdata['money'],1,12,'店铺充值');
					// shop_balance_log($shop_id,$postdata['money'],1,6,'给'.$member_info['username'].'充值,扣除'.$postdata['money']);
					account_change($member_info['id'],$postdata['money'],7,12,'累计充值');
					$grade_choices = C('GRADE_CHOICES');//会员升级选择
					if(C('IS_MEMBER_UPGRADE')){
						if($grade_choices == 2){//累计充值升级
							// user_ranks($member_info['user_id']);
						}
					}
					$order_data['order_sn'] = createOrderNo();
					$order_data['type'] = 2;
					$order_data['uid'] = $member_info['id'];
					$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;
					$order_data['distribute'] = $shopsinfo['login_name'];
					M('OrderInfo')->add($order_data);
					shop_give_double($member_info['id'],$postdata['money']);
				}
			}else{
				$total_revenues = idtoname($shop_id,'Shops','total_revenues','id',false);
				if($total_revenues>=$postdata['money']){
					if($postdata['money']){
					account_change($member_info['id'],$postdata['money'],1,12,'店铺充值');
					// shop_balance_log($shop_id,-$postdata['money'],1,6,'给'.$member_info['username'].'充值,扣除'.$postdata['money']);
					account_change($member_info['id'],$postdata['money'],7,12,'累计充值');
					$grade_choices = C('GRADE_CHOICES');//会员升级选择
						if(C('IS_MEMBER_UPGRADE')){
							if($grade_choices == 2){//累计充值升级
								// user_ranks($member_info['user_id']);
							}
						}
					}
					$order_data['order_sn'] = createOrderNo();
					$order_data['type'] = 2;
					$order_data['uid'] = $member_info['id'];
					$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;
					$order_data['distribute'] = $shopsinfo['login_name'];
					M('OrderInfo')->add($order_data);
					shop_give_double($member_info['id'],$postdata['money']);
				}else{
					$return['info'] = '店铺余额不足';
					return $return;
				}
				
			}
		}
		$return['status'] = 1;
		$return['info'] = '充值成功';
		return $return;
	}
    /**
      *门店套餐发放
      *@return array
      */
    public function sub_packagesend($postdata){
        $return['status'] = 0;
        if(!$postdata['phone'] || !$postdata['package_id'] || !$postdata['pay_id']){
			$return['info']='参数错误!';
			return $return;
		}
        $shop_info = M('Shops')->where('id='.MID)->field('id,name,paypassword,salt,total_revenues')->find();
        $whereor['mobile']=array('eq',$postdata['phone']);
        $whereor['card_sn']=array('eq',$postdata['phone']);
        $whereor['_logic'] = 'or';
        $map['_complex'] = $whereor;
		$member_info = M('Member')->where($map)->field('id,username,paypassword,salt,mobile,card_sn,money')->find();
        if(!$member_info){
			$return['info']='卡号或手机号错误!';
            return $return;
		}
        if(!$member_info['card_sn']){
       	    $return['info']='该会员暂未开卡!';
			return $return;
        }unset($map);
		$map['id'] = $postdata['package_id'];
        $shop_ids = ','.MID.',';
        $map['goods_type'] = 6;
		$map['_string'] = "shop_ids like '%".$shop_ids."%' OR shop_ids='0'";
		$package_info = M('Goods')->where($map)->field('id,goods_name,shop_price,contain_service,continued')->find();
		if(!$package_info){
			$return['info']='套餐错误!';
			return $return;
		}
        if($postdata['pay_id'] == 1){
			$password=creatpwd($postdata['paypassword'],'HANCHAO'.$member_info['salt']);
			if ($password != $member_info['paypassword']){
                $return['msg']='会员支付密码输入错误';
                return $return;
            }
            if($member_info['money'] < $package_info['shop_price']){
			     $return['info']='会员余额不足!';
			     return $return;
		    }
        }elseif($postdata['pay_id'] == 2){
			$password=creatpwd($postdata['paypassword'],'HANCHAO'.$shop_info['salt']);
			if ($password != $shop_info['paypassword']){
                $return['msg']='仓库支付密码输入错误';
                return $return;
            }
            if($shop_info['total_revenues'] < $package_info['shop_price']){
			     $return['info']='仓库余额不足!';
			     return $return;
		    }
        }else{
            $return['info']='支付方式错误!';
            return $return;
        }
        $contain_service = json_decode($package_info['contain_service'],true);
		if($contain_service){
			foreach($contain_service as $k=>$v){
				$m_package = array();
				$m_package['uid'] = $member_info['id'];
				$m_package['order_id'] = '-1';//代表线下发放
				$m_package['service_sn'] = createOrderNo();
				$m_package['package_id'] = $postdata['package_id'];
				$m_package['goods_id'] = $k;
				$m_package['service_num'] = $v;
				$m_package['service_type'] = 0;
				$m_package['use_shop'] = 0;
				$m_package['use_num'] = 0;
				$m_package['continued'] = $package_info['continued'];
				if($postdata['package_id']){
					$gwhere['id'] = $k;
					$g_info = M("Goods")->where($gwhere)->field('id,supply_price')->find();
				}
				$m_package['addtime'] = NOW_TIME;
				$endtime = NOW_TIME + $package_info['continued']*86400;
				$m_package['endtime'] = $endtime;
				$m_package['package_price'] = $package_info['shop_price'];//余额
				$m_package['supply_price'] = $g_info['supply_price'];
				$m_package['use_time'] = 0;
				// print_r($m_package);exit;
				M('MemberPackage')->add($m_package);
			}unset($k,$v);
		}else{
            $return['info']='套餐错误!';
            return $return;
		}
        $p_log = array();
        $p_log['uid'] = $member_info['id'];
        $p_log['package_id'] = $postdata['package_id'];
        $p_log['package_name'] = $package_info['goods_name'];
        $p_log['package_price'] = $package_info['shop_price'];
        $p_log['pay_way'] = $postdata['pay_id'];
        $p_log['member_card'] = $member_info['card_sn'];
        $p_log['member_name'] = $member_info['username'];
        $p_log['member_mobile'] = $member_info['mobile'];
        $p_log['add_time'] = NOW_TIME;
        $p_log['send_shop'] = MID;
        $p_log['remarks'] = $postdata['remarks'];
        M('PackagesendLog')->add($p_log);
        if($postdata['pay_id'] == 1){
            account_change($member_info['id'], -$package_info['shop_price'], 1, 4, "通过仓库购买套餐");
        }
        if($postdata['pay_id'] == 2){
            shop_balance_log($shop_info['id'],-$package_info['shop_price'],1,4,'发放套餐,扣除'.$package_info['shop_price']);
        }
        $return['status']=1;
        $return['info']='发放成功!';
        return $return;
		
    }

	/**
     * 礼品码查询
     */
	public function select_win_gift($postdata){
		$result = array();$result['status'] = 1;
		$where = array();$where['sn_number'] = $postdata['sn_number'];
		$win_gift_info=M('WinGift')->where($where)->field(true)->find();
		if(empty($win_gift_info)){
			$result['status'] = 0;
			$result['info'] = '奖品码错误';
			return $result;
		}
		if($win_gift_info['check_status'] != 0){
			$result['status'] = 0;
			$result['info'] = '该奖品码已领取，不能核销';
			return $result;
		}
		$result['uid'] = $win_gift_info['user_id'];
		$result['user_name'] = show_user_name($win_gift_info['user_id']);
		if($win_gift_info['win_type'] == 5){
			$result['prize_name'] = '注：积分已赠送';
			$result['prize_goods'] = $win_gift_info['gift_name'];
			
			$result['status'] = 0;
			$result['info'] = '签到积分已赠送，不能再次核销';
			return $result;
		}else{
            $map = array();
    		$map['id'] = $win_gift_info['gift_id'];
    		$prize_info = M('Prize')->where($map)->field(true)->find();
            $prize_goods=$prize_info['prize_goods'];
    		if($prize_info['prize_type'] == 4){
    			$map = array();$map['id'] = $prize_info['prize_goods'];
    			$prize_goods = M('Coupons')->where($map)->getField('title');
				
				$result['status'] = 0;
				$result['info'] = '优惠券不能核销，请在公众号领取！';
				return $result;
    		}
            if($prize_info['prize_type'] == 2){$prize_goods.='元';}
            if($prize_info['prize_type'] == 3){$prize_goods.='积分';}
            $prize_goods.='('.$prize_info["prize_name"].')';
			$result['prize_name'] = $prize_info["prize_name"];
			$result['prize_goods'] = $prize_goods;
		}
		return $result;
	}
    /**礼品码核销（1积分商品）
     * @param $postdata=array(
            'sn_number'=>sn验证码,
     * )
     */
    public function check_win_gift($postdata){
        $return=array('status'=>0,'msg'=>'');
        if(empty($postdata['sn_number'])){
            $return['msg']='奖品验证码不能为空！';
            return $return;
        }
        $map=array();
        $map['sn_number']=$postdata['sn_number'];
        $gift_info=M("WinGift")->where($map)->field(true)->find();
        if(empty($gift_info)){
            $return['msg']='此礼品核销码错误！';
            return $return;
        }
        if($gift_info['check_status']==1){
            $return['msg']='该礼品核销码已核销！';
            return $return;
        }
        $data=array();
        $data['check_time']=NOW_TIME;
        $data['check_status']=1;
        $data['location_id']=$postdata['location_id'];
        $data['hx_name']=$postdata['user_name'];
        $res=M("WinGift")->where($map)->save($data);
        if($res){
            $return['status']=1;
            $return['msg']='核销成功！';
			
			if($gift_info['win_type'] == 2){
				M('Prize')->where('id='.$gift_info['gift_id'])->setDec('goods_num',1);
			}
        }else{
            $return['msg']='核销失败！';
        }
        return $return;
    }
	/**
      * 礼品码核销记录
	  * shop_id 门店id
      * @return array
      */
	public function check_gift_jilu($shop_id){
		$return['status'] = 0;
		if(!$shop_id){$result['msg'] = '参数错误！';}
		$map = array();$map['location_id'] = $shop_id;$map['check_status'] = 1;
		$map['gift_type'] = 0;$map['check_time'] = array('gt',0);$map['win_type'] = array('neq',5);
		$order_list = page_list('WinGift',$map,'id desc',true,20,'',1);
		if(empty($order_list['list'])){
			$return['msg']='没有更多数据了!';return $return;
		}else{
			$list = $order_list['list'];
			foreach($list as $k=>$v){
				$list[$k] = $v;
				$list[$k]['hx_sn']=$v['sn_number'];
				$list[$k]['hx_time']=time_format($v['check_time']);
				$list[$k]['user_name'] = show_user_name($v['user_id']);
				$map = array();$map['id'] = $v['gift_id'];
				$list[$k]['name'] = M('Prize')->where($map)->getField('prize_goods');
			}
			
			if($list){
				$return['status']=1;
				$return['data'] = $list;
			}else{
				$return['msg']='没有更多数据了!';
			}
			return $return;
		}
	}
	/**
     * 提货码查询
     */
	public function select_thm($postdata){
		$result = array();$result['status'] = 0;
		$where = array();$where['order_sn'] = $postdata['sn_number'];
		$order_info=M('OrderInfo')->where($where)->field(true)->find();
		if(empty($order_info)){
			$result['info'] = '提货码错误';
			return $result;
		}
		if($order_info['send_type'] != 1){
			$result['info'] = '该订单不是自提订单，不能核销';
			return $result;
		}
		if($order_info['pay_status'] != 3){
			$result['info'] = '该订单没有完全支付，不能核销';
			return $result;
		}
		if($order_info['status'] == 3){
			$result['info'] = '该订单已完成，不能核销';
			return $result;
		}
		if($order_info['status'] == 4){
			$result['info'] = '该订单已取消，不能在核销';
			return $result;
		}else{
			$result['status'] = 1;
			$result['order_sn'] = $order_info['order_sn'];
			$result['pay_status'] = $order_info['pay_status'];
			$result['order_amount'] = format_price($order_info['order_amount']);
			if($order_info['pay_status'] == 3){$result['order_paystatus'] = '已付款';}
			else{$result['order_paystatus'] = '未付款';}
			$result['uid'] = $order_info['uid'];
			$result['user_name'] = show_user_name($order_info['uid']);
			$result['mobile'] = $order_info['mobile'];
			
			$map = array();$map['order_id'] = $order_info['id'];
			$order_goods = M('OrderGoods')->where($map)->field('goods_name,goods_sn,goods_number,attr_price,goods_attr')->select();
		}
		
		if($order_goods){
			// $result['order_goods_count'] = count($order_goods);
			// if($result['order_goods_count'] == 1){$result['order_goods'] = $order_goods[0];}
			// else{$result['order_goods'] = $order_goods;}
			foreach($order_goods as $k=>$v){
				$attr=unserialize($v['goods_attr']);
				$order_goods[$k]['attr_name']=$attr['name'];
				$order_goods[$k]['goods_number']=intval($v['goods_number']);
			}unset($k,$v);
			$result['order_goods'] = $order_goods;
		}
		return $result;
	}
    /**
     * 提货码核销
     */
    public function check_goods($postdata){
        $return=array('status'=>0,'msg'=>'');
        if(empty($postdata['sn_number'])){
            $return['msg']='提货码不能为空！';
            return $return;
        }
        if(empty($postdata['location_id'])){
            $return['msg']='仓库不能为空！';
            return $return;
        }
        $map=array();
        $map['order_sn']=$postdata['sn_number'];
        $sn=M("OrderInfo")->where($map)->field('order_sn,check_status,id,shop_id')->find();;
        if(empty($sn)){
            $return['msg']='没有此提货码！';
            return $return;
        }
        if($sn['check_status']==1){
            $return['msg']='该提货码已验证！';
            return $return;
        }

        if($sn['shop_id']!=$postdata['location_id']){
            $return['msg']='该仓库没有此提货码的验证权限！';
            return $return;
        }
        $map=$data=array();
        $map['order_sn']=$postdata['sn_number'];
        $data['check_status']=1;
        $res=M("OrderInfo")->where($map)->save($data);
        if($res){
            $info=array();
            $info['order_id']=$sn['id'];
            $result=D("Common/OrderInfo")->confirm_order($info);
            if($result['status']==0){
                $return['msg']=$result['msg'];
                return $return;
            }
            $return['status']=1;
            $return['msg']='核销成功！';
        }else{
            $return['msg']='核销失败！';
        }
        return $return;
    }
	/**
      * 提货码核销记录
	  * shop_id 门店id
      * @return array
      */
	public function check_thm_jilu($shop_id){
		$return['status'] = 0;
		if(!$shop_id){$result['msg'] = '参数错误！';}
		$map = array();$map['shop_id'] = $shop_id;$map['check_status'] = 1;
		$map['send_type'] = 1;$map['finish_time'] = array('gt',0);$map['status'] = 3;
		$order_list = page_list('OrderInfo',$map,'id desc',true,20,'',1);
		if(empty($order_list['list'])){
			$return['msg']='没有更多数据了!';return $return;
		}else{
			$list = $order_list['list'];
			foreach($list as $k=>$v){
				$list[$k] = $v;
				$list[$k]['hx_sn']=$v['order_sn'];
				$list[$k]['hx_time']=time_format($v['finish_time']);
				$list[$k]['user_name'] = show_user_name($v['uid']);
			}
			
			if($list){
				$return['status']=1;
				$return['data'] = $list;
			}else{
				$return['msg']='没有更多数据了!';
			}
			return $return;
		}
	}
	//门店缺货商品
	public function qh_index($shop_id,$page){
		$return=array('status'=>1,'msg'=>'');
		if(empty($shop_id)){
			$return['status']=0;$return['msg']='参数获取错误！';return $return;
		}
		$map['shop_id'] = $shop_id;
		$map['qh_count'] = array('neq',0);
		$map['_string'] = 'qh_count > goods_number';
		$goods_list = page_list('ShopsGoodsStock',$map,'','','5','',$page);
		if($goods_list['list']){
			$return['data'] = $goods_list['list'];
		}else{
			$return['status'] = 1;
		}
		return $return;
	}
}
?>