Pārlūkot izejas kodu

Merge branch 'master' of https://git.7in6.com/liuhairui/mes-dc-server-api

liuhairui 2 dienas atpakaļ
vecāks
revīzija
8ca17b2db7

+ 1 - 0
application/api/controller/OrderSuperLoss.php

@@ -101,6 +101,7 @@ class OrderSuperLoss extends Api
                 'g.工单入仓数量 as 入库数量',
                 'g.款式',
                 'g.客户编号 as 客人编号',
+                'g.辅料计划入库时间 as 辅料计划入库时间',
 
                 // 面料入库时间 - 使用子查询避免重复
                 \think\Db::raw('(SELECT MIN(rq) FROM 库存_出入库明细 WHERE order_id = g.订单编号 AND name = "入库") as 面料入库时间'),

+ 181 - 0
application/api/controller/WorkOrderProcess.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 
 use app\common\controller\Api;
+use PhpOffice\PhpSpreadsheet\IOFactory;
 use think\Db;
 
 /**
@@ -664,4 +665,184 @@ class WorkOrderProcess extends Api
         $this->success('排序成功');
     }
         
+
+    /**
+     * 工单工艺excel导入
+     *  @param workorder 工单编号
+     * 
+     */
+    public function importProcess()
+    {
+        if (!$this->request->isPost()) {
+            $this->error('请求方法错误');
+        }
+        $params = $this->request->post();
+        if (empty($params['workorder'])) {
+            $this->error('工单编号不能为空');
+        }
+        if (empty($params['sys_id'])) {
+            $this->error('操作人不能为空');
+        }
+        $file = $this->request->file('file');
+        if (!$file) {
+            $this->error('文件不能为空');
+        }
+        $uploadDir = ROOT_PATH . 'public' . DS . 'uploads';
+        $info = $file->validate(['size' => 1024 * 1024 * 10, 'ext' => 'xlsx,xls,csv,txt'])
+            ->move($uploadDir);
+        if (!$info) {
+            $this->error($file->getError());
+        }
+        $filePath = $uploadDir . DS . $info->getSaveName();
+        // 定额表:上方为标题与元数据,第 5 行表头,第 6 行起为数据
+        $data = $this->readExcel($filePath, 5, 6);
+        if (empty($data)) {
+            $this->error('文件内容为空');
+        }
+        $seenSeq = [];
+        foreach ($data as $row) {
+            $seq = isset($row['序号']) ? $row['序号'] : null;
+            if ($seq === null || $seq === '') {
+                continue;
+            }
+            $seqKey = is_scalar($seq) ? (string)$seq : $seq;
+            if (isset($seenSeq[$seqKey])) {
+                $this->error('工序序号重复,请重新调整之后再上传');
+            }
+            $seenSeq[$seqKey] = true;
+        }
+        $now = date('Y-m-d H:i:s');
+        $partNameToCode = [];
+        $nextPartCode = 0;
+        foreach ($data as $row) {
+            $partName = isset($row['部件名称']) ? $row['部件名称'] : '';
+            if ($partName === '' || isset($partNameToCode[$partName])) {
+                continue;
+            }
+            $partNameToCode[$partName] = ++$nextPartCode;
+        }
+        $workOrderParts = [];
+        $i = 0;
+        foreach ($partNameToCode as $partName => $partCode) {
+            $workOrderParts[$i++] = [
+                'work_order' => $params['workorder'],
+                'part_name' => $partName,
+                'part_code' => $partCode,
+                'part_type' => '',
+                'remark' => '',
+                'status' => 1,
+                'sys_id' => $params['sys_id'],
+                'sys_rq' => $now,
+            ];
+        }
+        Db::startTrans();
+        try {
+            //删除数据库现有的工单部件数据
+            Db::name('工单_部件资料')->where('work_order', $params['workorder'])->delete();
+            //插入工单部件数据
+            $partInsertCount = Db::name('工单_部件资料')->insertAll($workOrderParts);
+            if ($partInsertCount === false) {
+                throw new \Exception('工单部件导入失败');
+            }
+            //提交事务
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+
+        $workOrderProcesses = [];
+        foreach ($data as $value) {
+            $name = isset($value['部件名称']) ? $value['部件名称'] : '';
+            $partCode = ($name !== '' && isset($partNameToCode[$name])) ? $partNameToCode[$name] : '';
+            $workOrderProcesses[] = [
+                'work_order' => $params['workorder'],
+                'part_code' => $partCode,
+                'part_name' => $name,
+                'process_code' => $value['序号'],
+                'process_name' => $value['工序名称'],
+                'big_process' => $value['生产工序'],
+                'standard_hour' => $value['秒'],
+                'standard_minutes' => $value['分'],
+                'standard_score' => $value['定额分'],
+                'money' => $value['金额'],
+                'coefficient' => $value['难度系数'],
+                'remark' => isset($value['备注']) ? $value['备注'] : '',
+                'sys_id' => $params['sys_id'],
+                'sys_rq' => $now,
+            ];
+        }
+        Db::startTrans();
+        try {
+            //删除数据库现有的工单工艺数据
+            Db::name('工单_基础工艺资料')->where('work_order', $params['workorder'])->delete();
+            //插入工单工艺数据
+            $processInsertCount = Db::name('工单_基础工艺资料')->insertAll($workOrderProcesses);
+            if ($processInsertCount === false) {
+                throw new \Exception('工单工艺导入失败');
+            }
+            //提交事务
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        $this->success('导入成功');
+    }
+
+    /**
+     * 读取 Excel/CSV(PhpSpreadsheet)
+     *
+     * @param string $filePath
+     * @param int    $headerRowNum   表头所在行(1 起计,如定额表为第 5 行)
+     * @param int    $dataStartRowNum 首条数据行(1 起计,须大于表头行,如第 6 行)
+     * @return array 每行一条关联数组,键为表头单元格文本
+     */
+    public function readExcel($filePath, $headerRowNum = 5, $dataStartRowNum = 6)
+    {
+        if (!is_file($filePath) || !is_readable($filePath)) {
+            return [];
+        }
+        if ($dataStartRowNum <= $headerRowNum) {
+            return [];
+        }
+        $spreadsheet = IOFactory::load($filePath);
+        $sheet = $spreadsheet->getActiveSheet();
+        $rows = $sheet->toArray();
+        if (empty($rows)) {
+            return [];
+        }
+        $headerIdx = $headerRowNum - 1;
+        $dataStartIdx = $dataStartRowNum - 1;
+        if (!isset($rows[$headerIdx])) {
+            return [];
+        }
+        $headers = array_map(function ($cell) {
+            return is_string($cell) ? trim($cell) : $cell;
+        }, $rows[$headerIdx]);
+        $data = [];
+        $rowCount = count($rows);
+        for ($r = $dataStartIdx; $r < $rowCount; $r++) {
+            $row = $rows[$r];
+            $hasCell = false;
+            foreach ($row as $cell) {
+                if ($cell !== null && $cell !== '') {
+                    $hasCell = true;
+                    break;
+                }
+            }
+            if (!$hasCell) {
+                continue;
+            }
+            $assoc = [];
+            foreach ($headers as $i => $key) {
+                if ($key === '' || $key === null) {
+                    continue;
+                }
+                $assoc[$key] = array_key_exists($i, $row) ? $row[$i] : null;
+            }
+            $data[] = $assoc;
+        }
+        return $data;
+    }
 }