cqp 4 ore fa
parent
commit
73ceb6c6d2

+ 13 - 0
app/Http/Controllers/Api/MiddleGroundController.php

@@ -20,6 +20,19 @@ class MiddleGroundController extends BaseController
         }
     }
 
+    public function setUseRange(Request $request)
+    {
+        $service = new MiddleGroundService();
+        $user = $request->userData;
+        list($status,$data) = $service->setUseRange($request->all(),$user);
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     public function initializationCompanyList(Request $request)
     {
         $service = new MiddleGroundService();

+ 12 - 0
app/Model/UseRangeDetails.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Model;
+
+class UseRangeDetails extends DataScopeBaseModel
+{
+    protected $guarded = [];
+    protected $table = "use_range_details"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 60 - 0
app/Service/MiddleGroundService.php

@@ -11,6 +11,7 @@ use App\Model\RoleMenu;
 use App\Model\RoleMenuButton;
 use App\Model\SysMenu;
 use App\Model\SysMenuButton;
+use App\Model\UseRangeDetails;
 use App\Model\WorkRangeDetails;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Hash;
@@ -289,6 +290,65 @@ class MiddleGroundService extends Service
         return [true, ['account' => $account, 'password' => $password]];
     }
 
+    public function setUseRange($data, $user){
+        if(empty($data['id'])) return [false, 'ID不能为空'];
+        if(empty($data['use_range'])) return [false, '授权时段不能为空'];
+
+        $validRanges = []; // 用来存放已经校验过格式的合规时段
+
+        $insert = [];
+        foreach ($data['use_range'] as $index => $range) {
+            $num = $index + 1;
+
+            // 1. 基础非空与格式校验
+            if (empty($range['start_time']) || empty($range['end_time'])) {
+                return [false, "第 {$num} 行时段的开始或结束时间不能为空"];
+            }
+            if (!preg_match('/^\d{4}-\d{2}$/', $range['start_time']) || !preg_match('/^\d{4}-\d{2}$/', $range['end_time'])) {
+                return [false, "第 {$num} 行时段格式错误,必须为 YYYY-MM"];
+            }
+            if ($range['start_time'] > $range['end_time']) {
+                return [false, "第 {$num} 行时段有误:开始时间不能大于结束时间"];
+            }
+
+            // 2. 核心:直接拿当前组,去和之前已经验证合法的数组做“两两相交”比对
+            foreach ($validRanges as $passed) {
+                if ($range['start_time'] <= $passed['end_time'] && $range['end_time'] >= $passed['start_time']) {
+                    return [false, "时间段存在冲突:[{$range['start_time']}~{$range['end_time']}] 与 [{$passed['start_time']}~{$passed['end_time']}] 相交重复"];
+                }
+            }
+
+            // 3. 没冲突,说明当前组安全,存入蓄水池,供下一轮循环比对
+            $validRanges[] = $range;
+
+            $insert[] = [
+                'top_depart_id' => $data['id'],
+                'start_time'     => $range['start_time'],
+                'start_time_stamp' => strtotime($range['start_time']),
+                'end_time'       => $range['end_time'],
+                'end_time_stamp'   => strtotime("last day of " . $range['end_time'] . " 23:59:59"),
+            ];
+        }
+
+        try {
+            DB::beginTransaction();
+
+            $time = time();
+            UseRangeDetails::where('del_time',0)
+                ->where('top_depart_id', $data['id'])
+                ->update(['del_time' => $time]);
+
+            UseRangeDetails::insert($insert);
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
     function generateAt8CharPassword() {
         $lettersNumbers = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
 

+ 2 - 0
routes/c_api.php

@@ -27,6 +27,8 @@ Route::group(['middleware'=> ['checkCenterLogin']],function ($route){
     $route->any('initializationCompany', 'Api\MiddleGroundController@initializationCompany');
     //公司列表
     $route->any('initializationCompanyList', 'Api\MiddleGroundController@initializationCompanyList');
+    //公司使用时段授权
+    $route->any('setUseRange', 'Api\MiddleGroundController@setUseRange');
 
     //菜单列表 为了设置角色权限用的
     $route->any('menuList', 'Api\MiddleGroundController@menuList');