<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class SearchController extends Controller
{
    /**
     * 搜索商品
     */
    public function nc()
    {
        $keyword = I("keyword");
        $goods = M("goods")
            ->join("g LEFT JOIN `am_goods_categories` gc ON g.`pcate`=gc.`id`")
            ->where("(g.`goods_name` LIKE '%{$keyword}%' OR gc.`name` LIKE '%{$keyword}%') AND g.`deleted`=-1")
            ->field("g.*")
            ->select();
        foreach ($goods as $kg => $vg) {
            $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vg['thumb']}";
        }
        unset($kg, $vg);
        if (!$goods) {
            $goods = array();
        }
        apiReturn($goods);
    }

    /**
     * 搜索食材
     */
    public function foods()
    {
        $keyword = I("keyword");
        $where = "`is_material`=2";
        if ($keyword) {
            $where .= " AND `goods_name` LIKE '%{$keyword}%'";
        }
        $list = M("goods")
            ->where($where)
            ->field("`id`,`thumb`,`goods_name` AS `name`,`goods_price` AS `price`")
            ->order("`sort` ASC,`id` DESC")
            ->select();
        foreach ($list as $key => $value) {
            $list[$key]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['thumb'];
        }
        if (!$list) {
            $list = array();
        }
        apiReturn($list);
    }

    /**
     * 搜索商家
     */
    public function stores()
    {
        $keyword = I("keyword");
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];
        $county_id = $_GET['county_id'];
        $bool_1 = $lat && $lat != "null" && $lat != "undefined";
        $bool_2 = $lng && $lng != "null" && $lng != "undefined";
        $bool_3 = $county_id && $county_id != "null" && $county_id != "undefined";
        if (!$bool_1) {
            $data = array(
                "status" => -1,
                "msg" => "纬度有误"
            );
        } elseif (!$bool_2) {
            $data = array(
                "status" => -1,
                "msg" => "经度有误"
            );
        } elseif (!$bool_3) {
            $data = array(
                "status" => -1,
                "msg" => "区ID有误"
            );
        } else {
            $data = array();
            // 区下所有商家
            $list = $this
                ->where("`county_id`={$county_id} AND `status`=1 AND `name` LIKE '%{$keyword}%'")
                ->select();
            // 抽取位于有效范围内的商家
            foreach ($list as $key => $value) {
                $distance = getDistance($_GET['lat'], $_GET['lng'], $value['lat'], $value['lng']);
                $value['logo'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['logo'];
                $row = M("setStore")->find();
                $radius = $row['radius'];    // 获取后台设置的有效半径
                if ($distance < $radius) {
                    $value['distance'] = $distance;
                    array_push(
                        $data,
                        $value
                    );
                }
            }
            // 距离排序
            usort($data, array($this, "sortByDistance"));
        }
        apiReturn($data);
    }

    /**
     * 距离排序规则
     * @param  array $a 上元素
     * @param  array $b 下元素
     * @return int      -1.小于 0.等于 1.大于
     */
    public function sortByDistance($a, $b)
    {
        if ($a['distance'] > $b['distance']) {
            return 1;
        } elseif ($a['distance'] == $b['distance']) {
            return 0;
        } elseif ($a['distance'] < $b['distance']) {
            return -1;
        }
    }
}