request->isGet()) { $this->error('请求错误'); } // 3. 统一查询条件,避免重复代码 $baseQuery = function() { return \db('设备_基本资料') ->where('设备编组', '<>', '') ->where('sys_sbID', '<>', ''); }; $data = []; // 4. 获取部门列表(添加性能优化) $department = $baseQuery() ->distinct(true) ->where('使用部门', '<>', self::EXCLUDED_DEPARTMENT) ->order('设备编组') ->column('使用部门'); if (empty($department)) { $this->success('未获取到机台数据', []); } // 5. 批量查询优化,减少数据库查询次数 foreach ($department as $dept) { $trimmedDept = rtrim($dept); // 获取该部门下的所有设备编组 $benchClasses = $baseQuery() ->where('使用部门', $dept) ->distinct(true) ->order('设备编组') ->column('设备编组'); foreach ($benchClasses as $benchClass) { $trimmedClass = rtrim($benchClass); if ($trimmedClass === '') { continue; // 跳过空编组 } // 获取该编组下的所有设备 $machines = $baseQuery() ->where('使用部门', $dept) ->where('设备编组', $benchClass) ->field([ 'rtrim(设备编号) as 设备编号', 'rtrim(设备名称) as 设备名称', '设备编号 as original_id', // 保留原始ID便于调试 '设备名称 as original_name' ]) ->order('设备编号') ->select(); // 6. 使用数组映射简化数据处理 $formattedMachines = array_map(function($machine) { return $machine['设备编号'] . '-->' . $machine['设备名称']; }, $machines); if (!empty($formattedMachines)) { $data[$trimmedDept][$trimmedClass] = $formattedMachines; } } } $this->success('成功', $data); } /** * 获取组件清单 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getEquipmentList() { if (!$this->request->isGet()) { $this->error('请求错误'); } $equipmentList = db('设备_组件清单') ->field('序号,部件编号,部件名称,Uniqid') ->select(); if (empty($equipmentList)) { $this->success('未找到组件数据'); } foreach ($equipmentList as $key => $equipment) { $equipmentList[$key]['名称'] = $equipment['部件编号'] . '-->' . $equipment['部件名称']; } $this->success('成功', $equipmentList); } /** * 创建机台组件配置 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function CreateEquipmentInfo() { if (!$this->request->isPost()) { $this->error('请求错误'); } $data = Request::instance()->post(); if (empty($data['ids']) || empty($data['jtbh'])) { $this->error('参数错误'); } $ids = explode(',',$data['ids']); $equipmentList = db('设备_组件清单') ->field('序号,部件编号,部件名称') ->whereIn('Uniqid',$ids) ->select(); db('设备_组件配置清单')->where('设备编号',$data['jtbh'])->delete(); $machineEquipmentList = []; foreach ($equipmentList as $equipment) { $machineEquipmentList[] = [ '设备编号' => $data['jtbh'], '部件编号' => $equipment['部件编号'], '部件名称' => $equipment['部件名称'], 'Sys_id' => $data['sys_id'], 'Sys_rq' => date('Y-m-d H:i:s', time()), ]; } $sql = db('设备_组件配置清单')->fetchSql(true)->insertAll($machineEquipmentList); $res = \db()->query($sql); if ($res === false){ $this->error('创建失败'); }else{ $this->success('创建成功'); } } /** * 设备部件寿命跟踪上方列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getMachineEquipmentList() { if (!$this->request->isGet()) { $this->error('请求错误'); } $params = $this->request->param(); if (empty($params['machine'])) { $this->error('参数错误'); } // 优化1:使用链式查询,添加设备编号过滤条件 $list = db('设备_组件配置清单') ->alias('a') ->join('设备_组件清单 b', 'a.部件编号 = b.部件编号') ->where('a.设备编号', $params['machine']) // 添加设备编号过滤 ->field('a.设备编号,a.部件编号,a.部件名称,a.组件编号,a.小时寿命,a.阈值,a.更换日期,a.Uniqid,b.序号') ->order('b.序号') ->select(); if (empty($list)) { $this->error('未找到机台部件配置,请先配置部件...'); } // 优化2:提取有更换日期的记录进行批量查询 $hasReplacement = []; foreach ($list as $key => $equipment) { $list[$key]['运行工时'] = 0; // 收集需要查询运行工时的设备信息 if (!empty($equipment['更换日期'])) { $hasReplacement[$key] = [ 'device' => $equipment['设备编号'], 'date' => $equipment['更换日期'] ]; } } // 优化3:批量查询运行工时数据 if (!empty($hasReplacement)) { // 按设备编号分组,避免重复查询 $deviceRunTimes = []; foreach ($hasReplacement as $item) { $device = $item['device']; $date = $item['date']; // 如果该设备还未查询过,则进行查询 if (!isset($deviceRunTimes[$device])) { $deviceRunTimes[$device] = db('设备_产量计酬') ->where('sczl_jtbh', $device) ->where('sczl_rq', '>=', $date) ->field('SUM(sczl_设备运行工时) as total_hours') ->find(); } // 为每个记录设置运行工时 $key = array_search($item, $hasReplacement); $list[$key]['运行工时'] = $deviceRunTimes[$device]['total_hours']; } } $this->success('成功', $list); } /** * 设备部件寿命下方列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getEquipmentDetailList() { if (!$this->request->isGet()) { $this->error('请求错误'); } $params = $this->request->param(); if (empty($params['machine']) || empty($params['date'])) { $this->error('参数错误'); } $list = db('设备_产量计酬') ->alias('a') ->join('工单_印件资料 b', 'a.sczl_gdbh = b.Yj_Gdbh and a.sczl_yjno = b.yj_Yjno') ->field('a.sczl_gdbh as 工单编号,a.sczl_yjno as 印件号,b.yj_yjmc as 印件名称,a.sczl_rq as 日期,SUM(a.sczl_设备运行工时) as 工时,a.sczl_jtbh as 机台编号') ->where([ 'a.sczl_jtbh' => $params['machine'], 'a.sczl_rq' => ['>=', $params['date']], ]) ->group('a.sczl_gdbh, a.sczl_yjno') ->select(); if (empty($list)) { $this->error('未找到详情数据'); }else{ $this->success('成功', $list); } } /** * 部件数据修改 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function getEquipmentUpdate() { if (!$this->request->isPost()) { $this->error('请求错误'); } $params = Request::instance()->post(); if(empty($params['id'])) { $this->error('参数错误'); } $data = [ '组件编号' => $params['code'], '小时寿命' => $params['hour'], '更换日期' => $params['date'], '阈值' => $params['number'] ]; $rql = db('设备_组件配置清单') ->where('Uniqid', $params['id']) ->fetchSql(true) ->update($data); $res = \db()->query($rql); if ($res === false){ $this->error('修改失败'); }else{ $this->success('修改成功'); } } }