cqp 3 месяцев назад
Родитель
Сommit
7733747274
1 измененных файлов с 32 добавлено и 31 удалено
  1. 32 31
      app/Service/RuleSetService.php

+ 32 - 31
app/Service/RuleSetService.php

@@ -421,13 +421,14 @@ class RuleSetService extends Service
         $data['month'] = $monthStart;
         $monthEnd = strtotime('+1 month', $monthStart) - 1;
 
-        // 1. 获取在该月份有效期内的项目及其名称
+        // 1. 获取在该月份有效期内的项目及其信息 (ID, 标题, 编号)
         $itemsData = DB::table('item')
             ->where('top_depart_id', $data['top_depart_id'])
             ->where('del_time', 0)
             ->where('start_time', '<=', $monthEnd)
             ->where('end_time', '>=', $monthStart)
-            ->pluck('title', 'id') // 获取 [id => title] 映射
+            ->get(['id', 'title', 'code']) // 获取项目 ID, 标题, 编号
+            ->keyBy('id')
             ->toArray();
 
         if (empty($itemsData)) return [false, "该月份下无项目信息"];
@@ -440,10 +441,8 @@ class RuleSetService extends Service
             ->where('del_time', 0)
             ->get();
 
-        $manIds = [];
-        $devIds = [];
-        $manMap = [];
-        $deviceMap = [];
+        $manIds = []; $devIds = [];
+        $manMap = []; $deviceMap = [];
 
         foreach ($details as $row) {
             if ($row->type == ItemDetails::type_one) {
@@ -455,18 +454,20 @@ class RuleSetService extends Service
             }
         }
 
-        // 3. 批量获取人员和设备名称映射
-        $manNames = DB::table('employee')
+        // 3. 批量获取人员和设备信息 (含编号)
+        $manInfos = DB::table('employee')
             ->whereIn('id', array_unique($manIds))
-            ->pluck('title', 'id')->toArray();
+            ->get(['id', 'title', 'number']) // 这里的 number 是工号
+            ->keyBy('id')->toArray();
 
-        $devNames = DB::table('device')
+        $devInfos = DB::table('device')
             ->whereIn('id', array_unique($devIds))
-            ->pluck('title', 'id')->toArray();
+            ->get(['id', 'title', 'code']) // 这里的 code 是资产编号
+            ->keyBy('id')->toArray();
 
-        // 4. 生成随机比例分配,传入名称映射
-        $man_list = $this->distributeRates($manMap, RuleSetDetails::type_one, $manNames, $itemsData);
-        $device_list = $this->distributeRates($deviceMap, RuleSetDetails::type_two, $devNames, $itemsData);
+        // 4. 生成分配列表
+        $man_list = $this->distributeRates($manMap, RuleSetDetails::type_one, $manInfos, $itemsData);
+        $device_list = $this->distributeRates($deviceMap, RuleSetDetails::type_two, $devInfos, $itemsData);
 
         return [true, [
             'month' => date('Y-m', $monthStart),
@@ -475,27 +476,22 @@ class RuleSetService extends Service
         ]];
     }
 
-    /**
-     * 核心算法:增加了名称字段的映射
-     */
-    private function distributeRates($map, $type, $nameMap, $itemNames)
+    private function distributeRates($map, $type, $infoMap, $itemsData)
     {
         $result = [];
         foreach ($map as $dataId => $items) {
             $count = count($items);
             $rates = [];
 
+            // --- 随机分配逻辑 (保持不变) ---
             if ($count === 1) {
                 $rates = [100];
             } else {
-                $sum = 0;
-                $temp = [];
+                $sum = 0; $temp = [];
                 for ($i = 0; $i < $count; $i++) {
                     $rand = rand(10, 100);
-                    $temp[] = $rand;
-                    $sum += $rand;
+                    $temp[] = $rand; $sum += $rand;
                 }
-
                 $currentTotal = 0;
                 foreach ($temp as $i => $weight) {
                     if ($i === $count - 1) {
@@ -503,20 +499,25 @@ class RuleSetService extends Service
                     } else {
                         $val = (int)round(($weight / $sum) * 100);
                         $val = $val <= 0 ? 1 : $val;
-                        $rates[] = $val;
-                        $currentTotal += $val;
+                        $rates[] = $val; $currentTotal += $val;
                     }
                 }
             }
 
+            // --- 数据组装 ---
             foreach ($items as $index => $itemId) {
+                $info = $infoMap[$dataId] ?? null;
+                $itemInfo = $itemsData[$itemId] ?? null;
+
                 $result[] = [
-                    'type' => $type,
-                    'data_id' => $dataId,
-                    'data_title' => $nameMap[$dataId] ?? '', // 人员姓名或设备名称
-                    'item_id' => $itemId,
-                    'item_title' => $itemNames[$itemId] ?? '', // 项目名称
-                    'rate' => $rates[$index]
+                    'type'       => $type,
+                    'data_id'    => $dataId,
+                    'data_title' => $info->title ?? '',
+                    'data_code'  => ($type == RuleSetDetails::type_one) ? ($info->number ?? '') : ($info->code ?? ''),
+                    'item_id'    => $itemId,
+                    'item_title' => $itemInfo->title ?? '',
+                    'item_code'  => $itemInfo->code ?? '', // 项目编号
+                    'rate'       => $rates[$index]
                 ];
             }
         }