<?php

namespace app\common\api;

use app\common\model\IntegralProject;

class IntegralProjectApi {

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

    public static function getList($params) {
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $where[] = ["delete", "=", 0];
        if ($params["type"]) {
            $where[] = ["type", "=", $params["type"]];
        }
        if ($params["name"]) {
            $where[] = ["name", "like", "%{$params["name"]}%"];
        }
        if ($params["projectType"]) {
            $where[] = ["projectType", "=", $params["projectType"]];
        }
        if ($params["active"]) {
            $where[] = ["active", "=", $params["active"]];
        }
        $count = IntegralProject::where($where)->count();
        $list = IntegralProject::where($where)->field("*,if(updateTime is null,createTime,updateTime) as orderTime")->limit($offset, $limit)->order("orderTime desc")->select()->toArray();
        return ["total" => $count, "rows" => $list];
    }

    public static function getAll($where = []) {
        $where[] = ["delete", "=", 0];
        $list = IntegralProject::where($where)->field("*,if(updateTime is null,createTime,updateTime) as orderTime")->order("orderTime desc")->select()->toArray();
        return $list;
    }

    public static function edit($params) {
        $model = new IntegralProject();
        $data["type"] = $params["type"];
        $data["name"] = $params["name"];
        $data["projectType"] = $params["projectType"];
        $data["active"] = $params["active"];
        $data["limit"] = $params["limit"];
        if ($data["limit"] == 1) {
            $data["yearly"] = $params["yearly"];
            $data["max"] = $params["max"];
        } else {
            $data["yearly"] = 0;
            $data["max"] = 0;
        }
        if ($params["id"]) {
            $project = $model->find($params["id"]);
            $data["updateTime"] = date("Y-m-d H:i:s");
            $data["updateUser"] = session("user")["uid"];
            return $project->save($data);
        } else {
            $data["createTime"] = date("Y-m-d H:i:s");
            $data["createUser"] = session("user")["uid"];
            return $model->save($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");
        //同时删除子项
        $upd["delete"] = 1;
        $upd["updateTime"] = date("Y-m-d H:i:s");
        $upd["updateTime"] = session("user")["uid"];
        $where[] = ["projectId", "=", $id];
        \app\common\model\IntegralItem::where($where)->save($upd);
        return IntegralProject::update($data);
    }

    public static function chkExist($name, $type, $projectType, $id = 0) {
        $where = [];
        $where[] = ["type", "=", $type];
        $where[] = ["projectType", "=", $projectType];
        $where[] = ["name", "=", $name];
        $where[] = ["delete", "=", 0];
        if ($id) {
            $where[] = ["id", "<>", $id];
        }
        return IntegralProject::where($where)->find();
    }

}
