Station.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace catchAdmin\device\model;
  3. use catcher\base\CatchModel as Model;
  4. use catchAdmin\permissions\model\DataRangScopeTrait;
  5. use catchAdmin\device\model\get\StationGet;
  6. use think\facade\Db;
  7. class Station extends Model
  8. {
  9. use DataRangScopeTrait;
  10. use StationGet;
  11. // 表名
  12. public $name = 'stations';
  13. // 数据库字段映射
  14. public $field = array(
  15. 'id',
  16. 'mac',
  17. 'shortcode',
  18. 'name',
  19. 'model',
  20. 'serial_number',
  21. 'department_id',
  22. 'version',
  23. 'open_status',
  24. 'sim_no',
  25. 'longitude',
  26. 'latitude',
  27. 'online_time',
  28. 'address',
  29. 'remark',
  30. 'open_time',
  31. 'open_user_id',
  32. 'creator_id',
  33. 'created_at',
  34. 'updated_at',
  35. 'deleted_at'
  36. );
  37. /**
  38. * 获取基站列表
  39. */
  40. public function getList()
  41. {
  42. $res = $this->dataRange()
  43. ->catchSearch()
  44. ->append(['is_online'])
  45. ->order($this->aliasField('online_time'), 'desc')
  46. ->paginate()->each(function($item, $key) {
  47. $wgsLoc = \algorithm\Geometry::convertGcj02ToBd09((float)$item['latitude'],(float)$item['longitude']);
  48. $item['longitude']=$wgsLoc['lng'];
  49. $item['latitude']=$wgsLoc['lat'];
  50. });
  51. return $res;
  52. }
  53. public function getAllList(){
  54. $res = $this
  55. ->catchSearch()
  56. // ->field('mac,name,latitude,longitude,online_time')
  57. ->append(['is_online_text','is_online'])
  58. ->cache(120)
  59. ->select()
  60. ->each(function($item, $key) {
  61. $wgsLoc = \algorithm\Geometry::gcj02ToWgs84((float)$item['latitude'],(float)$item['longitude']);
  62. $item['longitude']=$wgsLoc['lng'];
  63. $item['latitude']=$wgsLoc['lat'];
  64. })->toArray();
  65. return $res;
  66. }
  67. /**
  68. * 根据名称搜索
  69. */
  70. public function searchNameAttr($query, $value, $data)
  71. {
  72. return $query->where('name', 'like', '%'.$value.'%');
  73. }
  74. /**
  75. * 根据编号搜索
  76. */
  77. public function searchShortCodeAttr($query, $value, $data)
  78. {
  79. return $query->where('shortcode|mac', 'like', '%'.$value.'%');
  80. }
  81. /**
  82. * 根据状态搜索
  83. */
  84. public function searchOnlineStateAttr($query, $value, $data)
  85. {
  86. $time=date('Y-m-d H:i:s',time()-300);
  87. if($value=='1'){
  88. return $query->where('online_time', '>=', $time);
  89. }elseif($value=='2'){
  90. return $query->where(function ($query) use( $time) {
  91. $query->whereOr('online_time','<', $time)->whereOr('online_time','=', null);
  92. });
  93. }
  94. }
  95. }