DeviceGet.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace catchAdmin\device\model\get;
  3. use catchAdmin\permissions\model\SysConfig;
  4. use catchAdmin\system\model\SysDictData;
  5. use think\facade\Db;
  6. trait DeviceGet
  7. {
  8. /**
  9. * 获取导入时间(文本)
  10. */
  11. public function getCreateAtAttr($value)
  12. {
  13. if ($value) {
  14. return date('Y-m-d H:i:s', $value);
  15. } else {
  16. return '';
  17. }
  18. }
  19. /**
  20. * 获取设备型号(文本)
  21. */
  22. public function getModelTextAttr($value)
  23. {
  24. $value = $this->model;
  25. return (new SysDictData())->getValueByCode('DeviceModel', $value) ?: '';
  26. }
  27. /**
  28. * 获取绑定物体(文本)
  29. */
  30. public function getBindNumberAttr($value)
  31. {
  32. $bid = $this->bind_id;
  33. return Db::table('vehicles')->where('id', $bid)->value('license_plate') ?: '';
  34. }
  35. /**
  36. * 获取导入用户(文本)
  37. */
  38. public function getCreatorUserAttr($value)
  39. {
  40. $uid = $this->creator_id;
  41. return Db::table('users')->where('id', $uid)->value('username') ?: '';
  42. }
  43. /**
  44. * 获取使用状态(文本)
  45. */
  46. public function getDeviceStateAttr($value)
  47. {
  48. if($this->bind_id){
  49. return 1;
  50. }else{
  51. return 0;
  52. }
  53. }
  54. /**
  55. * 获取部门名称(文本)
  56. */
  57. public function getDepartNameAttr()
  58. {
  59. $id = $this->getData('department_id');
  60. return Db::table('departments')->where('id', $id)->value('department_name');
  61. }
  62. /**
  63. * 获取定位模式(文本)
  64. */
  65. public function getLocModeTextAttr()
  66. {
  67. $code = $this->getData('loc_mode');
  68. if(!$code){
  69. return '未知';
  70. }
  71. return (new SysDictData())->getValueByCode('DeviceLocationMode', $code) ?: '';
  72. }
  73. /**
  74. * 获取告警原因(文本)
  75. */
  76. public function getAlarmReasonListAttr()
  77. {
  78. $alarm_list=[];
  79. $list=Db::table('alarm_records')->where('device_number', $this->imei)->where('state','start')->select();
  80. foreach($list as $val){
  81. $text=(new SysDictData())->getValueByCode('AlarmType', $val['alarm_reason']);
  82. $continue_time=time()-$val['start_time'];
  83. $hour=$continue_time/3600;
  84. $minute=($continue_time % 3600)/60;
  85. $second=($continue_time % 3600)%60;
  86. $continue_str=(int)$hour.'小时'.(int)$minute.'分'.$second.'秒';
  87. $item=array('value'=>$val['alarm_reason'],'text'=> $text,'start_time'=>date('Y-m-d H:i:s',$val['start_time']),'continue_time'=>$continue_str);
  88. $alarm_list[]=$item;
  89. }
  90. return $alarm_list;
  91. }
  92. /**
  93. * 获取告警状态文本
  94. */
  95. public function getAlarmStateTextAttr($value)
  96. {
  97. $alarm_state = $this->getData('alarm_state');
  98. return $alarm_state ? '告警' : '正常';
  99. }
  100. /**
  101. * 获取在线状态文本
  102. */
  103. public function getNetStateAttr($value)
  104. {
  105. $online_time = $this->getData('online_time');
  106. $offline_intval = (new SysConfig())->getConfigValueBy('device_offline_interval','basic_config');
  107. if($online_time){
  108. $diff = time()-strtotime($online_time);
  109. if($diff - $offline_intval >= 0){
  110. $value = 1;
  111. }else{
  112. $value = 0;
  113. }
  114. }else{
  115. $value = 2;
  116. }
  117. return $value;
  118. }
  119. /**
  120. * 电量
  121. */
  122. public function getBatteryLevelAttr($value)
  123. {
  124. $online_time = $this->getData('online_time');
  125. if($online_time==''){
  126. return '--';
  127. }elseif($value==0){
  128. return '<5';
  129. }
  130. return $value;
  131. }
  132. /**
  133. * 获取在线状态图标
  134. */
  135. public function getStatusIconAttr($value)
  136. {
  137. $online_time = $this->getData('online_time');
  138. $offline_intval = (new SysConfig())->getConfigValueBy('device_offline_interval','basic_config');
  139. if($online_time){
  140. $diff = time()-strtotime($online_time);
  141. if($diff - $offline_intval >= 0){
  142. $value = 'offline';
  143. }else{
  144. $value = 'run';
  145. }
  146. }else{
  147. $value = 'unuse';
  148. }
  149. return $value;
  150. }
  151. /**
  152. * 获取在线状态图标
  153. */
  154. public function getStatusTextAttr($value)
  155. {
  156. $online_time = $this->getData('online_time');
  157. $offline_intval = (new SysConfig())->getConfigValueBy('device_offline_interval','basic_config');
  158. if($online_time){
  159. $diff = time()-strtotime($online_time);
  160. if($diff - $offline_intval >= 0){
  161. $value = '离线';
  162. }else{
  163. $value = '在线';
  164. }
  165. }else{
  166. $value = '未使用';
  167. }
  168. return $value;
  169. }
  170. /**
  171. * 获取在线时间
  172. */
  173. public function getLastOnlineTimeAttr()
  174. {
  175. $online_time = $this->getData('online_time');
  176. $wifi_online_time = $this->getData('wifi_online_time');
  177. if($online_time<$wifi_online_time){
  178. return $wifi_online_time;
  179. }else{
  180. return $online_time;
  181. }
  182. }
  183. }