qiuenguang 11 месяцев назад
Родитель
Сommit
2419e89f90
1 измененных файлов с 47 добавлено и 16 удалено
  1. 47 16
      application/api/controller/OrderSuperLoss.php

+ 47 - 16
application/api/controller/OrderSuperLoss.php

@@ -1194,37 +1194,68 @@ class OrderSuperLoss extends Api
                 }
                 }
             }
             }
         }
         }
+
+//        $result = [];
+//        foreach ($data as $item) {
+//            $cj = $item['cj'];
+//            $bh = $item['bh'];
+//            $je = round($item['je'],2); // 保留为整数格式
+//
+//            // 如果 cj 不存在于结果数组中,则初始化
+//            if (!isset($result[$cj])) {
+//                $result[$cj] = array();
+//            }
+//
+//            // 检查 bh 是否已经存在于当前 cj 的子数组中
+//            $found = false;
+//            foreach ($result[$cj] as &$subItem) {
+//                if ($subItem['bh'] === $bh) {
+//                    $subItem['je'] = (float)($subItem['je'] ?? 0);
+//                    $found = true;
+//                    break;
+//                }
+//            }
+//
+//            // 如果 bh 不存在,则添加新的子数组
+//            if (!$found) {
+//                $result[$cj][] = array(
+//                    'bh' => $bh,
+//                    'xm' => $item['xm'],
+//                    'je' => (float)$je, // 保留为浮点数格式
+//                    'cj' => $cj
+//                );
+//            }
+//        }
         $result = [];
         $result = [];
+
         foreach ($data as $item) {
         foreach ($data as $item) {
             $cj = $item['cj'];
             $cj = $item['cj'];
             $bh = $item['bh'];
             $bh = $item['bh'];
-            $je = round($item['je'],2); // 保留为整数格式
+            $je = round($item['je'], 2); // 保留两位小数
 
 
             // 如果 cj 不存在于结果数组中,则初始化
             // 如果 cj 不存在于结果数组中,则初始化
             if (!isset($result[$cj])) {
             if (!isset($result[$cj])) {
-                $result[$cj] = array();
-            }
-
-            // 检查 bh 是否已经存在于当前 cj 的子数组中
-            $found = false;
-            foreach ($result[$cj] as &$subItem) {
-                if ($subItem['bh'] === $bh) {
-                    $subItem['je'] = (float)($subItem['je'] ?? 0);
-                    $found = true;
-                    break;
-                }
+                $result[$cj] = []; // 初始化车间数据
             }
             }
 
 
-            // 如果 bh 不存在,则添加新的子数组
-            if (!$found) {
-                $result[$cj][] = array(
+            // 如果 bh 已经存在于当前车间,累加 je
+            if (isset($result[$cj][$bh])) {
+                $result[$cj][$bh]['je'] += (float)$je; // 累加 je
+            } else {
+                // 如果 bh 不存在,则添加新的记录
+                $result[$cj][$bh] = [
                     'bh' => $bh,
                     'bh' => $bh,
                     'xm' => $item['xm'],
                     'xm' => $item['xm'],
                     'je' => (float)$je, // 保留为浮点数格式
                     'je' => (float)$je, // 保留为浮点数格式
                     'cj' => $cj
                     'cj' => $cj
-                );
+                ];
             }
             }
         }
         }
+
+        // 将每个车间的子数组变成索引数组
+        foreach ($result as $cjName => &$items) {
+            $items = array_values($items); // 重建索引
+        }
         $this->success('成功', $result);
         $this->success('成功', $result);
     }
     }