<?php

namespace app\Admin\controller;

use think\Db;
class Stock2 extends Base

{
//    public $post="";
//    public $userinfo="";
   public function __construct()
   {
      parent:: __construct();
//       $post=request()->post();
//       $this->post=$post;
    }

    public function video()
    {
        $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
        $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";

        $where = ['is_del'=>0];
        if ($status != "") {
            $where ['status'] = $status;
        }
        $video_sn = isset($this->post['video_sn']) && $this->post['video_sn'] !== "" ? trim($this->post['video_sn']) : "";
        if ($video_sn !== "") {
            $where['video_sn'] = ["like", "%{$video_sn}%"];
        }
        $video_name = isset($this->post['video_name']) && $this->post['video_name'] !== "" ? trim($this->post['video_name']) : "";
        if ($video_name) {
            $where['video_name'] = ["like", "%{$video_name}%"];
        }
        $video_url = isset($this->post['video_url']) && $this->post['video_url'] !== "" ? trim($this->post['video_url']) : "";
        if ($video_url) {
            $where['video_url'] = ["like", "%{$video_url}%"];
        }
        $video_img = isset($this->post['video_img']) && $this->post['video_img'] !== "" ? trim($this->post['video_img']) : "";
        if ($video_img) {
            $where['video_img'] = ["like", "%{$video_img}%"];
        }
        $count = Db::name("video")->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $video = Db::name("video")->where($where)->page($page, $size)->order("addtime desc")->select();
        return app_show(0, "获取成功", ["video" => $video, "count" => $count]);
    }

