request->isGet() === false) { $this->error('请求错误'); } $customerRows = \db('产品_基本资料') ->where('客户编号', '<>', '') ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称') ->group('客户编号') ->order('客户编号 asc') ->select(); $productCustomers = []; foreach ($customerRows as $row) { $customerCode = rtrim((string)$row['客户编号']); if ($customerCode === '') { continue; } $productCustomers[] = [ 'code' => $customerCode, 'name' => rtrim((string)$row['客户名称']), ]; } $orders = \db('工单_基本资料') ->where('行号', '1') ->where('gd_statu', 'in', ['2-生产中', '3-计划中']) ->field('rtrim(成品代号) as 成品代号, rtrim(Gd_客户代号) as Gd_客户代号, MAX(rtrim(Gd_客户名称)) as Gd_客户名称') ->group('成品代号, Gd_客户代号') ->order('Gd_客户代号 asc') ->select(); if (empty($productCustomers) && empty($orders)) { $this->success('', []); } $ordersByInitial = []; foreach ($orders as $index => $order) { $orders[$index]['成品代号'] = rtrim((string)$order['成品代号']); $orders[$index]['Gd_客户代号'] = rtrim((string)$order['Gd_客户代号']); $orders[$index]['Gd_客户名称'] = rtrim((string)$order['Gd_客户名称']); $productCode = $orders[$index]['成品代号']; if ($productCode !== '') { $ordersByInitial[strtoupper($productCode[0])][] = $orders[$index]; } } $data = [ '印刷工单' => [], '糊盒工单' => [], ]; $usedCodes = []; $appendCustomer = function ($code, $name) use (&$data, &$usedCodes) { $code = rtrim((string)$code); $codeKey = strtoupper($code); if ($code === '' || isset($usedCodes[$codeKey])) { return; } $usedCodes[$codeKey] = true; $label = $code . rtrim((string)$name); if (strpos($code, 'J') !== false) { $data['糊盒工单'][] = $label; } elseif (strpos($code, 'Y') !== false) { $data['印刷工单'][] = $label; } }; foreach ($productCustomers as $customer) { $customerCode = $customer['code']; $customerName = $customer['name']; $matchedOrders = []; foreach ($ordersByInitial[strtoupper($customerCode[0])] ?? [] as $order) { if (stripos($order['成品代号'], $customerCode) === 0) { $matchedOrders[] = $order; } } if (empty($matchedOrders)) { continue; } if ($customerName !== '') { $appendCustomer($customerCode, $customerName); continue; } foreach ($matchedOrders as $order) { if ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '') { continue; } $appendCustomer( $order['Gd_客户代号'] !== '' ? $order['Gd_客户代号'] : $customerCode, $order['Gd_客户名称'] ); } } foreach ($orders as $order) { $productCode = $order['成品代号']; $matchedProductCustomer = false; if ($productCode !== '') { foreach ($productCustomers as $customer) { $customerCode = $customer['code']; if ($customerCode !== '' && strcasecmp($productCode[0], $customerCode[0]) === 0 && stripos($productCode, $customerCode) === 0) { $matchedProductCustomer = true; break; } } } if ($matchedProductCustomer || ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '')) { continue; } $appendCustomer($order['Gd_客户代号'], $order['Gd_客户名称']); } $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'] . '%']; } $listQuery = \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-生产中') ->where('a.Gd_gdbh', 'like', 'Y%'); $workOrderNo = trim((string)($param['工单编号'] ?? '')); if ($workOrderNo !== '') { $listQuery->where('a.Gd_gdbh', 'like', '%' . $this->courseLikeKeyword($workOrderNo) . '%'); } $productName = trim((string)($param['产品名称'] ?? '')); if ($productName !== '') { $nameLike = '%' . $this->courseLikeKeyword($productName) . '%'; $listQuery->where(function ($query) use ($nameLike) { $query->where('a.成品名称', 'like', $nameLike) ->whereOr('a.Gd_cpmc', 'like', $nameLike) ->whereOr('b.yj_yjmc', 'like', $nameLike); }); } $salesOrderNo = trim((string)($param['销售订单号'] ?? '')); if ($salesOrderNo !== '') { $listQuery->where('a.销售订单号', 'like', '%' . $this->courseLikeKeyword($salesOrderNo) . '%'); } $this->applyCourseRange($listQuery, 'a.投料大箱', $param['投料大箱起'] ?? '', $param['投料大箱止'] ?? ''); $this->applyCourseRange($listQuery, 'a.接单日期', $param['接单日期起'] ?? '', $param['接单日期止'] ?? '', true); $this->applyCourseRange($listQuery, 'a.交货日期', $param['交货日期起'] ?? '', $param['交货日期止'] ?? '', true); $this->applyCourseRange($listQuery, 'b.yj_平张投料', $param['计划投料起'] ?? '', $param['计划投料止'] ?? ''); $list = $listQuery ->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); } /** * 范围条件只在传入起或止时生效,日期止取到当天结束 */ private function applyCourseRange($query, $field, $from, $to, $isDate = false) { $from = trim((string)$from); $to = trim((string)$to); if ($from !== '') { $query->where($field, '>=', $from); } if ($to !== '') { if ($isDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) { $to .= ' 23:59:59'; } $query->where($field, '<=', $to); } } private function courseLikeKeyword($keyword) { return addcslashes((string)$keyword, '%_\\'); } }