<?php

namespace app\admin\controller\life;

use app\admin\validate\life\CinemaValidate;
use app\controller\Curd;
use app\model\Cinema as CinemaModel;
use app\model\CinemaIlk;
use app\model\CinemaPerformers;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class CinemaController extends Curd
{

    public function __construct()
    {
        $this->model = new CinemaModel();
        $this->validate = true;
        $this->validateClass = new CinemaValidate();
    }

    /**
     * @Desc 影片列表
     * @Author Gorden
     * @Date 2024/2/26 13:42
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $where['cinema_is_del'] = 0;
        $order = $request->get('order', 'desc');
        $field = $field ?? 'cinema_addTime';
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * @Desc 查询后数据处理
     * @Author Gorden
     * @Date 2024/3/21 14:13
     *
     * @param $items
     * @return void
     */
    public function afterQuery($items)
    {
        foreach ($items as &$item) {
            $item->cinema_img = !empty($item->cinema_img) ? getenv('STORAGE_DOMAIN') . $item->cinema_img : '';
            $item->cinema_video = !empty($item->cinema_video) ? getenv('STORAGE_DOMAIN') . $item->cinema_video : '';
            $item->cinema_time = !empty($item->cinema_time) ? date('Y-m-d H:i:s', strtotime($item->cinema_time)) : '';
            $item->cinema_label = !empty($item->cinema_label) ? explode(',', $item->cinema_label) : [];
            $item->cinema_director = !empty($item->cinema_director) ? array_map('intval', explode(',', $item->cinema_director)) : '';
            $item->cinema_star = !empty($item->cinema_star) ? array_map('intval', explode(',', $item->cinema_star)) : '';
            $item->cinema_performers = !empty($item->cinema_performers) ? array_map('intval', explode(',', $item->cinema_performers)) : '';
            $item->cinema_mold = !empty($item->cinema_mold) ? array_map('intval', explode(',', $item->cinema_mold)) : '';
            if (!empty($item->cinema_director)) {
                $cinemaDirector = CinemaPerformers::whereIn('performers_id', $item->cinema_director)->pluck('performers_name');
                if ($cinemaDirector) {
                    $cinemaDirector = $cinemaDirector->toArray();
                    $item->cinemaDirector = implode(',', $cinemaDirector);
                }
            }
            if (!empty($item->cinema_star)) {
                $cinemaStar = CinemaPerformers::whereIn('performers_id', $item->cinema_star)->pluck('performers_name');
                if ($cinemaStar) {
                    $cinemaStar = $cinemaStar->toArray();
                    $item->cinemaStar = implode(",", $cinemaStar);
                }
            }
            if (!empty($item->cinema_performers)) {
                $cinemaPerformers = CinemaPerformers::whereIn('performers_id', $item->cinema_performers)->pluck('performers_name');
                if ($cinemaPerformers) {
                    $cinemaPerformers = $cinemaPerformers->toArray();
                    $item->cinemaPerformers = implode(',', $cinemaPerformers);
                }
            }
            if (!empty($item->cinema_mold)) {
                $cinemaIlk = CinemaIlk::whereIn('ilk_id', $item->cinema_mold)->pluck('ilk_name');
                if ($cinemaIlk) {
                    $cinemaIlk = $cinemaIlk->toArray();
                    $item->cinemaIlk = implode(',', $cinemaIlk);
                }
            }
        }

        return $items;
    }

    /**
     * @Desc 新增数据处理
     * @Author Gorden
     * @Date 2024/3/21 14:11
     *
     * @param Request $request
     * @return array
     * @throws \support\exception\BusinessException
     */
    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());
        if (!empty($data['cinema_img'])) {
            $data['cinema_img'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_img']);
        }
        if (!empty($data['cinema_video'])) {
            $data['cinema_video'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_video']);
        }

        return $data;
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        $data['cinema_img'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_img']);
        $data['cinema_video'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['cinema_video']);

        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 2);
        }

        unset($data[$primary_key]);
        return [$id, $data];
    }

    /**
     * @Desc 影片软删除
     * @Author Gorden
     * @Date 2024/2/26 13:43
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doSoftDelete($ids, ['cinema_is_del' => 1]);

        return json_success('success');
    }
}