request->isGet() === false) { $this->error('请求错误'); } $customers = \db('产品_基本资料') ->where('客户编号', '<>', '') ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称') ->group('客户编号') ->order('客户编号 asc') ->select(); if (empty($customers)) { $this->success('', []); } $productCodes = \db('工单_基本资料') ->where('行号', '1') ->where('成品代号', '<>', '') ->where('gd_statu', 'in', ['2-生产中', '3-计划中']) ->distinct(true) ->column('成品代号'); $codesByInitial = []; foreach ($productCodes as $productCode) { $productCode = rtrim((string)$productCode); if ($productCode === '') { continue; } $codesByInitial[$productCode[0]][] = $productCode; } $data = [ '印刷工单' => [], '糊盒工单' => [], ]; foreach ($customers as $value) { $customerCode = rtrim((string)$value['客户编号']); if ($customerCode === '') { continue; } $matched = false; foreach ($codesByInitial[$customerCode[0]] ?? [] as $productCode) { if (stripos($productCode, $customerCode) === 0) { $matched = true; break; } } if (!$matched) { continue; } $name = $customerCode . $value['客户名称']; if (strpos($customerCode, 'J') !== false) { $data['糊盒工单'][] = $name; } elseif (strpos($customerCode, 'Y') !== false) { $data['印刷工单'][] = $name; } } $this->success('成功', $data); } /** * 右侧工单详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function workOrderDetail() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); $where = []; if (!empty($param['order'])) { $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%']; } if (!empty($param['search'])) { $where['a.Gd_gdbh'] = ['like', '%' . $param['search'] . '%']; } $list = \db('工单_基本资料') ->alias('a') ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh') ->field('b.Yj_Gdbh as 工单编号,b.yj_Yjno,b.yj_Yjdh as 印件代号,b.yj_yjmc as 印件名称,b.yj_ls as 联数,a.投料大箱 as 投料大箱,b.yj_平张投料 as 计划投料') ->where($where) ->where('a.gd_statu', '2-生产中') ->group('b.Yj_Gdbh,b.yj_Yjno') ->select(); if (empty($list)) { $this->success(''); } $orderNos = []; foreach ($list as $row) { $orderNos[$row['工单编号']] = $row['工单编号']; } $processRows = \db('工单_工艺资料') ->alias('a') ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left') ->where('a.Gy0_gdbh', 'in', array_values($orderNos)) ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量') ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh') ->order('a.Gy0_gxh asc') ->select(); $latestNumSql = \db('设备_产量计酬') ->where('sczl_gdbh', 'in', array_values($orderNos)) ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num') ->group('sczl_gdbh, sczl_yjno, sczl_gxh') ->buildSql(); $latestRows = \db('设备_产量计酬') ->alias('t') ->join([$latestNumSql => 'm'], 't.sczl_gdbh = m.sczl_gdbh AND t.sczl_yjno = m.sczl_yjno AND t.sczl_gxh = m.sczl_gxh AND t.sczl_num = m.max_num') ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq') ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num') ->select(); $latestMap = []; foreach ($latestRows as $latest) { $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh']; $latestMap[$latestKey] = $latest; } $processMap = []; foreach ($processRows as $process) { $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']); $processMap[$mapKey][] = $process; } foreach ($list as $key => $value) { $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']); $index = 1; foreach ($processMap[$mapKey] ?? [] as $process) { $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : ''; $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh']; $latest = $latestMap[$latestKey] ?? []; $flowNo = rtrim((string)($latest['sczl_num'] ?? '')); if ($flowNo === '0') { $flowNo = ''; } $reportDate = (string)($latest['sczl_rq'] ?? ''); if ($reportDate !== '') { $reportDate = substr($reportDate, 0, 10); } $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']'; $index++; } unset($list[$key]['yj_Yjno']); } $this->success('成功', $list); } }