PowerManagement.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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(function($query) use ($month) {
  108. $startDate = $month . '-01';
  109. $endDate = date('Y-m-d', strtotime($startDate . ' +1 month'));
  110. $query->where('sczl_rq', '>=', $startDate)
  111. ->where('sczl_rq', '<', $endDate);
  112. })
  113. ->select();
  114. // 按设备编号和月份分组存储
  115. foreach ($data as $row) {
  116. $results[$row['sczl_jtbh']][$month] = [
  117. '主电表' => $row['主电表'] ?? 0,
  118. '辅电表' => $row['辅电表'] ?? 0,
  119. ];
  120. }
  121. }
  122. return $results;
  123. }
  124. /**
  125. * 获取电表数据的私有方法
  126. * @param $machineId
  127. * @param $date
  128. * @return array|bool|\PDOStatement|string|\think\Model|null
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @throws \think\exception\DbException
  132. */
  133. private function getMeterData($machineId, $date)
  134. {
  135. return db('设备_产量计酬')
  136. ->field('主电表, 辅电表')
  137. ->where('sczl_rq', 'like', "$date%")
  138. ->where('主电表', '<>', 0)
  139. ->where('辅电表', '<>', 0)
  140. ->where('sczl_jtbh', $machineId)
  141. ->order('UniqId desc')
  142. ->find();
  143. }
  144. /**
  145. * 机台电表数据详情
  146. * @return void
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. */
  151. public function MachineDetail()
  152. {
  153. if ($this->request->isGet() === false){
  154. $this->error('请求错误');
  155. }
  156. $param = $this->request->param();
  157. if (!isset($param['mouth']) || !isset($param['machine'])){
  158. $this->error('参数错误');
  159. }
  160. // 转换为标准日期格式
  161. $month = substr($param['mouth'], 0, 4) . '-' . substr($param['mouth'], 4, 2);
  162. $where = [
  163. 'sczl_rq' => ['like',$month.'%'],
  164. 'sczl_jtbh' => $param['machine'],
  165. '主电表' => ['<>',0]
  166. ];
  167. $list = db('设备_产量计酬')
  168. ->field('sczl_jtbh as 机台编号,开工时间,主电表,辅电表')
  169. ->where($where)
  170. ->order('sczl_rq desc')
  171. ->select();
  172. if (empty($list)){
  173. $this->error('未找到数据');
  174. }else{
  175. $this->success('成功',$list);
  176. }
  177. }
  178. }