Explorar o código

多批次同时存在

sugangqiang %!s(int64=2) %!d(string=hai) anos
pai
achega
f9966b62ba
Modificáronse 1 ficheiros con 47 adicións e 5 borrados
  1. 47 5
      app/common/api/BatchApi.php

+ 47 - 5
app/common/api/BatchApi.php

@@ -65,6 +65,9 @@ class BatchApi {
         if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
             return ["msg" => "公示截止时间错误"];
         }
+        if ($params["active"] == 1 && !self::checkIsAllowedActive(["startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
+            return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次添加失败!"];
+        }
 
         $id = getStringId();
         $data["id"] = $id;
@@ -87,6 +90,9 @@ class BatchApi {
     }
 
     public static function update($params) {
+        $batch = self::getOne($params["id"]);
+        if (!$batch)
+            return ["msg" => "修改的批次信息不存在"];
         if (!$params["type"])
             return ["msg" => "申报类别不能为空"];
         if (!$params["batch"])
@@ -114,12 +120,15 @@ class BatchApi {
         if (!strtotime($params["publicEndTime"]) && $params["publicEndTime"]) {
             return ["msg" => "公示截止时间错误"];
         }
+        if ($batch["active"] == 1 && !self::checkIsAllowedActive(["id" => $params["id"], "startTime" => $params["startTime"], "endTime" => $params["endTime"], "type" => $params["type"], "source" => $params["source"]])) {
+            return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次编辑失败!"];
+        }
 
         $data["id"] = $params["id"];
         $data["type"] = $params["type"];
         $data["source"] = $params["source"];
         $data["batch"] = $params["batch"];
-        $data["active"] = $params["active"] ?: 2;
+        $data["active"] = $batch["active"] ?: 2;
         $data["description"] = $params["description"];
         $data["type"] = $params["type"];
         $data["startTime"] = $params["startTime"];
@@ -140,15 +149,46 @@ class BatchApi {
     }
 
     public static function setActive($id, $active) {
+        if ($active == 1 && !self::checkIsAllowedActive($id)) {
+            return ["msg" => "申报时间不能与已经开启的批次存在交叉时间,批次启动失败!"];
+        }
         $data["id"] = $id;
         $data["active"] = $active ?: 2;
         Batch::update($data);
-        if ($active == 1) {
-            self::setOtherNoActive($id);
-        }
         return ["code" => 200, "msg" => "成功"];
     }
 
+    /**
+     * 
+     * @param type $param 当值为整数时,表示id;当值为array时,表示各种参数的集合
+     * @return boolean
+     */
+    public static function checkIsAllowedActive($params) {
+        if (is_array($params)) {
+            if ($params["id"]) {
+                $where[] = ["id", "<>", $params["id"]];
+            }
+            $startTime = $params["startTime"];
+            $endTime = $params["endTime"];
+            $where[] = ["type", "=", $params["type"]];
+            $where[] = ["source", "=", $params["source"]];
+        } else {
+            $id = $params;
+            $batch = self::getOne($id);
+            $startTime = $batch["startTime"];
+            $endTime = $batch["endTime"];
+            $where[] = ["id", "<>", $id];
+            $where[] = ["type", "=", $batch["type"]];
+            $where[] = ["source", "=", $batch["source"]];
+        }
+        $where[] = ["active", "=", 1];
+        $whereRaw = sprintf("startTime between '%s'and '%s' or endTime between '%s' and '%s'", $startTime, $endTime, $startTime, $endTime);
+        $exists = Batch::where($where)->whereRaw($whereRaw)->find();
+        if ($exists)
+            return false;
+        return true;
+    }
+
     public static function setOtherNoActive($except_id) {
         $open = self::getOne($except_id);
         if ($open) {
@@ -186,7 +226,9 @@ class BatchApi {
                 return ["msg" => "提交时间已截止,无法操作"];
         } else {
             $where[] = ["active", "=", 1];
-            $batch = Batch::where($where)->order("startTime desc")->order("endTime desc")->find();
+            $date = date("Y-m-d H:i:s");
+            $whereRaw = sprintf("startTime < '%s' and endTime > '%s'", $date, $date);
+            $batch = Batch::where($where)->whereRaw($whereRaw)->order("startTime desc")->order("endTime desc")->find();
             if (!$batch)
                 return ["msg" => "该申报未启动"];
             if (strtotime($batch["startTime"]) > $now)