PowerManagement.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // // 获取请求参数
  56. // $params = $this->request->param();
  57. //
  58. // // 检查必需参数
  59. // if (empty($params) || !isset($params['mouth'])) {
  60. // return $this->error('参数错误');
  61. // }
  62. //
  63. // // 转换为标准日期格式
  64. // $month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
  65. //
  66. // // 构建查询条件
  67. // $conditions = [];
  68. // if (!empty($params['workShop'])) {
  69. // $conditions['使用部门'] = $params['workShop'];
  70. // }
  71. //
  72. // // 获取机台列表
  73. // $machineList = db('设备_基本资料')
  74. // ->where($conditions)
  75. // ->whereNotNull('sys_sbID')
  76. // ->where('sys_sbID', '<>', '')
  77. // ->order('设备编组, 设备编号')
  78. // ->column('rtrim(设备名称)', '设备编号');
  79. //
  80. // // 准备数据容器
  81. // $data = [];
  82. //
  83. // // 查询电表数据
  84. // foreach ($machineList as $machineId => $machineName) {
  85. // $lastData = $this->getMeterData($machineId, date('Y-m', strtotime($month . ' -1 month')));
  86. // $newData = $this->getMeterData($machineId, $month);
  87. //
  88. // if (!empty($lastData) || !empty($newData)) {
  89. // $res = [
  90. // 'MachineCode' => $machineId,
  91. // 'MachineName' => $machineName,
  92. // 'lastMain' => $lastData['主电表'] ?? 0,
  93. // 'lastAuxiliary' => $lastData['辅电表'] ?? 0,
  94. // 'newMain' => $newData['主电表'] ?? 0,
  95. // 'newAuxiliary' => $newData['辅电表'] ?? 0
  96. // ];
  97. // $res['mainNumber'] = $res['newMain'] - $res['lastMain'];
  98. // $res['auxiliaryNumber'] = $res['newAuxiliary'] - $res['lastAuxiliary'];
  99. // $data[] = $res;
  100. // }
  101. // }
  102. // $this->success('成功',$data);
  103. // }
  104. public function MachineList()
  105. {
  106. // 判断请求方式是否为GET
  107. if (!$this->request->isGet()) {
  108. $this->error('请求错误');
  109. }
  110. // 获取请求参数
  111. $params = $this->request->param();
  112. // 检查必需参数
  113. if (empty($params) || !isset($params['mouth'])) {
  114. $this->error('参数错误');
  115. }
  116. // 转换为标准日期格式
  117. $month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
  118. // 构建查询条件
  119. $conditions = !empty($params['workShop']) ? ['使用部门' => $params['workShop']] : [];
  120. // 获取机台列表
  121. $machineList = db('设备_基本资料')
  122. ->where($conditions)
  123. ->whereNotNull('sys_sbID')
  124. ->where('sys_sbID', '<>', '')
  125. ->order('设备编组, 设备编号')
  126. ->column('rtrim(设备名称)', '设备编号');
  127. // 准备数据容器
  128. $data = [];
  129. $monthLast = date('Y-m', strtotime("{$month} -1 month"));
  130. // 查询电表数据并优化为一次性查询
  131. $machineIds = array_keys($machineList);
  132. $meterData = $this->getBatchMeterData($machineIds, [$monthLast, $month]);
  133. // 遍历机台列表
  134. foreach ($machineList as $machineId => $machineName) {
  135. $lastData = $meterData[$machineId][$monthLast] ?? [];
  136. $newData = $meterData[$machineId][$month] ?? [];
  137. if (!empty($lastData) || !empty($newData)) {
  138. $res = [
  139. 'MachineCode' => $machineId,
  140. 'MachineName' => $machineName,
  141. 'lastMain' => $lastData['主电表'] ?? 0,
  142. 'lastAuxiliary' => $lastData['辅电表'] ?? 0,
  143. 'newMain' => $newData['主电表'] ?? 0,
  144. 'newAuxiliary' => $newData['辅电表'] ?? 0,
  145. 'mainNumber' => ($newData['主电表'] ?? 0) - ($lastData['主电表'] ?? 0),
  146. 'auxiliaryNumber' => ($newData['辅电表'] ?? 0) - ($lastData['辅电表'] ?? 0),
  147. ];
  148. $data[] = $res;
  149. }
  150. }
  151. $this->success('成功', $data);
  152. }
  153. // 批量获取电表数据的方法
  154. private function getBatchMeterData(array $machineIds, array $months)
  155. {
  156. $results = [];
  157. // 根据机台ID和月份批量查询电表数据 (假设可用的电表数据模型)
  158. foreach ($months as $month) {
  159. $data = db('设备_产量计酬') // 替换为真实的电表数据表
  160. ->whereIn('sczl_jtbh', $machineIds)
  161. ->where('sczl_rq', '>=', $month . '-01')
  162. ->where('sczl_rq', '<=', $month . '-' . date('t', strtotime($month)))
  163. ->select();
  164. // 按设备编号和月份分组存储
  165. foreach ($data as $row) {
  166. $results[$row['sczl_jtbh']][$month] = [
  167. '主电表' => $row['主电表'] ?? 0,
  168. '辅电表' => $row['辅电表'] ?? 0,
  169. ];
  170. }
  171. }
  172. return $results;
  173. }
  174. /**
  175. * 获取电表数据的私有方法
  176. * @param $machineId
  177. * @param $date
  178. * @return array|bool|\PDOStatement|string|\think\Model|null
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. * @throws \think\exception\DbException
  182. */
  183. private function getMeterData($machineId, $date)
  184. {
  185. return db('设备_产量计酬')
  186. ->field('主电表, 辅电表')
  187. ->where('sczl_rq', 'like', "$date%")
  188. ->where('主电表', '<>', 0)
  189. ->where('辅电表', '<>', 0)
  190. ->where('sczl_jtbh', $machineId)
  191. ->order('UniqId desc')
  192. ->find();
  193. }
  194. /**
  195. * 机台电表数据详情
  196. * @return void
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. * @throws \think\exception\DbException
  200. */
  201. public function MachineDetail()
  202. {
  203. if ($this->request->isGet() === false){
  204. $this->error('请求错误');
  205. }
  206. $param = $this->request->param();
  207. if (!isset($param['mouth']) || !isset($param['machine'])){
  208. $this->error('参数错误');
  209. }
  210. // 转换为标准日期格式
  211. $month = substr($param['mouth'], 0, 4) . '-' . substr($param['mouth'], 4, 2);
  212. $where = [
  213. 'sczl_rq' => ['like',$month.'%'],
  214. 'sczl_jtbh' => $param['machine'],
  215. '主电表' => ['<>',0]
  216. ];
  217. $list = db('设备_产量计酬')
  218. ->field('sczl_jtbh as 机台编号,开工时间,主电表,辅电表')
  219. ->where($where)
  220. ->order('开工时间 desc')
  221. ->select();
  222. if (empty($list)){
  223. $this->error('为找打数据');
  224. }else{
  225. $this->success('成功',$list);
  226. }
  227. }
  228. }