<?php

namespace app\common\api;

use app\common\model\FileType;

/**
 * Description of FileTypeApi
 *
 * @author sgq
 */
class FileTypeApi {

    public static function getList($params) {
        $order = $params["order"] ?: "desc";
        $limit = $params["limit"] ?: 10;
        $offset = $params["offset"] ?: 0;
        $where[] = ["delete", "=", 0];
        if ($params["name"]) {
            $where[] = ["name", "like", "%" . $params["name"] . "%"];
        }
        if ($params["project"]) {
            $where[] = ["project", "=", $params["project"]];
        }
//        if ($params["type"]) {
//            $where[] = ["type", "=", session('user')['type']];
//        }
        if ($params["must"]) {
            $where[] = ["must", "=", $params["must"]];
        }
        if ($params["active"]) {
            $where[] = ["active", "=", $params["active"]];
        }
        $where[] = ["type", "=", session('user')['type']];
        $projectNameKvs = DictApi::selectByParentCode("declare_type");
        $count = FileType::where($where)->count();
        $list = FileType::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        foreach ($list as &$item) {
            $item["projectName"] = $projectNameKvs[$item["project"]];
            if ($item["templateUrl"]) {
                $item["templateUrl"] = "/storage/" . $item["templateUrl"];
            }
        }unset($item);
        return ["total" => $count, "rows" => $list];
    }

    public static function getOne($id) {
        return FileType::findOrEmpty($id)->toArray();
    }

    public static function getAll($where = []) {
        $where[] = ["delete", "=", 0];
        $list = FileType::where($where)->select()->toArray();
        return $list;
    }

    public static function edit($request) {
        $params = $request->param();
        $data["type"] = $params["type"];
        $data["project"] = $params["project"];
        $data["name"] = $params["name"];
        $data["isConditionFile"] = $params["isConditionFile"] ?: 0;
        $data["step"] = $params["step"];
        $data["must"] = $params["must"];
        $data["active"] = $params["active"];
        $data["description"] = $params["description"];
        $data["templateUrl"] = $params["templateUrl"];
        $data["sn"] = $params["sn"];
        $data["api"] = $params["api"];
        $data["rel"] = $params["rel"];
        $relationIds = array_filter(explode(",", $params["relationIds_hidden"]));
        $data["relationIds"] = $relationIds ? implode(",", $relationIds) : "";
        $enterpriseIds = array_filter(explode(",", $params["enterpriseIds_hidden"]));
        $data["enterpriseIds"] = $enterpriseIds ? implode(",", $enterpriseIds) : "";
        if ($request->file()) {
            $uploader = new UploadApi();
            $res = $uploader->uploadOne($request->file("templateFile"), "file", "talent/filetype/template");
            $data["templateUrl"] = $res->filepath;
        }
        if ($params["id"]) {
            $data["id"] = $params["id"];
            $data["updateUser"] = session("user")["uid"];
            $data["updateTime"] = date("Y-m-d H:i:s");
            $info = self::getOne($params["id"]);
            if ($request->file() && $info["templateUrl"]) {
                $path = "storage/" . $info["templateUrl"];
                if (file_exists($path))
                    @unlink($path);
            }
            return FileType::update($data);
        } else {
            $data["createUser"] = session("user")["uid"];
            $data["createTime"] = date("Y-m-d H:i:s");
            return FileType::insert($data);
        }
    }

    public static function delete($id) {
        $data["id"] = $id;
        $data["delete"] = 1;
        $data["updateUser"] = session("user")["uid"];
        $data["updateTime"] = date("Y-m-d H:i:s");
        return FileType::update($data);
    }

}
