CinemaSchedRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 crmeb\services\ApiResponseService;
  11. use think\facade\Db;
  12. /**
  13. * Class CinemaSchedRepository
  14. * @package app\common\repositories\movie
  15. * @author xaboy
  16. * @mixin CinemaSched
  17. */
  18. class CinemaSchedRepository extends BaseRepository
  19. {
  20. protected $dao;
  21. /**
  22. * ProductRepository constructor.
  23. * @param CinemaSched $dao
  24. */
  25. public function __construct(CinemaSched $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * @param $film_sign
  31. * @return array
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function getFilmSchedDate($film_sign)
  37. {
  38. $field = 'show_date';
  39. $where = ['film_sign' => $film_sign, 'date' => strtotime(date('Y-m-d'))];
  40. $show_date = $this->dao->search(null, $where)->field([$field])->order('show_date asc')->group('show_date')->select()->toArray();
  41. foreach ($show_date as $key => $value) {
  42. $show_date[$key]['date'] = date('m-d', $value['show_date']);
  43. }
  44. return $show_date;
  45. }
  46. /**
  47. * @param $page
  48. * @param $limit
  49. * @param $longitude
  50. * @param $latitude
  51. * @param $show_date
  52. * @param $with
  53. * @param $order
  54. * @return array
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $city_sign)
  60. {
  61. /** @var MovieRepository $movieRepository */
  62. $movieRepository = app()->make(MovieRepository::class);
  63. $result = $movieRepository->getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $city_sign);
  64. $cinema_sched = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  65. ? $result['data']['list'] : [];
  66. $count = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  67. ? $result['data']['paging']['total'] : 0;
  68. $pages = (isset($result['code']) && ($result['code'] == ApiResponseService::DEFAULT_SUCCESS_CODE))
  69. ? $result['data']['paging']['pages'] : 0;
  70. return compact('cinema_sched', 'count', 'pages');
  71. }
  72. /**
  73. * @param $page
  74. * @param $limit
  75. * @param $longitude
  76. * @param $latitude
  77. * @param $show_date
  78. * @param $with
  79. * @param $order
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getFilmSchedBak($page, $limit, $film_sign, $longitude, $latitude, $show_date, $show_time, $with = [], $order = 1)
  86. {
  87. $field = 'sched_sign,cinema_sign,cinema_name,address,show_date,price,longitude,latitude';
  88. $where = ['film_sign' => $film_sign, 'show_date' => $show_date];
  89. if ($show_date == strtotime(date('Y-m-d'))) $where['show_time'] = $show_time;
  90. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  91. $movie_commission_rate_config = !empty($movie_commission_rate_config) ? json_decode($movie_commission_rate_config['value'], true) : [];
  92. $cinema_sched = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  93. $query->with($with);
  94. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  95. $query->field([$field, $this->distance($latitude, $longitude)]);
  96. })->when($page && $limit, function ($query) use ($page, $limit) {
  97. $query->page($page, $limit);
  98. })->when(!is_null($order), function ($query) use ($order) {
  99. if ($order == 1) {//距离最近
  100. $query->order('distance asc');
  101. } else {//播放日期
  102. $query->order('show_time asc');
  103. }
  104. })->field([$field])->group('cinema_sign')->select()->toArray();
  105. foreach ($cinema_sched as $key => $value) {
  106. $cinema_sched[$key]['distance'] = isset($value['distance']) ? round($value['distance'] / 1000, 2) : 0.00;
  107. $cinema_sched[$key]['show_date'] = date('m-d', $value['show_date']);
  108. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['price'], $movie_commission_rate_config);
  109. $cinema_sched[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  110. }
  111. $count = $this->dao->search(null, $where)->when($with, function ($query) use ($with) {
  112. $query->with($with);
  113. })->when($latitude && $longitude, function ($query) use ($field, $longitude, $latitude) {
  114. $query->field([$field, $this->distance($latitude, $longitude)]);
  115. })->group('cinema_sign')->count();
  116. return compact('cinema_sched', 'count');
  117. }
  118. /**
  119. * 获取影片场次列表
  120. *
  121. * @param $cinema_sign
  122. * @param $film_sign
  123. * @param $show_date
  124. * @param $show_time
  125. * @return array
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\DbException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. */
  130. public function getCinemaFilmSchedNew($film_sign, $cinema_sign)
  131. {
  132. /** @var FilmRepository $filmRepository */
  133. $filmRepository = app()->make(FilmRepository::class);
  134. $filminfo = $filmRepository->getFilmInfo($film_sign);
  135. if (empty($filminfo)) throw new \Exception('影片不存在');
  136. /** @var CinemaRepository $cinemaRepository */
  137. $cinemaRepository = app()->make(CinemaRepository::class);
  138. $cinemainfo = $cinemaRepository->getCinemaInfo($cinema_sign);
  139. // 获取排片场次
  140. /** @var MovieRepository $movieRepository */
  141. $movieRepository = app()->make(MovieRepository::class);
  142. $cinema_sched = $movieRepository->getCinemaSched($film_sign, $cinema_sign);
  143. // 排片日期
  144. $show_date = [];
  145. $show_date_tmp = array_keys($cinema_sched);
  146. foreach ($show_date_tmp as $sdKey => $sdValue) {
  147. $show_date[$sdKey] = [
  148. 'date_timestamp' => strtotime(date('Y') . '-' . $sdValue),
  149. 'date' => $sdValue,
  150. ];
  151. }
  152. // 场次列表
  153. foreach ($cinema_sched as $date => &$cinema_schedValue) {
  154. foreach ($cinema_schedValue as $key => $value) {
  155. $cinema_schedValue[$key]['cinema_sign'] = $cinemainfo->cinema_sign;
  156. $cinema_schedValue[$key]['cinema_name'] = $cinemainfo->cinema_name;
  157. $cinema_schedValue[$key]['end_time'] = date('H:i', (strtotime($value['show_time']) + $filminfo['duration'] * 60));
  158. $cinema_schedValue[$key]['film'] = [
  159. 'film_sign' => $filminfo->film_sign,
  160. 'publish_date' => date('Y-m-d', $filminfo->publish_date),
  161. 'language' => $filminfo->language,
  162. 'type' => $filminfo->type,
  163. 'duration' => $filminfo->duration,
  164. 'version' => $filminfo->version
  165. ];
  166. unset($cinema_schedValue[$key]['channel_price']);
  167. unset($cinema_schedValue[$key]['cost_price']);
  168. }
  169. }
  170. return compact('show_date', 'cinema_sched');
  171. }
  172. /**
  173. * @param $page
  174. * @param $limit
  175. * @param $cinema_sign
  176. * @param $film_sign
  177. * @param $show_date
  178. * @param $show_time
  179. * @return array
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function getCinemaFilmSched($page, $limit, $cinema_sign, $film_sign, $show_date, $show_time)
  185. {
  186. $field = 'relation_id,sched_sign,film_sign,film_name,show_date,show_time,cinema_sign,cinema_name,hall_sign,hall_name,price';
  187. $where = ['cinema_sign' => $cinema_sign, 'film_sign' => $film_sign, 'show_date' => $show_date];
  188. if ($show_date == strtotime(date('Y-m-d'))) $where['show_time'] = $show_time;
  189. $cinema_sched = $this->dao->search(null, $where)
  190. ->when($page && $limit, function ($query) use ($page, $limit) {
  191. $query->page($page, $limit);
  192. })->order('show_time asc')->field([$field])->select()->toArray();
  193. $filmList = [];
  194. $film_sign_list = array_column($cinema_sched, 'film_sign');
  195. if (!empty($film_sign_list)) {
  196. $tmpFilmList = Db::name('movie_film')->whereIn('film_sign', $film_sign_list)->field('film_sign,publish_date,language,type,duration,version')->select()->toArray();
  197. foreach ($tmpFilmList as $fKey => $fValue) {
  198. $fValue['publish_date'] = date('Y-m-d', $fValue['publish_date']);
  199. $filmList[$fValue['film_sign']] = $fValue;
  200. }
  201. }
  202. $movie_commission_rate_config = Db::name('system_config_value')->where('config_key', 'movie_commission_rate_config')->find();
  203. $movie_commission_rate_config = !empty($movie_commission_rate_config) ? json_decode($movie_commission_rate_config['value'], true) : [];
  204. foreach ($cinema_sched as $key => $value) {
  205. $pcYanglaojinGongxian = MovieRepository::getPvYanglaojinScore($value['price'], $movie_commission_rate_config);
  206. $cinema_sched[$key]['price'] = $pcYanglaojinGongxian['cost_price'];
  207. $cinema_sched[$key]['yanglaojin'] = $pcYanglaojinGongxian['yanglaojin'];
  208. $cinema_sched[$key]['pv'] = $pcYanglaojinGongxian['pv'];
  209. $cinema_sched[$key]['score'] = $pcYanglaojinGongxian['score'];
  210. $cinema_sched[$key]['gongxian'] = $pcYanglaojinGongxian['gongxian'];
  211. $film = $filmList[$value['film_sign']] ?? [];
  212. $cinema_sched[$key]['end_time'] = !empty($film) ? date('H:i', (strtotime($value['show_time']) + $film['duration'] * 60)) : '';
  213. $cinema_sched[$key]['film'] = $film;
  214. }
  215. $count = $this->dao->search(null, $where)->count();
  216. return compact('cinema_sched', 'count');
  217. }
  218. /**
  219. * 经纬度排序计算
  220. * @param string $latitude
  221. * @param string $longitude
  222. * @return string
  223. */
  224. private function distance(string $latitude, string $longitude)
  225. {
  226. return "(round(6378137 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2)
  227. + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
  228. }
  229. /**
  230. * 获取影片场次详情
  231. *
  232. * @param $relation_id
  233. * @param $sched_sign
  234. * @return CinemaSched|array|\think\Model|null
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\DbException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. */
  239. public function getCinemaSchedInfo($relation_id, $sched_sign)
  240. {
  241. return $this->dao->search(null, ['relation_id' => $relation_id, 'sched_sign' => $sched_sign])->find();
  242. }
  243. }