Sfoglia il codice sorgente

糊盒工单工艺复制

unknown 5 mesi fa
parent
commit
05c79e3b42
1 ha cambiato i file con 240 aggiunte e 0 eliminazioni
  1. 240 0
      application/api/controller/WorkOrder.php

+ 240 - 0
application/api/controller/WorkOrder.php

@@ -2923,4 +2923,244 @@ class WorkOrder extends Api
         }
     }
 
+
+    /**
+     *查询糊盒标准工单
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     *
+     */
+    public function HuheWorkOrderSearch()
+    {
+        if ($this->request->isGet() === false){
+            $this->error('请求错误');
+        }
+        $param = $this->request->param();
+        if (empty($param['search'])){
+            $this->error('参数错误');
+        }
+        $where['Gd_gdbh|Gd_cpmc|成品名称'] = ['like','%'.$param['search'].'%'];
+        $where['Gd_lx'] = '糊盒标准工单';
+        $list = \db('工单_基本资料')
+            ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品编号,Gd_cpmc as 产品名称,成品代号,成品名称,Uniqid')
+            ->where($where)
+            ->group('Gd_gdbh,Gd_cpdh,Gd_cpmc')
+            ->order('Gd_gdbh,Gd_cpdh')
+            ->select();
+        if (empty($list)){
+            $this->error('未找到数据');
+        }else{
+            $this->success('成功',$list);
+        }
+    }
+
+
+    /**
+     * 复制糊盒工单
+     * @return void
+     */
+    public function HuheWorkOrderCopy()
+    {
+        try {
+            // 1. 请求验证
+            if (!$this->request->isGet()) {
+                throw new \Exception('请求方式错误,请使用GET请求');
+            }
+
+            $param = $this->request->param();
+            $this->validateCopyParams($param);
+
+            // 2. 查询工单基本信息
+            $sourceWorkOrder = $this->getWorkOrderInfo($param['form_Uniqid'], ['Gd_gdbh', '行号']);
+            $targetWorkOrder = $this->getWorkOrderInfo($param['to_Uniqid'], ['Gd_gdbh', '行号', '订单数量', '实际投料']);
+
+            // 3. 复制印件资料和工艺资料
+            $this->copyPrintingData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
+            $this->copyProcessData($sourceWorkOrder, $targetWorkOrder, $param['sys_id']);
+
+            $this->success('复制成功');
+
+        } catch (\Exception $e) {
+
+            $this->error($e->getMessage());
+        }
+    }
+
+    /**
+     * 验证复制所需的参数
+     * @param array $params
+     * @throws \Exception
+     */
+    private function validateCopyParams(array $params): void
+    {
+        if (empty($params['to_Uniqid'])) {
+            throw new \Exception('目标工单ID(to_Uniqid)不能为空');
+        }
+
+        if (empty($params['form_Uniqid'])) {
+            throw new \Exception('源工单ID(form_Uniqid)不能为空');
+        }
+
+        if (empty($params['sys_id'])) {
+            throw new \Exception('系统ID(sys_id)不能为空');
+        }
+    }
+
+    /**
+     * 获取工单信息
+     * @param string $uniqid 工单唯一标识
+     * @param array $fields 需要查询的字段
+     * @return array 工单信息
+     * @throws \Exception
+     */
+    private function getWorkOrderInfo(string $uniqid, array $fields = []): array
+    {
+        $info = \db('工单_基本资料')
+            ->field($fields)
+            ->where('Uniqid', $uniqid)
+            ->find();
+
+        if (empty($info)) {
+            throw new \Exception("工单不存在,ID: {$uniqid}");
+        }
+
+        return $info;
+    }
+
+    /**
+     * 复制印件资料
+     * @param array $sourceWorkOrder 源工单信息
+     * @param array $targetWorkOrder 目标工单信息
+     * @param string $sysId 系统ID
+     * @throws \Exception
+     */
+    private function copyPrintingData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
+    {
+        // 常量定义,便于维护
+        $tableName = '工单_印件资料';
+        $excludeFields = ['Uniqid', 'Yj_Gdbh', 'yj_Yjno', 'yj_成品数量', 'yj_平张投料', '印件完工日期', 'Sys_id', 'Sys_rq', 'Mod_rq'];
+
+        // 查询源印件资料
+        $printingData = \db($tableName)
+            ->where('Yj_Gdbh', $sourceWorkOrder['Gd_gdbh'])
+            ->where('yj_Yjno', $sourceWorkOrder['行号'])
+            ->find();
+
+        if (empty($printingData)) {
+            throw new \Exception('未找到源工单印件资料');
+        }
+
+        // 构建新的印件资料
+        $newPrintingData = $this->buildCopyData(
+            $printingData,
+            $excludeFields,
+            [
+                'Yj_Gdbh' => $targetWorkOrder['Gd_gdbh'],
+                'yj_Yjno' => $targetWorkOrder['行号'],
+                'yj_成品数量' => $targetWorkOrder['订单数量'],
+                'yj_平张投料' => $targetWorkOrder['实际投料'],
+                '印件完工日期' => null,
+                'Sys_id' => $sysId,
+                'Sys_rq' => date('Y-m-d H:i:s'),
+                'Mod_rq' => null
+            ]
+        );
+
+        \db($tableName)
+            ->where('Yj_Gdbh',$newPrintingData['Yj_Gdbh'])
+            ->where('yj_Yjno',$newPrintingData['yj_Yjno'])
+            ->delete();
+        // 执行插入操作(保留fetchSql和query兼容中文字段)
+        $sql = \db($tableName)->fetchSql(true)->insert($newPrintingData);
+        $result = \db()->query($sql);
+        if ($result === false) {
+            throw new \Exception("印件资料复制失败,SQL: {$sql}");
+        }
+    }
+
+    /**
+     * 复制工艺资料
+     * @param array $sourceWorkOrder 源工单信息
+     * @param array $targetWorkOrder 目标工单信息
+     * @param string $sysId 系统ID
+     * @throws \Exception
+     */
+    private function copyProcessData(array $sourceWorkOrder, array $targetWorkOrder, string $sysId): void
+    {
+        // 常量定义
+        $tableName = '工单_工艺资料';
+        $excludeFields = ['UniqId', 'Gy0_gdbh', 'Gy0_yjno', 'Gy0_sbbh', 'Gy0_计划接货数', 'Gy0_最早开工时间', 'Gy0_sj1', 'Gy0_sj2', 'PD_WG', 'Sys_id', 'Sys_rq', 'Mod_rq'];
+        $defaultDate = '1900-01-01 00:00:00';
+        $currentTime = date('Y-m-d H:i:s');
+
+        // 查询源工艺资料列表
+        $processList = \db($tableName)
+            ->where('Gy0_gdbh', $sourceWorkOrder['Gd_gdbh'])
+            ->where('Gy0_yjno', $sourceWorkOrder['行号'])
+            ->select();
+
+        if (empty($processList)) {
+            throw new \Exception('未找到源工单工艺资料');
+        }
+
+        // 构建新的工艺资料列表
+        $newProcessData = [];
+        foreach ($processList as $item) {
+            $newProcessData[] = $this->buildCopyData(
+                $item,
+                $excludeFields,
+                [
+                    'Gy0_gdbh' => $targetWorkOrder['Gd_gdbh'],
+                    'Gy0_yjno' => $targetWorkOrder['行号'],
+                    'Gy0_sbbh' => '',
+                    'Gy0_计划接货数' => $targetWorkOrder['实际投料'],
+                    'Gy0_最早开工时间' => $defaultDate,
+                    'Gy0_sj1' => $defaultDate,
+                    'Gy0_sj2' => $defaultDate,
+                    'PD_WG' => $defaultDate,
+                    'Sys_id' => $sysId,
+                    'Sys_rq' => $currentTime,
+                    'Mod_rq' => null
+                ]
+            );
+        }
+
+        \db($tableName)
+            ->where('Gy0_gdbh', $targetWorkOrder['Gd_gdbh'])
+            ->where('Gy0_yjno', $targetWorkOrder['行号'])
+            ->delete();
+        // 执行批量插入(保留fetchSql和query兼容中文字段)
+        if (!empty($newProcessData)) {
+            $sql = \db($tableName)->fetchSql(true)->insertAll($newProcessData);
+            $result = \db()->query($sql);
+            if ($result === false) {
+                throw new \Exception("工艺资料复制失败,SQL: {$sql}");
+            }
+        }
+    }
+
+    /**
+     * 构建复制的数据
+     * @param array $sourceData 源数据
+     * @param array $excludeFields 需要排除的字段
+     * @param array $overrideData 需要覆盖的字段和值
+     * @return array 构建后的新数据
+     */
+    private function buildCopyData(array $sourceData, array $excludeFields, array $overrideData): array
+    {
+        $newData = [];
+
+        // 复制源数据中不需要排除的字段
+        foreach ($sourceData as $key => $value) {
+            if (!in_array($key, $excludeFields)) {
+                $newData[$key] = $value;
+            }
+        }
+
+        // 覆盖需要替换的字段
+        return array_merge($newData, $overrideData);
+    }
+
 }