PowerManagement.php 5.2 KB

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