GatewayService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Device;
  4. use App\Model\Gateway;
  5. use App\Model\Screen;
  6. use App\Model\ScreenGateway;
  7. class GatewayService extends Service
  8. {
  9. public function equipmentEdit($data){
  10. list($status,$msg) = $this->equipmentRule($data,false);
  11. if(!$status) return [$status,$msg];
  12. $model = Gateway::where('id',$data['id'])->first();
  13. $model->code = $data['code'] ?? '';
  14. $model->title = $data['title'] ?? '';
  15. $model->remark = $data['remark'] ?? '';
  16. $model->band = $data['band'];
  17. $model->protocol = $data['protocol'] ?? '';
  18. $model->dns = $data['dns'] ?? '';
  19. $model->ip = $data['ip'] ?? '';
  20. $model->save();
  21. return [true,'保存成功!'];
  22. }
  23. public function equipmentAdd($data){
  24. list($status,$msg) = $this->equipmentRule($data);
  25. if(!$status) return [$status,$msg];
  26. $model = new Gateway();
  27. $model->code = $data['code'] ?? '';
  28. $model->title = $data['title'] ?? '';
  29. $model->remark = $data['remark'] ?? '';
  30. $model->band = $data['band'];
  31. $model->protocol = $data['protocol'] ?? '';
  32. $model->dns = $data['dns'] ?? '';
  33. $model->ip = $data['ip'] ?? '';
  34. $model->save();
  35. return [true,'保存成功!'];
  36. }
  37. public function equipmentDel($data){
  38. if($this->isEmpty($data,'id')) return [false,'ID必须!'];
  39. $bool = Device::where('del_time',0)->whereIn('gateway_id',$data['id'])->exists();
  40. if($bool) return [false,'网关已经绑定设备'];
  41. $bool = ScreenGateway::where('del_time',0)->whereIn('gateway_id',$data['id'])->exists();
  42. if($bool) return [false,'网关已经绑定仓'];
  43. Gateway::whereIn('id',$data['id'])->update([
  44. 'del_time' => time()
  45. ]);
  46. return [true,'删除成功'];
  47. }
  48. public function equipmentList($data){
  49. $model = Gateway::where('del_time',0)
  50. ->select('*');
  51. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  52. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  53. if(! empty($data['ip'])) $model->where('ip', 'LIKE', '%'.$data['ip'].'%');
  54. $list = $this->limit($model,'',$data);
  55. $list = $this->fillData($list);
  56. return [true,$list];
  57. }
  58. public function equipmentRule($data,$is_add = true){
  59. if(empty($data['title'])) return [false,'网关编码不能为空'];
  60. if(empty($data['code'])) return [false,'网关编码不能为空'];
  61. if(! isset($data['band'])) return [false,'网络频带不能为空'];
  62. if(empty($data['protocol'])) return [false,'协议不能为空'];
  63. if($is_add){
  64. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  65. ->where('del_time',0)
  66. ->exists();
  67. }else{
  68. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  69. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  70. ->where('id','<>',$data['id'])
  71. ->where('del_time',0)
  72. ->exists();
  73. }
  74. if($bool) return [false,'网关编码不能重复'];
  75. return [true,''];
  76. }
  77. public function fillData($data){
  78. if(empty($data['data'])) return $data;
  79. //获取仓是否在线离线状态
  80. $online = SystemlService::getIsOnlineStatusGateWay(array_column($data['data'],'id'));
  81. foreach ($data['data'] as $key => $value){
  82. $tmp_online = $online[$value['id']] ?? 0;
  83. $data['data'][$key]['is_online'] = $tmp_online;
  84. $data['data'][$key]['is_online_name'] = Gateway::$online[$tmp_online] ?? '';
  85. }
  86. return $data;
  87. }
  88. }