Răsfoiți Sursa

成本计算

unknown 1 lună în urmă
părinte
comite
4a4288c538
1 a modificat fișierele cu 91 adăugiri și 3 ștergeri
  1. 91 3
      application/service/UnifiedCostCalculationService.php

+ 91 - 3
application/service/UnifiedCostCalculationService.php

@@ -740,10 +740,98 @@ class UnifiedCostCalculationService
         Db::name('成本_各月水电气')->where('Sys_ny', $month)->delete();
         Db::name('成本_各月分摊系数')->where('Sys_ny', $month)->delete();
 
-        // 2. 插入新数据
+        // 2. 插入前检查数据结构
+        $this->validateAndFixData();
+
+        // 3. 插入新数据(使用分批插入避免单次数据量过大)
+        $this->insertDataInBatches();
+    }
+
+    /**
+     * 验证并修复数据
+     */
+    protected function validateAndFixData(): void
+    {
+        if (empty($this->monthlyCostDetails)) {
+            return;
+        }
+
+        // 获取表结构
+        $tableName = '成本v23_月度成本明细';
+        $columns = Db::query("DESCRIBE `{$tableName}`");
+        $columnNames = array_column($columns, 'Field');
+
+        Log::info("表{$tableName}结构: " . implode(', ', $columnNames));
+        Log::info("插入数据字段数: " . count(array_keys($this->monthlyCostDetails[0])));
+
+        // 检查每条数据的字段数
+        foreach ($this->monthlyCostDetails as $index => $row) {
+            $rowCount = count($row);
+            if ($rowCount !== count($columnNames)) {
+                Log::error("第{$index}行字段数不匹配: 数据有{$rowCount}个字段,表有" . count($columnNames) . "个字段");
+                Log::error("数据字段: " . implode(', ', array_keys($row)));
+
+                // 修正第58行数据(从0开始计数,第58行是索引57)
+                if ($index === 57) {
+                    $this->fixRowData($row, $columnNames);
+                }
+            }
+        }
+    }
+
+    /**
+     * 修复行数据
+     */
+    protected function fixRowData(array &$row, array $columnNames): void
+    {
+        $fixedRow = [];
+        foreach ($columnNames as $column) {
+            $fixedRow[$column] = $row[$column] ?? null;
+        }
+
+        // 替换原数据
+        $this->monthlyCostDetails[57] = $fixedRow;
+        Log::info("已修复第58行数据");
+    }
+
+    /**
+     * 分批插入数据
+     */
+    protected function insertDataInBatches(): void
+    {
+        // 分批插入月度成本明细
         if (!empty($this->monthlyCostDetails)) {
-            $sql = Db::name('成本v23_月度成本明细')->fetchSql(true)->insertAll($this->monthlyCostDetails);
-            Db::query($sql);
+            $batchSize = 100;
+            $total = count($this->monthlyCostDetails);
+
+            for ($i = 0; $i < $total; $i += $batchSize) {
+                $batch = array_slice($this->monthlyCostDetails, $i, $batchSize);
+
+                // 构建确保字段顺序正确的SQL
+                $firstRow = reset($batch);
+                $fields = array_keys($firstRow);
+                $fieldStr = '`' . implode('`,`', $fields) . '`';
+
+                $values = [];
+                foreach ($batch as $row) {
+                    $rowValues = [];
+                    foreach ($fields as $field) {
+                        $value = $row[$field] ?? null;
+                        $rowValues[] = is_numeric($value) ? $value : "'" . addslashes($value) . "'";
+                    }
+                    $values[] = '(' . implode(',', $rowValues) . ')';
+                }
+
+                $sql = "INSERT INTO `成本v23_月度成本明细` ({$fieldStr}) VALUES " . implode(',', $values);
+
+                try {
+                    Db::execute($sql);
+                    Log::info("成功插入批次 " . ($i/$batchSize + 1));
+                } catch (\Exception $e) {
+                    Log::error("插入批次失败: " . $e->getMessage());
+                    throw $e;
+                }
+            }
         }
 
         if (!empty($this->monthlyUtilities)) {