<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\UploadApi;
use think\facade\Db;

/**
 * Description of TalentCommonFile
 *
 * @author sgq
 */
class TalentCommonFile extends AdminController {

    public function uploadBatch() {
        $response = new \stdClass();
        $response->code = 500;

        $memo = $this->request["memo"];
        $fileTag = $this->request["fileTag"];

        if (!$fileTag) {
            $response->msg = "没有提交文件类型标识，无法按文件类型归档，上传被中止";
            return json($response);
        }
        if (!$memo) {
            $response->msg = "没有提交批次信息，无法按批次归档，上传被中止";
            return json($response);
        }

        if (!$this->request->file()) {
            $response->msg = "没有上传任何材料";
            return json($response);
        }
        preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u", ucwords($fileTag), $matches);
        $pathName = "TalentCommonFile";
        if ($matches[0]) {
            $pathName = join("", $matches[0]);
        }
        $file = $this->request->file("file");
        $upload = new UploadApi();
        $result = $upload->uploadOne($file, "system", "talent/{$pathName}");
        if ($result->code != 200) {
            $response->msg = $result->msg;
            return json($response);
        }
        $data["id"] = getStringId();
        $data["fileTag"] = $fileTag;
        $data["url"] = $result->filepath;
        $data["originalName"] = $file->getOriginalName();
        $data["memo"] = $memo;
        $data["createTime"] = date("Y-m-d H:i:s");
        $data["createUser"] = $this->user["uid"];
        $res = Db::table("new_non_predefined_file")->insert($data);

        $response->code = 200;
        $response->msg = "上传成功";
        return json($response);
    }

}
