<?php

namespace app\common\api;

use app\common\model\Batch;

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

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

    public static function getList($request) {
        $order = trim($request->param("order")) ?: "desc";
        $offset = trim($request->param("offset")) ?: 0;
        $limit = trim($request->param("limit")) ?: 10;
        $type = trim($request->param("type"));
        $active = trim($request->param("active"));
        $where = [];
        if ($type) {
            $where[] = ["type", "=", $type];
        }
        if ($active) {
            $where[] = ["active", "=", $active];
        }
        $count = Batch::where($where)->count();
        $list = Batch::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $projects = DictApi::selectByParentCode("declare_type"); //申报类型
        foreach ($list as $key => $item) {
            $list[$key]["type"] = $projects[$item["type"]];
        }
        return ["total" => $count, "rows" => $list];
    }

    public static function create($params) {
        if (!$params["type"])
            return ["msg" => "申报类别不能为空"];
        if (!$params["batch"])
            return ["msg" => "批次不能为空"];
        $where[] = ["type", "=", $params["type"]];
        $where[] = ["batch", "=", $params["batch"]];
        $where[] = ["source", "=", $params["source"]];
        $count = Batch::where($where)->count();
        if ($count > 0) {
            return ["msg" => "该申报类别的批次重复"];
        }
        if (!strtotime($params["startTime"]) && $params["startTime"]) {
            return ["msg" => "申报开始时间错误"];
        }
        if (!strtotime($params["endTime"]) && $params["endTime"]) {
            return ["msg" => "申报截止时间错误"];
        }
        if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
            return ["msg" => "提交截止时间错误"];
        }
        if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
            return ["msg" => "公示开始时间错误"];
        }
        if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
            return ["msg" => "公示截止时间错误"];
        }

        $id = getStringId();
        $data["id"] = $id;
        $data["type"] = $params["type"];
        $data["source"] = $params["source"];
        $data["batch"] = $params["batch"];
        $data["active"] = $params["active"] ?: 2;
        $data["description"] = $params["description"];
        $data["type"] = $params["type"];
        $data["startTime"] = $params["startTime"];
        $data["endTime"] = $params["endTime"];
        $data["submitEndTime"] = $params["submitEndTime"];
        $data["publicStartTime"] = $params["publicStartTime"];
        $data["publicEndTime"] = $params["publicEndTime"];
        $data["averageWage"] = $params["averageWage"];
        $data["createTime"] = date("Y-m-d H:i:s");
        $data["createUser"] = session("user")["uid"];
        Batch::insert($data);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function update($params) {
        if (!$params["type"])
            return ["msg" => "申报类别不能为空"];
        if (!$params["batch"])
            return ["msg" => "批次不能为空"];
        $where[] = ["type", "=", $params["type"]];
        $where[] = ["batch", "=", $params["batch"]];
        $where[] = ["source", "=", $params["source"]];
        $where[] = ["id", "<>", $params["id"]];
        $count = Batch::where($where)->count();
        if ($count > 0) {
            return ["msg" => "该申报类别的批次重复"];
        }
        if (!strtotime($params["startTime"]) && $params["startTime"]) {
            return ["msg" => "申报开始时间错误"];
        }
        if (!strtotime($params["endTime"]) && $params["endTime"]) {
            return ["msg" => "申报截止时间错误"];
        }
        if (!strtotime($params["submitEndTime"]) && $params["submitEndTime"]) {
            return ["msg" => "提交截止时间错误"];
        }
        if (!strtotime($params["publicStartTime"]) && $params["publicStartTime"]) {
            return ["msg" => "公示开始时间错误"];
        }
        if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
            return ["msg" => "公示截止时间错误"];
        }

        $data["id"] = $params["id"];
        $data["type"] = $params["type"];
        $data["source"] = $params["source"];
        $data["batch"] = $params["batch"];
        $data["active"] = $params["active"] ?: 2;
        $data["description"] = $params["description"];
        $data["type"] = $params["type"];
        $data["startTime"] = $params["startTime"];
        $data["endTime"] = $params["endTime"];
        $data["submitEndTime"] = $params["submitEndTime"];
        $data["publicStartTime"] = $params["publicStartTime"];
        $data["publicEndTime"] = $params["publicEndTime"];
        $data["averageWage"] = $params["averageWage"];
        $data["updateTime"] = date("Y-m-d H:i:s");
        $data["updateUser"] = session("user")["uid"];
        \think\facade\Db::table("sys_batch")->update($data);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function delete($id) {
        Batch::where(["id" => $id])->delete();
        return ["code" => 200, "msg" => "成功"];
    }

    public static function setActive($id, $active) {
        $data["id"] = $id;
        $data["active"] = $active ?: 2;
        Batch::update($data);
        if ($active == 1) {
            self::setOtherNoActive($id);
        }
        return ["code" => 200, "msg" => "成功"];
    }

    public static function setOtherNoActive($except_id) {
        $open = self::getOne($except_id);
        if ($open) {
            $where[] = ["type", "=", $open["type"]];
            $where[] = ["source", "=", $open["source"]];
            $where[] = ["id", "<>", $except_id];
            $data["active"] = 2;
            Batch::where($where)->update($data);
        }
    }

    /**
     * 
     * @param type $type 申报类别
     * @param type $talentType 人才类型
     */
    public static function checkBatchValid($params, $talentType) {
        $now = time();
        $where = [];
        $where[] = ["active", "=", 1];
        $where[] = ["source", "=", $talentType];
        if ($params["type"]) {
            $where[] = ["type", "=", $params["type"]];
        }
        if ($params["year"]) {
            $where[] = ["batch", "=", $params["year"]];
        }
        $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
        if (!$batch)
            return ["msg" => "该申报未启动"];
        if (strtotime($batch["startTime"]) > $now)
            return ["msg" => sprintf("申报还未开始，日期为：%s - %s", $batch["startTime"], $batch["endTime"])];
        if (strtotime($batch["endTime"]) < $now)
            return ["msg" => "申报已结束，无法申报"];
        return ["code" => 200, "batch" => $batch["batch"]];
    }

    /**
     * 
     * @param type $type 申报项目
     * @param type $talentType 人才类型
     * @return type
     */
    public static function getValidBatch($type, $talentType) {
        $now = date("Y-m-d H:i:s");
        $where = [];
        $where[] = ["active", "=", 1];
        $where[] = ["type", "=", $type];
        $where[] = ["source", "=", $talentType];
        $where[] = ["startTime", "<", $now];
        $where[] = ["endTime", ">", $now];
        return $batch = Batch::where($where)->order("createTime desc")->findOrEmpty();
    }

}
