GatewayService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if(isset($data['is_online'])) $model->where('is_online', $data['is_online']);
  55. $list = $this->limit($model,'',$data);
  56. $list = $this->fillData($list);
  57. return [true,$list];
  58. }
  59. public function equipmentRule($data,$is_add = true){
  60. if(empty($data['title'])) return [false,'网关编码不能为空'];
  61. if(empty($data['code'])) return [false,'网关编码不能为空'];
  62. if(! isset($data['band'])) return [false,'网络频带不能为空'];
  63. if(empty($data['protocol'])) return [false,'协议不能为空'];
  64. if($is_add){
  65. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  66. ->where('del_time',0)
  67. ->exists();
  68. }else{
  69. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  70. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  71. ->where('id','<>',$data['id'])
  72. ->where('del_time',0)
  73. ->exists();
  74. }
  75. if($bool) return [false,'网关编码不能重复'];
  76. return [true,''];
  77. }
  78. public function fillData($data){
  79. if(empty($data['data'])) return $data;
  80. //获取仓是否在线离线状态
  81. $online = SystemlService::getIsOnlineStatusGateWay(array_column($data['data'],'id'));
  82. foreach ($data['data'] as $key => $value){
  83. $tmp_online = $online[$value['id']] ?? 0;
  84. $data['data'][$key]['is_online'] = $tmp_online;
  85. $data['data'][$key]['is_online_name'] = Gateway::$online[$tmp_online] ?? '';
  86. }
  87. return $data;
  88. }
  89. }