GatewayService.php 3.1 KB

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