123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Service;
- use App\Model\Device;
- use App\Model\Gateway;
- use App\Model\Screen;
- use App\Model\ScreenGateway;
- class GatewayService extends Service
- {
- public function equipmentEdit($data){
- list($status,$msg) = $this->equipmentRule($data,false);
- if(!$status) return [$status,$msg];
- $model = Gateway::where('id',$data['id'])->first();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->remark = $data['remark'] ?? '';
- $model->band = $data['band'];
- $model->protocol = $data['protocol'] ?? '';
- $model->dns = $data['dns'] ?? '';
- $model->ip = $data['ip'] ?? '';
- $model->save();
- return [true,'保存成功!'];
- }
- public function equipmentAdd($data){
- list($status,$msg) = $this->equipmentRule($data);
- if(!$status) return [$status,$msg];
- $model = new Gateway();
- $model->code = $data['code'] ?? '';
- $model->title = $data['title'] ?? '';
- $model->remark = $data['remark'] ?? '';
- $model->band = $data['band'];
- $model->protocol = $data['protocol'] ?? '';
- $model->dns = $data['dns'] ?? '';
- $model->ip = $data['ip'] ?? '';
- $model->save();
- return [true,'保存成功!'];
- }
- public function equipmentDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $bool = Device::where('del_time',0)->whereIn('gateway_id',$data['id'])->exists();
- if($bool) return [false,'网关已经绑定设备'];
- $bool = ScreenGateway::where('del_time',0)->whereIn('gateway_id',$data['id'])->exists();
- if($bool) return [false,'网关已经绑定仓'];
- Gateway::whereIn('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- return [true,'删除成功'];
- }
- public function equipmentList($data){
- $model = Gateway::where('del_time',0)
- ->select('*');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['ip'])) $model->where('ip', 'LIKE', '%'.$data['ip'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true,$list];
- }
- public function equipmentRule($data,$is_add = true){
- if(empty($data['title'])) return [false,'网关编码不能为空'];
- if(empty($data['code'])) return [false,'网关编码不能为空'];
- if(! isset($data['band'])) return [false,'网络频带不能为空'];
- if(empty($data['protocol'])) return [false,'协议不能为空'];
- if($is_add){
- $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
- ->where('del_time',0)
- ->exists();
- }else{
- if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
- $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false,'网关编码不能重复'];
- return [true,''];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- //获取仓是否在线离线状态
- $online = SystemlService::getIsOnlineStatusGateWay(array_column($data['data'],'id'));
- foreach ($data['data'] as $key => $value){
- $tmp_online = $online[$value['id']] ?? 0;
- $data['data'][$key]['is_online'] = $tmp_online;
- $data['data'][$key]['is_online_name'] = Gateway::$online[$tmp_online] ?? '';
- }
- return $data;
- }
- }
|