request->isGet() === false){ $this->error('请求错误'); } $year = date('Y',time()); $lastYear = date('Y', strtotime('-1 year')); //年度维修统计 $yearResult1 = $this->getYearSite($year); $yearResult2 = $this->getYearSite($lastYear); $data['1、年度维修统计'] = [ $year => $yearResult1, $lastYear => $yearResult2 ]; //月度维修记录 $lastMouth = date('Y-m', strtotime('-1 year')); $mouthReault = $this->getMouthSite($lastMouth); $data['2、月度维修记录'] = $mouthReault; //年度保养记录 $YearMaintenanceSist1 = $this->getYearMaintenanceSist($year); $YearMaintenanceSist2 = $this->getYearMaintenanceSist($lastYear); if (!empty($YearMaintenanceSist1)){ $data['3、年度保养统计'][$year] = $YearMaintenanceSist1; } if (!empty($YearMaintenanceSist2)){ $data['3、年度保养统计'][$lastYear] = $YearMaintenanceSist2; } //月度保养记录 $mouthMaintenanceSist = $this->getMouthMaintenanceSist($lastMouth); $data['4、月度保养记录'] = $mouthMaintenanceSist; $this->success('成功',$data); } private function getYearSite($year) { $sist = db('设备_维修记录') ->where('报障时间','like',$year.'%') ->group('使用部门') ->column('rtrim(使用部门)'); $data = []; if (!empty($sist)){ foreach ($sist as $item){ $count = db('设备_维修记录') ->where('使用部门',$item) ->where('报障时间','like',$year.'%') ->count(); if ($count !== 0){ $data[] = $item.'('.$count.'台次)'; } } } return $data; } private function getMouthSite($mouth) { $mouth = db('设备_维修记录') ->where('报障时间','>',$mouth.'-01 00:00:00') ->where('报障时间','<',date('Y-m-d H:i:s',time())) ->group('date') ->order('date desc') ->column('DATE_FORMAT(报障时间, "%Y-%m") AS date'); $data = []; foreach ($mouth as $item){ $sist = db('设备_维修记录') ->where('报障时间','like',$item.'%') ->group('使用部门') ->column('rtrim(使用部门)'); foreach ($sist as $value){ $count = db('设备_维修记录') ->where('使用部门',$value) ->where('报障时间','like',$item.'%') ->count(); $data[$item][] = $value.'('.$count.'台次)'; } } return $data; } private function getYearMaintenanceSist($year) { $sist = db('设备_保养记录') ->where('保养开始时间','like',$year.'%') ->group('使用部门') ->column('rtrim(使用部门)'); $data = []; if (!empty($sist)){ foreach ($sist as $item){ $count = db('设备_保养记录') ->where('使用部门',$item) ->where('保养开始时间','like',$year.'%') ->count(); if ($count !== 0){ $data[] = $item.'('.$count.'台次)'; } } } return $data; } private function getMouthMaintenanceSist($mouth) { $mouth = db('设备_保养记录') ->where('保养开始时间','>',$mouth.'-01 00:00:00') ->where('保养开始时间','<',date('Y-m-d H:i:s',time())) ->group('date') ->order('date desc') ->column('DATE_FORMAT(保养开始时间, "%Y-%m") AS date'); $data = []; foreach ($mouth as $item){ $sist = db('设备_保养记录') ->where('保养开始时间','like',$item.'%') ->group('使用部门') ->column('rtrim(使用部门)'); foreach ($sist as $value){ $count = db('设备_保养记录') ->where('使用部门',$value) ->where('保养开始时间','like',$item.'%') ->count(); $data[$item][] = $value.'('.$count.'台次)'; } } return $data; } /** * 年度维修记录上方列表 * @return Json|void */ public function YearMaintenanceList() { // 验证请求方法 if (!$this->request->isGet()) { $this->error('请求错误'); } // 获取并验证参数 $param = $this->request->param(); if (empty($param) || !isset($param['sist']) || !isset($param['year'])) { $this->error('参数错误'); } $department = $param['sist']; $year = $param['year']; try { // 获取该部门该年份的所有维修设备编号 $machineQuery = db('设备_维修记录') ->where('使用部门', $department) ->where('报障时间', 'like', $year . '%') ->order('设备编号'); $machines = $machineQuery->column('distinct(设备编号) as 设备编号'); if (empty($machines)) { $this->success('成功', []); } // 预加载设备名称 $machineNames = db('设备_基本资料') ->whereIn('设备编号', $machines) ->column('rtrim(设备名称) as 设备名称', '设备编号'); $result = []; // 一次性获取所有维修记录 $allRecords = db('设备_维修记录') ->where('使用部门', $department) ->where('报障时间', 'like', $year . '%') ->field([ '设备编号', 'DATE_FORMAT(报障时间, "%m") AS month', // 直接提取月份数字 'COUNT(*) AS count' ]) ->group('设备编号, DATE_FORMAT(报障时间, "%m")') ->select(); // 按设备编号组织数据 $recordsByMachine = []; foreach ($allRecords as $record) { $monthKey = $record['month']; // 直接使用月份数字作为键 $recordsByMachine[$record['设备编号']][$monthKey] = $record['count']; } // 构建结果数组 foreach ($machines as $machineId) { $monthData = []; $total = 0; if (isset($recordsByMachine[$machineId])) { foreach ($recordsByMachine[$machineId] as $month => $count) { $monthData[$month] = $count; // 保持月份键为字符串 $total += $count; } } // 确保月份键是两位数格式 $formattedMonthData = []; foreach ($monthData as $month => $count) { $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT); $formattedMonthData[$formattedMonth] = $count; } $result[] = [ '使用部门' => $department, '设备编号' => $machineId, '设备名称' => $machineNames[$machineId] ?? '未知设备', 'total' => $total ] + $formattedMonthData; } return json([ 'code' => 1, 'msg' => '成功', 'time' =>time(), 'data' => $result ]); } catch (\Exception $e) { $this->error('查询失败: ' . $e->getMessage()); } } /** * 设备维修记录配件详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function YearAccessoriesList() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['machine']) || !isset($param['year']) || !isset($param['sist'])) { $this->error('参数错误'); } $list = db('物料_收发记录') ->alias('a') ->join('物料_仓库信息 b', 'a.仓库编号 = b.编号','left') ->join('设备_基本资料 c', 'a.st_jtbh = c.设备编号','left') ->join('物料_存货编码 d', 'a.st_wlbh = d.物料代码') ->where('a.st_jtbh',$param['machine']) ->where('a.st_rq','like',$param['year'].'%') ->where(function ($query) { $query->where('a.仓库编号', '107') ->whereOr('a.仓库编号', 'Y107'); }) ->where('st_jylb','like','修理领用%') ->field(['DATE_FORMAT(a.st_rq, "%y-%m-%d") AS 日期','rtrim(a.st_jylb) as 交易类别','a.st_jtbh AS 机台编号','rtrim(a.仓库编号) as 仓库编号', 'rtrim(b.名称) as 仓库名称','rtrim(c.使用部门) as 领用部门','a.st_wlbh as 物料编号','rtrim(d.物料名称) as 物料名称','a.st_sl as 领用数量', 'rtrim(a.st_dw) as 领用单位','a.领用单价','a.st_sl*a.领用单价 as 金额','a.st_desc as 备注']) ->order('日期 DESC') ->select(); if (empty($list)) { $this->error('未找到数据'); }else{ $this->success('成功', $list); } } /** * 年度保养统计上方几台列表 * @return Json|void */ public function YearMaintainList() { // 验证请求方法 if (!$this->request->isGet()) { $this->error('请求错误'); } // 获取并验证参数 $param = $this->request->param(); if (empty($param) || !isset($param['sist']) || !isset($param['year'])) { $this->error('参数错误'); } $department = $param['sist']; $year = $param['year']; try { // 获取该部门该年份的所有维修设备编号 $machineQuery = db('设备_保养记录') ->where('使用部门', $department) ->where('保养开始时间', 'like', $year . '%') ->order('设备编号'); $machines = $machineQuery->column('distinct(设备编号) as 设备编号'); if (empty($machines)) { $this->success('成功', []); } // 预加载设备名称 $machineNames = db('设备_基本资料') ->whereIn('设备编号', $machines) ->column('rtrim(设备名称) as 设备名称', '设备编号'); $result = []; // 一次性获取所有维修记录 $allRecords = db('设备_保养记录') ->where('使用部门', $department) ->where('保养开始时间', 'like', $year . '%') ->field([ '设备编号', 'DATE_FORMAT(保养开始时间, "%m") AS month', // 直接提取月份数字 'COUNT(*) AS count' ]) ->group('设备编号, DATE_FORMAT(保养开始时间, "%m")') ->select(); // 按设备编号组织数据 $recordsByMachine = []; foreach ($allRecords as $record) { $monthKey = $record['month']; // 直接使用月份数字作为键 $recordsByMachine[$record['设备编号']][$monthKey] = $record['count']; } // 构建结果数组 foreach ($machines as $machineId) { $monthData = []; $total = 0; if (isset($recordsByMachine[$machineId])) { foreach ($recordsByMachine[$machineId] as $month => $count) { $monthData[$month] = $count; // 保持月份键为字符串 $total += $count; } } // 确保月份键是两位数格式 $formattedMonthData = []; foreach ($monthData as $month => $count) { $formattedMonth = str_pad($month, 2, '0', STR_PAD_LEFT); $formattedMonthData[$formattedMonth] = $count; } $result[] = [ '使用部门' => $department, '设备编号' => $machineId, '设备名称' => $machineNames[$machineId] ?? '未知设备', 'total' => $total ] + $formattedMonthData; } return json([ 'code' => 1, 'msg' => '成功', 'time' =>time(), 'data' => $result ]); } catch (\Exception $e) { $this->error('查询失败: ' . $e->getMessage()); } } //月度设备维修记录上方机台列表 // public function MonthAccessoriesList() // { // if ($this->request->isGet() === false) { // $this->error('请求错误'); // } // $param = $this->request->param(); // if (empty($param) || !isset($param['sist']) || !isset($param['mouth'])) { // $this->error('参数错误'); // } // $list = db('设备_维修记录') // ->alias('a') // ->join('_ b', 'a. = b.','left') // ->where('使用部门', $param['sist']) // ->where('保障时间', 'like', $param['mouth'] . '%') // ->field('设备编号,设备名称,报障时间,维修受理时间,修复时间,实际维修工时 as 故障维修工时,验收情况,'); // } }