<?php
/**
 * 管理员管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author dongziyi <2066207038@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class NearbyModel extends Model{
    protected $tableName="Shops";
    /**
     * 获取门店列表
     * @param array $postdata 筛选数据
     * @return array
     */
    public function get_shops_list($postdata) {
        $return = array('status'=>2,'msg'=>'没有更多的数据了');
        //有效门店
        $true_list=array();
        $user_shop=0;
        if(intval(C('IS_LOCK_USER'))==1){
            $map=array();
            $map['id']=$postdata['uid'];
            $user_shop=M("Member")->where($map)->getField("user_shop");
        }
        $ret = D('Wxsmall/Login')->bind_shop_param($postdata['lng'],$postdata['lat'],1);
        if($ret['status']==1){
            $shop_data['true_count'] = count($ret['data']);
            $true_list = $ret['data'];
        }else{
            $shop_data['true_count'] = 0;
        }
        $shop_list_true=$shop_list_false=array();
        foreach ($true_list as $k => $v) {
            $map=array();
            $map['id']=$v['id'];
            $info=M("Shops")->where($map)->field(true)->find();
            if($user_shop){
                if($v['id']==$user_shop) {
                    $shop_list_true[$k] = D('Wxsmall/Nearby')->format_location($info);
                    $shop_list_true[$k]['id'] = $v['id'];
                    $shop_list_true[$k]['distance'] = $v['distance'];
                }
                $ids[]=$user_shop;
            }else{
                $shop_list_true[$k] = D('Wxsmall/Nearby')->format_location($info);
                $shop_list_true[$k]['id'] = $v['id'];
                $shop_list_true[$k]['distance'] = $v['distance'];
                $ids[]=$v['id'];
            }
        }
        //无效门店
        $map=array();
        $map['id']=array('notin',$ids);
        $shop_list_false=$info=M("Shops")->where($map)->field(true)->select();
        foreach($shop_list_false as $key=>$val){
            $shop_list_false[$key] = $this->format_location($val);
        }
        if(intval(C('IS_LOCK_USER'))==1){
            $shop_list_false=array();
        }
        $shop_data['true_list']=$shop_list_true;
        $shop_data['false_list']=$shop_list_false;
        $shop_data['false_count']=count($shop_list_false);
        $return['status'] = 1;
        $return['data'] = $shop_data;
        return  $return;
    }
    //格式化门店数据
    public function format_location($data){
        $format = array();
        $data['thumb']='';
        $format['thumb'] = thumb($data['thumb'],224,133,'noimg_300x140.png');
        $format['address'] = $data['address'];
        $format['name'] = $data['name'];
        $format['id']=$data['id'];
        $format['contacts_phone'] = $data['contacts_phone'];
        $point = D('Wxsmall/Login')->Convert_BD09_To_GCJ02($data['ypoint'],$data['xpoint']);
        $format['xpoint'] = $point['lng'];
        $format['ypoint'] = $point['lat'];
        $format['business_time'] = $this->format_business_time($data);
        return $format;
    }
    //格式化门店营业时间
    public function format_business_time($data){
        $timestr = '';
        $k_s = $data['business_week_start'];
        $k_e = $data['business_week_end'];
        $h_s = $data['business_hours_start'];
        $h_e = $data['business_hours_end'];
        
        if($k_s){
            if($k_s==$k_e){
                $timestr = $k_s.' ';
            }else{
                $timestr = $k_s.' - ';
            }
        }
        if($k_e&&$k_s!=$k_e){
            $timestr .= $k_e.' ';
        }
        if($h_s){
            $timestr .= $h_s.' - ';
        }
        if($h_e){
            $timestr .= $h_e;
        }
        if(!$h_e&&!$h_s){
            $timestr .= '全天';
        }
        return $timestr;
    }
    /**
     * 按照当前定位获取门店的距离排序
     * @param string $alias
     * @return array
     */
    public function get_position_param($param,$alias='a'){
        $lng=$param['lng'];//经度 xpoint
        $lat=$param['lat'];//纬度 ypoint
        $return = array();
        if($lng>0){
            $pi = PI();  //圆周率
            $r = 6378137;  //地球平均半径(米)
            $return['append_field'] = array("(ACOS(SIN(($lat * $pi)/180)*SIN(($alias.ypoint*$pi)/180)+COS(($lat*$pi)/180 )* COS(($alias.ypoint*$pi)/180)*COS(($lng*$pi)/180-($alias.xpoint*$pi)/180))*$r)"=>"distance",'*');
            $return['order'] = array("distance"=>"asc");
        }else{
            $return['append_field'] = array('*');
            $return['order'] = array("$alias.id"=>"desc");
        }
        return $return;
    }
}