post(); $data['org_id'] = cur_org_id(); $validate = new \app\common\validate\Device(); $result = $validate->check($data,[]); if(true !== $result){ $this->error = $validate->getError(); return false; } if(!$data['device_user']){ $this->error = '维修人员不能为空'; return false; } $data['imgs']=implode(',',$data['imgs']); //修改操作 if($data['id'] > 0){ Db::startTrans(); try { $data['update_time'] = getTime(); $addDevice=$this::update($data,['id'=>$data['id']]); if(!$addDevice){ exception('编辑失败'); } $newDeviceUser=explode(',',$data['device_user']); $deviceUser=$this->findUser($data['id']); $oldDeviceUser = []; foreach ($deviceUser as $k=>$v){ $oldDeviceUser[]=$v['user_id']; } $usernew = array_diff($newDeviceUser,$oldDeviceUser); $userdef = array_diff($oldDeviceUser,$newDeviceUser); if($userdef){ $delDeviceUser=Db::name('device_user')->where('device_id',$data['id'])->whereIn('user_id',$userdef)->delete(); if(!$delDeviceUser){ exception('编辑维修人员失败'); } } if($usernew){ $buser = array(); foreach ($usernew as $k=>$v){ $buser[] = array( 'device_id' => $data['id'], 'user_id' => $v ); } $saveDeviceUser=Db::name('device_user')->insertAll($buser); if(!$saveDeviceUser){ exception('编辑维修人员失败'); } } Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 $this->error = $e->getMessage(); Db::rollback(); return false; } }else{ //添加操作 Db::startTrans(); try { unset($data['id']); $data['create_time'] = getTime(); $addres=$this->save($data); if(!$addres){ exception('操作失败'); } $addDeviceID = $this->id; $deviceUser=explode(',',$data['device_user']); foreach ($deviceUser as $k=>$v){ $data=[ 'user_id'=>$v, 'device_id'=>$addDeviceID, ]; $addDeviceUser=Db::name('device_user')->insert($data); if(!$addDeviceUser){ exception('操作失败'); } } Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 $this->error = $e->getMessage(); Db::rollback(); return false; } } } //获取维护用户 public function findUser($deviceId){ $ret=Db::name('device_user')->where('device_id',$deviceId)->select(); return $ret; } public function sweepCode($code,$userId,$orgId,$taskId=0,$deviceId=0) { $codeArr = get_qrcode_arr($code); if(!$codeArr){ $this->error = '设备不存在'; return false; } if($codeArr['type'] != 'device'){ $this->error = '设备不存在'; return false; } if($codeArr['ucode'] != config('app.ucode')){ $this->error = '设备不存在'; return false; } if($taskId > 0 ){ if($codeArr['id'] != $deviceId){ $this->error = '设备不存在'; return false; } $task = Db::name('device_task') ->where('id',$taskId) ->where('del',0) ->where('org_id',$orgId) ->find(); if(!$task){ $this->error = '任务不存在'; return false; } if($task['end_time'] < date('Y-m-d H:i:s') || $task['start_time'] > date('Y-m-d H:i:s')){ $this->error = '不在时间范围内'; return false; } // $taskAddr = Db::name('device_task_addr') // ->where('task_id',$task['id']) // ->column('device_id'); // $taskAddr = $taskAddr?$taskAddr:[]; // if(!in_array($codeArr['id'],$taskAddr)){ // $this->error = '地点不正确'; // return false; // } $workerAuth =1; }else{ $device =Db::name('device_user') ->where('device_id',$codeArr['id']) ->where('user_id',$userId) ->find(); $workerAuth = $device?1:0; } $map[] = ['org_id','=',$orgId]; $map[] = ['id','=',$codeArr['id']]; $map[] = ['enable','=',1]; $map[] = ['del','=',0]; $device = Db::name('device') ->field('del,enable',true) ->where($map) ->find(); if(!$device){ $this->error = '设备不存在'; return false; } $lookAllAuth = (new Roles())->getAppAuth($userId,AppAuth::DEVICE_LOOK_ALL_AUTH); $ret['device_id'] = $codeArr['id']; $ret['worker_auth'] = $workerAuth; $ret['look_all_auth'] = $lookAllAuth?1:0; return $ret; } public function deviceInfo($id,$orgId){ $map[] = ['org_id','=',$orgId]; $map[] = ['id','=',$id]; $map[] = ['enable','=',1]; $map[] = ['del','=',0]; $device = Db::name('device') ->field('del,enable',true) ->where($map) ->find(); if(!$device){ $this->error = '设备不存在'; return false; } $formId=empty($device['device_form']) ? [] : explode(',',$device['device_form']); $deviceForm = Db::name('device_form') ->field('del,enable',true) ->where('enable',1) ->where('del',0) ->whereIn('id',$formId) ->select(); $device['device_form'] = $deviceForm; return $device; } /** * 设备台账工作量统计 * * @author wst * @date 2021/4/13 8:38 */ public function DeviceWork($orgId){ $list = $this ->field('id,title') ->where('enable',1) ->where('del',0) ->where('org_id',$orgId) ->select(); $list = $list?$list:array(); $count = $count1 = $count2 =$count3 = 0; foreach ($list as $k=>$v){ $query = Db::name('device_record') ->where('org_id',$orgId) ->where('device_id',$v['id']); $list[$k]['count'] = $query ->count(); $count += $list[$k]['count']; //本月 $list[$k]['count1'] = $query ->where('create_yyyymm',date('Ym')) ->count(); $count1 += $list[$k]['count1']; //本周 $list[$k]['count2'] = $query ->whereIn('create_yyyymmdd',get_week_date()) ->count(); $count2 += $list[$k]['count2']; //今天 $list[$k]['count3'] = $query ->where('create_yyyymmdd',date('Ymd')) ->count(); $count3 += $list[$k]['count3']; } return array('list'=>$list,'count'=>$count,'count1'=>$count1,'count2'=>$count2,'count3'=>$count3); } /** * 根据设备id获取维修人员列表 * * @author wst * @date 2021/9/6 11:27 * @return array */ public function getDeviceUserList($id){ $list = Db::name('device_user') ->where('device_id',$id) ->select(); $user = []; foreach ($list as $k=>$v){ $user[]=(string)$v['user_id']; } return $user; } /** * 根据设备id获取维修人员姓名 * * @author wst * @date 2021/9/6 11:27 * @return array */ public function getByIdUserName($id){ $user_name=Db::name('device_user') ->alias('du') ->join('user u','u.id=du.user_id') ->field('u.real_name') ->where('du.device_id',$id) ->select(); $name=[]; foreach ($user_name as $kk=>$vv){ $name[$kk]=$vv['real_name']; } return $name; } public function taskAddrAll($taskId){ $info = Db::name('device_task') ->field('id,start_time,end_time,title,status,create_time') ->where('id',$taskId) ->where('del',0) ->find(); $info['users'] = Db::name('user') ->alias('u') ->field('u.real_name') ->join('device_task_user dtu','dtu.user_id=u.id') ->where('dtu.task_id',$info['id']) ->where('u.del',0) ->where('u.enable',1) ->select(); $info['device'] = Db::name('device') ->alias('d') ->field('d.id,d.title') ->join('device_task_addr dta','dta.device_id=d.id') ->where('dta.task_id',$info['id']) ->where('d.del',0) ->where('d.enable',1) ->select(); foreach ($info['device'] as $k=>$v){ $record = Db::name('device_record') ->where('task_id',$taskId) ->where('device_id',$v['id']) ->find(); $info['device'][$k]['status'] = $record ? 1 : 0; $info['device'][$k]['device_record_id'] = $record ? $record['id'] : 0; } return $info; } public function myTaskRecord($type,$page,$size,$userId,$orgId,$status){ // $etime = date('Y-m-d H:i:s',time() + 12*60*60); $record = []; if($type == 1){ $record = Db::name('device_task_user') ->alias('a') ->join('device_task b','a.task_id = b.id') ->join('task t','t.bus_id = b.id') ->where('b.del',0) ->where('t.type',4) // ->where('b.start_time','<',$etime) ->where('a.user_id',$userId) ->where('b.org_id',$orgId) ->where('b.status','in',[0,1]) ->field('b.id,b.title,b.start_time,b.end_time,b.status') ->order('b.start_time desc,b.id desc') ->distinct(true) ->page($page,$size) ->select(); }else if($type == 2){ if($status != ''){ $map[] = ['b.status','=',$status]; }else{ $map[] = ['b.status','in',[2,3]]; } $record = Db::name('device_task_user') ->alias('a') ->join('device_task b','a.task_id = b.id') ->where('b.del',0) // ->where('b.start_time','<',$etime) ->where('a.user_id',$userId) ->where('b.org_id',$orgId) ->where($map) ->field('b.id,b.title,b.start_time,b.end_time,b.status') ->order('b.status desc,b.start_time desc,b.id desc') ->page($page,$size) ->distinct(true) ->select(); } foreach ($record as $k=>$v){ $info = Db::name('device_task_delay') ->where('task_id',$v['id']) ->where('status',1) ->field('end_time') ->order('id desc') ->find(); if($info){ $record[$k]['end_time'] = $info['end_time']; } } return $record?$record:[]; } public function taskCount($userId,$orgId){ $record = Db::name('device_task_user') ->alias('a') ->join('device_task b','a.task_id = b.id') ->join('task t','t.bus_id = b.id') ->where('b.del',0) ->where('t.type',4) // ->where('b.start_time','<',$etime) ->where('a.user_id',$userId) ->where('b.org_id',$orgId) ->where('b.status','in',[0,1]) ->field('b.id,b.title,b.start_time,b.end_time,b.status') ->order('b.start_time desc,b.id desc') ->distinct(true) ->count(); return $record?$record:0; } }