<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Config;
use app\model\HousingEstate;
use app\model\Tuanzhang;
use think\facade\Db;

class HousingestateController extends Base
{

    public function list()
    {
        $keyword = input('post.keyword', '', 'serach_in');
        $config = Config::getconfig();

        $latitude =  $this->userInfo['cityinfo']['latitude'];
        $longitude = $this->userInfo['cityinfo']['longitude'];

        if(empty($latitude)){
            $latitude = input('post.latitude', '', 'serach_in'); //纬度信息
        }
        if(empty($longitude)){
            $longitude = input('post.longitude', '', 'serach_in'); //经度信息
        }
        
        
        if (!empty($longitude) && !empty($latitude)) {
            $sql = "select * from (select id,weid,sort,tzid,title,city_name,district_name,area_name,house_number,image,status, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from " . (new HousingEstate)->getTable() . " order by distance desc ) as a where status=1 ";
        } else {
            $sql = "select * from " . (new HousingEstate)->getTable() . " where status=1 ";
        }

        $sql .=  " and `weid` = " . weid();

        if (trim($keyword)) {
            $sql .=  " and `title` LIKE '%" . $keyword . "%'";
        }

        $data = Db::query($sql);

        foreach ($data as &$vo) {
            if (!empty($vo['tzid'])) {
                $Tuanzhang = Tuanzhang::find($vo['tzid']);
                if (!empty($Tuanzhang)) {
                    $vo['tz_touxiang'] = toimg($Tuanzhang['touxiang']);
                    $vo['community_title'] = $Tuanzhang['community_title'];
                    $vo['tz_title'] = $Tuanzhang['title'];
                    $vo['tz_tel'] = $Tuanzhang['tel'];
                }
            }

            $vo['image'] = toimg($vo['image']);
            if($vo['distance']){
                $vo['distance'] = round(($vo['distance']) / 1000, 1);
            }else{
                $vo['distance'] = 0.5;
            }
        }
        $res['data'] = $data;
        return $this->json(['data' => $res]);
    }
}
