Przeglądaj źródła

成品入仓同步接口

m0_70156489 22 godzin temu
rodzic
commit
42e5f4a086
1 zmienionych plików z 143 dodań i 1 usunięć
  1. 143 1
      application/api/controller/Synchronization.php

+ 143 - 1
application/api/controller/Synchronization.php

@@ -2091,4 +2091,146 @@ class Synchronization extends Api
             $this->success('物料领用记录同步成功');
         }
     }
-}
+
+    /**
+     * 成品入仓数据同步中间校验接口
+     * @desc 逐条工单独立校验,一单一条操作日志
+     * 前置校验:先查询【成品入仓】表,校验该工单是否存在末板数据,无末版直接判定同步失败
+     * 固定三步校验逻辑:
+     * 第一步:查询【工单_印件资料】
+     * 第二步:查询【工单_bom资料】提取有效物料
+     * 第三步:查询【物料_收发记录】提取已有台账物料
+     * 比对第二步物料集合与第三步物料集合:完全一致则同步成功,否则同步失败
+     * 无论成功/失败,每条工单单独写入一条操作日志
+     * @request POST
+     * @body JSON 支持单条对象 / 多条数组批量提交
+     */
+    public function syncFinishStockMiddle()
+    {
+        if (!$this->request->isPost()) {
+            return json(['code' => 0, 'msg' => '仅支持POST请求']);
+        }
+        $jsonStr = file_get_contents('php://input');
+        $param   = json_decode($jsonStr, true);
+        if (!is_array($param)) {
+            return json(['code' => 0, 'msg' => 'JSON参数解析失败']);
+        }
+
+        // 兼容单条、多条入参格式
+        $taskList = isset($param[0]) && is_array($param[0]) ? $param : [$param];
+        $resultList = [];
+        // 循环处理每一条工单
+        foreach ($taskList as $item) {
+            $gdbh    = trim($item['Gdbh'] ?? '');
+            $yjno    = trim($item['yjno'] ?? '');
+            $sysId   = $item['sys_id'] ?? '';
+            $type    = $item['type'] ?? '';
+            $syncRq  = date('Y-m-d H:i:s');
+            // 默认标记为失败
+            $status  = '同步失败';
+            $msg     = '';
+            $yjmc    = '';
+            $yjdh    = '';
+            $wlbmStr = '';
+            $bomCodes = [];
+
+            try {
+                if (empty($gdbh) || empty($yjno)) {
+                    throw new Exception('工单编号或印件号为空');
+                }
+
+                // 【前置校验】查询成品入仓表,判断该工单是否存在末板数据
+                $cprc = Db::name('成品入仓')
+                    ->where([
+                        'jjcp_gdbh' => $gdbh,
+                        'jjcp_yjno' => $yjno,
+                        'jjcp_smb'  => '末 板'
+                    ])
+                    ->field('jjcp_gdbh,jjcp_yjno,jjcp_smb,jjcp_cpmc')
+                    ->find();
+                // 未查询到末版数据,直接抛出异常终止当前工单后续流程
+                if (empty($cprc)) {
+                    throw new Exception("工单【{$gdbh}】印件号【{$yjno}】未录入末版数据,无法继续同步");
+                }
+
+                // 第一步 查询工单_印件资料
+                $yinInfo = Db::name('工单_印件资料')
+                    ->where([
+                        'Yj_Gdbh' => $gdbh,
+                        'yj_Yjno' => $yjno
+                    ])
+                    ->field('Yj_Gdbh,yj_Yjdh,yj_yjmc,yj_Yjno')
+                    ->find();
+                if (empty($yinInfo)) {
+                    throw new Exception('工单_印件资料无数据');
+                }
+                $yjmc = $yinInfo['yj_yjmc'];
+                $yjdh = $yinInfo['yj_Yjdh'];
+
+                // 第二步 查询工单_bom资料
+                $bomCodes = Db::name('工单_bom资料')
+                    ->where([
+                        'BOM_工单编号' => $gdbh,
+                        'BOM_产品编号' => $yjdh
+                    ])
+                    ->where('BOM_实际用量', '<>', 0)
+                    ->group('BOM_物料编码')
+                    ->column('BOM_物料编码');
+                if (empty($bomCodes)) {
+                    throw new Exception('工单_bom资料无有效物料数据');
+                }
+                $wlbmStr = implode(',', $bomCodes);
+
+                // 第三步 查询物料_收发记录
+                $receiveCodes = Db::name('物料_收发记录')
+                    ->whereIn('st_wlbh', $bomCodes)
+                    ->group('st_wlbh')
+                    ->column('st_wlbh');
+
+                //对比第二步、第三步物料是否完全一致
+                $diffCodes = array_diff($bomCodes, $receiveCodes);
+                if (!empty($diffCodes)) {
+                    throw new Exception('物料_收发记录缺失物料:' . implode('、', $diffCodes));
+                }
+
+                //全部校验通过
+                $status = '同步成功';
+                $msg    = '工单_印件资料、工单_bom资料、物料_收发记录,校验全部一致';
+
+            } catch (Exception $e) {
+                // 捕获所有异常,记录失败原因
+                $msg = $e->getMessage();
+            }
+
+            //插入日志,成功、失败日志
+            $logInsert = [
+                'Gdbh'       => $gdbh,
+                'wlbm'       => $wlbmStr,
+                'yjmc'       => $yjmc,
+                'Yjdh'       => $yjdh,
+                'Yjno'       => $yjno,
+                'sys_id'     => $sysId,
+                'sys_rq'     => $syncRq,
+                'type'       => $type,
+                'status'     => $status,
+                'status_msg' => $msg
+            ];
+
+            echo "<pre>";print_r($logInsert);echo "<pre>";
+//            Db::name('工单_成品入仓同步操作日志')->insert($logInsert);
+
+            $resultList[] = [
+                '工单编号' => $gdbh,
+                '印件号'   => $yjno,
+                '状态'     => $status,
+                '说明'     => $msg
+            ];
+        }
+
+        return json([
+            'code' => 1,
+            'msg'  => '同步处理完成',
+            'data' => $resultList
+        ]);
+    }
+}