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

use app\admin\model\LifeCinemaPerformers;

class PerformersServer
{
    /**
     * Notes:获取演职列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getPerformersList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeCinemaPerformers::getPerformersList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['performers_create_time'] = date('Y-m-d H:i:s',$v['performers_create_time']);
                if (!empty($v['performers_update_time'])){
                    $list[$k]['performers_update_time'] = date('Y-m-d H:i:s',$v['performers_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:所有演职
     * @return array
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function getPerformersAll($type)
    {
        return LifeCinemaPerformers::getPerformersAll($type);
    }

    /**
     * Notes:修改演职
     * @param string $performers_name
     * @param int $performers_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updatePerformers($performers_id, $performers_name, $performers_img, $performers_type, $performers_introduction, $admin_id)
    {
        LifeCinemaPerformers::affairBegin();
        try {
            $where = [];
            $where['performers_id'] = $performers_id;
            $data = [];
            $data['performers_name'] = $performers_name;
            $data['performers_img'] = $performers_img;
            $data['performers_type'] = $performers_type;
            $data['performers_introduction'] = $performers_introduction;
            $data['performers_update_time'] = time();
            $result = LifeCinemaPerformers::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦影院演职-编号: ' . $performers_id;
                plog('life-performers-update', '悦活-万悦影院-修改演职', $msg);
                LifeCinemaPerformers::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinemaPerformers::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加演职
     * @param string $performers_name
     * @param array $performers_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertPerformers($performers_name, $performers_img, $performers_type, $performers_introduction, $admin_id)
    {

        LifeCinemaPerformers::affairBegin();
        try {
            $data = [];
            $data['performers_name'] = $performers_name;
            $data['performers_img'] = $performers_img;
            $data['performers_type'] = $performers_type;
            $data['performers_introduction'] = $performers_introduction;
            $data['performers_create_time'] = time();
            $result =  LifeCinemaPerformers::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦影院演职-编号: ' . $result;
                plog('life-performers-create', '悦活-万悦影院-添加演职', $msg);
                LifeCinemaPerformers::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeCinemaPerformers::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询演职
     * @param int $performers_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function performersInfo($performers_id)
    {
        $where = [];
        $where['performers_id'] = $performers_id;
        $result = LifeCinemaPerformers::where($where)->first();
        if (!empty($result)){
            $result['performers_create_time'] = date('Y-m-d H:i:s',$result['performers_create_time']);
            if (!empty($result['performers_update_time'])){
                $result['performers_update_time'] = date('Y-m-d H:i:s',$result['performers_update_time']);
            }

        }
        return $result;
    }

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

}
