<?php
namespace app\admin\server\life;

use app\admin\model\LifeCinema;
use app\admin\model\SystemMenu;
use support\Redis;

class CinemaServer
{
    /**
     * Notes:获取影片列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getCinemaList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeCinema::getCinemaList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['cinema_director'] = explode(',',$v['cinema_director']);
                $list[$k]['cinema_star'] = explode(',',$v['cinema_star']);
                $list[$k]['cinema_performers'] = explode(',',$v['cinema_performers']);
                $list[$k]['cinema_create_time'] = date('Y-m-d H:i:s',$v['cinema_create_time']);
                if (!empty($v['cinema_update_time'])){
                    $list[$k]['cinema_update_time'] = date('Y-m-d H:i:s',$v['cinema_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改影片
     * @param string $cinema_name
     * @param int $cinema_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateCinema($cinema_id, $cinema_name, $cinema_video, $cinema_mold, $cinema_director, $cinema_star, $cinema_price, $cinema_introduction, $cinema_performers, $cinema_seat, $cinema_length, $cinema_time, $cinema_label, $cinema_img, $cinema_status, $cinema_week, $admin_id)
    {
        LifeCinema::affairBegin();
        try {
            $where = [];
            $where['cinema_id'] = $cinema_id;
            $data = [];
            $data['cinema_name'] = $cinema_name;
            $data['cinema_video'] = $cinema_video;
            $data['cinema_mold'] = implode(',',$cinema_mold);
            $data['cinema_director'] = implode(',',$cinema_director);
            $data['cinema_price'] = $cinema_price;
            $data['cinema_star'] = implode(',',$cinema_star);
            $data['cinema_introduction'] = $cinema_introduction;
            $data['cinema_performers'] = implode(',',$cinema_performers);
            $data['cinema_seat'] = $cinema_seat;
            $data['cinema_length'] = $cinema_length;
            $data['cinema_week'] = is_array($cinema_week) ? implode(',', $cinema_week) : $cinema_week;
            $data['cinema_time'] = is_array($cinema_time) ? implode('~', $cinema_time) : $cinema_time;
            $data['cinema_label'] = $cinema_label;
            $data['cinema_img'] = $cinema_img;
            $data['cinema_status'] = $cinema_status;
            $data['cinema_update_time'] = time();
            $result = LifeCinema::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦影院影片-编号: ' . $cinema_id;
                plog('life-cinema-update', '悦活-万悦影院-修改影片', $msg);
                LifeCinema::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinema::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除影片
     * @param int $cinema_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function delCinema($cinema_id,$admin_id)
    {
        LifeCinema::affairBegin();
        try {
            $where = [];
            $where['cinema_id'] = $cinema_id;
            $data['cinema_is_del'] = 1;
            $result = LifeCinema::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除万悦影院影片-编号: ' . $cinema_id;
                plog('life-cinema-delete', '悦活-万悦影院-删除影片', $msg);
                LifeCinema::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeCinema::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加影片
     * @param string $cinema_name
     * @param array $cinema_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertCinema($cinema_name, $cinema_video, $cinema_mold, $cinema_director, $cinema_star, $cinema_price, $cinema_introduction, $cinema_performers, $cinema_seat, $cinema_length, $cinema_time, $cinema_label, $cinema_img, $cinema_status, $cinema_week, $admin_id)
    {

        LifeCinema::affairBegin();
        try {
            $data = [];
            $data['cinema_name'] = $cinema_name;
            $data['cinema_video'] = $cinema_video;
            $data['cinema_mold'] = implode(',',$cinema_mold);
            $data['cinema_director'] = implode(',',$cinema_director);
            $data['cinema_price'] = $cinema_price;
            $data['cinema_star'] = implode(',',$cinema_star);
            $data['cinema_introduction'] = $cinema_introduction;
            $data['cinema_performers'] = implode(',',$cinema_performers);
            $data['cinema_seat'] = $cinema_seat;
            $data['cinema_length'] = $cinema_length;
            $data['cinema_week'] = is_array($cinema_week) ? implode(',', $cinema_week) : $cinema_week;
            $data['cinema_time'] = is_array($cinema_time) ? implode('~', $cinema_time) : $cinema_time;
            $data['cinema_label'] = $cinema_label;
            $data['cinema_img'] = $cinema_img;
            $data['cinema_status'] = $cinema_status;
            $data['cinema_create_time'] = time();
            $result =  LifeCinema::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦影院影片-编号: ' . $result;
                plog('life-cinema-create', '悦活-万悦影院-添加影片', $msg);
                LifeCinema::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinema::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询影片
     * @param int $cinema_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function cinemaInfo($cinema_id)
    {
        $where = [];
        $where['cinema_id'] = $cinema_id;
        $result = LifeCinema::where($where)->first();
        if (!empty($result)){
            $cinema_mold = explode(',',$result['cinema_mold']);
            if (!empty($cinema_mold)){
                for ($i=0;$i<count($cinema_mold);$i++){
                    $mold[] = (int)$cinema_mold[$i];
                }
                $result['cinema_mold'] = $mold;
            }
            $director = explode(',',$result['cinema_director']);
            if (!empty($director)){
                for ($s=0;$s<count($director);$s++){
                    $cinema_director[] = (int)$director[$s];
                }
                $result['cinema_director'] = $cinema_director;
            }
            $cinema_star = explode(',',$result['cinema_star']);
            if (!empty($cinema_star)){
                for ($k=0;$k<count($cinema_star);$k++){
                    $star[] = (int)$cinema_star[$k];
                }
                $result['cinema_star'] = $star;
            }
            $cinema_performers = explode(',',$result['cinema_performers']);
            if (!empty($cinema_performers)){

                for ($a=0;$a<count($cinema_performers);$a++){
                    $performers[] = (int)$cinema_performers[$a];
                }
                $result['cinema_performers'] = $performers;
            }
            $result['cinema_create_time'] = date('Y-m-d H:i:s',$result['cinema_create_time']);
            if (!empty($result['cinema_update_time'])){
                $result['cinema_update_time'] = date('Y-m-d H:i:s',$result['cinema_update_time']);
            }

        }
        return $result;
    }

    /**
     * Notes:修改影片状态
     * @param string $cinema_name
     * @param int $cinema_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($cinema_id, $cinema_status)
    {
        LifeCinema::affairBegin();
        try {
            $where = [];
            $where['cinema_id'] = $cinema_id;
            $data = [];
            $data['cinema_status'] = $cinema_status;
            $result = LifeCinema::where($where)->update($data);
            if ($result !== false){
                LifeCinema::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinema::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
