Store.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\api\model;
  3. use think\Model;
  4. class Store extends BaseModel
  5. {
  6. // 表名
  7. protected $name = 'store';
  8. static function SWKey($s_id)
  9. {
  10. return "store:w:update:{$s_id}";
  11. }
  12. public function fetch($city_code, $county_code, array $lng_lat, $search_text = null, $hot = false, $distance = false, $page = 1, $size = 10)
  13. {
  14. $where = [
  15. // 'city_code' => $city_code,
  16. 'status' => \E_BASE_STATUS::Normal,
  17. ];
  18. if ($county_code) {
  19. $where["county_code"] = $county_code;
  20. }
  21. $subQuery = $this->field("*,round(st_distance_sphere (point($lng_lat[0],$lng_lat[1]),point ( `lng`, `lat` ))) distance")
  22. ->where($where)
  23. ->where("lng IS NOT NULL")
  24. ->where("lat IS NOT NULL");
  25. if ($search_text)
  26. $subQuery->where("name", "like", "%$search_text%");
  27. $query = $this->table($subQuery->buildSql() . ' distance')
  28. ->where($where)
  29. ->where("lng IS NOT NULL")
  30. ->where("lat IS NOT NULL");
  31. if ($search_text)
  32. $query->where("name", "like", "%$search_text%");
  33. if ($hot)
  34. $query->where('hot', 1);
  35. return $query
  36. ->order('distance', 'ASC')
  37. ->page($page)
  38. ->paginate($size);
  39. }
  40. }