瀏覽代碼

报工接口

liuhairui 1 年之前
父節點
當前提交
9dd82509c8
共有 2 個文件被更改,包括 1196 次插入332 次删除
  1. 1141 284
      application/api/controller/WorkOrderSpotCheck.php
  2. 55 48
      application/api/controller/WorkOrderVerification.php

+ 1141 - 284
application/api/controller/WorkOrderSpotCheck.php

@@ -3,18 +3,16 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use think\db\Query;
+use \think\Request;
+use \think\Db;
 
-/**
- * 工单抽检记录维护接口
- */
-class WorkOrderSpotCheck extends Api
-{
+class WorkOrderSpotCheck extends Api{
     protected $noNeedLogin = ['*'];
     protected $noNeedRight = ['*'];
 
     /**
      * 首页
-     *
      */
     public function index()
     {
@@ -22,325 +20,1184 @@ class WorkOrderSpotCheck extends Api
     }
 
     /**
-     * 获取工单抽检记录侧边栏
-     * @ApiMethod (GET)
+     * 报工查询
+     * 通过订单编号列表显示
      */
-    public function getTab()
-    {
-        //get请求
-        if(!$this->request->isGet()){
+    public function getList() {
+        // 判断请求是否为 GET 请求
+        if (!$this->request->isGet()) {
             $this->error('请求方式错误');
         }
-        $rows = db()->table('db_抽检记录')
-            ->field('LEFT(Sys_rq, 10) as date, COUNT(*) as counts')
-            ->group('date')
-            ->order('UniqId desc')
-            ->limit(45)
-            ->select();
-
-        $arr = db()->table('db_抽检记录')
-            ->field('LEFT(Sys_rq, 10) as date, rtrim(Sys_id) as Sys_id, COUNT(*) as count')
-            ->where('Sys_rq','>=',$rows[count($rows)-1]['date'])
-            ->group('date, Sys_id')
-            ->order('UniqId desc')
-            ->select();
-
-        foreach($rows as $key=>$value){
-            $rows[$key]['sys'] = [];
-            foreach($arr as $k=>$v){
-                if($value['date'] == $v['date']){
-                    unset($v['date']);
-                    array_push($rows[$key]['sys'],$v);
-                    unset($arr[$k]);
-                }
+        $params = $this->request->param();
+        // 校验参数是否存在且不为空
+        $requiredParams = ['order', 'code'];
+        foreach ($requiredParams as $param) {
+            if (!isset($params[$param]) || empty($params[$param])) {
+                $this->error("$param 参数错误");
             }
-            $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
         }
-        $this->success('成功',$rows);
+        // 如果订单编号没有 'DC' 前缀,自动添加 'DC'
+        if (strpos($params['order'], 'DC') !== 0) {
+            $params['order'] = 'DC' . $params['order'];
+        }
+        $order = $params['order'];
+
+        // 根据不同的 code 值执行不同的逻辑
+        switch ($params['code']) {
+            case '出库':
+                $query = \db('工单_基本资料')->alias('j')
+                    ->field('y.订单编号, y.子订单编号, y.颜色, y.款号, j.款式, y.ck_rq')
+                    ->distinct('y.子订单编号')
+                    ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'inner');
+
+                if (strpos($order, '-') !== false) {
+                    $query->where('y.子订单编号', $order);
+                } else {
+                    $query->where('y.订单编号', $order);
+                }
+                $query->where(function($query) {
+                    $query->whereNull('y.Mod_rq')
+                        ->whereOr('y.Mod_rq', '');
+                });
+                $data = $query->select();
+
+                if (isset($data)) {
+                    // 对数据进行排序,首先按是否出库排序,然后按 ck_rq 时间倒序排序
+                    usort($data, function($a, $b) {
+                        // 判断是否出库
+                        $a_status = !empty($a['ck_rq']) ? 1 : 0;
+                        $b_status = !empty($b['ck_rq']) ? 1 : 0;
+
+                        if ($a_status == $b_status) {
+                            // 如果出库状态相同,则按 ck_rq 倒序排序
+                            return strcmp($b['ck_rq'], $a['ck_rq']);
+                        } else {
+                            // 先显示未出库(0),再显示已出库(1)
+                            return $a_status - $b_status;
+                        }
+                    });
+
+                    foreach ($data as &$item) {
+                        $item['是否出库'] = !empty($item['ck_rq']) ? '已出库' : '未出库';  // 判断是否出库
+                        $item['ck_rq'] = substr($item['ck_rq'], 0, 10);  // 判断是否出库
+//                        unset($item['ck_rq']);  // 移除 ck_rq 字段
+                    }
+                    $total = count($data);
+                    $result = [
+                        'data' => $data,
+                        'rq' => date("Y-m-d"),
+                        'total' => $total
+                    ];
+                    $this->success('请求成功', $result);
+                }
+                break;
+            case '裁切':
+                // 查询订单编号对应的数据
+                function getOrderData($order) {
+                    $query = \db('工单_基本资料')->alias('j')
+                        ->field('y.订单编号,y.子订单编号,y.款号,j.生产款号, j.款式,y.颜色,y.zdtotal,y.sctotal, j.单位, y.Sys_id, y.Sys_rq,y.UniqId')
+                        ->distinct('y.子订单编号')
+                        ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
+                        ->where(function($query) {
+                            $query->whereNull('y.Mod_rq')
+                                ->whereOr('y.Mod_rq', '');
+                        })// 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')
+                                ->where('y.ck_rq', '<>', '');
+                        })// 查询出库数据
+                        ->where('y.订单编号', 'like', '%' . $order . '%');
+                    $data = $query->select();
+                    return $data;
+                }
+
+                // 查询子订单编号对应的数据
+                function getSubOrderData($order) {
+                    $query = \db('工单_基本资料')->alias('j')
+                        ->field('y.订单编号, y.子订单编号, y.款号, j.生产款号, j.款式, y.颜色, y.zdtotal, y.sctotal, j.单位, y.Sys_id, y.Sys_rq, y.UniqId,y.sc_rq,
+                 y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10,
+                 y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10,
+                 y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5, y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10')
+                        ->distinct('y.子订单编号')
+                        ->join('工单_印件资料 y', 'j.订单编号 = y.订单编号', 'left')
+                        ->where(function($query) {
+                            $query->whereNull('y.Mod_rq')
+                                ->whereOr('y.Mod_rq', '');
+                        }) // 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')
+                                ->where('y.ck_rq', '<>', '');
+                        }) // 查询出库数据
+                        ->where('y.子订单编号', '=', $order);
+
+                    $data = $query->select();
+                    // 构建主要数据 (data) 和列表数据 (list)
+                    $dataEntry = [];
+                    $list = [];
+
+                    foreach ($data as $item) {
+                        // 主要数据部分,变成数组格式
+                        $dataEntry[] = [
+                            '订单编号' => $item['订单编号'],
+                            '子订单编号' => $item['子订单编号'],
+                            '生产款号' => $item['生产款号'],
+                            '款式' => $item['款式'],
+                            '颜色' => $item['颜色'],
+                            'zdtotal' => $item['zdtotal'],
+                            'sctotal' => $item['sctotal'],
+                            '单位' => $item['单位'],
+                            'Sys_id' => $item['Sys_id'],
+                            'Sys_rq' => $item['sc_rq'],
+                        ];
+
+                        // 列表部分
+                        $entry = [];
+                        for ($i = 1; $i <= 10; $i++) {
+                            $entry['cm' . $i] = ($item['cm' . $i] === 0 || is_null($item['cm' . $i])) ? '' : $item['cm' . $i];
+                            $entry['cmsl' . $i] = ($item['cmsl' . $i] === 0 || is_null($item['cmsl' . $i])) ? '' : $item['cmsl' . $i];
+                            $entry['scsl' . $i] = ($item['scsl' . $i] === 0 || is_null($item['scsl' . $i])) ? '' : $item['scsl' . $i];
+                        }
+                        $entry['订单编号'] = $item['订单编号'];
+                        $entry['子订单编号'] = $item['子订单编号'];
+                        $entry['UniqId'] = $item['UniqId'];
+                        $entry['zdtotal'] = $item['zdtotal'];
+                        $list[] = $entry;
+                    }
+
+                    return ['data' => $dataEntry, 'list' => $list];
+                }
+
+                if (strpos($order, '-') !== false) {
+                    $resultData = getSubOrderData($order);
+
+                    $result = [
+                        'data' => $resultData['data'], // data 是数组格式
+                        'list' => $resultData['list'],
+                        'total' => count($resultData['list']),
+                    ];
+                } else {
+                    // 查询订单编号对应的数据
+                    $data = getOrderData($order);
+                    $result = [
+                        'data' => $data,
+                        'list' => '',
+                        'total' => count($data),
+                    ];
+                }
+                $this->success('请求成功', $result);
+              break;
+            case '车缝':
+                // 查询流水号
+                function getSerial($order) {
+                    $serial = \db('设备_产量计酬')
+                        ->field('子订单编号, MAX(serial) as serial, MAX(serial_num) as serial_num')
+                        ->where('子订单编号', $order)
+                        ->group('子订单编号')
+                        ->find();
+                    // 如果 serial 为空,则默认返回 '000',如果 serial_num 为空,则默认返回 0
+                    return [
+                        'serial' => empty($serial['serial']) ? '000' : $serial['serial'],
+                        'serial_num' => empty($serial['serial_num']) ? 0 : $serial['serial_num']
+                    ];
+                }
+
+                // 查询历史记录并生成尺寸汇总
+                function getSizeSummary($order) {
+                    $where = [];
+                    if (strpos($order, '-') !== false) {
+                        // 如果订单包含 '-',则按子订单编号精确匹配
+                        $where['c.子订单编号'] = $order;
+                    } else {
+                        // 如果订单不包含 '-',则按订单编号进行模糊匹配
+                        $where['c.订单编号'] = ['like', '%' . $order . '%'];
+                    }
+
+                    $where['c.工序编号'] = '3';
+                    $existingRecords = \db('设备_产量计酬')->alias('c')
+                        ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.sczl_bh, c.尾包, c.sys_rq, c.serial, c.serial_num,
+                y.zdtotal, y.sctotal, j.款式, y.颜色, CAST(c.serial_num AS SIGNED) as serial_num_numeric')
+                        ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+                        ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+                        ->where($where)
+                        ->whereNull('c.mod_rq')
+                        ->whereNotNull('y.ck_rq')
+                        ->where('y.ck_rq', '<>', '')
+                        ->order('c.sys_rq desc, serial_num_numeric desc')
+                        ->distinct('c.子订单编号')
+                        ->select();
+
+                    $sizeSummary = [];
+                    $sizes = [];
+
+                    foreach ($existingRecords as $record) {
+                        $size = $record['尺码'];
+                        $quantity = $record['数量'];
+                        $serial = $record['serial'];
+                        $serialNum = $record['serial_num']; // serial_num 分组
+                        $processNumber = $record['工序编号']; // 工序编号
+
+                        // 初始化尺码
+                        if (!in_array($size, $sizes)) {
+                            $sizes[] = $size;
+                        }
+
+                        // 检查是否已有相同的 serial_num 组合
+                        if (!isset($sizeSummary[$serialNum])) {
+                            $sizeSummary[$serialNum] = [
+                                '订单编号' => $record['订单编号'],
+                                '子订单编号' => $record['子订单编号'],
+                                '款号' => $record['款号'],
+                                '款式' => $record['款式'],
+                                '工序编号' => $record['工序编号'],
+                                '工序名称' => $record['工序名称'],
+                                '数量' => 0, // 记录总数量
+                                '尾包' => $record['尾包'] == 1 ? '是' : '否',
+                                '颜色' => $record['颜色'],
+                                '机台号' => $record['sczl_jtbh'],
+                                '组别' => $record['sczl_bh'],
+                                'serial' => [], // 流水号数组
+                                'sys_rq' => $record['sys_rq'],
+                                '尺码数据' => [] // 用于存储每个尺码对应的流水号和数量
+                            ];
+                        }
+
+                        // 将流水号添加到 serial 数组
+                        $sizeSummary[$serialNum]['serial'][] = $serial;
+
+                        // 累加数量到总数量
+                        $sizeSummary[$serialNum]['数量'] += $quantity;
+
+                        // 记录尺码数据
+                        if (!isset($sizeSummary[$serialNum]['尺码数据'][$size])) {
+                            $sizeSummary[$serialNum]['尺码数据'][$size] = [];
+                        }
+
+                        // 添加尺码对应的流水号和数量
+                        $sizeSummary[$serialNum]['尺码数据'][$size][] = $serial . '(' . $quantity . ')';
+                    }
+
+                    // 对每个工序的 serial 进行处理,并转换为逗号分隔的字符串
+                    $finalRecords = [];
+                    foreach ($sizeSummary as &$summary) {
+                        // 将 serial 数组按数值降序排序
+                        sort($summary['serial'], SORT_NUMERIC);
+                        $summary['serial'] = implode(',', $summary['serial']);
+
+                        // 合并尺码数据到同一层级
+                        foreach ($summary['尺码数据'] as $size => $serials) {
+                            $summary[$size] = implode(', ', $serials); // 尺码对应的流水号和数量
+                        }
+
+                        unset($summary['尺码数据']); // 移除尺码数据数组,直接将数据合并到 summary
+                        $finalRecords[] = $summary;
+                    }
+
+                    // 尺码排序(数字在前,字母在后)
+                    usort($sizes, function ($a, $b) {
+                        $sizeOrder = ['S' => 1, 'M' => 2, 'L' => 3, 'XL' => 4, 'XXL' => 5, 'XXXL' => 6];
+                        if (is_numeric($a) && is_numeric($b)) return $a - $b;
+                        if (is_numeric($a)) return -1;
+                        if (is_numeric($b)) return 1;
+                        return ($sizeOrder[$a] ?? 100) - ($sizeOrder[$b] ?? 100);
+                    });
+
+                    return ['headers' => $sizes, 'records' => $finalRecords];
+                }
+                if (strpos($order, '-') !== false) {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    // 检查是否领料
+                    $yin_list = \db('工单_印件资料')->where('子订单编号', $order)->whereNull('mod_rq')->find();
+                    if (!$yin_list) {
+                        $this->error('该子订单还生成颜色资料,请新增颜色资料');
+                        return;
+                    }
+
+                    $paichenglist = \db('工单_排程班次')->where('子订单编号', $order)->whereNull('mod_rq')->find();
+                    if (!$paichenglist) {
+                        $this->error('该子订单还未领料,请领料后再报工');
+                        return;
+                    }
+
+                    // 报工记录
+                    $cm_zd_sc_list = \db('工单_印件资料')->alias('y')
+                        ->field('y.zdtotal, y.sctotal, y.颜色, y.款号, j.款式, y.cm1, y.cm2, y.cm3, y.cm4, y.cm5, y.cm6, y.cm7, y.cm8, y.cm9, y.cm10, y.Sys_id,
+                                y.cmsl1, y.cmsl2, y.cmsl3, y.cmsl4, y.cmsl5, y.cmsl6, y.cmsl7, y.cmsl8, y.cmsl9, y.cmsl10, 
+                                y.scsl1, y.scsl2, y.scsl3, y.scsl4, y.scsl5,y.scsl6, y.scsl7, y.scsl8, y.scsl9, y.scsl10')
+                        ->join('工单_基本资料 j', 'y.订单编号 = j.订单编号', 'left')
+                        ->where('y.子订单编号', $order)
+                        ->select();
+                    // 遍历每条记录,检查字段是否为 0,并将其替换为空
+                    foreach ($cm_zd_sc_list as &$record) {
+                        foreach ($record as $key => $value) {
+                            if ($value === 0) {
+                                $record[$key] = ''; // 将值为 0 的字段设置为空
+                            }
+                        }
+                    }
+                    $sizeData = getSizeSummary($order);
+                    $serial = getSerial($order);
+                    $this->success('请求成功', [
+                        'serial' => $serial['serial'], // 流水号
+                        'serial_num' => $serial['serial_num'], // 序号
+                        'list' => $cm_zd_sc_list, // 报工
+                        'headers' => $sizeData['headers'], // 尺码表头
+                        'records' => $sizeData['records'] // 查询历史记录
+                    ]);
+
+                } else {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    $sizeData = getSizeSummary($order);
+
+                    $serial = getSerial($order);
+                    $this->success('请求成功', [
+                        'serial' => $serial['serial'], // 流水号
+                        'serial_num' => $serial['serial_num'], // 序号
+                        'list' => '', // 无需报工
+                        'headers' => $sizeData['headers'], // 尺码表头
+                        'records' => $sizeData['records'] // 查询历史记录
+                    ]);
+                }
+                break;
+            case '后道':
+                // 验证sys_sbID参数是否存在且非空
+                if (strpos($order, ',') !== false) {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    $paramArray = explode(',', $order);
+
+                    //获取车缝生成小票的UniqId
+                    $Uniqid = \db('设备_产量计酬')->field('UniqId')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4]
+                        ])->find();
+
+                    $res = \db('设备_产量计酬')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4],
+                            'sczl_jtbh' => $params['sys_sbID'],
+                        ])
+                        ->order('sys_rq desc') // 按时间倒序排列,确保最新的记录在最前
+                        ->select();
+
+                    if (empty($res)) {
+                        // 如果查询结果为空,直接返回 $paramArray[4]
+                        $ci_num = $paramArray[4];
+                    } else {
+                        // 累加 s_num
+                        $total_s_num = 0;
+                        foreach ($res as $item) {
+                            $total_s_num += $item['s_num'];
+                        }
+
+                        // 判断累加结果与 $paramArray[4] 的关系
+                        if ($total_s_num >= $paramArray[4]) {
+                            $ci_num = 0; // 如果累加结果等于或大于 $paramArray[4],返回 0
+                        } else {
+                            // 否则返回最新记录中的 ci_num
+                            $ci_num = $res[0]['ci_num']; // 最新时间的记录已经通过 order 排在第一位
+                        }
+                    }
+
+                    $result = [
+                        'sys_sbID' => $params['sys_sbID'],//设备编号
+                        'ci_num' => $ci_num,//剩余数量
+                        'UniqId' => $Uniqid['UniqId'],
+                        'order' => $paramArray[0],//订单编号
+                        'zb' => $params['sys_sbID'],//组别
+                        'gx' => 4,//默认固定工序
+                        'cm' => $paramArray[3],//尺码
+                        'sl' => $paramArray[4],//数量
+                        'wb' => $paramArray[5]//尾包
+                    ];
+
+                    // 查询历史记录,按 sys_rq 时间倒序排序
+                    $existingRecords = \db('设备_产量计酬')->alias('c')
+                        ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
+                         c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
+                        ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+                        ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+                        ->where('c.子订单编号', $paramArray[0])
+                        ->where('c.sczl_jtbh', $params['sys_sbID'])
+                        ->whereNull('c.mod_rq') // 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
+                        }) // 查询出库数据
+                        ->order('c.UniqId', 'desc')
+                        ->distinct('c.子订单编号')
+                        ->select();
+
+                    $sizeSummary = [];
+                    $sizes = [];
+
+                    foreach ($existingRecords as $record) {
+                        $size = $record['尺码'];
+                        $quantity = $record['数量'];
+                        $serial = $record['serial'];
+
+                        // 如果尺码还没有出现过,加入尺码列表
+                        if (!in_array($size, $sizes)) {
+                            $sizes[] = $size;
+                        }
+                        if (!isset($sizeSummary[$record['UniqId']])) {
+                            $sizeSummary[$record['UniqId']] = [
+                                'UniqId' => $record['UniqId'],
+                                '订单编号' => $record['订单编号'],
+                                '子订单编号' => $record['子订单编号'],
+                                '上报数量' => $record['s_num'],
+                                '次品数量' => $record['数量'] - $record['ci_num'],
+                                '生产款号' => $record['款号'],
+                                '尺码' => $record['尺码'],
+                                '工序编号' => $record['工序编号'],
+                                '工序名称' => $record['工序名称'],
+                                '数量' => $quantity, // 每个记录都单独显示数量
+                                '尾包' => $record['尾包'] == 1 ? '是' : '否',
+                                '颜色' => $record['颜色'],
+                                '组别' => $record['sczl_jtbh'],
+                                'serial' => $serial,
+                                'sys_rq' => $record['sys_rq'] // 上报日期
+                            ];
+                        }
+                    }
+
+                    // 将 $sizeSummary 转换为索引数组
+                    $sizeSummary = array_values($sizeSummary);
+
+                    // 对 $sizeSummary 按 sys_rq 降序排序
+                    usort($sizeSummary, function ($a, $b) {
+                        return strtotime($b['UniqId']) - strtotime($a['UniqId']);
+                    });
+
+
+                    $this->success('请求成功', [
+                        'result' => $result,
+                        'headers' => $sizes, // 尺码表头
+                        'records' => $sizeSummary // 返回记录
+                    ]);
+                } else {
+                    $this->success('请扫描子订单编号', ['result' => '', 'records' => []]);
+                }
+                break;
+            case '大烫':
+                if (strpos($order, ',') !== false) {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    $paramArray = explode(',', $order);
+                    //获取车缝生成小票的UniqId
+                    $Uniqid = \db('设备_产量计酬')->field('UniqId')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4]
+                        ])->find();
+
+                    $res = \db('设备_产量计酬')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4],
+                            'sczl_jtbh' => $params['sys_sbID'],
+                        ])
+                        ->order('sys_rq desc') // 按时间倒序排列,确保最新的记录在最前
+                        ->select();
+
+                    if (empty($res)) {
+                        // 如果查询结果为空,直接返回 $paramArray[4]
+                        $ci_num = $paramArray[4];
+                    } else {
+                        // 累加 s_num
+                        $total_s_num = 0;
+                        foreach ($res as $item) {
+                            $total_s_num += $item['s_num'];
+                        }
+
+                        // 判断累加结果与 $paramArray[4] 的关系
+                        if ($total_s_num >= $paramArray[4]) {
+                            $ci_num = 0; // 如果累加结果等于或大于 $paramArray[4],返回 0
+                        } else {
+                            // 否则返回最新记录中的 ci_num
+                            $ci_num = $res[0]['ci_num']; // 最新时间的记录已经通过 order 排在第一位
+                        }
+                    }
+
+                    $result = [
+                        'sys_sbID' => $params['sys_sbID'],//设备编号
+                        'ci_num' => $ci_num,//剩余数量
+                        'UniqId' => $Uniqid['UniqId'],
+                        'order' => $paramArray[0],//订单编号
+                        'zb' => $params['sys_sbID'],//组别
+                        'gx' => 4,//默认固定工序
+                        'cm' => $paramArray[3],//尺码
+                        'sl' => $paramArray[4],//数量
+                        'wb' => $paramArray[5]//尾包
+                    ];
+
+                    // 查询历史记录,按 sys_rq 时间倒序排序
+                    $existingRecords = \db('设备_产量计酬')->alias('c')
+                        ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
+                         c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
+                        ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+                        ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+                        ->where('c.子订单编号', $paramArray[0])
+                        ->where('c.sczl_jtbh', $params['sys_sbID'])
+                        ->whereNull('c.mod_rq') // 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
+                        }) // 查询出库数据
+                        ->order('c.UniqId', 'desc')
+                        ->distinct('c.子订单编号')
+                        ->select();
+
+                    $sizeSummary = [];
+                    $sizes = [];
+
+                    foreach ($existingRecords as $record) {
+                        $size = $record['尺码'];
+                        $quantity = $record['数量'];
+                        $serial = $record['serial'];
+
+                        // 如果尺码还没有出现过,加入尺码列表
+                        if (!in_array($size, $sizes)) {
+                            $sizes[] = $size;
+                        }
+                        if (!isset($sizeSummary[$record['UniqId']])) {
+                            $sizeSummary[$record['UniqId']] = [
+                                'UniqId' => $record['UniqId'],
+                                '订单编号' => $record['订单编号'],
+                                '子订单编号' => $record['子订单编号'],
+                                '上报数量' => $record['s_num'],
+                                '次品数量' => $record['数量'] - $record['ci_num'],
+                                '生产款号' => $record['款号'],
+                                '尺码' => $record['尺码'],
+                                '工序编号' => $record['工序编号'],
+                                '工序名称' => $record['工序名称'],
+                                '数量' => $quantity, // 每个记录都单独显示数量
+                                '尾包' => $record['尾包'] == 1 ? '是' : '否',
+                                '颜色' => $record['颜色'],
+                                '组别' => $record['sczl_jtbh'],
+                                'serial' => $serial,
+                                'sys_rq' => $record['sys_rq'] // 上报日期
+                            ];
+                        }
+                    }
+
+                    // 将 $sizeSummary 转换为索引数组
+                    $sizeSummary = array_values($sizeSummary);
+
+                    // 对 $sizeSummary 按 sys_rq 降序排序
+                    usort($sizeSummary, function ($a, $b) {
+                        return strtotime($b['UniqId']) - strtotime($a['UniqId']);
+                    });
+
+
+                    $this->success('请求成功', [
+                        'result' => $result,
+                        'headers' => $sizes, // 尺码表头
+                        'records' => $sizeSummary // 返回记录
+                    ]);
+                } else {
+                    $this->success('请扫描子订单编号', ['result' => '', 'records' => []]);
+                }
+                // 添加大烫逻辑
+                break;
+            case '总检':
+                if (strpos($order, ',') !== false) {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    $paramArray = explode(',', $order);
+                    //获取车缝生成小票的UniqId
+                    $Uniqid = \db('设备_产量计酬')->field('UniqId')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4]
+                        ])->find();
+
+                    $res = \db('设备_产量计酬')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4],
+                            'sczl_jtbh' => $params['sys_sbID'],
+                        ])
+                        ->order('sys_rq desc') // 按时间倒序排列,确保最新的记录在最前
+                        ->select();
+
+                    if (empty($res)) {
+                        // 如果查询结果为空,直接返回 $paramArray[4]
+                        $ci_num = $paramArray[4];
+                    } else {
+                        // 累加 s_num
+                        $total_s_num = 0;
+                        foreach ($res as $item) {
+                            $total_s_num += $item['s_num'];
+                        }
+
+                        // 判断累加结果与 $paramArray[4] 的关系
+                        if ($total_s_num >= $paramArray[4]) {
+                            $ci_num = 0; // 如果累加结果等于或大于 $paramArray[4],返回 0
+                        } else {
+                            // 否则返回最新记录中的 ci_num
+                            $ci_num = $res[0]['ci_num']; // 最新时间的记录已经通过 order 排在第一位
+                        }
+                    }
+
+                    $result = [
+                        'sys_sbID' => $params['sys_sbID'],//设备编号
+                        'ci_num' => $ci_num,//剩余数量
+                        'UniqId' => $Uniqid['UniqId'],
+                        'order' => $paramArray[0],//订单编号
+                        'zb' => $params['sys_sbID'],//组别
+                        'gx' => 4,//默认固定工序
+                        'cm' => $paramArray[3],//尺码
+                        'sl' => $paramArray[4],//数量
+                        'wb' => $paramArray[5]//尾包
+                    ];
+
+                    // 查询历史记录,按 sys_rq 时间倒序排序
+                    $existingRecords = \db('设备_产量计酬')->alias('c')
+                        ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
+                         c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
+                        ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+                        ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+                        ->where('c.子订单编号', $paramArray[0])
+                        ->where('c.sczl_jtbh', $params['sys_sbID'])
+                        ->whereNull('c.mod_rq') // 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
+                        }) // 查询出库数据
+                        ->order('c.UniqId', 'desc')
+                        ->distinct('c.子订单编号')
+                        ->select();
+
+                    $sizeSummary = [];
+                    $sizes = [];
+
+                    foreach ($existingRecords as $record) {
+                        $size = $record['尺码'];
+                        $quantity = $record['数量'];
+                        $serial = $record['serial'];
+
+                        // 如果尺码还没有出现过,加入尺码列表
+                        if (!in_array($size, $sizes)) {
+                            $sizes[] = $size;
+                        }
+                        if (!isset($sizeSummary[$record['UniqId']])) {
+                            $sizeSummary[$record['UniqId']] = [
+                                'UniqId' => $record['UniqId'],
+                                '订单编号' => $record['订单编号'],
+                                '子订单编号' => $record['子订单编号'],
+                                '上报数量' => $record['s_num'],
+                                '次品数量' => $record['数量'] - $record['ci_num'],
+                                '生产款号' => $record['款号'],
+                                '尺码' => $record['尺码'],
+                                '工序编号' => $record['工序编号'],
+                                '工序名称' => $record['工序名称'],
+                                '数量' => $quantity, // 每个记录都单独显示数量
+                                '尾包' => $record['尾包'] == 1 ? '是' : '否',
+                                '颜色' => $record['颜色'],
+                                '组别' => $record['sczl_jtbh'],
+                                'serial' => $serial,
+                                'sys_rq' => $record['sys_rq'] // 上报日期
+                            ];
+                        }
+                    }
+
+                    // 将 $sizeSummary 转换为索引数组
+                    $sizeSummary = array_values($sizeSummary);
+
+                    // 对 $sizeSummary 按 sys_rq 降序排序
+                    usort($sizeSummary, function ($a, $b) {
+                        return strtotime($b['UniqId']) - strtotime($a['UniqId']);
+                    });
+
+
+                    $this->success('请求成功', [
+                        'result' => $result,
+                        'headers' => $sizes, // 尺码表头
+                        'records' => $sizeSummary // 返回记录
+                    ]);
+                } else {
+                    $this->success('请扫描子订单编号', ['result' => '', 'records' => []]);
+                }
+                break;
+            case '包装':
+                if (strpos($order, ',') !== false) {
+                    if (!isset($params['sys_sbID']) || empty($params['sys_sbID'])) {
+                        $this->error('sys_sbID 参数错误');
+                    }
+
+                    $paramArray = explode(',', $order);
+                    //获取车缝生成小票的UniqId
+                    $Uniqid = \db('设备_产量计酬')->field('UniqId')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4]
+                        ])->find();
+
+                    $res = \db('设备_产量计酬')
+                        ->where([
+                            '子订单编号' => $paramArray[0],
+                            '尺码' => $paramArray[3],
+                            '数量' => $paramArray[4],
+                            'sczl_jtbh' => $params['sys_sbID'],
+                        ])
+                        ->order('sys_rq desc') // 按时间倒序排列,确保最新的记录在最前
+                        ->select();
+
+                    if (empty($res)) {
+                        // 如果查询结果为空,直接返回 $paramArray[4]
+                        $ci_num = $paramArray[4];
+                    } else {
+                        // 累加 s_num
+                        $total_s_num = 0;
+                        foreach ($res as $item) {
+                            $total_s_num += $item['s_num'];
+                        }
+
+                        // 判断累加结果与 $paramArray[4] 的关系
+                        if ($total_s_num >= $paramArray[4]) {
+                            $ci_num = 0; // 如果累加结果等于或大于 $paramArray[4],返回 0
+                        } else {
+                            // 否则返回最新记录中的 ci_num
+                            $ci_num = $res[0]['ci_num']; // 最新时间的记录已经通过 order 排在第一位
+                        }
+                    }
+
+                    $result = [
+                        'sys_sbID' => $params['sys_sbID'],//设备编号
+                        'ci_num' => $ci_num,//剩余数量
+                        'UniqId' => $Uniqid['UniqId'],
+                        'order' => $paramArray[0],//订单编号
+                        'zb' => $params['sys_sbID'],//组别
+                        'gx' => 4,//默认固定工序
+                        'cm' => $paramArray[3],//尺码
+                        'sl' => $paramArray[4],//数量
+                        'wb' => $paramArray[5]//尾包
+                    ];
+
+                    // 查询历史记录,按 sys_rq 时间倒序排序
+                    $existingRecords = \db('设备_产量计酬')->alias('c')
+                        ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
+                         c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
+                        ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+                        ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+                        ->where('c.子订单编号', $paramArray[0])
+                        ->where('c.sczl_jtbh', $params['sys_sbID'])
+                        ->whereNull('c.mod_rq') // 查询未删除数据
+                        ->where(function($query) {
+                            $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
+                        }) // 查询出库数据
+                        ->order('c.UniqId', 'desc')
+                        ->distinct('c.子订单编号')
+                        ->select();
+
+                    $sizeSummary = [];
+                    $sizes = [];
+
+                    foreach ($existingRecords as $record) {
+                        $size = $record['尺码'];
+                        $quantity = $record['数量'];
+                        $serial = $record['serial'];
+
+                        // 如果尺码还没有出现过,加入尺码列表
+                        if (!in_array($size, $sizes)) {
+                            $sizes[] = $size;
+                        }
+                        if (!isset($sizeSummary[$record['UniqId']])) {
+                            $sizeSummary[$record['UniqId']] = [
+                                'UniqId' => $record['UniqId'],
+                                '订单编号' => $record['订单编号'],
+                                '子订单编号' => $record['子订单编号'],
+                                '上报数量' => $record['s_num'],
+                                '次品数量' => $record['数量'] - $record['ci_num'],
+                                '生产款号' => $record['款号'],
+                                '尺码' => $record['尺码'],
+                                '工序编号' => $record['工序编号'],
+                                '工序名称' => $record['工序名称'],
+                                '数量' => $quantity, // 每个记录都单独显示数量
+                                '尾包' => $record['尾包'] == 1 ? '是' : '否',
+                                '颜色' => $record['颜色'],
+                                '组别' => $record['sczl_jtbh'],
+                                'serial' => $serial,
+                                'sys_rq' => $record['sys_rq'] // 上报日期
+                            ];
+                        }
+                    }
+
+                    // 将 $sizeSummary 转换为索引数组
+                    $sizeSummary = array_values($sizeSummary);
+
+                    // 对 $sizeSummary 按 sys_rq 降序排序
+                    usort($sizeSummary, function ($a, $b) {
+                        return strtotime($b['UniqId']) - strtotime($a['UniqId']);
+                    });
+
+
+                    $this->success('请求成功', [
+                        'result' => $result,
+                        'headers' => $sizes, // 尺码表头
+                        'records' => $sizeSummary // 返回记录
+                    ]);
+                } else {
+                    $this->success('请扫描子订单编号', ['result' => '', 'records' => []]);
+                }
+                break;
+            default:
+                $this->error('无效的 code 参数');
+                break;
+        }
     }
 
