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

use app\admin\model\LifeCinemaScheduling;

class SchedulingServer
{
    /**
     * Notes:获取排期列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getSchedulingList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeCinemaScheduling::getSchedulingList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['scheduling_start_time'] = date('Y-m-d H:i:s',$v['scheduling_start_time']);
                $list[$k]['scheduling_end_time'] = date('Y-m-d H:i:s',$v['scheduling_end_time']);
                $list[$k]['scheduling_create_time'] = date('Y-m-d H:i:s',$v['scheduling_create_time']);
                if (!empty($v['scheduling_update_time'])){
                    $list[$k]['scheduling_update_time'] = date('Y-m-d H:i:s',$v['scheduling_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改排期
     * @param string $scheduling_name
     * @param int $scheduling_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateScheduling($scheduling_id, $scheduling_start_time, $scheduling_end_time, $scheduling_cinema_id, $admin_id)
    {
        LifeCinemaScheduling::affairBegin();
        try {
            $where = [];
            $where['scheduling_id'] = $scheduling_id;
            $data = [];
            $data['scheduling_start_time'] = $scheduling_start_time;
            $data['scheduling_end_time'] = $scheduling_end_time;
            $data['scheduling_cinema_id'] = $scheduling_cinema_id;
            $data['scheduling_update_time'] = time();
            $result = LifeCinemaScheduling::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦影院排期-编号: ' . $scheduling_id;
                plog('life-scheduling-update', '悦活-万悦影院-修改排期', $msg);
                LifeCinemaScheduling::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinemaScheduling::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加排期
     * @param string $scheduling_name
     * @param array $scheduling_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertScheduling($scheduling_start_time, $scheduling_end_time, $scheduling_cinema_id, $admin_id)
    {

        LifeCinemaScheduling::affairBegin();
        try {
            $data = [];
            $data['scheduling_start_time'] = $scheduling_start_time;
            $data['scheduling_end_time'] = $scheduling_end_time;
            $data['scheduling_cinema_id'] = $scheduling_cinema_id;
            $data['scheduling_create_time'] = time();
            $result =  LifeCinemaScheduling::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦影院排期-编号: ' . $result;
                plog('life-scheduling-create', '悦活-万悦影院-添加排期', $msg);
                LifeCinemaScheduling::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinemaScheduling::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询排期
     * @param int $scheduling_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function schedulingInfo($scheduling_id)
    {
        $where = [];
        $where['scheduling_id'] = $scheduling_id;
        $result = LifeCinemaScheduling::where($where)->first();
        if (!empty($result)){
            $result['scheduling_start_time'] = date('Y-m-d H:i:s',$result['scheduling_start_time']);
            $result['scheduling_end_time'] = date('Y-m-d H:i:s',$result['scheduling_end_time']);
            $result['scheduling_create_time'] = date('Y-m-d H:i:s',$result['scheduling_create_time']);
            if (!empty($result['scheduling_update_time'])){
                $result['scheduling_update_time'] = date('Y-m-d H:i:s',$result['scheduling_update_time']);
            }

        }
        return $result;
    }

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

}
