CinemaSchedRepository.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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, 'date' => strtotime(date('Y-m-d'))];
  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 $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, $show_time, $with = [], $order = 1)
  59. {
  60. $field = 'sched_sign,cinema_sign,cinema_name,address,show_date,price,longitude,latitude';
  61. $where = ['film_sign' => $film_sign, 'show_date' => $show_date];
  62. if ($show_date == strtotime(date('Y-m-d'))) $where['show_time'] = $show_time;
  63. $cinema_sched = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  64. $query->with($with);
  65. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  66. $query->field([$field, $this->distance($latitude, $longitude)]);
  67. })->when($page && $limit, function ($query) use ($page, $limit) {
  68. $query->page($page, $limit);
  69. })->when(!is_null($order), function ($query) use ($order) {
  70. if ($order == 1) {//距离最近
  71. $query->order('distance asc');
  72. } else {//播放日期
  73. $query->order('show_time asc');
  74. }
  75. })->field([$field])->group('cinema_sign')->select()->toArray();
  76. foreach ($cinema_sched as $key => $value) {
  77. $cinema_sched[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
  78. $cinema_sched[$key]['show_date'] = date('m-d', $value['show_date']);
  79. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['price']);
  80. $cinema_sched[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  81. }
  82. $count = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  83. $query->with($with);
  84. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  85. $query->field([$field, $this->distance($latitude, $longitude)]);
  86. })->group('cinema_sign')->count();
  87. return compact('cinema_sched', 'count');
  88. }
  89. /**
  90. * @param $page
  91. * @param $limit
  92. * @param $cinema_sign
  93. * @param $film_sign
  94. * @param $show_date
  95. * @param $show_time
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function getCinemaFilmSched($page, $limit, $cinema_sign, $film_sign, $show_date, $show_time)
  102. {
  103. $field = 'relation_id,sched_sign,film_sign,film_name,show_date,show_time,cinema_sign,cinema_name,hall_sign,hall_name,price';
  104. $where = ['cinema_sign' => $cinema_sign, 'film_sign' => $film_sign, 'show_date' => $show_date];
  105. if ($show_date == strtotime(date('Y-m-d'))) $where['show_time'] = $show_time;
  106. $cinema_sched = $this->dao->search(null, $where)
  107. ->when($page && $limit, function ($query) use ($page, $limit) {
  108. $query->page($page, $limit);
  109. })->order('show_time asc')->field([$field])->select()->toArray();
  110. $filmList = [];
  111. $film_sign_list = array_column($cinema_sched, 'film_sign');
  112. if (!empty($film_sign_list)) {
  113. $tmpFilmList = Db::name('movie_film')->whereIn('film_sign', $film_sign_list)->field('film_sign,publish_date,language,type,duration,version')->select()->toArray();
  114. foreach ($tmpFilmList as $fKey => $fValue) {
  115. $fValue['publish_date'] = date('Y-m-d', $fValue['publish_date']);
  116. $filmList[$fValue['film_sign']] = $fValue;
  117. }
  118. }
  119. foreach ($cinema_sched as $key => $value) {
  120. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['price']);
  121. $cinema_sched[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  122. $cinema_sched[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  123. $cinema_sched[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  124. $cinema_sched[$key]['score'] = $pcYanglaojinGongxian['score'];
  125. $cinema_sched[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  126. $film = $filmList[$value['film_sign']] ?? [];
  127. $cinema_sched[$key]['end_time'] = !empty($film) ? date('H:i', (strtotime($value['show_time']) + $film['duration'] * 60)) : '';
  128. $cinema_sched[$key]['film'] = $film;
  129. }
  130. $count = $this->dao->search(null, $where)->count();
  131. return compact('cinema_sched', 'count');
  132. }
  133. /**
  134. * 经纬度排序计算
  135. * @param string $latitude
  136. * @param string $longitude
  137. * @return string
  138. */
  139. private function distance(string $latitude, string $longitude)
  140. {
  141. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  142. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  143. }
  144. /**
  145. * 获取影片场次详情
  146. *
  147. * @param $relation_id
  148. * @param $sched_sign
  149. * @return CinemaSched|array|\think\Model|null
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getCinemaSchedInfo($relation_id, $sched_sign)
  155. {
  156. return $this->dao->search(null, ['relation_id' => $relation_id, 'sched_sign' => $sched_sign])->find();
  157. }
  158. }