<?php

namespace app\common\api;

use app\common\model\IntegralItem;

class IntegralItemApi {

    public static function getOne($id) {
        return IntegralItem::alias("i")->leftJoin("new_integral_project p", "p.id=i.projectId")->field("i.*,p.type,if(stepNeedAmount > 0,2,1) as plan")->findOrEmpty($id);
    }

    public static function getList($params) {
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $where[] = ["i.delete", "=", 0];
        if ($params["type"]) {
            $where[] = ["p.type", "=", $params["type"]];
        }
        if ($params["name"]) {
            $where[] = ["i.name", "like", "%{$params["name"]}%"];
        }
        if ($params["projectType"]) {
            $where[] = ["p.projectType", "=", $params["projectType"]];
        }
        if ($params["active"]) {
            $where[] = ["i.active", "=", $params["active"]];
        }
        $count = IntegralItem::alias("i")->leftJoin("new_integral_project p", "p.id=i.projectId")->where($where)->count();
        $list = IntegralItem::alias("i")->leftJoin("new_integral_project p", "p.id=i.projectId")->where($where)
                        ->field("i.*,p.type,p.name as projectName,p.projectType,if(i.updateTime is null,i.createTime,i.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 = IntegralItem::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 IntegralItem();
        $data["projectId"] = $params["projectId"];
        $data["name"] = $params["name"];
        $data["unit"] = $params["unit"];
        $data["fstNeedAmount"] = $params["fstNeedAmount"];
        $data["fstGainPoints"] = $params["fstGainPoints"];
        $data["fileTypeId"] = $params["fileTypeId"] ? implode(",", $params["fileTypeId"]) : null;
        $data["active"] = $params["active"];
        if ($params["plan"] == 2) {
            $data["stepNeedAmount"] = $params["stepNeedAmount"];
            $data["stepGainPoints"] = $params["stepGainPoints"];
        } else {
            $data["stepNeedAmount"] = 0;
            $data["stepGainPoints"] = 0;
        }
        $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"]) {
            $item = $model->find($params["id"]);
            $data["updateTime"] = date("Y-m-d H:i:s");
            $data["updateUser"] = session("user")["uid"];
            return $item->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");
        return IntegralItem::update($data);
    }

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

}
