PowerManagement.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\api\controller;
  3. use think\Request;
  4. use app\common\controller\Api;
  5. /**
  6. * 设备电量管理
  7. */
  8. class PowerManagement extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 设备电量消耗管理左侧菜单
  14. * @return void|null
  15. */
  16. public function getTab()
  17. {
  18. // 检查请求方法
  19. if (!$this->request->isGet()) {
  20. return $this->error('请求错误');
  21. }
  22. // 计算一年前的月份
  23. $newMonth = date('Y-m', strtotime('-1 year'));
  24. // 获取月份数据
  25. $months = db('设备_产量计酬')
  26. ->where('开工时间', '>', $newMonth . '-01 00:00:00')
  27. ->group('mouth')
  28. ->order('mouth desc')
  29. ->column('DATE_FORMAT(开工时间, "%Y%m") AS mouth');
  30. // 获取所有车间名称
  31. $workShops = db('设备_基本资料')
  32. ->whereNotNull('sys_sbID')
  33. ->where('sys_sbID', '<>', '')
  34. ->where('使用部门','<>','检验车间')
  35. ->distinct('使用部门')
  36. ->column('使用部门');
  37. // 组织数据
  38. $data = array_fill_keys($months, $workShops);
  39. // 返回成功响应
  40. $this->success('成功', $data);
  41. }
  42. /**
  43. * 上方表格机台列表
  44. * @return void|null
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function MachineList()
  50. {
  51. // 判断请求方式是否为GET
  52. if (!$this->request->isGet()) {
  53. $this->error('请求错误');
  54. }
  55. // 获取请求参数
  56. $params = $this->request->param();
  57. // 检查必需参数
  58. if (empty($params) || !isset($params['mouth'])) {
  59. $this->error('参数错误');
  60. }
  61. // 转换为标准日期格式
  62. $month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
  63. // 构建查询条件
  64. $conditions = !empty($params['workShop']) ? ['使用部门' => $params['workShop']] : [];
  65. // 获取机台列表
  66. $machineList = db('设备_基本资料')
  67. ->where($conditions)
  68. ->whereNotLike('设备编号', 'YQZ%')
  69. ->whereNotNull('sys_sbID')
  70. ->where('sys_sbID', '<>', '')
  71. ->order('设备编组, 设备编号')
  72. ->column('rtrim(设备名称)', '设备编号');
  73. // 准备数据容器
  74. $data = [];
  75. $monthLast = date('Y-m', strtotime("{$month} -1 month"));
  76. // 查询电表数据并优化为一次性查询
  77. $machineIds = array_keys($machineList);
  78. $meterData = $this->getBatchMeterData($machineIds, [$monthLast, $month]);
  79. // 遍历机台列表
  80. foreach ($machineList as $machineId => $machineName) {
  81. $lastData = $meterData[$machineId][$monthLast] ?? [];
  82. $newData = $meterData[$machineId][$month] ?? [];
  83. if (!empty($lastData) || !empty($newData)) {
  84. $res = [
  85. 'MachineCode' => $machineId,
  86. 'MachineName' => $machineName,
  87. 'lastMain' => $lastData['主电表'] ?? 0,
  88. 'lastAuxiliary' => $lastData['辅电表'] ?? 0,
  89. 'newMain' => $newData['主电表'] ?? 0,
  90. 'newAuxiliary' => $newData['辅电表'] ?? 0,
  91. 'mainNumber' => ($newData['主电表'] ?? 0) - ($lastData['主电表'] ?? 0),
  92. 'auxiliaryNumber' => ($newData['辅电表'] ?? 0) - ($lastData['辅电表'] ?? 0),
  93. ];
  94. $data[] = $res;
  95. }
  96. }
  97. $this->success('成功', $data);
  98. }
  99. // 批量获取电表数据的方法
  100. private function getBatchMeterData(array $machineIds, array $months)
  101. {
  102. $results = [];
  103. // 根据机台ID和月份批量查询电表数据 (假设可用的电表数据模型)
  104. foreach ($months as $month) {
  105. $data = db('设备_产量计酬') // 替换为真实的电表数据表
  106. ->whereIn('sczl_jtbh', $machineIds)
  107. ->where('开工时间', '>=', $month . '-01')
  108. ->where('开工时间', '<=', $month . '-' . date('t', strtotime($month)))
  109. ->select();
  110. // 按设备编号和月份分组存储
  111. foreach ($data as $row) {
  112. $results[$row['sczl_jtbh']][$month] = [
  113. '主电表' => $row['主电表'] ?? 0,
  114. '辅电表' => $row['辅电表'] ?? 0,
  115. ];
  116. }
  117. }
  118. return $results;
  119. }
  120. /**
  121. * 获取电表数据的私有方法
  122. * @param $machineId
  123. * @param $date
  124. * @return array|bool|\PDOStatement|string|\think\Model|null
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. * @throws \think\exception\DbException
  128. */
  129. private function getMeterData($machineId, $date)
  130. {
  131. return db('设备_产量计酬')
  132. ->field('主电表, 辅电表')
  133. ->where('开工时间', 'like', "$date%")
  134. ->where('主电表', '<>', 0)
  135. ->where('辅电表', '<>', 0)
  136. ->where('sczl_jtbh', $machineId)
  137. ->order('UniqId desc')
  138. ->find();
  139. }
  140. /**
  141. * 机台电表数据详情
  142. * @return void
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public function MachineDetail()
  148. {
  149. if ($this->request->isGet() === false){
  150. $this->error('请求错误');
  151. }
  152. $param = $this->request->param();
  153. if (!isset($param['mouth']) || !isset($param['machine'])){
  154. $this->error('参数错误');
  155. }
  156. // 转换为标准日期格式
  157. $month = substr($param['mouth'], 0, 4) . '-' . substr($param['mouth'], 4, 2);
  158. $where = [
  159. '开工时间' => ['like',$month.'%'],
  160. 'sczl_jtbh' => $param['machine'],
  161. '主电表' => ['<>',0]
  162. ];
  163. $list = db('设备_产量计酬')
  164. ->field('sczl_jtbh as 机台编号,开工时间,主电表,辅电表')
  165. ->where($where)
  166. ->order('开工时间 desc')
  167. ->select();
  168. if (empty($list)){
  169. $this->error('未找到数据');
  170. }else{
  171. $this->success('成功',$list);
  172. }
  173. }
  174. }