<?php

namespace app\admin\logic;

use app\model\CommonModel;
use app\model\VideoModel;
use think\exception\ValidateException;
use think\response\Json;

class VideoLogic extends BaseLogic
{
    //获取视频列表
    public static function List(array $data = []): Json
    {
        $db = VideoModel::where('is_del', CommonModel::$del_normal);
        if ($data['status'] != '') $db->where('status', $data['status']);
        if ($data['video_sn'] !== '') $db->whereLike('video_sn', '%' . $data['video_sn'] . '%');
        if ($data['video_name']) $db->whereLike('video_name', '%' . $data['video_name'] . '%');
        if ($data['video_url'] != '') $db->whereLike('video_url', '%' . $data['video_url'] . '%');

        $count = $db->count('id');

        $video = $db
            ->field('id,video_name,remark,status,addtime')
            ->page($data['page'], $data['size'])
            ->order('addtime desc')
            ->select()
            ->toArray();

        return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
    }

    //添加视频
    public static function Add(array $data = []): Json
    {

        $rs = VideoModel::field('id')
            ->where(['is_del' => CommonModel::$del_normal, 'video_name' => $data['video_name']])
            ->findOrEmpty()
            ->isEmpty();

        if (!$rs) throw new ValidateException('该视频名称已存在');

        $video_sn = makeNo("FC");
        $data = array_merge($data, [
            "video_sn" => $video_sn,
            "is_del" => CommonModel::$del_normal,
            "status" => CommonModel::$status_normal,
            "addtime" => date("Y-m-d H:i:s"),
            "updatetime" => date("Y-m-d H:i:s"),
        ]);

//        write_log("视频{$video}新建成功", $this->userinfo, "account", "add");

        $res = VideoModel::create($data)->save();

        return $res ? json_show(CommonModel::$success, '视频新建成功') : json_show(CommonModel::$error_param, '视频新建失败');
    }

    //读取视频详情
    public static function Read(int $id = 0): Json
    {
        $rs = VideoModel::field(true)
            ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->findOrEmpty()
            ->toArray();
        return json_show(CommonModel::$success, '获取视频详情成功', $rs);
    }

    //编辑视频
    public static function Edit(array $data = []): Json
    {
        $rs = VideoModel::field('id,video_name')
            ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
            ->findOrEmpty();

        if ($rs->isEmpty()) throw new ValidateException('该视频不存在');

        //修改了视频名称,检查视频名称是否重复
        if ($rs->video_name != $data['video_name']) {
            $temp = VideoModel::field('id')
                ->where(['is_del' => CommonModel::$del_normal, 'video_name' => $data['video_name']])
                ->findOrEmpty()
                ->isEmpty();
            if (!$temp) throw new ValidateException('该视频名称重复');
        }

        $data = array_merge($data, [
            "updatetime" => date("Y-m-d H:i:s"),
        ]);

//        write_log("视频{$video}新建成功", $this->userinfo, "account", "add");

        $res = VideoModel::where(['id' => $data['id']])->save($data);

        return $res ? json_show(CommonModel::$success, '视频内容编辑成功') : json_show(CommonModel::$error_param, '视频内容编辑失败');

    }

    //视频启禁用
    public static function Change(array $data = []): Json

    {
        $where = [
            ['id', '=', $data['id']],
            ['is_del', '=', CommonModel::$del_normal],
            ['status', '<>', $data['status']],
        ];
        $rs = VideoModel::field('id,status')
            ->where($where)
            ->findOrEmpty()
            ->isEmpty();

        if ($rs) throw new ValidateException('该视频不存在或重复操作');

//        write_log("视频{$video}新建成功", $this->userinfo, "account", "add");

        $res = VideoModel::where($where)->save($data);

        return $res ? json_show(CommonModel::$success, '更新成功') : json_show(CommonModel::$error_param, '更新失败');

    }

    //删除视频
    public static function Delete(int $id = 0): Json
    {
        $rs = VideoModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->save(['is_del' => CommonModel::$del_deleted]);

        return $rs ? json_show(CommonModel::$success, '视频删除成功') : json_show(CommonModel::$error_param, '视频不存在或重复删除');
    }

}