CinemaRepository.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @package merchant
  4. * @Author: Qinii
  5. * @Date: 2020/5/8
  6. */
  7. namespace app\common\repositories\movie;
  8. use app\common\dao\movie\order\MovieOrderDao;
  9. use app\common\model\movie\cinema\Cinema;
  10. use app\common\repositories\BaseRepository;
  11. use think\facade\Db;
  12. /**
  13. * Class CinemaRepository
  14. * @package app\common\repositories\movie
  15. * @author xaboy
  16. * @mixin MovieOrderDao
  17. */
  18. class CinemaRepository extends BaseRepository
  19. {
  20. protected $dao;
  21. /**
  22. * ProductRepository constructor.
  23. * @param Cinema $dao
  24. */
  25. public function __construct(Cinema $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * @param $page
  31. * @param $limit
  32. * @param $longitude
  33. * @param $latitude
  34. * @param $where
  35. * @param $with
  36. * @param $order
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function getLocalCinema($page, $limit, $longitude, $latitude, $where = [], $with = [], $order = 1)
  43. {
  44. $field = 'cinema_sign,cinema_name,address,tel,city_sign,city_name,area_sign,area_name';
  45. $result = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  46. $query->with($with);
  47. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  48. $query->field([$field, $this->distance($latitude, $longitude)]);
  49. })->when($page && $limit, function ($query) use ($page, $limit) {
  50. $query->page($page, $limit);
  51. })->when(!is_null($order), function ($query) use ($order) {
  52. if ($order == 1) {//距离最近
  53. $query->order('distance asc');
  54. } else {//最新需求
  55. $query->order('create_time desc');
  56. }
  57. })->field([$field])->select()->toArray();
  58. foreach ($result as $key => $value) {
  59. $result[$key]['distance'] = round($value['distance'] / 1000, 2);
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 经纬度排序计算
  65. * @param string $latitude
  66. * @param string $longitude
  67. * @return string
  68. */
  69. private function distance(string $latitude, string $longitude)
  70. {
  71. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  72. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  73. }
  74. }