CinemaRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Cinema
  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,longitude,latitude';
  45. $cinema_list = $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 ($cinema_list as $key => $value) {
  59. $cinema_list[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
  60. }
  61. $count = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  62. $query->with($with);
  63. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  64. $query->field([$field, $this->distance($latitude, $longitude)]);
  65. })->count();
  66. return compact('cinema_list', 'count');
  67. }
  68. /**
  69. * 经纬度排序计算
  70. * @param string $latitude
  71. * @param string $longitude
  72. * @return string
  73. */
  74. private function distance(string $latitude, string $longitude)
  75. {
  76. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  77. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  78. }
  79. /**
  80. * 获取影院信息
  81. *
  82. * @param $cinema_sign
  83. * @return Cinema|array|\think\Model|null
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function getCinemaInfo($cinema_sign)
  89. {
  90. return $this->dao->search(null, ['cinema_sign' => $cinema_sign])->find();
  91. }
  92. }