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

use app\admin\model\LifeCinemaMold;

class MoldServer
{
    /**
     * Notes:获取影片类型列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getMoldList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeCinemaMold::getMoldList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['mold_create_time'] = date('Y-m-d H:i:s',$v['mold_create_time']);
                if (!empty($v['mold_update_time'])){
                    $list[$k]['mold_update_time'] = date('Y-m-d H:i:s',$v['mold_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    public static function getMoldAll()
    {
        return LifeCinemaMold::where(['mold_is_del'=>0])->select(['mold_id','mold_name'])->get()->toArray();
    }

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

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

    /**
     * Notes: 添加影片类型
     * @param string $mold_name
     * @param array $mold_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertMold($mold_name, $admin_id)
    {

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

    /**
     * Notes:查询影片类型
     * @param int $mold_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function moldInfo($mold_id)
    {
        $where = [];
        $where['mold_id'] = $mold_id;
        $result = LifeCinemaMold::where($where)->first();
        if (!empty($result)){
            $result['mold_create_time'] = date('Y-m-d H:i:s',$result['mold_create_time']);
            if (!empty($result['mold_update_time'])){
                $result['mold_update_time'] = date('Y-m-d H:i:s',$result['mold_update_time']);
            }

        }
        return $result;
    }

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

}
