unknown пре 1 недеља
родитељ
комит
c41149e8dc
1 измењених фајлова са 177 додато и 18 уклоњено
  1. 177 18
      application/api/controller/WorkOrderProcess.php

+ 177 - 18
application/api/controller/WorkOrderProcess.php

@@ -1421,52 +1421,211 @@ class WorkOrderProcess extends Api
 
 
 
-    //复制目标工单的工艺到当前工单
+    /**
+     * 复制目标工单的部件与工艺到当前工单
+     * @param target_workorder 目标工单编号
+     * @param current_workorder 当前工单编号
+     * @param sys_id 操作人
+     */
     public function copyProcessToCurrentWorkOrder()
     {
         if (!$this->request->isPost()) {
             $this->error('请求方法错误');
         }
-        $params = $this->request->param();
+        $params = $this->request->post();
         if (empty($params['target_workorder'])) {
             $this->error('目标工单编号不能为空');
         }
         if (empty($params['current_workorder'])) {
             $this->error('当前工单编号不能为空');
         }
-        //查询目标工单的大工序、部件信息、工艺信息
-        $targetWorkOrderInfo = $this->getWorkOrderInfo($params['target_workorder']);
-        if (empty($currentWorkOrderInfo)) {
+        if (empty($params['sys_id'])) {
+            $this->error('操作人不能为空');
+        }
+        if ($params['target_workorder'] === $params['current_workorder']) {
+            $this->error('目标工单与当前工单不能相同');
+        }
+
+        if (!$this->workOrderExists($params['target_workorder'])) {
             $this->error('目标工单不存在');
         }
-        //查询目标工单的大工序、部件信息、工艺信息
-        $currentWorkOrderInfo = $this->getWorkOrderInfo($params['current_workorder']);
-        if (empty($targetWorkOrderInfo)) {
+        if (!$this->workOrderExists($params['current_workorder'])) {
             $this->error('当前工单不存在');
         }
+
+        $targetWorkOrderInfo = $this->getWorkOrderInfo($params['target_workorder']);
+        if (empty($targetWorkOrderInfo['partInfo']) && empty($targetWorkOrderInfo['processInfo'])) {
+            $this->error('目标工单没有可复制的部件或工艺');
+        }
+
+        $currentWorkOrderInfo = $this->getWorkOrderInfo($params['current_workorder']);
+        $oldPartCount = count($currentWorkOrderInfo['partInfo']);
+        $oldProcessCount = count($currentWorkOrderInfo['processInfo']);
+
+        $now = date('Y-m-d H:i:s');
+        $partInsertList = $this->buildWorkOrderPartCopyRows(
+            $targetWorkOrderInfo['partInfo'],
+            $params['current_workorder'],
+            $params['sys_id'],
+            $now
+        );
+        $processInsertList = $this->buildWorkOrderProcessCopyRows(
+            $targetWorkOrderInfo['processInfo'],
+            $params['current_workorder'],
+            $params['sys_id'],
+            $now
+        );
+
+        $logData = [
+            'order_no' => $params['current_workorder'],
+            'change_field' => $params['target_workorder'],
+            'old_value' => sprintf('部件%d条,工艺%d条', $oldPartCount, $oldProcessCount),
+            'new_value' => sprintf('部件%d条,工艺%d条', count($partInsertList), count($processInsertList)),
+            'oper_type' => '复制工单工艺',
+            'oper_user_name' => $params['sys_id'],
+            'oper_time' => $now,
+        ];
+
+        Db::startTrans();
+        try {
+            if ($oldPartCount > 0 || $oldProcessCount > 0) {
+                $partDeleteResult = Db::table('工单_部件资料')
+                    ->where('work_order', $params['current_workorder'])
+                    ->whereNull('del_rq')
+                    ->update(['del_rq' => $now]);
+                if ($partDeleteResult === false) {
+                    throw new \Exception('清除当前工单部件失败');
+                }
+                $processDeleteResult = Db::table('工单_基础工艺资料')
+                    ->where('work_order', $params['current_workorder'])
+                    ->whereNull('del_rq')
+                    ->update(['del_rq' => $now]);
+                if ($processDeleteResult === false) {
+                    throw new \Exception('清除当前工单工艺失败');
+                }
+            }
+
+            if (!empty($partInsertList)) {
+                $partInsertResult = Db::name('工单_部件资料')->insertAll($partInsertList);
+                if ($partInsertResult === false || $partInsertResult === 0) {
+                    throw new \Exception('复制部件失败');
+                }
+            }
+            if (!empty($processInsertList)) {
+                $processInsertResult = Db::name('工单_基础工艺资料')->insertAll($processInsertList);
+                if ($processInsertResult === false || $processInsertResult === 0) {
+                    throw new \Exception('复制工艺失败');
+                }
+            }
+
+            $logResult = Db::name('work_order_operation_log')->insert($logData);
+            if ($logResult === false || $logResult === 0) {
+                throw new \Exception('写入操作日志失败');
+            }
+            Db::commit();
+        } catch (\think\exception\HttpResponseException $e) {
+            throw $e;
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error('复制失败:' . $e->getMessage());
+        }
+
+        $this->success('复制成功', [
+            'part_count' => count($partInsertList),
+            'process_count' => count($processInsertList),
+        ]);
     }
 
+    /**
+     * 判断工单是否存在
+     * @param string $workorder
+     * @return bool
+     */
+    private function workOrderExists($workorder)
+    {
+        return Db::table('工单_基本资料')
+                ->where('订单编号', $workorder)
+                ->whereNull('Mod_rq')
+                ->count() > 0;
+    }
 
+    /**
+     * 查询工单部件与工艺(未删除、工艺仅 status=0)
+     * @param string $workorder
+     * @return array
+     */
     private function getWorkOrderInfo($workorder)
     {
-        $data = [];
-        //查询工单的部件信息
         $partInfo = db('工单_部件资料')
             ->where('work_order', $workorder)
-            ->where('del_rq', null)
+            ->whereNull('del_rq')
             ->field('part_code,part_name,remark,part_type,status')
+            ->order('part_code')
             ->select();
-        $data['partInfo'] = $partInfo;
-        //查询工单的工艺信息
+
         $processInfo = db('工单_基础工艺资料')
             ->where('work_order', $workorder)
-            ->where('del_rq', null)
-            ->field('work_order,part_code,part_name,process_code,process_name,standard_hour,standard_minutes,
-            standard_score,money,coefficient,remark,status,big_process')
+            ->whereNull('del_rq')
+            ->where('status', 0)
+            ->field('part_code,part_name,process_code,process_name,standard_hour,standard_minutes,
+                standard_score,money,coefficient,remark,big_process')
             ->order('process_code')
             ->select();
-        $data['processInfo'] = $processInfo;
-        return $data;
+
+        return [
+            'partInfo' => $partInfo ?: [],
+            'processInfo' => $processInfo ?: [],
+        ];
+    }
+
+    /**
+     * 组装待复制的部件数据
+     */
+    private function buildWorkOrderPartCopyRows($partList, $workOrder, $sysId, $operTime)
+    {
+        $rows = [];
+        foreach ($partList as $part) {
+            $rows[] = [
+                'work_order' => $workOrder,
+                'part_code' => $part['part_code'],
+                'part_name' => $part['part_name'],
+                'remark' => isset($part['remark']) ? $part['remark'] : '',
+                'part_type' => isset($part['part_type']) ? $part['part_type'] : '',
+                'status' => 0,
+                'sys_id' => $sysId,
+                'sys_rq' => $operTime,
+            ];
+        }
+        return $rows;
+    }
+
+    /**
+     * 组装待复制的工艺数据
+     */
+    private function buildWorkOrderProcessCopyRows($processList, $workOrder, $sysId, $operTime)
+    {
+        $rows = [];
+        foreach ($processList as $process) {
+            $rows[] = [
+                'work_order' => $workOrder,
+                'part_code' => $process['part_code'],
+                'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
+                'process_code' => $process['process_code'],
+                'process_name' => $process['process_name'],
+                'big_process' => $process['big_process'],
+                'standard_hour' => $process['standard_hour'],
+                'standard_minutes' => $process['standard_minutes'],
+                'standard_score' => $process['standard_score'],
+                'money' => $process['money'],
+                'coefficient' => $process['coefficient'],
+                'remark' => isset($process['remark']) ? $process['remark'] : '',
+                'status' => 0,
+                'pid' => 0,
+                'sys_id' => $sysId,
+                'sys_rq' => $operTime,
+            ];
+        }
+        return $rows;
     }