request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); // $where = [ // 'gd_statu' => '1-已完工', // ]; if (empty($param['search'])){ $where['Mod_rq'] = ['between',[date('Y-m-d 00:00:00',time()-1209600),date('Y-m-d H:i:s',time())]]; }else{ $where['Gd_gdbh|Gd_客户代号|成品名称'] = ['like','%'.$param['search'].'%']; } $list = \db('工单_基本资料') ->field('Gd_gdbh,Gd_cpmc,行号') ->where($where) ->group('行号') ->select(); if (empty($list)){ $this->success('未找到工单'); } foreach ($list as $key=>$value){ $list[$key]['工单'] = $value['Gd_gdbh'].'-'.$value['行号'].'-'.$value['Gd_cpmc']; } $this->success('成功',$list); } /** * 获取左侧菜单栏 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetList() { if ($this->request->isGet() === false){ $this->error('参数错误'); } $param = $this->request->param(); if (empty($param) || isset($param['gdbh']) === false || isset($param['yjno']) === false){ $this->error('参数错误'); } $list = db('设备_产量计酬') ->field('DISTINCT(sczl_num) as num') ->where('sczl_gdbh',$param['gdbh']) ->where('sczl_yjno',$param['yjno']) ->order('num') ->select(); if (empty($list)){ $this->success('未找到流程单信息'); } foreach ($list as $key => $value){ $list[$key]['流程单'] = '第'.$value['num'].'个流程单'; } // $cpmc = db('工单_基本资料') // ->where('Gd_gdbh',$param['gdbh']) // ->where('行号',$param['yjno']) // ->column('Gd_cpmc')[0]; // $data[$param['gdbh'].'-'.$cpmc]['印件'.$param['yjno']] = $list; $this->success('成功',$list); } /** * 获取工单信息 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetOrderDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || isset($param['gdbh']) === false || isset($param['yjno']) === false){ $this->error('参数错误'); } $list = db('工单_基本资料') ->field('Gd_gdbh,Gd_cpdh,Gd_cpmc,订单数量,实际投料,计量单位,投料率') ->where('Gd_gdbh',$param['gdbh']) ->where('行号',$param['yjno']) ->find(); if (empty($list)){ $this->success('未找到该工单信息'); }else{ $this->success('成功',$list); } } /** * 工艺及生产班组数据获取 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ProcessList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (!isset($param['gdbh']) || !isset($param['yjno']) || !isset($param['num'])){ $this->error('参数错误'); } $query= db('设备_产量计酬') ->alias('a') ->field('a.sczl_num as 流程单号,a.sczl_gxh as 工序号,a.sczl_gxmc as 工序名称,a.sczl_rq as 生产日期, a.sczl_jtbh as 机台编号,a.sczl_bh1,a.sczl_bh2,a.sczl_bh3,a.sczl_bh4,a.sczl_bh5,a.sczl_bh6,a.sczl_bh7, a.sczl_bh8,a.sczl_bh9,a.sczl_bh10,d1.员工姓名 as name1,d2.员工姓名 as name2,d3.员工姓名 as name3,d4.员工姓名 as name4,d5.员工姓名 as name5,d6.员工姓名 as name6,d7.员工姓名 as name7,d8.员工姓名 as name8 ,d9.员工姓名 as name9,d10.员工姓名 as name10'); // 循环连接 人事_基本资料 表(仅当 sczl_bh 不为空时才连接) for ($i = 1; $i <= 10; $i++) { $field = 'a.sczl_bh' . $i; $alias = 'd' . $i; $query->join("人事_基本资料 $alias", "$field = {$alias}.员工编号 AND {$field} IS NOT NULL", 'LEFT'); } $list = $query->where('a.sczl_gdbh',$param['gdbh']) ->where('a.sczl_yjno',$param['yjno']) ->where('a.sczl_num',$param['num']) ->order('工序号,生产日期') ->select(); $total = count($list); $data = [ 'data' => $list, 'total' => $total ]; if (empty($list)){ $this->success('未找到该流程单的工艺数据'); }else{ $this->success('成功',$data); } } /** * 制程检验记录 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ProcessInspection() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){ $this->error('参数错误'); } $list = db('制程检验_记录') ->field('工单编号,流程单号,工序名称,班组编号,类别,检验项目,检验结果,检验备注 as 项目备注,相关标准 as 修改标准') ->where('工单编号',$param['gdbh']) ->where('印件号',$param['yjno']) ->where('流程单号',$param['num']) ->where('类别', 'neq', '现场巡查记录') ->order('流程单号') ->select(); $total = count($list); $data = [ 'data' => $list, 'total' => $total ]; if (empty($list)){ $this->error('未找到制程检验记录'); } $this->success('成功',$data); } /** * 制程异常记录 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ProcessAnomaly() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (!isset($param['gdbh']) || !isset($param['yjno'])|| !isset($param['num'])){ $this->error('参数错误'); } $list = db('制程检验_记录附加') ->field('流程单号,缺陷备注') ->where('工单编号',$param['gdbh']) ->where('印件号',$param['yjno']) ->where('流程单号',$param['num']) ->order('流程单号') ->select(); if (empty($list)){ $this->error('未找到制程异常记录'); } foreach ($list as $key => $value){ $list[$key]['数量'] = ''; $list[$key]['用户'] = ''; } $total = count($list); $data = [ 'data' => $list, 'total' => $total ]; $this->success('成功',$data); } /** * 物料批次信息 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function MaterialsBatch() { // 1. 简化请求方法验证 if (!$this->request->isGet()) { $this->error('请求错误'); } // 2. 集中参数验证 $requiredParams = ['gdbh', 'yjno', 'num']; $param = $this->request->param(); $missingParams = []; foreach ($requiredParams as $requiredParam) { if (!isset($param[$requiredParam])) { $missingParams[] = $requiredParam; } } if (!empty($missingParams)) { $this->error('参数错误,缺少:' . implode(', ', $missingParams)); } // 3. 使用常量或配置定义静态数据 $department = [ 'Y200' => ['裁切','滚切','胶印','上光','切废'], 'Y201' => ['丝印','喷码','覆膜'], 'Y202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'], 'Y203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'], 'Y204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'], '200' => ['裁切','滚切','胶印','上光','切废'], '201' => ['丝印','喷码','覆膜'], '202' => ['卷凹','单凹','圆烫','圆切','复卷','分条','配卷','拼板','机检(圆切)','手检(圆切)','包装(圆切)'], '203' => ['烫金','烫金+凹凸(压纹)','模切','模切+凹凸(压纹)','凹凸(压纹)','拆片','覆膜','对裱'], '204' => ['手检','次品检','机检(初检)','二次机检','核检','抽检','包装'], ]; $validWarehouseCodes = ['101','Y101','102','Y102','103','Y103','200','201','202','203','204','Y200','Y201','Y202','Y203','Y204']; // 4. 优化数据库查询,使用链式操作提高可读性 $list = db('物料_收发记录') ->alias('a') ->join('物料_存货编码 b', 'a.st_wlbh = b.物料代码') ->field([ 'a.st_wlbh as 物料编号', 'a.供方批次', 'a.仓库编号', 'rtrim(b.物料名称) as 物料名称' ]) ->where('a.st_gdbh', $param['gdbh']) ->where('a.cpdh', $param['cpdh'] ?? '') // 使用空值合并运算符 ->whereNotNull('a.供方批次') ->group('a.st_wlbh') ->select(); if (empty($list)) { $this->error('未找到物料资料'); } // 5. 提前查询工序数据,避免在循环中重复处理 $process = db('设备_产量计酬') ->alias('a') ->join('工单_工艺资料 b', 'a.sczl_gdbh = b.Gy0_gdbh AND a.sczl_yjno = b.Gy0_yjno AND a.sczl_gxh = b.Gy0_gxh') ->where([ 'a.sczl_gdbh' => $param['gdbh'], 'a.sczl_yjno' => $param['yjno'] ]) ->field([ 'a.sczl_gxh as 工序号', 'b.Gy0_gxmc', 'a.sczl_gxmc as 工序名称' ]) ->group('a.sczl_gxh') ->select(); // 6. 重构数据处理逻辑 $data = []; $processMap = []; // 缓存工序数据 // 如果需要按工序名称快速查找,可以先建立映射 foreach ($process as $proc) { $processMap[$proc['Gy0_gxmc']] = $proc['工序名称']; } foreach ($list as $item) { $warehouseCode = $item['仓库编号']; $baseData = [ '流程单号' => $param['num'], '物料编号' => $item['物料编号'], '物料名称' => $item['物料名称'], '供方批次' => $item['供方批次'] ]; // 7. 优化条件判断逻辑 if (in_array($warehouseCode, $validWarehouseCodes, true)) { if ($warehouseCode === '101' || $warehouseCode === 'Y101' || $warehouseCode === '102' || $warehouseCode === 'Y102' || $warehouseCode === '103' || $warehouseCode === 'Y103') { if (!empty($process[0])) { $baseData['工序名称'] = $process[0]['工序名称']; } } else { // 其他仓库的处理逻辑 if (isset($department[$warehouseCode])) { $relatedProcesses = $department[$warehouseCode]; foreach ($relatedProcesses as $processName) { if (isset($processMap[$processName])) { $baseData['工序名称'] = $processMap[$processName]; } } } } } $data[] = $baseData; } $result = [ 'data' => $data, 'total' => count($data), ]; $this->success('成功',$result); } /** * 印版记录 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function PrintingPlateList() { // 1. 请求方法验证 if (!$this->request->isGet()) { $this->error('请求错误'); } // 2. 参数验证与提取 $param = $this->request->param(); $requiredParams = ['gdbh', 'yjno']; foreach ($requiredParams as $requiredParam) { if (empty($param[$requiredParam])) { $this->error("参数错误,缺少:{$requiredParam}"); } } $gdbh = $param['gdbh']; $yjno = $param['yjno']; // 3. 主查询 - 获取印版领用记录 $list = db('工单_印版领用记录') ->alias('a') ->join('物料_存货编码 b', 'a.Yb_存货编码 = b.物料代码') ->where([ 'a.Yb_工单编号' => $gdbh, 'a.Yb_印件号' => $yjno, 'a.Yb_领用机台' => ['<>', ''] ]) ->field([ 'a.Yb_存货编码 as 存货编码', 'a.Yb_供方批号 as 供方批号', 'a.Yb_领用机台 as 领用机台', 'TRIM(b.物料名称) as 物料名称', // 使用TRIM替代rtrim 'a.Sys_id as 用户' ]) ->group('a.Yb_存货编码, a.Yb_供方批号') // 明确指定表字段 ->order('a.Yb_存货编码') ->select(); if (empty($list)) { $this->error('未找到印版数据'); } // 4. 收集所有机台号,准备批量查询 $machineCodes = array_column($list, '领用机台'); $machineCodes = array_unique(array_filter($machineCodes)); // 去重并过滤空值 // 5. 批量查询产量计酬数据 - 解决N+1查询问题 $processMap = []; if (!empty($machineCodes)) { $processList = db('设备_产量计酬') ->where([ 'sczl_gdbh' => $gdbh, 'sczl_yjno' => $yjno, 'sczl_jtbh' => ['IN', $machineCodes] ]) ->field([ 'sczl_jtbh as 机台编号', 'sczl_gxmc as 工序名称', 'sczl_num as 流程单号' ]) ->order('sczl_num, sczl_jtbh') ->select(); // 按机台编号分组,建立映射 foreach ($processList as $process) { $machineCode = $process['机台编号']; if (!isset($processMap[$machineCode])) { $processMap[$machineCode] = []; } $processMap[$machineCode][] = [ '工序名称' => $process['工序名称'], '流程单号' => $process['流程单号'] ]; } } // 6. 数据组装 $data = []; foreach ($list as $item) { $machineCode = $item['领用机台']; $baseData = [ '存货编码' => $item['存货编码'], '供方批号' => $item['供方批号'], '领用机台' => $machineCode, '物料名称' => $item['物料名称'], '用户' => $item['用户'] ]; // 如果该机台有对应的工序数据 if (isset($processMap[$machineCode]) && !empty($processMap[$machineCode])) { foreach ($processMap[$machineCode] as $process) { $data[] = array_merge($baseData, $process); } } else { // 如果机台没有对应工序,至少返回基础信息 $data[] = array_merge($baseData, [ '工序名称' => '', '流程单号' => '' ]); } } // 7. 按流程单号排序(如果需要的话) usort($data, function($a, $b) { return strcmp($a['流程单号'] ?? '', $b['流程单号'] ?? ''); }); $result = [ 'data' => $data, 'total' => count($data), ]; $this->success('成功', $result); } }