cqp 22 часов назад
Родитель
Сommit
ebd4aa44bb
1 измененных файлов с 53 добавлено и 86 удалено
  1. 53 86
      app/Service/U8ThirtyPartyDatabaseServerService.php

+ 53 - 86
app/Service/U8ThirtyPartyDatabaseServerService.php

@@ -478,51 +478,6 @@ class U8ThirtyPartyDatabaseServerService extends Service
         }
     }
 
-    public function updateDhOrder($update, $insert)
-    {
-        try {
-            // 1. 先通过心跳探测获取一个可靠的连接
-            $db = $this->safeDb();
-
-            return $db->transaction(function () use ($update, $insert,$db) {
-
-                // 1. 处理更新部分 (子表: PU_ArrivalVouchs)
-                if (!empty($update)) {
-                    foreach ($update as $autoId => $fields) {
-                        $db->table('PU_ArrivalVouchs') // 修正为子表名
-                            ->where('Autoid', $autoId)  // U8 习惯大写 A 小写 utoid,但 SQL 不敏感
-                            ->update($fields);
-                    }
-                }
-
-                // 2. 处理插入部分 (拆分行)
-                if (!empty($insert)) {
-                    $mainId = $insert[0]['ID'] ?? null;
-
-                    if ($mainId) {
-                        // 获取全表最大 Autoid,确保全局唯一
-                        $maxRowID = $db->table('PU_ArrivalVouchs') // 修正为子表名
-                                ->max('Autoid') ?? 0;
-
-                        foreach ($insert as &$newRow) {
-                            $maxRowID++;
-                            $newRow['Autoid'] = $maxRowID;
-                        }
-
-                        // 使用批量插入
-                        $db->table('PU_ArrivalVouchs')
-                            ->insert($insert);
-                    }
-                }
-
-                return [true, '']; // 事务成功返回
-            });
-        } catch (\Throwable $e) {
-            // 打印错误堆栈有助于调试
-            return [false, "修改采购到货单失败: " . $e->getMessage()];
-        }
-    }
-
     public function getCgOrder($id)
     {
         try {
@@ -584,46 +539,6 @@ class U8ThirtyPartyDatabaseServerService extends Service
         }
     }
 
-    public function updateLLOrder($update, $insert)
-    {
-        try {
-            $db = $this->safeDb();
-            return $db->transaction(function () use ($update, $insert, $db) {
-
-                // 1. 处理更新部分 (子表: MaterialAppVouchs)
-                if (!empty($update)) {
-                    foreach ($update as $autoId => $fields) {
-                        $db->table('MaterialAppVouchs') // 领料申请单子表
-                            ->where('autoid', $autoId)    // 注意字段名在 U8 里可能是 autoid 或 AutoID
-                            ->update($fields);
-                    }
-                }
-
-                // 2. 处理插入部分 (拆分行/新增行)
-                if (!empty($insert)) {
-                    // 获取当前领料申请单子表的最大 Autoid,确保手动分配不冲突
-                    $maxRowID = $db->table('MaterialAppVouchs')
-                            ->max('autoid') ?? 0;
-
-                    foreach ($insert as &$newRow) {
-                        $maxRowID++;
-                        // U8 的 autoid 通常必须手动指定
-                        $newRow['autoid'] = $maxRowID;
-                    }
-
-                    // 使用批量插入
-                    $db->table('MaterialAppVouchs')
-                        ->insert($insert);
-                }
-
-                return [true, ''];
-            });
-        } catch (\Throwable $e) {
-            // 建议记录详细日志
-            return [false, "修改领料申请单失败: " . $e->getMessage()];
-        }
-    }
-
     public function getLLOrder($id)
     {
         try {
@@ -713,6 +628,58 @@ class U8ThirtyPartyDatabaseServerService extends Service
         }
     }
 
+    public function rebuildLLDetails($mainId, $insertData)
+    {
+        try {
+            $db = $this->safeDb();
+
+            $accId = '200';
+            return $db->transaction(function () use ($mainId, $insertData, $db, $accId) {
+
+                // 1. 获取并锁定当前的流水号
+                $identity = $db->table('UFSystem.dbo.UA_Identity')
+                    ->where('cAcc_Id', $accId)
+                    ->where('cVouchType', 'mv')
+                    ->lockForUpdate()
+                    ->first();
+
+                if (!$identity) return [false, "未在 UA_Identity 中找到账套 {$accId} 的 mv 类型配置"];
+
+                // 拿到当前的“起始种子”
+                $currentSeed = (int)$identity->iChildId;
+
+                // 2. 删除旧明细
+                $db->table('MaterialAppVouchs')->where('ID', $mainId)->delete();
+
+                // 3. 分配新 AutoID
+                $newData = [];
+                foreach ($insertData as $row) {
+                    $currentSeed++; // 每有一行,种子就往上加 1
+
+                    $row['AutoID'] = 1000000000 + $currentSeed;
+                    $row['ID'] = $mainId;
+                    $newData[] = $row;
+                }
+
+                if (!empty($newData)) {
+                    $db->table('MaterialAppVouchs')->insert($newData);
+                }
+
+                // 4. 【关键】此时的 $currentSeed 已经是经过循环累加后的最大值了
+                // 比如初始 2000,循环 3 次,现在就是 2003
+                $db->statement("
+                UPDATE UFSystem.dbo.UA_Identity 
+                SET iChildId = ? 
+                WHERE cAcc_Id = ? AND cVouchType = 'mv'
+            ", [$currentSeed, $accId]);
+
+                return [true, ''];
+            });
+        } catch (\Throwable $e) {
+            return [false, "领料申请单更新失败: " . $e->getMessage()];
+        }
+    }
+
     public function rebuildDhDetails($mainId, $insertData)
     {
         try {
@@ -745,7 +712,7 @@ class U8ThirtyPartyDatabaseServerService extends Service
         }
     }
 
-    public function rebuildLLDetails($mainId, $insertData)
+    public function rebuildLLDetails1($mainId, $insertData)
     {
         try {
             $db = $this->safeDb();