+
+
+
+
     /**
-     * 获取侧边栏通过工单编号
-     * @ApiMethod (GET)
+     * 出库报工
+     * 确认出库 恢复出库状态
      */
-    public function getTabByGdbh()
-    {
-        //get请求
-        if(!$this->request->isGet()){
-            $this->error('请求方式错误');
+    public function getTab() {
+        if (!$this->request->isPost()) {
+            $this->error('非法请求');
+        }
+
+        $params = $this->request->param();
+
+        // 校验子订单编号和出库日期
+        if (empty($params['order_id'])) {
+            $this->error('子订单编号不能为空');
         }
-        $date = date('Y-m-d',strtotime("-1 year"));
-
-        $rows = db()->table('db_抽检记录')->alias('d')->cache(true)
-            ->field('d.Sczl_gdbh, rtrim(y.Gd_cpmc) as Gd_cpmc')
-            ->join('工单_基本资料 y','y.Gd_gdbh = d.Sczl_gdbh','left')
-            // ->where('d.sys_rq','>=',$date)
-            ->group('d.Sczl_gdbh')
-            ->order('d.Sczl_gdbh desc')
-            ->limit(65)
-            ->select();
-        $arr = db()->table('db_抽检记录')->cache(true)
-            ->field('Sczl_gdbh, rtrim(Sys_id) as Sys_id, COUNT(*) as count')
-            ->where('Sczl_gdbh','>=',$rows[count($rows)-1]['Sczl_gdbh'])
-            ->group('Sczl_gdbh, Sys_id')
-            ->select();
-        foreach($rows as $key=>$value){
-            $rows[$key]['sys'] = [];
-            foreach($arr as $k=>$v){
-                if($value['Sczl_gdbh'] == $v['Sczl_gdbh']){
-                    unset($v['Sczl_gdbh']);
-                    array_push($rows[$key]['sys'],$v);
-                    unset($arr[$k]);
+
+        // 处理出库逻辑
+        if ($params['code'] == '出库') {
+            if (strpos($params['order_id'], '-') !== false) {
+                $cklist = \db('工单_印件资料')
+                    ->field('子订单编号, ck_rq')
+                    ->where('子订单编号', $params['order_id'])
+                    ->find();
+
+                if ($cklist) {
+                    if (!empty($cklist['ck_rq'])) {
+                        $this->error('该子订单已完成出库操作,请勿重复操作。');
+                    } else {
+                        // 更新 '工单_印件资料' 表
+                        $result1 = \db('工单_印件资料')
+                            ->where('子订单编号', $params['order_id'])
+                            ->fetchSql(true)
+                            ->update(['ck_rq' => date("Y-m-d 00:00:00")]);
+                        $updateCount1 = \db()->query($result1);
+
+                        // 更新 '工单_基本资料' 表
+                        $order = explode('-', $params['order_id'])[0];
+                        $result2 = \db('工单_基本资料')
+                            ->where('订单编号', $order)
+                            ->fetchSql(true)
+                            ->update(['出库日期' => date("Y-m-d 00:00:00"),'gd_statu'=>'2-生产中']);
+                        $updateCount2 = \db()->query($result2);
+
+                        if ($updateCount1 > 0 && $updateCount2 > 0) {
+                            // 插入日志
+                            $addlist = [
+                                'order_id' => $params['order_id'],
+                                'rq' => date("Y-m-d H:i:s"),
+                                'name' => $params['code'],
+                                'sys_id' => $params['sys_id']
+                            ];
+                            \db('设备_报工日志')->insertGetId($addlist);
+                            $this->success($params['order_id'] . "已出库");
+                        } else {
+                            $this->error('出库失败');
+                        }
+                    }
+                } else {
+                    $this->error('未找到对应的子订单');
                 }
+            } else {
+                $this->error('请扫描子订单编号进行出库');
             }
         }
 
-        $this->success('成功',$rows);
-    }
+        // 处理恢复逻辑
+        else if ($params['code'] == '恢复') {
+            $cklist = \db('工单_印件资料')
+                ->field('子订单编号, ck_rq')
+                ->where('子订单编号', $params['order_id'])
+                ->find();
 
-    /**
-     * 获取工单抽检记录列表
-     * @ApiMethod (GET)
-     * @param string $date 时间
-     * @param string $sys_id 用户
-     */
-    public function getList()
-    {
-        //get请求
-        if(!$this->request->isGet()){
-            $this->error('请求方式错误');
-        }
-        $req = $this->request->param();
-
-        $page = 1;
-        $limit = 15;
-        if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
-        if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
-
-        $where = [];
-        if (isset($req['date']) && !empty($req['date'])){
-            if(strpos($req['date'],'-')){
-                $where['Sys_rq'] = ['like',$req['date'].'%'];
-            }else{
-                $where['Sczl_gdbh'] = $req['date'];
+            if (!empty($cklist['ck_rq'])) {
+                $result1 = \db('工单_印件资料')
+                    ->where('子订单编号', $params['order_id'])
+                    ->fetchSql(true)
+                    ->update(['ck_rq' => null]);
+                $updateCount1 = \db()->query($result1);
+
+                // 更新 '工单_基本资料' 表
+                $order = explode('-', $params['order_id'])[0];
+                $result2 = \db('工单_基本资料')
+                    ->where('订单编号', $order)
+                    ->fetchSql(true)
+                    ->update(['出库日期' => null,'gd_statu'=>'1-计划中']);
+                $updateCount2 = \db()->query($result2);
+
+                if ($updateCount1 > 0 && $updateCount2 > 0) {
+                    // 插入日志
+                    $addlist = [
+                        'order_id' => $params['order_id'],
+                        'rq' => date("Y-m-d H:i:s"),
+                        'name' => $params['code'],
+                        'sys_id' => $params['sys_id']
+                    ];
+                    \db('设备_报工日志')->insertGetId($addlist);
+                    $this->success($params['order_id'] . "恢复未出库");
+                } else {
+                    $this->error($params['order_id'] . '该子订单是未出库状态,无需再次恢复');
+                }
+            } else {
+                $this->error('该子订单是未出库状态,无需再次恢复');
             }
-        }else{
-            $this->error('参数错误');
-        }
-        if (isset($req['sys_id']) && !empty($req['sys_id'])) $where['Sys_id'] = ['LIKE',$req['sys_id'].'%'];
-
-        $rows = db()->table('db_抽检记录')
-            ->field('LEFT(Sczl_rq, 10) as Sczl_rq, Sczl_bh, Sczl_gdbh, rtrim(Sczl_gxmc) as Sczl_gxmc,
-            sczl_yjno, sczl_gxh, Sczl_num, Sczl_抽检数, sczl_A类废, sczl_B类废, sczl_C类废,
-            rtrim(Sczl_desc) as Sczl_desc, rtrim(Sys_id) as Sys_id, Sys_rq, Mod_rq, UniqId')
-            ->where($where)
-            ->order('UniqId asc')
-            ->page($page,$limit)
-            ->select();
-
-        $total = db()->table('db_抽检记录')->where($where)->count();
-        $gd = db()->table('工单_基本资料')->column('Gd_gdbh, Gd_cpmc');
-        $rs = db()->table('人事_基本资料')->column('员工编号, 员工姓名');
-        foreach ($rows as $key=>$value) {
-            $rows[$key]['sczl_A类废'] = $value['sczl_A类废']==0 ? '' :$value['sczl_A类废'];
-            $rows[$key]['sczl_B类废'] = $value['sczl_B类废']==0 ? '' :$value['sczl_B类废'];
-            $rows[$key]['sczl_C类废'] = $value['sczl_C类废']==0 ? '' :$value['sczl_C类废'];
-            $rows[$key]['Mod_rq'] = $value['Mod_rq']=='1900-01-01 00:00:00' ? '' :$value['Mod_rq'];
-            $rows[$key]['Gd_cpmc'] = array_key_exists($value['Sczl_gdbh'],$gd) ? trim($gd[$value['Sczl_gdbh']]) : '';
-            $rows[$key]['name'] = array_key_exists($value['Sczl_bh'],$rs) ? trim($rs[$value['Sczl_bh']]) : '';
         }
-
-        $data = [
-            'total'          => $total,
-            'rows'          => $rows,
-        ];
-        $this->success('成功',$data);
     }
-
     /**
-     * 获取工单抽检记录信息
-     * @ApiMethod (GET)
-     * @param string $UniqId UniqId
+     * 裁切完工报工
+     * 确定
      */
-    public function getInfo()
-    {
-        //get请求
-        if(!$this->request->isGet()){
-            $this->error('请求方式错误');
+    public function getInfo(){
+        if (!$this->request->isPost()) {
+            $this->error('非法请求');
         }
-        $req = $this->request->param();
-        if (isset($req['UniqId']) && !empty($req['UniqId'])){
-            $UniqId = $req['UniqId'];
-        }else{
-            $this->error('参数错误');
+        $params = $this->request->param();
+        // 检查子订单编号和日期是否都存在
+        if (!isset($params['order_id']) || empty($params['order_id'])) {
+            $this->error('参数错误,子订单编号不能为空');
         }
-
-        $rows = db()->table('db_抽检记录')
-        ->field('Sczl_bh,rtrim(rs.员工姓名) as name,rtrim(sczl_bzdh) as sczl_bzdh ,LEFT(Sczl_rq, 10) as Sczl_rq,
-        Sczl_gdbh,rtrim(g.Gd_cpmc) as Gd_cpmc, rtrim(Sczl_gxmc) as Sczl_gxmc,sczl_gxh,Sczl_num,Sczl_抽检数,
-        Sczl_A类废,Sczl_B类废,Sczl_C类废,rtrim(Sczl_desc) as Sczl_desc')
-        ->join('人事_基本资料 rs','rs.员工编号=db_抽检记录.Sczl_bh','LEFT')
-        ->join('工单_基本资料 g','g.Gd_gdbh=db_抽检记录.Sczl_gdbh','LEFT')
-        ->where('db_抽检记录.UniqId',$UniqId)->limit(1)->select();
-
-        if($rows){
-            $this->success('成功',$rows);
-        }else{
-            $this->error('失败');
+        if (!isset($params['rq']) || empty($params['rq'])) {
+            $this->error('参数错误,日期不能为空');
+        }
+        function setIfNotEmpty($value) {
+            return (!empty($value) && $value != 0) ? $value : '';
+        }
+        $updateData = [
+            'sc_rq' => date('Y-m-d H:i:s'),
+            'scsl1' => setIfNotEmpty($params['scsl1']),
+            'scsl2' => setIfNotEmpty($params['scsl2']),
+            'scsl3' => setIfNotEmpty($params['scsl3']),
+            'scsl4' => setIfNotEmpty($params['scsl4']),
+            'scsl5' => setIfNotEmpty($params['scsl5']),
+            'scsl6' => setIfNotEmpty($params['scsl6']),
+            'scsl7' => setIfNotEmpty($params['scsl7']),
+            'scsl8' => setIfNotEmpty($params['scsl8']),
+            'scsl9' => setIfNotEmpty($params['scsl9']),
+            'scsl10' => setIfNotEmpty($params['scsl10']),
+            'sctotal' => setIfNotEmpty($params['sctotal']),
+            // 添加其他要更新的字段
+        ];
+        // 更新数据库
+        $updateCount = \db('工单_印件资料')
+            ->where('子订单编号', $params['order_id'])
+            ->where('UniqId', $params['UniqId'])
+            ->update($updateData);
+        // 检查更新结果
+        if ($updateCount > 0) {
+            $this->success("更新成功");
+        } else {
+            // 查询数据库,是否能找到对应的子订单编号
+            $existingRecord = \db('工单_印件资料')
+                ->where('子订单编号', $params['order_id'])
+                ->where('UniqId', $params['UniqId'])
+                ->find();
+            if ($existingRecord) {
+                $this->error('更新失败,可能是提供的更新数据与现有数据相同,未进行任何更改');
+            } else {
+                $this->error('更新失败,未找到对应的子订单编号');
+            }
         }
-        
     }
+
     /**
-     * 定位
-     * @ApiMethod (GET)
+     * 后道、大烫、总检、包装完工报工【通用的报工操作函数】
      */
-    public function search()
-    {
-        //get请求
-        if(!$this->request->isGet()){
-            $this->error('请求方式错误');
+    function reportWork($params) {
+        $bglist = \db('设备_产量计酬')->field('订单编号,子订单编号,数量,ci_num,款号,sys_rq,serial,serial_num')->where('UniqId', $params['UniqId'])->fetchSql(true)->find();
+        $list_gd = \db()->query($bglist);
+
+        // 构建单条记录的函数,传递 $list_gd 作为参数
+        function buildRecords($item, $list_gd) {
+            return [
+                '订单编号' => $item['order'],
+                '子订单编号' => $item['order_id'],
+                '款号' => $list_gd[0]['款号'],
+                'serial' => $list_gd[0]['serial'],
+                'serial_num' => $list_gd[0]['serial_num'],
+                '工序编号' => $item['gx'],
+                '工序名称' => $item['gxmc'],
+                '尺码' => $item['cm'],
+                '数量' => $item['sl'],
+                'sczl_jtbh' => $item['sczl_jtbh'],
+                'sczl_bh' => $item['sczl_bh'],
+                '尾包' => $item['wb'],
+                'sys_id' => $item['sys_id'],
+                'sys_rq' => date('Y-m-d H:i:s'),
+                'sczl_rq' => date('Y-m-d'),
+                'ci_num' => $item['ci_num'] - $item['s_num'],
+                's_num' => $item['s_num'],
+            ];
         }
-        $req = $this->request->param();
-        if (isset($req['search']) && !empty($req['search'])){
-            $search = $req['search'];
-        }else{
-            $this->error('参数错误');
+        // 检查是否是索引数组(多条记录)
+        if (isset($params[0]) && is_array($params[0])) {
+            foreach ($params as $item) {
+                $list[] = buildRecords($item, $list_gd);
+            }
+        } else {
+            //处理单条记录
+            $list[] = buildRecords($params, $list_gd);
         }
-        $page = 1;
-        $limit = 10;
-        if (isset($req['page']) && !empty($req['page'])) $page = $req['page'];
-        if (isset($req['limit']) && !empty($req['limit'])) $limit = $req['limit'];
-
-        $gd = db()->table('工单_基本资料')->where('Gd_cpmc',$search)->value('Gd_gdbh');
-        if($gd){
-            $search=$gd;
+        $result = \db('设备_产量计酬')->insertAll($list);
+        if ($result) {
+            $this->success('数据插入成功 : ' . date('H:i:s'));
+        } else {
+            $this->error('数据插入失败');
         }
-        $rows = db()->table('db_抽检记录')
-        ->field('Sczl_bh,sczl_yjno,rtrim(rs.员工姓名) as name,LEFT(Sczl_rq, 10) as Sczl_rq,
-        Sczl_gdbh,rtrim(g.Gd_cpmc) as Gd_cpmc, rtrim(Sczl_gxmc) as Sczl_gxmc,sczl_gxh,Sczl_num,Sczl_抽检数,
-        Sczl_A类废,Sczl_B类废,Sczl_C类废,rtrim(Sczl_desc) as Sczl_desc,rtrim(db_抽检记录.Sys_id) as Sys_id,
-        db_抽检记录.Sys_rq,db_抽检记录.Mod_rq,db_抽检记录.UniqId')
-        ->join('人事_基本资料 rs','rs.员工编号=db_抽检记录.Sczl_bh','LEFT')
-        ->join('工单_基本资料 g','g.Gd_gdbh=db_抽检记录.Sczl_gdbh','LEFT')
-        ->where('db_抽检记录.Sczl_gdbh',$search)
-        ->page($page,$limit)->select();
-        $total = db()->table('db_抽检记录')->where('db_抽检记录.Sczl_gdbh',$search)->count();
-        $data = ['total'=> $total,'rows'=> $rows];
-         if($rows){
-            $this->success('成功',$data);
-         }else{
-            $this->error('失败');
-         }
     }
+
     /**
-     * 修改
-     * @ApiMethod POST
-    */
-    public function edit()
-    {
-        if(!$this->request->isPost()){
-            $this->error('请求方式错误');
-        }
-        $req = $this->request->param();
-        if (isset($req['UniqId']) && !empty($req['UniqId'])){
-            $UniqId = $req['UniqId'];
-        }else{
-            $this->error('参数错误');
-        }
-        if (isset($req['Sczl_gxmc']) && !empty($req['Sczl_gxmc'])){
-            $req['sczl_gxh'] = explode('-',$req['Sczl_gxmc'])[1];
-        }
-        if (isset($req['Sczl_rq']) && !empty($req['Sczl_rq'])){
-            $req['Sczl_rq'] = $req['Sczl_rq'].' 00:00:00';
-        }
-        $req['Mod_rq'] = date('Y-m-d H:i:s');
-
-        //开启事务
-        db()->startTrans();
-        try{
-            $sql = db()->table('db_抽检记录')->where('UniqId',$UniqId)->fetchSql(true)->update($req);
-            $res= db()->query($sql);
-            // 提交事务
-            db()->commit();
-        } catch (\Exception $e) {
-            // 回滚事务
-            db()->rollback();
-            $this->error($e->getMessage());
+     * 车缝新增、后道新增、大烫新增、总检新增、包装新增
+     * /work_order_spot_check/search
+     */
+    public function search(){
+        if (!$this->request->isPost()) {
+            $this->error('非法请求');
         }
+        $params = $this->request->param();
+        // 根据不同的 code 值执行不同的逻辑
+        switch ($params['code']) {
+            case '车缝':
+                $list = [];
+                $parts = explode('-', $params['order_id']); // 使用 '-' 分割字符串
+                $result = $parts[0]; // 获取 '-' 前面的部分
+                $sql = \db('工单_基本资料')->where('订单编号', $result)->fetchSql(true)->find();
+                $list_gd = \db()->query($sql); // 查询的结果可能是多条记录,确保是数组
 
-        if($res===false) $this->error('失败');
+                // 查询子订单编号对应的最大 serial 值
+                $list_order_id = \db('设备_产量计酬')
+                    ->field('子订单编号, MAX(serial) as serial')
+                    ->where('子订单编号',  $params['order_id'])
+                    ->group('子订单编号')
+                    ->find();
 
-        $this->success('成功');
-    }
-      /**
-     * 新增
-     * @ApiMethod POST
-    */
-    public function add()
-    {
-        if(!$this->request->isPost()){
-            $this->error('请求方式错误');
-        }
+                function buildRecord($item, $list_gd, $list_order_id) {
+                    return [
+                        '订单编号' => $list_gd[0]['订单编号'],
+                        '款号' => $list_gd[0]['生产款号'],
+                        '子订单编号' => $item['order_id'],
+                        '工序编号' => 3,
+                        '工序名称' => '车缝',
+                        'serial' => $item['serial'],
+                        'serial_num' => $item['serial_num'],
+                        '尺码' => $item['cm'],
+                        '数量' => $item['sl'],
+                        'sczl_jtbh' => $item['sczl_jtbh'],
+                        'sys_rq' => date('Y-m-d H:i:s'),
+                        'sczl_rq' => date('Y-m-d'),
+                        '尾包' => $item['wb'],
+                        'sys_id' => $item['sys_id'],
+                        'sczl_bh' => $item['sczl_bh']
+                    ];
+                }
+                if (isset($params[0]) && is_array($params[0])) {
+                    foreach ($params as $item) {
+                        $list[] = buildRecord($item, $list_gd, $list_order_id); // 所有记录使用相同的 serial
+                    }
+                } else {
+                    // 处理单条记录
+                    $list[] = buildRecord($params, $list_gd, $list_order_id); // 单条记录
+                }
+                $result = \db('设备_产量计酬')->insertAll($list);
 
-        $req = $this->request->param();
-        if (isset($req) && !empty($req)){
-        }else{
-            $this->error('参数错误');
-        }
-        if (isset($req['Sczl_gxmc']) && !empty($req['Sczl_gxmc'])){
-            $req['sczl_gxh'] = explode('-',$req['Sczl_gxmc'])[1];
-        }
-        if (isset($req['Sczl_rq']) && !empty($req['Sczl_rq'])){
-            $req['Sczl_rq'] = $req['Sczl_rq'].' 00:00:00';
-        }
-        $req['Sys_rq'] = date('Y-m-d H:i:s');
-        $UniqId = db()->table('db_抽检记录')->order('UniqId desc')->value('UniqId');
-        if ($UniqId < 1000000){
-            $UniqId = 1000000;
-        }else{
-            $UniqId = $UniqId + 1;
-        }
-        $req['UniqId'] = $UniqId;
-        //开启事务
-        db()->startTrans();
-        try{
-            $sql = db()->table('db_抽检记录')->fetchSql(true)->insert($req);
-            $res= db()->query($sql);
-            // 提交事务
-            db()->commit();
-        } catch (\Exception $e) {
-            // 回滚事务
-            db()->rollback();
-            $this->error($e->getMessage());
+                if ($result) {
+                    $this->success('数据插入成功 : ' . date('H:i:s'));
+                } else {
+                    $this->error('数据插入失败');
+                }
+                break;
+            case '后道':
+            case '大烫':
+            case '总检':
+            case '包装':
+                //调用reportWork方法名函数
+                $this->reportWork($params);
+                break;
+            default:
+                $this->error('无效的 code 参数');
+                break;
         }
+    }
 
-        if($res===false) $this->error('失败');
 
-        $this->success('成功');
+    /**
+     * 物理地址获取机台编号
+     * /work_order_spot_check/getMachineMac
+     */
+    public function getMachineMac(){
+        if ($this->request->isGet() == false){$this->error('非法请求');}
+        $params = $this->request->get();
+        if (empty($params['sys_sbID'])){$this->error('物理地址参数不能为空');}
+
+        $data = \db('设备_基本资料')->where('sys_sbID', $params['sys_sbID'])
+            ->field('设备编号 as 机台号,生产工序,工序,rtrim(设备编组) as 组别,组长')
+            ->where('生产工序',$params['code'])->find();
+        $this->success('请求成功', $data);
     }
-   /**
-     * 删除
-     * @ApiMethod (GET)
-     * @param string $UniqId UniqId
+
+    /**
+     * 通过子订单编号删除
+     * /work_order_spot_check/del
      */
-    public function del(){
-        //get请求
-        if(!$this->request->isGet()){
-            $this->error('请求方式错误');
-        }
-        $req = $this->request->param();
-        if (isset($req['UniqId']) && !empty($req['UniqId'])){
-            $UniqId = $req['UniqId'];
-        }else{
-            $this->error('参数错误');
-        }
+    public function del() {
+        if (!$this->request->isGet()) {return $this->error('请求错误');}
 
-        $rows = db()->table('db_抽检记录')->where('UniqId',$UniqId)->delete();
-        if($rows){
-           $this->success('成功');
-        }else{
-           $this->error('失败');
-        }
+        $param = $this->request->param();
+        if (empty($param['UniqId'])) {return $this->error('UniqId参数错误');}
 
+        // 将传过来的多个 UniqId 通过逗号分割为数组
+        $printIdArray = explode(',', $param['UniqId']);
+        // 构建更新字段
+        $updateData = [
+            'mod_rq' => date('Y-m-d H:i:s')
+        ];
+        // 初始化计数器
+        $failureCount = 0;
+        // 批量更新记录
+        foreach ($printIdArray as $uniqId) {
+            $result = \db('设备_产量计酬')->where('UniqId', $uniqId)->fetchSql(true) ->update($updateData);
+            $sql = \db()->query($result);
+            // 检查更新是否成功
+            if ($sql === false){
+                $failureCount++;
+            }
+        }
+        // 根据失败次数返回结果
+        if ($failureCount === 0) {
+            return $this->success('删除成功');
+        } else {
+            return $this->error("删除失败,失败次数:{$failureCount}");
+        }
     }
+
 }

+ 55 - 48
application/api/controller/WorkOrderVerification.php

@@ -24,46 +24,53 @@ class WorkOrderVerification extends Api
     }
 
     /**
-     * 获取工单核验单侧边栏
-     * @ApiMethod (GET)
+     * 获取报工历史几记录
+     *
      */
-    public function getTab()
-    {
+    public function getTab(){
         //get请求
         if(!$this->request->isGet()){
             $this->error('请求方式错误');
         }
-        if(!cache('WorkOrderVerification-getTable')){
-            $time = date('Y-m-d',time()-8640000);
-            $rows = db()->table('db_qczl')
-                ->field('LEFT(sys_rq, 10) as date, COUNT(*) as counts')
-                ->where('sys_rq','>=',$time)
-                ->group('date')
-                ->order('date desc')
-                ->limit(30)
-                ->select();
-            $arr = db()->table('db_qczl')
-                ->field('LEFT(sys_rq, 10) as date, rtrim(sys_id) as sys_id, COUNT(*) as count')
-                ->where('sys_rq','>=',$rows[count($rows)-1]['date'])
-                ->group('date, sys_id')
-                ->select();
-            foreach($rows as $key=>$value){
-                $rows[$key]['sys'] = [];
-                foreach($arr as $k=>$v){
-                    if($value['date'] == $v['date']){
-                        unset($v['date']);
-                        array_push($rows[$key]['sys'],$v);
-                        unset($arr[$k]);
-                    }
-                }
-                $rows[$key]['date'] = str_replace('-', '.', $rows[$key]['date']);
-            }
-            //设置缓存
-            cache('WorkOrderVerification-getTable',$rows,3600);
-        }else{
-            $rows =  cache('WorkOrderVerification-getTable');
+        $param = $this->request->param();
+        $where = [
+            '子订单编号' => $param['order_id'],
+            'sczl_jtbh' => $param['sczl_jtbh']
+        ];
+        //通过当前子订单编号和机台查看历史记录数据
+        $table_list = \db('设备_产量计酬')->alias('c')
+            ->field('c.订单编号, c.子订单编号, c.款号, c.工序编号, c.工序名称, c.尺码, c.数量, c.sczl_jtbh, c.尾包,c.UniqId,c.ci_num,c.s_num,
+                         c.sys_rq, c.serial, y.zdtotal, y.sctotal, j.款式, y.颜色')
+            ->join('工单_印件资料 y', 'c.子订单编号 = y.子订单编号', 'left')
+            ->join('工单_基本资料 j', 'c.订单编号 = j.订单编号', 'left')
+            ->where('c.子订单编号', $param['order_id'])
+            ->where('c.sczl_jtbh', $param['sczl_jtbh'])
+            ->whereNull('c.mod_rq') // 查询未删除数据
+            ->where(function($query) {
+                $query->whereNotNull('y.ck_rq')->where('y.ck_rq', '<>', '');
+            }) // 查询出库数据
+            ->order('c.UniqId', 'desc')
+            ->distinct('c.子订单编号')
+            ->select();
+        $output = [];
+        foreach ($table_list as $record) {
+            $output[] = [
+                'serial' => $record['serial'],
+                'sys_rq' => $record['sys_rq'],
+                '订单编号' => $record['订单编号'],
+                '子订单编号' => $record['子订单编号'],
+                '颜色' => $record['颜色'],
+                '尺码' => $record['尺码'],
+                '数量' => $record['数量'],
+                '上报数量' => $record['s_num'],
+                '尾包' => $record['尾包'],
+                '组别' => $record['sczl_jtbh'],
+            ];
         }
-        $this->success('成功',$rows);
+
+        $this->success('请求成功', [
+            'records' => $output
+        ]);
     }
 
     /**
@@ -202,7 +209,7 @@ class WorkOrderVerification extends Api
      * 获取单个工单核检单信息
      * @ApiMethod GET
      * @params string UniqId
-    */
+     */
     public function getOneWorkOrder(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -302,7 +309,7 @@ class WorkOrderVerification extends Api
      * @ApiMethod GET
      * @params string order
      * @params string yj_no
-    */
+     */
     public function getYjInfo(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -333,7 +340,7 @@ class WorkOrderVerification extends Api
      * 获取废品分类
      * @ApiMethod GET
      * @params string search
-    */
+     */
     public function getWastInfo(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -376,7 +383,7 @@ class WorkOrderVerification extends Api
      * @ApiMethod GET
      * @params string type
      * @params string order
-    */
+     */
     public function getGxAndLeader(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -431,7 +438,7 @@ class WorkOrderVerification extends Api
      * 修改工单核检单
      * @ApiMethod POST
      * @params array data
-    */
+     */
     public function edit(){
         if (Request::instance()->isPost() == false){
             $this->error('非法请求');
@@ -455,7 +462,7 @@ class WorkOrderVerification extends Api
      * 新增工单核检单
      * @ApiMethod POST
      * @params array data
-    */
+     */
     public function add(){
         if (Request::instance()->isPost() == false){
             $this->error('非法请求');
@@ -481,7 +488,7 @@ class WorkOrderVerification extends Api
      * 工单核检废品分布
      * @ApiMethod GET
      * @params string order
-    */
+     */
     public function wasteDistribution(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -593,7 +600,7 @@ class WorkOrderVerification extends Api
      * @params string order
      * 计划产量计算方式:上个工序计划产量-(基础损耗+损耗率*上个工序计划产量)*损耗系数*计损色数)
      * lastNum - (Gy0_Rate0 + Gy0_Rate1 * lastNum )* 损耗系数(Gy0_Rate3) * Gy0_Ms
-    */
+     */
     public function getOrderDate(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -660,7 +667,7 @@ class WorkOrderVerification extends Api
      * @ApiMethod POST
      * @params string UniqId
      * @params string date
-    */
+     */
     public function editOrderFinishDate(){
         if (Request::instance()->isPost() == false){
             $this->error('非法请求');
@@ -686,7 +693,7 @@ class WorkOrderVerification extends Api
      * 工单工序生产进程菜单栏
      * @ApiMethod GET
      * @params string order
-    */
+     */
     public function getOrderProcessLeft(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -727,7 +734,7 @@ class WorkOrderVerification extends Api
      * @ApiMethod GET
      * @params string order
      * @params string gxNo
-    */
+     */
     public function getOrderProcessRight(){
         if (Request::instance()->isGet() == false){
             $this->error('非法请求');
@@ -782,7 +789,7 @@ class WorkOrderVerification extends Api
      * @ApiMethod GET
      * @params date start_date
      * @params date end_date
-    */
+     */
     public function getDaysWast()
     {
         if (Request::instance()->isGet() == false) {
@@ -881,7 +888,7 @@ class WorkOrderVerification extends Api
      * 月度核检废品责任人统计
      * @ApiMethod GET
      * @params string date
-    */
+     */
     public function getMonthPeopleTotal(){
         if (Request::instance()->isGet() == false) {
             $this->error('非法请求');
@@ -896,7 +903,7 @@ class WorkOrderVerification extends Api
      * 工单质检废品统计
      * @ApiMethod GET
      * @params string order
-    */
+     */
     public function getOrderWasteTotal(){
         if (Request::instance()->isGet() == false) {
             $this->error('非法请求');