cqp il y a 3 semaines
Parent
commit
e90e5d91ce

+ 1 - 1
app/Http/Middleware/CheckLogin.php

@@ -42,7 +42,7 @@ class CheckLogin
         list($state, $msg) = LoginService::checkCompany($employee);
         if(! $state) return response()->json(['code' => 1,'msg' => $msg,'data' => null]);
         //人员角色 菜单权限
-        $employee = array_merge($employee, LoginService::getPersonRoleAndPermissions($employee));
+        $employee = array_merge($employee, LoginService::getPersonRoleAndPermissions($employee, $tree_type));
 
         //当前选择的系统菜单
         $employee['select_tree_type'] = $tree_type;

+ 11 - 5
app/Service/EmployeeService.php

@@ -155,9 +155,14 @@ class EmployeeService extends Service
                 EmployeeDepartPermission::insert($insert);
             }
 
-            EmployeeRole::where('employee_id',$data['id'])->update([
-                'del_time' => $time
-            ]);
+            EmployeeRole::join('role', 'role.id', '=', 'employee_role.role_id')
+                ->where('employee_role.employee_id', $data['id'])
+                ->where('role.tree_type', $user['select_tree_type'])
+                ->where('employee_role.del_time', 0)
+                ->update([
+                    'employee_role.del_time' => $time
+                ]);
+
             if(isset($data['role'])){
                 $insert = [];
                 foreach ($data['role'] as $value){
@@ -430,7 +435,7 @@ class EmployeeService extends Service
 
         // 获取员工ID并查询扩展数据
         $employee_ids = array_column($data['data'], 'id');
-        list($status, $extraMap) = $this->getEmployee($employee_ids);
+        list($status, $extraMap) = $this->getEmployee($employee_ids, $user);
 
         $map = [];
         if(isset($ergs['search_for_month_work'])) list($status, $map) = $this->getEmployeesMonthStats($employee_ids, $ergs['search_for_month_work'], $user);
@@ -561,7 +566,7 @@ class EmployeeService extends Service
         return [true, $result];
     }
 
-    public function getEmployee(array $employee_ids)
+    public function getEmployee(array $employee_ids, $user)
     {
         if (empty($employee_ids)) return [false, []];
 
@@ -570,6 +575,7 @@ class EmployeeService extends Service
             ->join('role as b', 'a.role_id', '=', 'b.id')
             ->where('a.del_time', 0)
             ->where('b.del_time', 0)
+            ->where('b.tree_type', $user['select_tree_type'])
             ->whereIn("a.employee_id", $employee_ids)
             ->select('a.employee_id', 'b.title', 'b.id')
             ->get();

+ 191 - 2
app/Service/ItemService.php

@@ -147,6 +147,7 @@ class ItemService extends Service
         $modelClass = $modelMap[$from] ?? "";
         $item = $modelClass::where('id', $id)->where('del_time', 0)->first();
         if (!$item) return [false, $fromMap[$from] . "不存在或已被删除"];
+        if ($item->state == Item::TYPE_THREE) return [false, '已完成状态不允许上传文件'];
 
         $itemId = $item->item_id ?? $item->id;
         $itemNodeId = $item->item_node_id ?? ($from == ItemFile::from_one ? 0 : $item->id);
@@ -196,7 +197,7 @@ class ItemService extends Service
         return [true, ''];
     }
 
-    public function itemFileUpDelete($data,$user){
+    public function itemFileUpDelete1($data,$user){
         $fromMap = [
             'custom_fields' => CustomFieldValue::class,
             'item_file' => ItemFile::class,
@@ -213,6 +214,21 @@ class ItemService extends Service
         $modelClass = $fromMap[$type] ?? "";
         $item = $modelClass::where('id', $id)->where('del_time', 0)->first();
         if (! $item) return [false, "文件不存在或已被删除"];
+        $modelMap = [
+            ItemFile::from_one => Item::class,
+            ItemFile::from_two => ItemNode::class,
+            ItemFile::from_three => ItemNodeMission::class,
+        ];
+        $modelClass = $modelMap[$item->from] ?? "";
+        if(! $modelClass) return [false, '来源不存在'];
+        $model2Map = [
+            ItemFile::from_one => 'item_id',
+            ItemFile::from_two => 'item_node_id',
+            ItemFile::from_three => 'item_node_mission_id',
+        ];
+        $id = $model2Map[$item->from];
+        $m = $modelClass::where('id', $id)->where('del_time', 0)->first();
+        if ($m->state == Item::TYPE_THREE) return [false, '已完成状态不允许删除文件'];
 
         try {
             DB::beginTransaction();
@@ -246,7 +262,94 @@ class ItemService extends Service
         return [true, ''];
     }
 
-    public function itemFileIsDelivery($data, $user)
+    public function itemFileUpDelete($data, $user)
+    {
+        $fromMap = [
+            'custom_fields' => CustomFieldValue::class,
+            'item_file'     => ItemFile::class,
+        ];
+
+        // 1. 基础参数校验
+        $type = $data['type'] ?? null;
+        if (empty($type)) return [false, '删除来源类型不能为空'];
+        if (!isset($fromMap[$type])) return [false, '删除来源类型错误'];
+
+        $id = $data['id'] ?? null;
+        if (empty($id)) return [false, '来源ID不能为空'];
+
+        // 2. 查询当前文件记录
+        $modelClass = $fromMap[$type];
+        $item = $modelClass::where('id', $id)->where('del_time', 0)->first();
+        if (!$item) return [false, "文件不存在或已被删除"];
+
+        // 3. 【核心统一】根据 from 动态映射对应的上级主体 Model 类名
+        $modelMap = [
+            ItemFile::from_one   => Item::class,
+            ItemFile::from_two   => ItemNode::class,
+            ItemFile::from_three => ItemNodeMission::class,
+        ];
+
+        $upperClass = $modelMap[$item->from] ?? null;
+        if (!$upperClass) return [false, '未知的来源所属类型'];
+
+        // 4. 根据 from 动态匹配当前模型里的外键字段名
+        $foreignKeyMap = [
+            ItemFile::from_one   => 'item_id',
+            ItemFile::from_two   => 'item_node_id',
+            ItemFile::from_three => 'item_node_mission_id',
+        ];
+        $foreignKey = $foreignKeyMap[$item->from] ?? 'item_id';
+
+        // 动态获取外键 ID 值(例如 $item->item_node_id)
+        $upperId = $item->$foreignKey ?? 0;
+
+        // 5. 查询上级主体,校验完结状态
+        $upperModel = $upperClass::where('id', $upperId)->where('del_time', 0)->first();
+        if ($upperModel && $upperModel->state == Item::TYPE_THREE) return [false, '已完成状态不允许删除文件'];
+
+        // 6. 执行删除事务与异步 OSS 任务
+        try {
+            DB::beginTransaction();
+
+            $time = time();
+
+            if ($type === 'custom_fields') {
+                if (empty($item->field_value)) return [false, '文件为空'];
+                $fileUrl = $item->field_value;
+
+                // 清空自定义字段
+                $item->update([
+                    'field_value' => '',
+                    'field_name'  => ''
+                ]);
+            } else {
+                if (empty($item->url)) return [false, '文件为空'];
+                $fileUrl = $item->url;
+
+                // 软删除附件记录
+                $item->update(['del_time' => $time]);
+            }
+
+            // OSS 文件真实清理任务
+            $pendingTask = [
+                'type'           => SysOssTasks::type_one,
+                'url'            => $fileUrl,
+                'top_depart_id'  => $user['top_depart_id'],
+                'crt_time'       => $time
+            ];
+            SysOssTasks::insert($pendingTask);
+            ProcessOssTask::dispatch()->onQueue(SysOssTasks::job);
+
+            DB::commit();
+        } catch (\Exception $exception) {
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+    public function itemFileIsDelivery1($data, $user)
     {
         $typeMap = [
             1 => '标记为交付物',
@@ -296,6 +399,92 @@ class ItemService extends Service
         return [true, ''];
     }
 
+    public function itemFileIsDelivery($data, $user)
+    {
+        $typeMap = [
+            1 => '标记为交付物',
+            2 => '解除交付物',
+        ];
+        $fromMap = [
+            'custom_fields' => CustomFieldValue::class,
+            'item_file' => ItemFile::class,
+        ];
+
+        // 1. 基础参数校验
+        if (empty($data['opt_type']) || !isset($typeMap[$data['opt_type']])) return [false, '操作标识错误'];
+        if (empty($data['data']) || !is_array($data['data'])) return [false, '文件数据不能为空'];
+
+        $statusValue = ($data['opt_type'] == 1) ? 1 : 0;
+
+        // 按文件分类归拢 ID
+        $idsByType = [
+            'custom_fields' => [],
+            'item_file' => [],
+        ];
+
+        foreach ($data['data'] as $value) {
+            if (empty($value['id'])) return [false, '文件ID不能为空'];
+            if (empty($value['type']) || !isset($fromMap[$value['type']])) return [false, '文件类型错误'];
+
+            $idsByType[$value['type']][] = $value['id'];
+        }
+
+        // 2. 【核心新增】上级主体完结状态校验(防止已完结的主体还能修改交付物状态)
+        $modelMap = [
+            ItemFile::from_one   => Item::class,
+            ItemFile::from_two   => ItemNode::class,
+            ItemFile::from_three => ItemNodeMission::class,
+        ];
+        $foreignKeyMap = [
+            ItemFile::from_one   => 'item_id',
+            ItemFile::from_two   => 'item_node_id',
+            ItemFile::from_three => 'item_node_mission_id',
+        ];
+
+        foreach ($idsByType as $type => $ids) {
+            if (empty($ids)) continue;
+
+            $modelClass = $fromMap[$type];
+            // 批量一次性查出这些文件记录
+            $files = $modelClass::whereIn('id', $ids)->where('del_time', 0)->get();
+
+            foreach ($files as $file) {
+                $upperClass = $modelMap[$file->from] ?? null;
+                $foreignKey = $foreignKeyMap[$file->from] ?? 'item_id';
+                $upperId = $file->$foreignKey ?? 0;
+
+                if ($upperClass && $upperId > 0) {
+                    // 校验上级主体状态
+                    $upperModel = $upperClass::where('id', $upperId)->where('del_time', 0)->first();
+                    if ($upperModel && $upperModel->state == Item::TYPE_THREE) {
+                        return [false, '相关项目/节点/任务已完结,无法修改交付物状态'];
+                    }
+                }
+            }
+        }
+
+        // 3. 执行批量更新事务
+        try {
+            DB::beginTransaction();
+
+            foreach ($idsByType as $type => $ids) {
+                if (!empty($ids)) {
+                    $modelClass = $fromMap[$type];
+                    $modelClass::whereIn('id', $ids)->update([
+                        'is_delivery' => $statusValue,
+                    ]);
+                }
+            }
+
+            DB::commit();
+        } catch (\Exception $exception) {
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
     public function changedCommon($user, $oldData){
         //是否存在审批流
         if(empty($oldData['review_id'])) return false;

+ 12 - 4
app/Service/LoginService.php

@@ -6,6 +6,7 @@ use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\EmployeeDepartPermission;
 use App\Model\EmployeeRole;
+use App\Model\Role;
 use App\Model\RoleMenu;
 use App\Model\RoleMenuButton;
 use App\Model\SysMenu;
@@ -183,7 +184,7 @@ class LoginService extends Service
         return $result;
     }
 
-    public static function getPersonRoleAndPermissions($employee) {
+    public static function getPersonRoleAndPermissions($employee, $tree_type) {
         // 初始化返回结构
         $result = [
             'role_ids'    => [],
@@ -195,10 +196,17 @@ class LoginService extends Service
 //            return $result;
 //        }
 
-        $role_ids = EmployeeRole::where('del_time', 0)
-            ->where('employee_id', $employee['id'])
-            ->pluck('role_id')
+        $role_ids = Role::from('role as a')
+            ->join('employee_role as b','b.role_id','a.id')
+            ->where('a.del_time',0)
+            ->where('b.del_time',0)
+            ->where('a.tree_type', $tree_type)
+            ->pluck('b.role_id')
             ->toArray();
+//        $role_ids = EmployeeRole::where('del_time', 0)
+//            ->where('employee_id', $employee['id'])
+//            ->pluck('role_id')
+//            ->toArray();
 
         if (empty($role_ids)) return $result;
         $result['role_ids'] = $role_ids;