unknown 2 дней назад
Родитель
Сommit
b0a64e0fd3

+ 185 - 193
application/api/controller/OrderSuperLoss.php

@@ -924,13 +924,8 @@ class OrderSuperLoss extends Api
         $sql =db('工单_印件资料')->where($where)->fetchSql(true)->update(['质量考核'=>$examine]);
         $res = \db()->query($sql);
         $returnData = $this->OneOrderSuperLoss($where['Yj_Gdbh'],$where['yj_Yjno']);
-        db('工单_质量考核汇总')
-            ->where('Gy0_gdbh',$where['Yj_Gdbh'])
-            ->where('印件及工序',$where['yj_Yjno'])
-            ->delete();
-        $sql = db('工单_质量考核汇总')->fetchSql(true)->insertAll($returnData);
-        $result = db()->query($sql);
-        if($result===false) $this->error('失败');
+        $result = $this->saveSuperLossResult($where['Yj_Gdbh'], $where['yj_Yjno'], $returnData);
+        if($result === false) $this->error('失败');
         if ($res !== false){
             $this->success('更新成功');
         }else{
@@ -1055,15 +1050,7 @@ class OrderSuperLoss extends Api
          **/
 
         $result = $this->OneOrderSuperLoss($list['st_gdbh'],$yjno);
-
-        //先删除数据
-        \db('工单_质量考核汇总')
-            ->where('Gy0_gdbh',$list['st_gdbh'])
-            ->where('印件及工序',$yjno)
-            ->delete();
-        //插入新数据
-        $lossSql = \db('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
-        $lossRes = \db()->query($lossSql);
+        $lossRes = $this->saveSuperLossResult($list['st_gdbh'], $yjno, $result);
         if ($i != 0 && $lossRes !== false){
             $this->success('更新成功');
         }else{
@@ -1277,17 +1264,17 @@ class OrderSuperLoss extends Api
 public function OneOrderSuperLoss($gdbh, $yjno)
 {
     // ========== 阶段1: 基础数据查询 ==========
-    // 获取工单末板入仓数据
-    $workOrderData = $this->getWorkOrderWarehouse($gdbh, $yjno);
-    if (empty($workOrderData)) {
-        $this->error("工单{$gdbh}-{$yjno}未查询到末板入仓记录");
+    $warehouseInfo = $this->getWorkOrderWarehouseInfo($gdbh, $yjno);
+    if (empty($warehouseInfo)) {
+        return false;
     }
-    
-    // 获取工单入仓总数量
-    $totalNumber = $this->getTotalWarehouseCount($gdbh, $yjno);
+
+    $workOrderData = $warehouseInfo['warehouse'];
+    $totalNumber = $warehouseInfo['total_number'];
+
     // 获取工单资料和投料数据
     $workOrderInfo = $this->getWorkOrderInfo($gdbh, $yjno);
-    
+
     // 投料为空则返回失败
     if (empty($workOrderInfo['投料'])) {
         return false;
@@ -1308,7 +1295,7 @@ public function OneOrderSuperLoss($gdbh, $yjno)
     // 获取有报工记录的工序列表
     $gxhValues = $this->getReportedProcesses($workOrderData['jjcp_gdbh'], $workOrderData['jjcp_yjno']);
     if (empty($gxhValues)) {
-        $this->error("工单{$workOrderData['jjcp_gdbh']}-{$workOrderData['jjcp_yjno']}未查询到报工信息");
+        return false;
     }
     
     // ========== 阶段4: 合格率与废品计算 ==========
@@ -1321,7 +1308,7 @@ public function OneOrderSuperLoss($gdbh, $yjno)
     // 处理核检单废品上报数据,分类统计各类废品
     $wasteResult = $this->processWasteData($workOrderData['jjcp_gdbh'], $workOrderData['jjcp_yjno']);
     $data = array_merge($data, $wasteResult['summary']);
-    $wasteDetails = $wasteResult['details'];
+    $wasteDetails = $wasteResult['details']; 
     
     // ========== 阶段6: 工序产量数据获取 ==========
     // 获取按班组分的产量数据和工序汇总数据
@@ -1338,42 +1325,66 @@ public function OneOrderSuperLoss($gdbh, $yjno)
     );
     
     // ========== 阶段8: 结果整合 ==========
-    // 构建最终的超节损结果数据结构
+    // 构建最终的超节损结果数据结构 
     return $this->buildResult($gyData, $data, $totalNumber, $workOrderData);
 }
 
 // ========== 私有方法实现 ==========
 
 /**
- * 获取工单末板入仓数据
- * 
+ * 获取工单末板入仓数据及入仓总量
+ *
  * @param string $gdbh 工单编号
  * @param string $yjno 印件号
- * @return array 入仓数据
+ * @return array|null
  */
-private function getWorkOrderWarehouse($gdbh, $yjno)
+private function getWorkOrderWarehouseInfo($gdbh, $yjno)
 {
-    return \db('成品入仓')
+    $warehouse = \db('成品入仓')
         ->where('jjcp_gdbh', $gdbh)
         ->where('jjcp_yjno', $yjno)
-        ->where('jjcp_smb', '末 板')  // 只查询末板记录
+        ->where('jjcp_smb', '末 板')
         ->find();
+
+    if (empty($warehouse)) {
+        return null;
+    }
+
+    $totalNumber = \db('成品入仓')
+        ->where('jjcp_gdbh', $gdbh)
+        ->where('jjcp_yjno', $yjno)
+        ->sum('jjcp_sl') ?: 0;
+
+    return [
+        'warehouse' => $warehouse,
+        'total_number' => $totalNumber,
+    ];
+}
+
+/**
+ * 获取工单末板入仓数据
+ *
+ * @param string $gdbh 工单编号
+ * @param string $yjno 印件号
+ * @return array
+ */
+private function getWorkOrderWarehouse($gdbh, $yjno)
+{
+    $info = $this->getWorkOrderWarehouseInfo($gdbh, $yjno);
+    return $info ? $info['warehouse'] : [];
 }
 
 /**
  * 获取工单入仓总数量
- * 
+ *
  * @param string $gdbh 工单编号
  * @param string $yjno 印件号
- * @return int 入仓总数量
+ * @return int
  */
 private function getTotalWarehouseCount($gdbh, $yjno)
 {
-    $result = \db('成品入仓')
-        ->where('jjcp_gdbh', $gdbh)
-        ->where('jjcp_yjno', $yjno)
-        ->column('SUM(jjcp_sl)');  // 汇总入仓数量
-    return $result ? $result[0] : 0;
+    $info = $this->getWorkOrderWarehouseInfo($gdbh, $yjno);
+    return $info ? (int)$info['total_number'] : 0;
 }
 
 /**
@@ -1692,60 +1703,81 @@ private function adjustProduction($item, $totalClData)
 
 /**
  * 计算班组质检废品
- * 
+ *
  * @param array $item 工序数据项
- * @param array $wasteDetails 废品详情数据
+ * @param array $wasteIndex 废品索引
  * @return int 质检废品数量
  */
-private function calculateQualityWaste($item, $wasteDetails)
+private function calculateQualityWaste($item, array $wasteIndex)
 {
-    $zjfp = 0;
-    
-    // 遍历废品详情,匹配工序和班组
+    $gxh = str_pad((string)(int)$item['Gy0_gxh'], 2, '0', STR_PAD_LEFT);
+    $bh = trim((string)($item['sczl_bh1'] ?: ($item['sczl_bh'] ?? '')));
+    return (int)($wasteIndex[$gxh . '-' . $bh] ?? 0);
+}
+
+/**
+ * 构建质检废品索引
+ *
+ * @param array $wasteDetails
+ * @return array
+ */
+private function buildWasteIndex(array $wasteDetails)
+{
+    $index = [];
     foreach ($wasteDetails as $entry) {
         for ($i = 1; $i <= 13; $i++) {
             $gxmc = $entry["fp_gxmc{$i}"] ?? '';
-            $bh = $entry["fp_bh{$i}"] ?? '';
-            
-            // 匹配工序编号和班组编号
-            if ((int)substr($gxmc, 0, 2) == $item['Gy0_gxh'] && $bh == $item['sczl_bh1']) {
-                $zjfp += $entry["fp_sl{$i}"] ?? 0;
+            $bh = trim((string)($entry["fp_bh{$i}"] ?? ''));
+            $slValue = (int)($entry["fp_sl{$i}"] ?? 0);
+            if ($gxmc === '' || $bh === '' || $slValue === 0) {
+                continue;
             }
+            $gxh = str_pad((string)(int)substr($gxmc, 0, 2), 2, '0', STR_PAD_LEFT);
+            $key = $gxh . '-' . $bh;
+            $index[$key] = ($index[$key] ?? 0) + $slValue;
         }
     }
-    
-    return $zjfp;
+    return $index;
+}
+
+/**
+ * 构建工序产量汇总索引
+ *
+ * @param array $totalClData
+ * @return array
+ */
+private function buildProductionSummaryIndex(array $totalClData)
+{
+    $index = [];
+    foreach ($totalClData as $row) {
+        $index[$row['Gy0_yjno'] . '-' . $row['Gy0_gxh']] = $row;
+    }
+    return $index;
 }
 
 /**
  * 计算工序产量
- * 
+ *
  * @param array $item 工序数据项
- * @param array $totalClData 产量汇总数据
+ * @param array $productionIndex 产量汇总索引
  * @return int 工序产量
  */
-private function calculateProcessProduction($item, $totalClData)
+private function calculateProcessProduction($item, array $productionIndex)
 {
-    foreach ($totalClData as $v) {
-        $banju = $v['版距'] / 1000;
-        $totalCl = $v['total_cl'];
-        
-        // 卷对卷需调整
-        if ($v['印刷方式'] === '卷对卷') {
-            $totalCl = $totalCl / $banju;
-        }
-        
-        // 匹配工序
-        if ($v['Gy0_yjno'] === $item['Gy0_yjno'] && $v['Gy0_gxh'] === $item['Gy0_gxh']) {
-            // 拆片工序使用报工产量,其他工序使用设备产量
-            // 注意:这里需要乘以 ls 和 ks,因为 total_cl 来自数据库原始值
-            return strpos($v['gxmc'], '拆片') !== false 
-                ? $v['cl'] * $item['Gy0_ls'] * $item['Gy0_ks']
-                : $totalCl * $item['Gy0_ls'] * $item['Gy0_ks'];
-        }
+    $row = $productionIndex[$item['Gy0_yjno'] . '-' . $item['Gy0_gxh']] ?? null;
+    if (empty($row)) {
+        return 0;
     }
-    
-    return 0;
+
+    $banju = $row['版距'] / 1000;
+    $totalCl = $row['total_cl'];
+    if ($row['印刷方式'] === '卷对卷' && $banju > 0) {
+        $totalCl = $totalCl / $banju;
+    }
+
+    return strpos($row['gxmc'], '拆片') !== false
+        ? (int)($row['cl'] * $item['Gy0_ls'] * $item['Gy0_ks'])
+        : (int)($totalCl * $item['Gy0_ls'] * $item['Gy0_ks']);
 }
 
 /**
@@ -1770,15 +1802,19 @@ private function cleanupProcessData(&$item)
  */
 private function calculateSuperLoss($gyData, $totalClData, $wasteDetails, $data, $workOrderData)
 {
-    $sumBz = [];  // 班组产量汇总
+    $sumBz = [];
     $planTotal = $this->getPlanLossTotal($workOrderData, $gyData);
+    $productionIndex = $this->buildProductionSummaryIndex($totalClData);
+    $wasteIndex = $this->buildWasteIndex($wasteDetails);
 
     // 第一步:预计算班组产量总和
     foreach ($gyData as &$item) {
         $this->calculateProduction($item);
-        $key = "{$item['sczl_bh1']}-{$item['Gy0_gxh']}";
+        $bh = $item['sczl_bh1'] ?: ($item['sczl_bh'] ?? '');
+        $key = "{$bh}-{$item['Gy0_gxh']}";
         $sumBz[$key] = isset($sumBz[$key]) ? $sumBz[$key] + $item['total_cl'] : $item['total_cl'];
     }
+    unset($item);
 
     // 第二步:收集所有员工编号用于批量查询
     $employeeIds = [];
@@ -1789,63 +1825,50 @@ private function calculateSuperLoss($gyData, $totalClData, $wasteDetails, $data,
             }
         }
     }
-    halt($gyData);
-    // 批量查询员工姓名
+
     $employeeNames = $this->getEmployeeNames(array_unique($employeeIds));
 
     // 第三步:计算每道工序的超节损数据
     foreach ($gyData as $kk => &$vv) {
-        // 注意:calculateProduction() 已在之前调用,total_cl 已乘以 ls 和 ks
-        // 现在只需要调整卷对卷的情况
         $vv['total_cl'] = $this->adjustProduction($vv, $totalClData);
-
-        // 计算计划损耗
         $vv['Gy0_计划损耗'] = $vv['Gy0_计划损耗'] * $vv['Gy0_ls'] * $vv['Gy0_ks'];
 
-        // 根据工序总产量计算班组分摊计划损耗
-        foreach ($totalClData as $v) {
-            if ($vv['Gy0_yjno'] == $v['Gy0_yjno'] && $vv['Gy0_gxh'] == $v['Gy0_gxh']) {
-                $cl = $v['total_cl'] ?: $v['cl'];
-                $vv['计划损耗'] = round($vv['total_cl'] / ($cl * $v['Gy0_ls']) * $vv['Gy0_计划损耗']);
-                break;
-            }
+        $productionRow = $productionIndex[$vv['Gy0_yjno'] . '-' . $vv['Gy0_gxh']] ?? null;
+        if (!empty($productionRow)) {
+            $cl = $productionRow['total_cl'] ?: $productionRow['cl'];
+            $denominator = $cl * $productionRow['Gy0_ls'];
+            $vv['计划损耗'] = $denominator > 0
+                ? round($vv['total_cl'] / $denominator * $vv['Gy0_计划损耗'])
+                : 0;
+        } else {
+            $vv['计划损耗'] = 0;
         }
-        
-        // 计算制程废品
+
         $vv['sczl_zcfp'] = $vv['sczl_zcfp'] ? $vv['sczl_zcfp'] * $vv['Gy0_ls'] : ($vv['fp'] * $vv['Gy0_ls']);
-        
-        // 设置工序名称和班组编号
         $vv['sczl_gxmc'] = $vv['sczl_gxmc'] ?: $vv['gxmc'];
         $vv['sczl_bh1'] = $vv['sczl_bh1'] ?: $vv['sczl_bh'];
-        
-        // 计算质检废品
-        $vv['zjfp'] = $this->calculateQualityWaste($vv, $wasteDetails);
-        $vv['质检废'] = round($vv['zjfp'] * ($vv['total_cl'] / $sumBz["{$vv['sczl_bh1']}-{$vv['Gy0_gxh']}"]));
-        
-        // 无形损分摊
-        // 计算工序无形损(基于计划损耗分摊)
+
+        $vv['zjfp'] = $this->calculateQualityWaste($vv, $wasteIndex);
+        $sumKey = "{$vv['sczl_bh1']}-{$vv['Gy0_gxh']}";
+        $teamTotal = $sumBz[$sumKey] ?? 0;
+        $vv['质检废'] = $teamTotal > 0 ? round($vv['zjfp'] * ($vv['total_cl'] / $teamTotal)) : 0;
+
         $rawIntangibleLoss = $planTotal > 0 ? $vv['计划损耗'] / $planTotal * $data['intangible_loss'] : 0;
-        // 计算工序计划产量的千分之三作为阈值
         $intangibleThreshold = $vv['计划产量'] * 0.003;
-        // 判断:如果工序无形损不超过千分之三,则计为0;超过千分之三的部分取正整数计入超节损
         if ($rawIntangibleLoss <= $intangibleThreshold) {
             $vv['无形损分摊'] = 0;
         } else {
-            // 超过千分之三的部分,取正整数
             $vv['无形损分摊'] = max(0, (int)($rawIntangibleLoss - $intangibleThreshold));
         }
-                
-        // 填充员工姓名
+
         for ($j = 1; $j <= 10; $j++) {
             $vv["sczl_name{$j}"] = !empty($vv["sczl_bh{$j}"]) ? ($employeeNames[$vv["sczl_bh{$j}"]] ?? '') : '';
         }
-        
-        // 计算工序产量
-        $vv['工序产量'] = $this->calculateProcessProduction($vv, $totalClData);
-        
-        // 清理临时字段
+
+        $vv['工序产量'] = $this->calculateProcessProduction($vv, $productionIndex);
         $this->cleanupProcessData($vv);
     }
+    unset($vv);
 
     // 第四步:计算节损奖
     // 查询产品规格,确定核算单价
@@ -1978,6 +2001,29 @@ private function getEmployeeNames($employeeIds)
         ->column('rtrim(员工姓名)', '员工编号');
 }
 
+/**
+ * 保存单工单超节损计算结果
+ *
+ * @param string $gdbh
+ * @param string $yjno
+ * @param array|false $result
+ * @return bool
+ */
+public function saveSuperLossResult($gdbh, $yjno, $result)
+{
+    if (empty($result) || !is_array($result)) {
+        return false;
+    }
+
+    Db::name('工单_质量考核汇总')
+        ->where('Gy0_gdbh', $gdbh)
+        ->where('印件及工序', $yjno)
+        ->delete();
+
+    $insertSql = Db::name('工单_质量考核汇总')->fetchSql(true)->insertAll($result);
+    return Db::query($insertSql) !== false;
+}
+
 
     /**
      * 超节损列表
@@ -2074,29 +2120,21 @@ private function getEmployeeNames($employeeIds)
             ->limit($limit)
             ->select();
         // 处理查询结果
-        foreach ($list as $key => $value) {
-            $status = \db('工单_印件资料')
-                ->where('Yj_Gdbh', $value['Gd_gdbh'])
-                ->where('yj_Yjno', $value['jjcp_yjno'])
-                ->value('质量考核');
-            $processDetail = \db('工单_工艺资料')
-                ->where('Gy0_gdbh', $value['Gd_gdbh'])
-                ->where('Gy0_yjno', $value['jjcp_yjno'])
-                ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_ls as 联数')
+        $statusMap = [];
+        if (!empty($list)) {
+            $orderIds = array_unique(array_column($list, 'Gd_gdbh'));
+            $statusRows = \db('工单_印件资料')
+                ->where('Yj_Gdbh', 'in', $orderIds)
+                ->field('Yj_Gdbh, yj_Yjno, 质量考核')
                 ->select();
-            $number1 = $number2 = 1;
-            foreach ($processDetail as $k => $v) {
-                if (strpos($v['工序名称'],'分切') !== false) {
-                    $number1 = $v['联数'];
-                }
-                if (strpos($v['工序名称'],'分切') === false && $v['联数'] > 1) {
-                    $number2 = $v['联数'];
-                }
-            }
-            $Multiple = $number1/$number2;
-            if (is_int($Multiple) === false) {
-                $Multiple = 1;
+            foreach ($statusRows as $row) {
+                $statusMap[$row['Yj_Gdbh'] . '|' . $row['yj_Yjno']] = $row['质量考核'];
             }
+        }
+
+        foreach ($list as $key => $value) {
+            $statusKey = $value['Gd_gdbh'] . '|' . $value['jjcp_yjno'];
+            $status = $statusMap[$statusKey] ?? null;
             $list[$key]['工单质检废'] = $value['废品合计'] - $value['zcfp'];
             $list[$key]['质量考核'] = $status;
             $list[$key]['考核'] = ($status == 1) ? '否' : '是';
@@ -2109,7 +2147,7 @@ private function getEmployeeNames($employeeIds)
                 $list[$key]['target_rate'] = null;
                 $list[$key]['real_rate'] = null;
             }
-            $list[$key]['reward_rate'] = '1'; // or some logic if needed
+            $list[$key]['reward_rate'] = '1';
         }
         if (!empty($list)) {
             $this->success('成功', $list);
@@ -2180,34 +2218,20 @@ private function getEmployeeNames($employeeIds)
             ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_ls as 联数')
             ->select();
 
-            $status = \db('工单_印件资料')
-    ->where('Yj_Gdbh', $param['order'])
-    ->where('yj_Yjno', $param['yjno'])
-    ->value('质量考核');
-
-    // 添加到 $list 数组中
-    $list['质量考核'] = $status;
-    $list['考核'] = ($status == 1) ? '不参与考核' : '参与考核';
-//        $number1 = $number2 = 1;
-//        foreach ($processDetail as $k => $v) {
-//            if (strpos($v['工序名称'],'分切') !== false) {
-//                $number1 = $v['联数'];
-//            }
-//            if (strpos($v['工序名称'],'分切') === false && $v['联数'] > 1) {
-//                $number2 = $v['联数'];
-//            }
-//        }
-//        $Multiple = $number1/$number2;
-//        if (is_int($Multiple) === false) {
-//            $Multiple = 1;
-//        }
+        $status = \db('工单_印件资料')
+            ->where('Yj_Gdbh', $param['order'])
+            ->where('yj_Yjno', $param['yjno'])
+            ->value('质量考核');
+
+        // 添加到 $list 数组中
+        $list['质量考核'] = $status;
+        $list['考核'] = ($status == 1) ? '不参与考核' : '参与考核';
         $list['实际投料'] = $list['实际投料']*10000;
         //目标合格率
         $list['target_rate'] = ($list['实际投料']-$list['计划损耗'])/$list['实际投料']*100;
         $list['target_rate'] = number_format($list['target_rate'],2);
         $list['target_rate'] = $list['target_rate'].'%';
         //实际合格率
-//        $list['real_rate'] = $list['warehousing_num']/($list['实际投料']*$Multiple)*100;
         $list['real_rate'] = $list['warehousing_num']/($list['实际投料'])*100;
         $list['real_rate'] = number_format($list['real_rate'],2);
         $list['real_rate'] = $list['real_rate'].'%';
@@ -2342,28 +2366,14 @@ private function getEmployeeNames($employeeIds)
             ->field('CONCAT(rtrim(Gy0_gxmc),"[",rtrim(Add_gxmc),"]") as 工序名称,Gy0_ls as 联数')
             ->select();
 
-            $status = \db('工单_印件资料')
-    ->where('Yj_Gdbh', $param['order'])
-    ->where('yj_Yjno', $param['yjno'])
-    ->value('质量考核');
+        $status = \db('工单_印件资料')
+            ->where('Yj_Gdbh', $param['order'])
+            ->where('yj_Yjno', $param['yjno'])
+            ->value('质量考核');
 
         // 添加到 $list 数组中
         $list['质量考核'] = $status;
         $list['考核'] = ($status == 1) ? '不参与考核' : '参与考核';
-//        $number1 = $number2 = 1;
-//        foreach ($processDetail as $k => $v) {
-//            if (strpos($v['工序名称'],'分切') !== false) {
-//                $number1 = $v['联数'];
-//            }
-//            if (strpos($v['工序名称'],'分切') === false && $v['联数'] > 1) {
-//                $number2 = $v['联数'];
-//            }
-//        }
-//        $Multiple = $number1/$number2;
-//        if (is_int($Multiple) === false) {
-//            $Multiple = 1;
-//        }
-
         $list['实际投料'] = $list['实际投料']*10000;
         //目标合格率
         $list['target_rate'] = ($list['实际投料']-$list['计划损耗'])/$list['实际投料']*100;
@@ -2636,26 +2646,8 @@ private function getEmployeeNames($employeeIds)
 
             // 14. 重新计算工单超节损数据
             $resultAdd = $this->OneOrderSuperLoss($param['st_gdbh'], $yjNo);
-
-            // 15. 更新质量考核汇总 - 先删除
-            $deleteSql = "DELETE FROM `工单_质量考核汇总` WHERE `Gy0_gdbh` = ? AND `印件及工序` = ?";
-            Db::execute($deleteSql, [$param['st_gdbh'], $yjNo]);
-
-            // 16. 插入新的质量考核数据
-            if (!empty($resultAdd)) {
-                foreach ($resultAdd as $item) {
-                    $fields = array_keys($item);
-                    $values = array_values($item);
-
-                    $fieldList = implode(', ', array_map(function($field) {
-                        return strpos($field, '`') === 0 ? $field : "`{$field}`";
-                    }, $fields));
-
-                    $placeholders = implode(', ', array_fill(0, count($values), '?'));
-
-                    $insertSql = "INSERT INTO `工单_质量考核汇总` ({$fieldList}) VALUES ({$placeholders})";
-                    Db::execute($insertSql, $values);
-                }
+            if (!$this->saveSuperLossResult($param['st_gdbh'], $yjNo, $resultAdd)) {
+                throw new Exception('超节损数据保存失败');
             }
 
             // 17. 提交事务

+ 5 - 11
application/job/OrderLossQueueWorker.php

@@ -96,17 +96,11 @@ class OrderLossQueueWorker
         $api = new OrderSuperLoss();
         $result = $api->OneOrderSuperLoss($task['gdbh'], $task['yjno']);
 
-        // 数据库操作
-        Db::name('工单_质量考核汇总')
-            ->where('Gy0_gdbh', $task['gdbh'])
-            ->where('印件及工序', $task['yjno'])
-            ->delete();
-
-        $insertSql = Db::name('工单_质量考核汇总')
-            ->fetchSql(true)
-            ->insertAll($result);
-        $insertRes = Db::query($insertSql);
-        if ($insertRes === false) {
+        if (empty($result) || !is_array($result)) {
+            return;
+        }
+
+        if (!$api->saveSuperLossResult($task['gdbh'], $task['yjno'], $result)) {
             throw new \RuntimeException("超节损队列插入失败: {$task['gdbh']}-{$task['yjno']}");
         }
     }

+ 1 - 10
application/job/OrderSuperLossJob.php

@@ -31,16 +31,7 @@ class OrderSuperLossJob
                 return;
             }
 
-            Db::name('工单_质量考核汇总')
-                ->where('Gy0_gdbh', $data['gdbh'])
-                ->where('印件及工序', $data['yjno'])
-                ->delete();
-
-            $insertSql = Db::name('工单_质量考核汇总')
-                ->fetchSql(true)
-                ->insertAll($result);
-            $insertRes = Db::query($insertSql);
-            if ($insertRes === false) {
+            if (!$api->saveSuperLossResult($data['gdbh'], $data['yjno'], $result)) {
                 throw new \RuntimeException('超节损队列写入失败');
             }
             $job->delete();