CinemaSchedRepository.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\model\movie\cinema\CinemaSched;
  9. use app\common\repositories\BaseRepository;
  10. use think\facade\Db;
  11. /**
  12. * Class CinemaSchedRepository
  13. * @package app\common\repositories\movie
  14. * @author xaboy
  15. * @mixin CinemaSched
  16. */
  17. class CinemaSchedRepository extends BaseRepository
  18. {
  19. protected $dao;
  20. /**
  21. * ProductRepository constructor.
  22. * @param CinemaSched $dao
  23. */
  24. public function __construct(CinemaSched $dao)
  25. {
  26. $this->dao = $dao;
  27. }
  28. /**
  29. * @param $film_sign
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function getFilmSchedDate($film_sign)
  36. {
  37. $field = 'show_date';
  38. $where = ['film_sign' => $film_sign];
  39. $show_date = $this->dao->search(null, $where)->field([$field])->order('show_date asc')->group('show_date')->select()->toArray();
  40. foreach ($show_date as $key => $value) {
  41. $show_date[$key]['date'] = date('m-d', $value['show_date']);
  42. }
  43. return compact('show_date');
  44. }
  45. /**
  46. * @param $page
  47. * @param $limit
  48. * @param $longitude
  49. * @param $latitude
  50. * @param $show_date
  51. * @param $with
  52. * @param $order
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $show_date, $with = [], $order = 1)
  59. {
  60. $field = 'cinema_sign,cinema_name,address,cost_price,longitude,latitude';
  61. $where = ['film_sign' => $film_sign, 'show_date' => $show_date];
  62. $cinema_sched = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  63. $query->with($with);
  64. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  65. $query->field([$field, $this->distance($latitude, $longitude)]);
  66. })->when($page && $limit, function ($query) use ($page, $limit) {
  67. $query->page($page, $limit);
  68. })->when(!is_null($order), function ($query) use ($order) {
  69. if ($order == 1) {//距离最近
  70. $query->order('distance asc');
  71. } else {//播放日期
  72. $query->order('show_date desc');
  73. }
  74. })->field([$field])->select()->toArray();
  75. foreach ($cinema_sched as $key => $value) {
  76. $cinema_sched[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
  77. }
  78. $count = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  79. $query->with($with);
  80. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  81. $query->field([$field, $this->distance($latitude, $longitude)]);
  82. })->count();
  83. return compact('cinema_sched', 'count');
  84. }
  85. /**
  86. * @param $page
  87. * @param $limit
  88. * @param $cinema_sign
  89. * @param $film_sign
  90. * @param $show_date
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getCinemaFilmSched($page, $limit, $cinema_sign, $film_sign, $show_date)
  97. {
  98. $field = 'relation_id,sched_sign,film_sign,film_name,show_date,show_time,cinema_sign,cinema_name,hall_sign,hall_name,cost_price';
  99. $where = ['cinema_sign' => $cinema_sign, 'film_sign' => $film_sign, 'show_date' => $show_date];
  100. $cinema_sched = $this->dao->search(null, $where)
  101. ->when($page && $limit, function ($query) use ($page, $limit) {
  102. $query->page($page, $limit);
  103. })->order('show_time asc')->field([$field])->select()->toArray();
  104. foreach ($cinema_sched as $key => $value) {
  105. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['cost_price']);
  106. $cinema_sched[$key]['cost_price'] = $pcYanglaojinGongxian['cost_price'];
  107. $cinema_sched[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  108. $cinema_sched[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  109. $cinema_sched[$key]['score'] = $pcYanglaojinGongxian['score'];
  110. $cinema_sched[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  111. $cinema_sched[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
  112. }
  113. $count = $this->dao->search(null, $where)->count();
  114. return compact('cinema_sched', 'count');
  115. }
  116. /**
  117. * 经纬度排序计算
  118. * @param string $latitude
  119. * @param string $longitude
  120. * @return string
  121. */
  122. private function distance(string $latitude, string $longitude)
  123. {
  124. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  125. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  126. }
  127. }