GatewayService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. $gateway_map = SystemlService::getIsOnlineStatusGateWay();
  50. $model = Gateway::where('del_time',0)
  51. ->select('*');
  52. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  53. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  54. if(! empty($data['ip'])) $model->where('ip', 'LIKE', '%'.$data['ip'].'%');
  55. if(isset($data['is_online'])){
  56. $search = [];
  57. foreach ($gateway_map as $gateway => $value){
  58. if($data['is_online'] && ! empty($value)){
  59. $search[] = $gateway;
  60. }elseif(! $data['is_online'] && empty($value)){
  61. $search[] = $gateway;
  62. }
  63. }
  64. $model->whereIn('id',$search);
  65. }
  66. $list = $this->limit($model,'',$data);
  67. $list = $this->fillData($list,$gateway_map);
  68. return [true,$list];
  69. }
  70. public function equipmentRule($data,$is_add = true){
  71. if(empty($data['title'])) return [false,'网关编码不能为空'];
  72. if(empty($data['code'])) return [false,'网关编码不能为空'];
  73. if(! isset($data['band'])) return [false,'网络频带不能为空'];
  74. if(empty($data['protocol'])) return [false,'协议不能为空'];
  75. if($is_add){
  76. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  77. ->where('del_time',0)
  78. ->exists();
  79. }else{
  80. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  81. $bool = Gateway::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  82. ->where('id','<>',$data['id'])
  83. ->where('del_time',0)
  84. ->exists();
  85. }
  86. if($bool) return [false,'网关编码不能重复'];
  87. return [true,''];
  88. }
  89. public function fillData($data,$online){
  90. if(empty($data['data'])) return $data;
  91. foreach ($data['data'] as $key => $value){
  92. $tmp_online = $online[$value['id']] ?? 0;
  93. $data['data'][$key]['is_online'] = $tmp_online;
  94. $data['data'][$key]['is_online_name'] = Gateway::$online[$tmp_online] ?? '';
  95. }
  96. return $data;
  97. }
  98. }