PowerManagement.php 5.3 KB

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