<?php

namespace app\common\api;

use app\common\model\CommonFile;

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

    public static function getOne($id) {
        return CommonFile::findOrEmpty($id);
    }

    public static function getList($params) {
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $count = CommonFile::where($where)->count();
        $list = CommonFile::alias("c")->leftJoin("sys_user u", "c.createUser=u.id")->field("c.*,u.name as createUser")->where($where)->limit($offset, $limit)->order("updateTime desc,createTime desc")->select()->toArray();
        foreach ($list as $key => $item) {
            $list[$key]["url"] = getStoragePath($item["url"], true);
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function getAll($where = []) {
        $list = CommonFile::where($where)->order("updateTime desc,createTime desc")->select()->toArray();
        return $list;
    }

    public static function edit($params) {
        $data["originalName"] = $params["originalName"];
        $data["url"] = $params["url"];
        $data["description"] = $params["description"];
        if ($params["id"]) {
            $data["id"] = $params["id"];
            $data["updateTime"] = date("Y-m-d H:i:s");
            $data["updateUser"] = session("user")["uid"];
            return CommonFile::update($data);
        } else {
            $data["id"] = getStringId();
            $data["createTime"] = date("Y-m-d H:i:s");
            $data["createUser"] = session("user")["uid"];
            return CommonFile::insert($data);
        }
    }

    public static function delete($id) {
        return CommonFile::where(["id" => $id])->delete();
    }

}
