CinemaSchedCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\command;
  3. use app\common\model\user\User;
  4. use app\common\repositories\movie\CinemaRepository;
  5. use app\common\repositories\movie\CinemaSchedRepository;
  6. use app\common\repositories\movie\FilmRepository;
  7. use app\common\repositories\movie\MovieRepository;
  8. use crmeb\services\ApiResponseService;
  9. use think\console\Command;
  10. use think\console\Input;
  11. use think\console\Output;
  12. use think\facade\Db;
  13. class CinemaSchedCommand extends Command
  14. {
  15. private $page = 1;
  16. private $limit = 1000000;
  17. protected function configure()
  18. {
  19. // 指令配置
  20. $this->setName('cinema_film_sched')->setDescription('电影-影院场次列表');
  21. }
  22. protected function execute(Input $input, Output $output)
  23. {
  24. $date = strtotime(date('Y-m-d'));
  25. try {
  26. /** @var CinemaRepository $cinema */
  27. $cinema = app()->make(CinemaRepository::class);
  28. // 获取影院列表
  29. $result = $cinema->getLocalCinema($this->page, $this->limit, '', '', [], [], 0);
  30. /** @var FilmRepository $filmRepository */
  31. $filmRepository = app()->make(FilmRepository::class);
  32. // 获取{2个月内}已上映影片列表
  33. $filmResult = $filmRepository->getFilmByStartEndDate($this->page, $this->limit, strtotime('-2 month', $date), $date);
  34. foreach ($filmResult as $key => $film) {
  35. foreach ($result['cinema_list'] as $subKey => $cinema) {
  36. // 是否未排片 @todo 影院未排片,不请求第三方接口获取排片场次
  37. if (!($this->isUnscheduledFilm($film['film_sign'], $cinema['cinema_sign']))) {
  38. $this->doCinemaSched($film, $cinema);
  39. }
  40. }
  41. }
  42. } catch (\Exception $exception) {
  43. var_dump($exception->getMessage(), $exception->getFile(), $exception->getLine());
  44. }
  45. }
  46. /**
  47. * 是否未排片
  48. *
  49. * @param $film_sign
  50. * @param $cinema_sign
  51. * @return bool
  52. */
  53. private function isUnscheduledFilm($film_sign = '', $cinema_sign = '')
  54. {
  55. $count = Db::name('movie_cinema_unsched')->where(['film_sign' => $film_sign, 'cinema_sign' => $cinema_sign])->count();
  56. return $count > 0;
  57. }
  58. /**
  59. * 电影-获取影院场次列表
  60. * @return void
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. private function doCinemaSched($film, $cinema)
  66. {
  67. /** @var MovieRepository $cinema_filmRepository */
  68. $cinema_filmRepository = app()->make(MovieRepository::class);
  69. // 获取排片场次
  70. $cinema_sched = $cinema_filmRepository->getCinemaSched($film['film_sign'], $cinema['cinema_sign']);
  71. if (!empty($cinema_sched)) {
  72. // 写入排片场次数据
  73. $this->createCinemaSched($cinema_sched, $cinema);
  74. } else {
  75. // 写入未排片影院
  76. $this->createUnschedCinema($film, $cinema);
  77. }
  78. }
  79. /**
  80. * 写入排片场次数据
  81. *
  82. * @param $list
  83. * @param $cinema
  84. * @return void
  85. * @throws \think\db\exception\DbException
  86. */
  87. private function createCinemaSched($list, $cinema)
  88. {
  89. foreach ($list as $key => $sched) {
  90. foreach ($sched as $subKey => $value) {
  91. $params = [
  92. 'relation_id' => $value['relation_id'],
  93. 'sched_sign' => $value['sched_sign'],
  94. 'film_sign' => $value['film_sign'],
  95. 'film_name' => $value['film_name'],
  96. 'show_date' => strtotime(date('Y') . '-' . $value['show_date']),
  97. 'show_time' => trim($value['show_time']),
  98. 'cinema_sign' => $cinema['cinema_sign'],
  99. 'cinema_name' => $cinema['cinema_name'],
  100. 'address' => $cinema['address'],
  101. 'longitude' => $cinema['longitude'],
  102. 'latitude' => $cinema['latitude'],
  103. 'hall_sign' => $value['hall_sign'],
  104. 'hall_name' => $value['hall_name'],
  105. 'stop_sell_time' => strtotime($value['stop_sell_time']),
  106. 'price' => $value['price'], // 销售价
  107. 'cost_price' => $value['cost_price'], // 原价
  108. 'channel_price' => json_encode($value['channel_price'], JSON_FORCE_OBJECT)
  109. ];
  110. $where = [
  111. 'relation_id' => $value['relation_id'], 'sched_sign' => $value['sched_sign'], 'film_sign' => $value['film_sign'], 'cinema_sign' => $params['cinema_sign']
  112. ];
  113. $model = Db::name('movie_cinema_sched')->where($where)->find();
  114. if (empty($model)) {
  115. $params['create_time'] = time();
  116. Db::name('movie_cinema_sched')->insert($params);
  117. } else {
  118. $params['update_time'] = time();
  119. Db::name('movie_cinema_sched')->where('id', $model['id'])->update($params);
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * 记录未排片影院
  126. *
  127. * @param $film
  128. * @param $cinema
  129. * @return void
  130. */
  131. private function createUnschedCinema($film, $cinema)
  132. {
  133. $params = [
  134. 'film_sign' => $film['film_sign'],
  135. 'film_name' => $film['film_name'],
  136. 'cinema_sign' => $cinema['cinema_sign'],
  137. 'cinema_name' => $cinema['cinema_name'],
  138. 'create_time' => time()
  139. ];
  140. Db::name('movie_cinema_unsched')->insert($params);
  141. }
  142. }