CinemaSchedRepository.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. * 获取影片场次列表
  91. *
  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 getCinemaFilmSchedNew($film_sign, $cinema_sign)
  102. {
  103. /** @var FilmRepository $filmRepository */
  104. $filmRepository = app()->make(FilmRepository::class);
  105. $filminfo = $filmRepository->getFilmInfo($film_sign);
  106. if (empty($filminfo)) throw new \Exception('影片不存在');
  107. /** @var CinemaRepository $cinemaRepository */
  108. $cinemaRepository = app()->make(CinemaRepository::class);
  109. $cinemainfo = $cinemaRepository->getCinemaInfo($cinema_sign);
  110. // 获取排片场次
  111. /** @var MovieRepository $movieRepository */
  112. $movieRepository = app()->make(MovieRepository::class);
  113. $cinema_sched = $movieRepository->getCinemaSched($film_sign, $cinema_sign);
  114. // 排片日期
  115. $show_date = [];
  116. $show_date_tmp = array_keys($cinema_sched);
  117. foreach ($show_date_tmp as $sdKey => $sdValue) {
  118. $show_date[$sdKey] = [
  119. 'date_timestamp' => strtotime(date('Y') . '-' . $sdValue),
  120. 'date' => $sdValue,
  121. ];
  122. }
  123. // 场次列表
  124. foreach ($cinema_sched as $date => &$cinema_schedValue) {
  125. foreach ($cinema_schedValue as $key => $value) {
  126. $cinema_schedValue[$key]['cinema_sign'] = $cinemainfo->cinema_sign;
  127. $cinema_schedValue[$key]['cinema_name'] = $cinemainfo->cinema_name;
  128. $cinema_schedValue[$key]['end_time'] = date('H:i', (strtotime($value['show_time']) + $filminfo['duration'] * 60));
  129. $cinema_schedValue[$key]['film'] = [
  130. 'film_sign' => $filminfo->film_sign,
  131. 'publish_date' => date('Y-m-d', $filminfo->publish_date),
  132. 'language' => $filminfo->language,
  133. 'type' => $filminfo->type,
  134. 'duration' => $filminfo->duration,
  135. 'version' => $filminfo->version
  136. ];
  137. unset($cinema_schedValue[$key]['channel_price']);
  138. unset($cinema_schedValue[$key]['cost_price']);
  139. }
  140. }
  141. return compact('show_date', 'cinema_sched');
  142. }
  143. /**
  144. * @param $page
  145. * @param $limit
  146. * @param $cinema_sign
  147. * @param $film_sign
  148. * @param $show_date
  149. * @param $show_time
  150. * @return array
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public function getCinemaFilmSched($page, $limit, $cinema_sign, $film_sign, $show_date, $show_time)
  156. {
  157. $field = 'relation_id,sched_sign,film_sign,film_name,show_date,show_time,cinema_sign,cinema_name,hall_sign,hall_name,price';
  158. $where = ['cinema_sign' => $cinema_sign, 'film_sign' => $film_sign, 'show_date' => $show_date];
  159. if ($show_date == strtotime(date('Y-m-d'))) $where['show_time'] = $show_time;
  160. $cinema_sched = $this->dao->search(null, $where)
  161. ->when($page && $limit, function ($query) use ($page, $limit) {
  162. $query->page($page, $limit);
  163. })->order('show_time asc')->field([$field])->select()->toArray();
  164. $filmList = [];
  165. $film_sign_list = array_column($cinema_sched, 'film_sign');
  166. if (!empty($film_sign_list)) {
  167. $tmpFilmList = Db::name('movie_film')->whereIn('film_sign', $film_sign_list)->field('film_sign,publish_date,language,type,duration,version')->select()->toArray();
  168. foreach ($tmpFilmList as $fKey => $fValue) {
  169. $fValue['publish_date'] = date('Y-m-d', $fValue['publish_date']);
  170. $filmList[$fValue['film_sign']] = $fValue;
  171. }
  172. }
  173. foreach ($cinema_sched as $key => $value) {
  174. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['price']);
  175. $cinema_sched[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  176. $cinema_sched[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  177. $cinema_sched[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  178. $cinema_sched[$key]['score'] = $pcYanglaojinGongxian['score'];
  179. $cinema_sched[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  180. $film = $filmList[$value['film_sign']] ?? [];
  181. $cinema_sched[$key]['end_time'] = !empty($film) ? date('H:i', (strtotime($value['show_time']) + $film['duration'] * 60)) : '';
  182. $cinema_sched[$key]['film'] = $film;
  183. }
  184. $count = $this->dao->search(null, $where)->count();
  185. return compact('cinema_sched', 'count');
  186. }
  187. /**
  188. * 经纬度排序计算
  189. * @param string $latitude
  190. * @param string $longitude
  191. * @return string
  192. */
  193. private function distance(string $latitude, string $longitude)
  194. {
  195. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  196. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  197. }
  198. /**
  199. * 获取影片场次详情
  200. *
  201. * @param $relation_id
  202. * @param $sched_sign
  203. * @return CinemaSched|array|\think\Model|null
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function getCinemaSchedInfo($relation_id, $sched_sign)
  209. {
  210. return $this->dao->search(null, ['relation_id' => $relation_id, 'sched_sign' => $sched_sign])->find();
  211. }
  212. }