123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace app\common\model;
- use app\common\util\AppAuth;
- use think\Db;
- class Device extends Base {
- public function updates(){
- $data = request()->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;
- }
- }
|