CinemaController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\admin\controller\life;
  3. use app\admin\validate\life\CinemaValidate;
  4. use app\controller\Curd;
  5. use app\model\Cinema as CinemaModel;
  6. use app\model\CinemaIlk;
  7. use app\model\CinemaPerformers;
  8. use support\exception\BusinessException;
  9. use support\Request;
  10. use support\Response;
  11. class CinemaController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new CinemaModel();
  16. $this->validate = true;
  17. $this->validateClass = new CinemaValidate();
  18. }
  19. /**
  20. * @Desc 影片列表
  21. * @Author Gorden
  22. * @Date 2024/2/26 13:42
  23. *
  24. * @param Request $request
  25. * @return Response
  26. * @throws \support\exception\BusinessException
  27. */
  28. public function select(Request $request): Response
  29. {
  30. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  31. $where['cinema_is_del'] = 0;
  32. $order = $request->get('order', 'desc');
  33. $field = $field ?? 'cinema_addTime';
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. /**
  38. * @Desc 查询后数据处理
  39. * @Author Gorden
  40. * @Date 2024/3/21 14:13
  41. *
  42. * @param $items
  43. * @return void
  44. */
  45. public function afterQuery($items)
  46. {
  47. foreach ($items as &$item) {
  48. $item->cinema_img = !empty($item->cinema_img) ? getenv('STORAGE_DOMAIN') . $item->cinema_img : '';
  49. $item->cinema_video = !empty($item->cinema_video) ? getenv('STORAGE_DOMAIN') . $item->cinema_video : '';
  50. $item->cinema_time = !empty($item->cinema_time) ? date('Y-m-d H:i:s', strtotime($item->cinema_time)) : '';
  51. $item->cinema_label = !empty($item->cinema_label) ? explode(',', $item->cinema_label) : [];
  52. $item->cinema_director = !empty($item->cinema_director) ? array_map('intval', explode(',', $item->cinema_director)) : '';
  53. $item->cinema_star = !empty($item->cinema_star) ? array_map('intval', explode(',', $item->cinema_star)) : '';
  54. $item->cinema_performers = !empty($item->cinema_performers) ? array_map('intval', explode(',', $item->cinema_performers)) : '';
  55. $item->cinema_mold = !empty($item->cinema_mold) ? array_map('intval', explode(',', $item->cinema_mold)) : '';
  56. if (!empty($item->cinema_director)) {
  57. $cinemaDirector = CinemaPerformers::whereIn('performers_id', $item->cinema_director)->pluck('performers_name');
  58. if ($cinemaDirector) {
  59. $cinemaDirector = $cinemaDirector->toArray();
  60. $item->cinemaDirector = implode(',', $cinemaDirector);
  61. }
  62. }
  63. if (!empty($item->cinema_star)) {
  64. $cinemaStar = CinemaPerformers::whereIn('performers_id', $item->cinema_star)->pluck('performers_name');
  65. if ($cinemaStar) {
  66. $cinemaStar = $cinemaStar->toArray();
  67. $item->cinemaStar = implode(",", $cinemaStar);
  68. }
  69. }
  70. if (!empty($item->cinema_performers)) {
  71. $cinemaPerformers = CinemaPerformers::whereIn('performers_id', $item->cinema_performers)->pluck('performers_name');
  72. if ($cinemaPerformers) {
  73. $cinemaPerformers = $cinemaPerformers->toArray();
  74. $item->cinemaPerformers = implode(',', $cinemaPerformers);
  75. }
  76. }
  77. if (!empty($item->cinema_mold)) {
  78. $cinemaIlk = CinemaIlk::whereIn('ilk_id', $item->cinema_mold)->pluck('ilk_name');
  79. if ($cinemaIlk) {
  80. $cinemaIlk = $cinemaIlk->toArray();
  81. $item->cinemaIlk = implode(',', $cinemaIlk);
  82. }
  83. }
  84. }
  85. return $items;
  86. }
  87. /**
  88. * @Desc 新增数据处理
  89. * @Author Gorden
  90. * @Date 2024/3/21 14:11
  91. *
  92. * @param Request $request
  93. * @return array
  94. * @throws \support\exception\BusinessException
  95. */
  96. protected function insertInput(Request $request): array
  97. {
  98. $data = $this->inputFilter($request->post());
  99. if (!empty($data['cinema_img'])) {
  100. $data['cinema_img'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_img']);
  101. }
  102. if (!empty($data['cinema_video'])) {
  103. $data['cinema_video'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_video']);
  104. }
  105. return $data;
  106. }
  107. protected function updateInput(Request $request): array
  108. {
  109. $primary_key = $this->model->getKeyName();
  110. $id = $request->post($primary_key);
  111. $data = $this->inputFilter($request->post());
  112. $data['cinema_img'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_img']);
  113. $data['cinema_video'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_video']);
  114. $model = $this->model->find($id);
  115. if (!$model) {
  116. throw new BusinessException('记录不存在', 2);
  117. }
  118. unset($data[$primary_key]);
  119. return [$id, $data];
  120. }
  121. /**
  122. * @Desc 影片软删除
  123. * @Author Gorden
  124. * @Date 2024/2/26 13:43
  125. *
  126. * @param Request $request
  127. * @return Response
  128. * @throws \support\exception\BusinessException
  129. */
  130. public function delete(Request $request): Response
  131. {
  132. $ids = $this->deleteInput($request);
  133. $this->doSoftDelete($ids, ['cinema_is_del' => 1]);
  134. return json_success('success');
  135. }
  136. }