    public function Create()
    {
        /**

         * @param video_name
         * @param video_url
         * @param video_img
         * @param remark
         */

        $video_sn = makeNo("FC");
        //var_dump($this->post);
        $video_name = isset($this->post['video_name']) && $this->post['video_name'] !== "" ? trim($this->post['video_name']) : "";
      // var_dump($video_name);
        if ($video_name == "") {
            return error_show(1004, "参数video_name  不能为空");
        }

        $isN = Db::name("video")->where(["is_del" => 0, "video_name" => $video_name])->find();
        //var_dump(Db::name("video")->getLastSql());
        if (!empty($isN)) {
            return error_show(1004, "视频名称已存在");
        }
        $video_url = isset($this->post['video_url']) && $this->post['video_url'] !== "" ? trim($this->post['video_url']) : "";
       // var_dump( $video_url);
        if ($video_url == "") {

            return error_show(1004, "参数video_url  视频地址不能为空");
        }

        $video_img = isset($this->post['video_img']) && $this->post['video_img'] !== "" ? trim($this->post['video_img']) :"";
        //var_dump($video_img);
        if ($video_img == "") {
            return error_show(1004, "参数video_img 视频图片不能为空");
        }
        $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
        $data = [
            "video_sn" => $video_sn,
            "video_name"=>$video_name,
            "video_url"=>$video_url,
            "video_img"=>$video_img,
            "remark"=>$remark,
            "is_del" => 0,
            "status" => 1,
            "addtime" => date("Y-m-d H:i:s"),
            "updatetime" => date("Y-m-d H:i:s"),
        ];
        Db::startTrans();
        try{
            $video = Db::name("video")->insert($data, false, true);
            //var_dump( Db::name("video")->getLastSql());
            if ($video) {
                write_log("视频{$video}新建成功", $this->userinfo, "account", "add");
                Db::commit();
                return app_show(0, "视频新建成功");
            }

            Db::rollback();
            return error_show(1005, "视频新建失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1003,$e->getMessage());
        }



}
    public function Read(){
        $id=isset($this->post['id'])&&$this->post["id"]!="" ? intval($this->post['id']):"";
        if($id==""){
            return error_show(1004,"参数id 不能为空");
        }
        $video_sn = db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
        if(empty($video_sn)){
            return error_show(1005,"未找到视频编号");
        }
        if($video_sn["is_del"]==1) {
            return error_show(1005, "视频编号已删除");
        }
        $video_sn['status_n'] = $video_sn['status']==0? "禁用视频": $video_sn['status']==1? "启用视频":"禁用视频";

        return app_show(0,"成功获取",$video_sn);

    }
    /**
     * @param video_sn
     * @param video_name
     * @param video_url
     * @param video_img
     * @param remark


     */
    public function change(){
        $id = isset($this->post['id'])? intval($this->post['id']) : "";
       // var_dump($this->post,$id);

        if($id==""){

            return error_show(1001,'参数ID 不能为空');
        }
        $act = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
        if(empty($act)){
            return error_show(1001,"未找到视频");
        }

        if($act['is_del']==1){
            return error_show(1003,'视频编号已删除');
        }
      $status = isset($this->post['status']) ? intval($this->post['status']) : "";
        if($status===""){
            return error_show(1004,"参数不能为空");
        }
        if(!in_array($status,[0,1])){
            return error_show(1004,"参数status无效值");
        }

        try{
            $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
            $result = Db::name("video")->where(["id"=>$id])->update($data);
            $msg = $status ==1?"启用":"禁用";
            //var_dump(Db::name("video")->getLastSql());


            if($result){
                write_log("视频{$act['video_name']}功能{$act['video_name']}{$msg}成功",$this->userinfo,"status","0");
                return app_show(0,"更新成功");
            }else{
                return error_show(1004,"更新失败");
            }
        }catch (\Exception $e){
            return error_show(1003,$e->getMessage());
        }


    }

    /**
     * @param video_sn
     * @param video_name
     * @param video_url
     * @param video_img

     */

    public function Edit(){
        $id = isset($this->post['id'])&&$this->post['id']!== "" ?trim($this->post['id']):"";
        if($id==""){
            return error_show(1005,"参数id不能为空");
        }
        $in = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
        //var_dump($in);
        if(empty($in)){
            return error_show(1004,"视频不存在");
        }
        $weight = isset($this->post['weight'])&&$this->post['weight']!==""?$this->post['weight']:$in["weight"];


        $video_name = isset($this->post['video_name']) ? trim($this->post['video_name']) : "";
        if($video_name==""){
            return error_show(1004,"视频名称不能为空");
        }
        $video_url = isset($this->post['video_url']) ? trim($this->post['video_url']) : "";
        if($video_name==""){
            return error_show(1004,"视频地址不能为空");
        }
        $video_img = isset($this->post['video_img']) ? trim($this->post['video_img']) : "";
        if($video_img==""){
            return error_show(1004,"视频图片不能为空");
        }
        $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
        Db::startTrans();
        try{
            $data=[
                "id"=>$id,
                "video_name"=>$video_name,
                "video_url"=>$video_url,
                "video_img"=>$video_img,
                "updatetime"=>date("Y-m-d H:i:s"),
                "weight"=>$weight,
                "remark"=>$remark
           ];

            $video_name= Db::name("video")->update($data);
           // var_dump(Db::name("video")->getLastSql());
            if($video_name){
                Db::commit();
                return app_show(0,"视频内容编辑成功");
            }
            Db::rollback();
            return error_show(1008,"视频内容修改失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1008,$e->getMessage());
        }

    }
    public function Delete(){
        $id = isset($this->post['id'])&&$this->post['id']!==""? intval($this->post['id']):"";

        if($id==""){
            return error_show(1005,"参数id不能为空");
        }
        $list = Db::name("video")->where(["id"=>$id,"is_del"=>0])->find();
        if(empty($list)){
            return error_show(1004,"未找到数据");
        }

        $del = Db::name("video")->where(["id"=>$id])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
       // var_dump( Db::name("video")->getLastSql());
        if($del){
            return app_show(0,"视频数据修改成功");
        }
        return error_show(1005,"视频数修改失败");
    }




}