|
|
@@ -58,11 +58,67 @@ class PowerManagement extends Api
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
* @throws \think\exception\DbException
|
|
|
*/
|
|
|
+// public function MachineList()
|
|
|
+// {
|
|
|
+// // 判断请求方式是否为GET
|
|
|
+// if (!$this->request->isGet()) {
|
|
|
+// return $this->error('请求错误');
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 获取请求参数
|
|
|
+// $params = $this->request->param();
|
|
|
+//
|
|
|
+// // 检查必需参数
|
|
|
+// if (empty($params) || !isset($params['mouth'])) {
|
|
|
+// return $this->error('参数错误');
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 转换为标准日期格式
|
|
|
+// $month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
|
|
|
+//
|
|
|
+// // 构建查询条件
|
|
|
+// $conditions = [];
|
|
|
+// if (!empty($params['workShop'])) {
|
|
|
+// $conditions['使用部门'] = $params['workShop'];
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 获取机台列表
|
|
|
+// $machineList = db('设备_基本资料')
|
|
|
+// ->where($conditions)
|
|
|
+// ->whereNotNull('sys_sbID')
|
|
|
+// ->where('sys_sbID', '<>', '')
|
|
|
+// ->order('设备编组, 设备编号')
|
|
|
+// ->column('rtrim(设备名称)', '设备编号');
|
|
|
+//
|
|
|
+// // 准备数据容器
|
|
|
+// $data = [];
|
|
|
+//
|
|
|
+// // 查询电表数据
|
|
|
+// foreach ($machineList as $machineId => $machineName) {
|
|
|
+// $lastData = $this->getMeterData($machineId, date('Y-m', strtotime($month . ' -1 month')));
|
|
|
+// $newData = $this->getMeterData($machineId, $month);
|
|
|
+//
|
|
|
+// if (!empty($lastData) || !empty($newData)) {
|
|
|
+// $res = [
|
|
|
+// 'MachineCode' => $machineId,
|
|
|
+// 'MachineName' => $machineName,
|
|
|
+// 'lastMain' => $lastData['主电表'] ?? 0,
|
|
|
+// 'lastAuxiliary' => $lastData['辅电表'] ?? 0,
|
|
|
+// 'newMain' => $newData['主电表'] ?? 0,
|
|
|
+// 'newAuxiliary' => $newData['辅电表'] ?? 0
|
|
|
+// ];
|
|
|
+// $res['mainNumber'] = $res['newMain'] - $res['lastMain'];
|
|
|
+// $res['auxiliaryNumber'] = $res['newAuxiliary'] - $res['lastAuxiliary'];
|
|
|
+// $data[] = $res;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// $this->success('成功',$data);
|
|
|
+// }
|
|
|
public function MachineList()
|
|
|
{
|
|
|
// 判断请求方式是否为GET
|
|
|
if (!$this->request->isGet()) {
|
|
|
- return $this->error('请求错误');
|
|
|
+ $this->error('请求错误');
|
|
|
}
|
|
|
|
|
|
// 获取请求参数
|
|
|
@@ -70,17 +126,14 @@ class PowerManagement extends Api
|
|
|
|
|
|
// 检查必需参数
|
|
|
if (empty($params) || !isset($params['mouth'])) {
|
|
|
- return $this->error('参数错误');
|
|
|
+ $this->error('参数错误');
|
|
|
}
|
|
|
|
|
|
// 转换为标准日期格式
|
|
|
$month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
|
|
|
|
|
|
// 构建查询条件
|
|
|
- $conditions = [];
|
|
|
- if (!empty($params['workShop'])) {
|
|
|
- $conditions['使用部门'] = $params['workShop'];
|
|
|
- }
|
|
|
+ $conditions = !empty($params['workShop']) ? ['使用部门' => $params['workShop']] : [];
|
|
|
|
|
|
// 获取机台列表
|
|
|
$machineList = db('设备_基本资料')
|
|
|
@@ -92,11 +145,16 @@ class PowerManagement extends Api
|
|
|
|
|
|
// 准备数据容器
|
|
|
$data = [];
|
|
|
+ $monthLast = date('Y-m', strtotime("{$month} -1 month"));
|
|
|
|
|
|
- // 查询电表数据
|
|
|
+ // 查询电表数据并优化为一次性查询
|
|
|
+ $machineIds = array_keys($machineList);
|
|
|
+ $meterData = $this->getBatchMeterData($machineIds, [$monthLast, $month]);
|
|
|
+
|
|
|
+ // 遍历机台列表
|
|
|
foreach ($machineList as $machineId => $machineName) {
|
|
|
- $lastData = $this->getMeterData($machineId, date('Y-m', strtotime($month . ' -1 month')));
|
|
|
- $newData = $this->getMeterData($machineId, $month);
|
|
|
+ $lastData = $meterData[$machineId][$monthLast] ?? [];
|
|
|
+ $newData = $meterData[$machineId][$month] ?? [];
|
|
|
|
|
|
if (!empty($lastData) || !empty($newData)) {
|
|
|
$res = [
|
|
|
@@ -105,16 +163,41 @@ class PowerManagement extends Api
|
|
|
'lastMain' => $lastData['主电表'] ?? 0,
|
|
|
'lastAuxiliary' => $lastData['辅电表'] ?? 0,
|
|
|
'newMain' => $newData['主电表'] ?? 0,
|
|
|
- 'newAuxiliary' => $newData['辅电表'] ?? 0
|
|
|
+ 'newAuxiliary' => $newData['辅电表'] ?? 0,
|
|
|
+ 'mainNumber' => ($newData['主电表'] ?? 0) - ($lastData['主电表'] ?? 0),
|
|
|
+ 'auxiliaryNumber' => ($newData['辅电表'] ?? 0) - ($lastData['辅电表'] ?? 0),
|
|
|
];
|
|
|
- $res['mainNumber'] = $res['newMain'] - $res['lastMain'];
|
|
|
- $res['auxiliaryNumber'] = $res['newAuxiliary'] - $res['lastAuxiliary'];
|
|
|
$data[] = $res;
|
|
|
}
|
|
|
}
|
|
|
- $this->success('成功',$data);
|
|
|
+
|
|
|
+ $this->success('成功', $data);
|
|
|
}
|
|
|
|
|
|
+// 批量获取电表数据的方法
|
|
|
+ private function getBatchMeterData(array $machineIds, array $months)
|
|
|
+ {
|
|
|
+ $results = [];
|
|
|
+
|
|
|
+ // 根据机台ID和月份批量查询电表数据 (假设可用的电表数据模型)
|
|
|
+ foreach ($months as $month) {
|
|
|
+ $data = db('设备_产量计酬') // 替换为真实的电表数据表
|
|
|
+ ->whereIn('sczl_jtbh', $machineIds)
|
|
|
+ ->where('sczl_rq', '>=', $month . '-01')
|
|
|
+ ->where('sczl_rq', '<=', $month . '-' . date('t', strtotime($month)))
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ // 按设备编号和月份分组存储
|
|
|
+ foreach ($data as $row) {
|
|
|
+ $results[$row['sczl_jtbh']][$month] = [
|
|
|
+ '主电表' => $row['主电表'] ?? 0,
|
|
|
+ '辅电表' => $row['辅电表'] ?? 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $results;
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取电表数据的私有方法
|
|
|
* @param $machineId
|