<?php
namespace app\api\controller\v1;
use app\api\business\SampleBus;
use app\api\model\Ry;
use app\api\model\Ryjr as RyjrModel;
use app\api\model\Subject;
use app\api\model\Ry as RyModel;
use OpenApi\Annotations as OA;
use app\BaseController;
use thans\jwt\facade\JWTAuth;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;
use think\facade\Request as re;
use think\Request;
use think\facade\Cache;
use thans\jwt\JWT;
use rsa\Rsa;

class Sample extends BaseController{

    use ResponseJson;
    protected $request;
    protected $middleware = [
        'jwt' => ['except' => ['uploadfile']],
//        'check' => ['except' => ['login']]

    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /*
* 上传文件
*/
    public function uploadfile(){
        $file = request() -> file('file');              //接收文件
//        dump($file);die();
        if ($file == null) {                                    //判断文件是否为空
            return $this ->jsonResponse(-1,'未上传文件');
        }
        /*
         * 截取上传文件名后缀
         * 将文件名以.分割为数组
         * 用PHP end函数取数组最后一个
         * 即可得到上传文件后缀
         */
        $temp = explode(".", $_FILES["file"]["name"]);
//        dump($temp);die();
        $extension = end($temp);
        /*
         * 判断上传文件是否合法
         * 判断截取上传文件名是否为
         * jpeg，jpg，png其中之一
         */
        if(!in_array($extension, array("pdf","PDF","DOC","doc","docx","DOCX","PNG","png"))){
            return $this ->jsonResponse(-2,'上传文件不合法');
        }
        try {
            validate(['file' => ['fileSize:10*1024*1024']])->check(['file' => $file]);
        } catch (ValidateException $e) {
            $return['msg'] = $e->getMessage();

        }
        /*
         * 调用disk方法
         * 此时根目录为/public/uploads
         * 再把文件移动至uploads下的photo文件夹里
         * 文件名用md5
         */
        $saveName = Filesystem::disk('public') -> putFile('file', $file, 'md5');
        /*
         * 这里只返回从uploads开始的路径
         * 可以根据自己的需求返回需要的路径
         */

        $res = str_replace('\\', '/', 'http://syjc.aielab.net/uploads/'.$saveName);
//       $res=(str_replace('\\', '', '/uploads/' . $saveName));

        if($res){
            return $this->jsonSuccessData($res);
        }
        else{
            return $this->jsonData(-1,"上传文件失败");
        }

    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取任务接口：param: id pageNum  pageSize
     */
    public function getSampleList(){
        $data = $this->request->post();
        $id=$data['id'];
        $pageNum=$data['pageNum'];
        $pageSize=$data['pageSize'];
        $res['rows'] = Db::name("cyd")->where('rwid','=',$id)->field('ypmc,ypbh,cyjs,cycs,status,brand')->page($pageNum,$pageSize)->select();
        $res['total'] = Db::name("cyd")->count();
        return $this->jsonSuccessData($res);
    }

//获取抽样单详情
    public function getSampleDetail(){
        $data = $this->request->post();
        $id=$data['id'];
        $res['rows'] = Db::name("cyd")->where('id','=',$id)
            ->field('ypmc,ypbh,cyjs,cycs,status,brand')
            ->leftJoin('t_','student_user.auth=student_teacher.auth and student_user.login_name=student_teacher.number and student_user.name=student_teacher.name')
            ->find();
        $res['total'] = Db::name("cyd")->count();
        return $this->jsonSuccessData($res);
    }
    /**、
     * @return \think\response\Json
     * 添加任务
     */
    public function addSample(){
        $data = $this->request->post();
        $resid=Db::name("cyd")->strict(false)->insertGetId($data);
        return $this->jsonSuccessData($resid);
    }


}
