| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\api\controller;
- use think\Request;
- use app\common\controller\Api;
- /**
- * 设备电量管理
- */
- class PowerManagement extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 设备电量消耗管理左侧菜单
- * @return void|null
- */
- public function getTab()
- {
- // 检查请求方法
- if (!$this->request->isGet()) {
- return $this->error('请求错误');
- }
- // 计算一年前的月份
- $newMonth = date('Y-m', strtotime('-1 year'));
- // 获取月份数据
- $months = db('设备_产量计酬')
- ->where('sczl_rq', '>', $newMonth . '-01 00:00:00')
- ->group('mouth')
- ->order('mouth desc')
- ->column('DATE_FORMAT(sczl_rq, "%Y%m") AS mouth');
- // 获取所有车间名称
- $workShops = db('设备_基本资料')
- ->whereNotNull('sys_sbID')
- ->where('sys_sbID', '<>', '')
- ->distinct('使用部门')
- ->column('使用部门');
- // 组织数据
- $data = array_fill_keys($months, $workShops);
- // 返回成功响应
- $this->success('成功', $data);
- }
- /**
- * 上方表格机台列表
- * @return void|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function MachineList()
- {
- // 判断请求方式是否为GET
- if (!$this->request->isGet()) {
- return $this->error('请求错误');
- }
- // 获取请求参数
- $params = $this->request->param();
- // 检查必需参数
- if (empty($params) || !isset($params['mouth'])) {
- return $this->error('参数错误');
- }
- // 转换为标准日期格式
- $month = substr($params['mouth'], 0, 4) . '-' . substr($params['mouth'], 4, 2);
- // 构建查询条件
- $conditions = [];
- if (!empty($params['workShop'])) {
- $conditions['使用部门'] = $params['workShop'];
- }
- // 获取机台列表
- $machineList = db('设备_基本资料')
- ->where($conditions)
- ->whereNotNull('sys_sbID')
- ->where('sys_sbID', '<>', '')
- ->order('设备编组, 设备编号')
- ->column('rtrim(设备名称)', '设备编号');
- // 准备数据容器
- $data = [];
- // 查询电表数据
- foreach ($machineList as $machineId => $machineName) {
- $lastData = $this->getMeterData($machineId, date('Y-m', strtotime($month . ' -1 month')));
- $newData = $this->getMeterData($machineId, $month);
- if (!empty($lastData) || !empty($newData)) {
- $res = [
- 'MachineCode' => $machineId,
- 'MachineName' => $machineName,
- 'lastMain' => $lastData['主电表'] ?? 0,
- 'lastAuxiliary' => $lastData['辅电表'] ?? 0,
- 'newMain' => $newData['主电表'] ?? 0,
- 'newAuxiliary' => $newData['辅电表'] ?? 0
- ];
- $res['mainNumber'] = $res['newMain'] - $res['lastMain'];
- $res['auxiliaryNumber'] = $res['newAuxiliary'] - $res['lastAuxiliary'];
- $data[] = $res;
- }
- }
- $this->success('成功',$data);
- }
- /**
- * 获取电表数据的私有方法
- * @param $machineId
- * @param $date
- * @return array|bool|\PDOStatement|string|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- private function getMeterData($machineId, $date)
- {
- return db('设备_产量计酬')
- ->field('主电表, 辅电表')
- ->where('sczl_rq', 'like', "$date%")
- ->where('主电表', '<>', 0)
- ->where('辅电表', '<>', 0)
- ->where('sczl_jtbh', $machineId)
- ->order('UniqId desc')
- ->find();
- }
- /**
- * 机台电表数据详情
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function MachineDetail()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (!isset($param['mouth']) || !isset($param['machine'])){
- $this->error('参数错误');
- }
- // 转换为标准日期格式
- $month = substr($param['mouth'], 0, 4) . '-' . substr($param['mouth'], 4, 2);
- $where = [
- 'sczl_rq' => ['like',$month.'%'],
- 'sczl_jtbh' => $param['machine'],
- '主电表' => ['<>',0]
- ];
- $list = db('设备_产量计酬')
- ->field('sczl_jtbh as 机台编号,开工时间,主电表,辅电表')
- ->where($where)
- ->order('开工时间 desc')
- ->select();
- if (empty($list)){
- $this->error('为找打数据');
- }else{
- $this->success('成功',$list);
- }
- }
- }
|