Station.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ->select()
  59. ->each(function($item, $key) {
  60. $wgsLoc = \algorithm\Geometry::gcj02ToWgs84((float)$item['latitude'],(float)$item['longitude']);
  61. $item['longitude']=$wgsLoc['lng'];
  62. $item['latitude']=$wgsLoc['lat'];
  63. })->toArray();
  64. return $res;
  65. }
  66. /**
  67. * 根据名称搜索
  68. */
  69. public function searchNameAttr($query, $value, $data)
  70. {
  71. return $query->where('name', 'like', '%'.$value.'%');
  72. }
  73. /**
  74. * 根据编号搜索
  75. */
  76. public function searchShortCodeAttr($query, $value, $data)
  77. {
  78. return $query->where('shortcode|mac', 'like', '%'.$value.'%');
  79. }
  80. /**
  81. * 根据状态搜索
  82. */
  83. public function searchOnlineStateAttr($query, $value, $data)
  84. {
  85. $time=date('Y-m-d H:i:s',time()-300);
  86. if($value=='1'){
  87. return $query->where('online_time', '>=', $time);
  88. }elseif($value=='2'){
  89. return $query->where(function ($query) use( $time) {
  90. $query->whereOr('online_time','<', $time)->whereOr('online_time','=', null);
  91. });
  92. }
  93. }
  94. }