Selaa lähdekoodia

批量新增工序

unknown 2 viikkoa sitten
vanhempi
sitoutus
a6be086887
1 muutettua tiedostoa jossa 241 lisäystä ja 1 poistoa
  1. 241 1
      application/api/controller/WorkOrderProcess.php

+ 241 - 1
application/api/controller/WorkOrderProcess.php

@@ -1288,5 +1288,245 @@ class WorkOrderProcess extends Api
         return $values;
     }
 
-   
+   /**
+    * 根据款号查询工单信息
+    * @param search 搜索内容
+    * @return array
+    * @throws \think\db\exception\DataNotFoundException
+    * @throws \think\db\exception\ModelNotFoundException
+    * @throws \think\exception\DbException
+    */
+   public function getWorkOrderInfoByStyle()
+   {
+        if (!$this->request->isGet()) {
+            $this->error('请求方法错误');
+        }
+        $params = $this->request->param();
+        if (empty($params['search'])) {
+            $this->error('搜索内容不能为空');
+        }
+        $where = [
+            'a.订单编号|a.生产款号|a.款式' => ['like', '%' . $params['search'] . '%'],
+            'a.Mod_rq' => null,
+        ];
+        $workOrder = db('工单_基本资料')->alias('a')
+            ->join('工单_基础工艺资料 b', 'a.订单编号 = b.work_order')
+            ->field('a.订单编号,a.生产款号,a.款式,a.客户编号')
+            ->where($where)
+            ->group('a.订单编号')
+            ->select();
+        if (empty($workOrder)) {
+            $this->error('未找到工单信息');
+        }
+        $this->success('成功', $workOrder);
+    }
+
+
+
+    //复制目标工单的工艺到当前工单
+    public function copyProcessToCurrentWorkOrder()
+    {
+        if (!$this->request->isPost()) {
+            $this->error('请求方法错误');
+        }
+        $params = $this->request->param();
+        if (empty($params['target_workorder'])) {
+            $this->error('目标工单编号不能为空');
+        }
+        if (empty($params['current_workorder'])) {
+            $this->error('当前工单编号不能为空');
+        }
+        //查询目标工单的大工序、部件信息、工艺信息
+        $targetWorkOrderInfo = $this->getWorkOrderInfo($params['target_workorder']);
+        if (empty($currentWorkOrderInfo)) {
+            $this->error('目标工单不存在');
+        }
+        //查询目标工单的大工序、部件信息、工艺信息
+        $currentWorkOrderInfo = $this->getWorkOrderInfo($params['current_workorder']);
+        if (empty($targetWorkOrderInfo)) {
+            $this->error('当前工单不存在');
+        }
+    }
+
+
+    private function getWorkOrderInfo($workorder)
+    {
+        $data = [];
+        //查询工单的部件信息
+        $partInfo = db('工单_部件资料')
+            ->where('work_order', $workorder)
+            ->where('del_rq', null)
+            ->field('part_code,part_name,remark,part_type,status')
+            ->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')
+            ->order('process_code')
+            ->select();
+        $data['processInfo'] = $processInfo;
+        return $data;
+    }
+
+
+    /**
+     * 批量新增工序资料(请求体为 JSON 数组,每条含 work_order 等字段)
+     * @param processList 工序信息
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     **/
+    public function batchAddProcess()
+    {
+        if (!$this->request->isPost()) {
+            $this->error('请求方法错误');
+        }
+
+        $processList = $this->parseBatchProcessList();
+        if (empty($processList)) {
+            $this->error('工艺数据不能为空');
+        }
+
+        $requiredFields = [
+            'work_order' => '工单编号',
+            'part_code' => '部件编号',
+            'part_name' => '部件名称',
+            'process_code' => '工序编号',
+            'process_name' => '工序名称',
+            'big_process' => '大工序',
+            'standard_hour' => '秒',
+            'standard_score' => '定额分',
+            'coefficient' => '难度系数',
+            'standard_minutes' => '分',
+            'money' => '金额',
+            'remark' => '备注',
+            'sys_id' => '操作人员',
+        ];
+
+        $workorder = '';
+        $now = date('Y-m-d H:i:s');
+        $insertList = [];
+        $processCodeMap = [];
+        $newBatchStatus0Sum = 0;
+
+        foreach ($processList as $index => $process) {
+            $rowNo = $index + 1;
+            if (!is_array($process)) {
+                $this->error('第' . $rowNo . '条工艺数据格式错误');
+            }
+            foreach ($requiredFields as $field => $label) {
+                if (!isset($process[$field]) || $process[$field] === '' || $process[$field] === null) {
+                    $this->error('第' . $rowNo . '条' . $label . '不能为空');
+                }
+            }
+
+            $itemWorkOrder = trim($process['work_order']);
+            if ($workorder === '') {
+                $workorder = $itemWorkOrder;
+            } elseif ($itemWorkOrder !== $workorder) {
+                $this->error('第' . $rowNo . '条工单编号与其它数据不一致');
+            }
+
+            $processCode = (string)$process['process_code'];
+            if (isset($processCodeMap[$processCode])) {
+                $this->error('批量数据中存在重复的工序编号:' . $processCode);
+            }
+            $processCodeMap[$processCode] = true;
+
+            $status = isset($process['status']) ? $process['status'] : 0;
+            if (in_array($status, [0, '0'], true)) {
+                $newBatchStatus0Sum += floatval($process['standard_score']);
+            }
+
+            $insertList[] = [
+                'work_order' => $workorder,
+                'part_code' => $process['part_code'],
+                'part_name' => $process['part_name'],
+                'process_code' => $processCode,
+                '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'],
+                'coefficient' => $process['coefficient'],
+                'money' => $process['money'],
+                'remark' => $process['remark'],
+                'status' => $status,
+                'sys_id' => $process['sys_id'],
+                'sys_rq' => $now,
+            ];
+        }
+
+        $existingCodes = Db::table('工单_基础工艺资料')
+            ->where('work_order', $workorder)
+            ->whereIn('process_code', array_keys($processCodeMap))
+            ->whereNull('del_rq')
+            ->column('process_code');
+        if (!empty($existingCodes)) {
+            $this->error('工序编号已存在:' . $existingCodes[0]);
+        }
+
+        $workOrder = Db::table('工单_基本资料')
+            ->where('订单编号', $workorder)
+            ->where('Mod_rq', null)
+            ->field('订单编号,计划制造工分')
+            ->find();
+        if (empty($workOrder)) {
+            $this->error('工单不存在');
+        }
+
+        $existingTotal = Db::table('工单_基础工艺资料')
+            ->where('work_order', $workorder)
+            ->where('status', 0)
+            ->whereNull('del_rq')
+            ->sum('standard_score');
+        $existingTotal = $existingTotal ? floatval($existingTotal) : 0;
+        if ($existingTotal + $newBatchStatus0Sum > floatval($workOrder['计划制造工分'])) {
+            $this->error('工序定额分和大于制造工分,请确认之后再修改');
+        }
+
+        Db::startTrans();
+        try {
+            $insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
+            if ($insertResult === false || $insertResult === 0) {
+                throw new \Exception('批量新增工序失败');
+            }
+            Db::commit();
+        } catch (\think\exception\HttpResponseException $e) {
+            throw $e;
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error('批量新增工序资料失败');
+        }
+        $this->success('批量新增工序资料成功', ['count' => count($insertList)]);
+    }
+
+    /**
+     * 解析批量工序请求体(form 数组 / JSON 数组 / 单字段 JSON 字符串)
+     * @return array
+     */
+    private function parseBatchProcessList()
+    {
+        $params = $this->request->post();
+        if (isset($params[0]) && is_array($params[0])) {
+            return $params;
+        }
+
+        $decoded = json_decode($this->request->getInput(), true);
+        if (is_array($decoded) && isset($decoded[0]) && is_array($decoded[0])) {
+            return $decoded;
+        }
+
+        $jsonStr = is_string($params) ? $params : (count($params) === 1 ? reset($params) : null);
+        if (is_string($jsonStr)) {
+            $decoded = json_decode($jsonStr, true);
+            return is_array($decoded) ? $decoded : [];
+        }
+
+        return [];
+    }
 }