TemperatureDevice.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class TemperatureDevice extends Base
  5. {
  6. protected $abnormal = [
  7. '正常',
  8. '设备离线',
  9. '传感器异常',
  10. '传感器未连接',
  11. ];
  12. public function getList($page,$size,$orgId,$userId,$groupId=0){
  13. $map[] = ['org_id', '=', $orgId];
  14. $map[] = ['del', '=', 0];
  15. if($groupId >0){
  16. $ids = Db::name('group_device')
  17. ->where('org_id',$orgId)
  18. ->where('group_id',$groupId)
  19. ->column('device_id');
  20. if(empty($ids)){
  21. $map[] = ['id', '=', -1];
  22. }else{
  23. $map[] = ['id', 'in', $ids];
  24. }
  25. }
  26. if($userId!=1){
  27. $ids = Db::name('temperature_auth')
  28. ->where('user_id',$userId)
  29. ->where('org_id',$orgId)
  30. ->column('temperature_id');
  31. if(!empty($ids)){
  32. $map[] = ['id', 'in',$ids];
  33. }else{
  34. $map[] = ['id', '=',-1];
  35. }
  36. }
  37. $lists = Db::name('temperature_device')
  38. ->where($map)->page($page, $size)
  39. ->select();
  40. foreach ($lists as $k => &$v) {
  41. $v['tj'] = '';
  42. $v['abnormal_name'] = $this->abnormal[$v['abnormal']];
  43. $gd = Db::name('group_device')
  44. ->where('device_id',$v['id'])
  45. ->column('group_id');
  46. if(empty($gd)){
  47. $t = '';
  48. }else{
  49. $x = Db::name('group')
  50. ->where('id','in',$gd)
  51. ->column('title');
  52. $t = implode(',',$x);
  53. $v['dev_name'] = $v['dev_name'].'('.$t.')';
  54. }
  55. }
  56. return $lists?$lists:[];
  57. }
  58. public function getGroup($orgId,$userId,$page,$size){
  59. $map[] = ['org_id', '=', $orgId];
  60. $map[] = ['del', '=', 0];
  61. if ($userId!=1){
  62. $ids = Db::name('temperature_auth')
  63. ->where('user_id',$userId)
  64. ->where('org_id',$orgId)
  65. ->column('temperature_id');
  66. if(!empty($ids)){
  67. $map[] = ['id', 'in',$ids];
  68. }else{
  69. $map[] = ['id', '=',-1];
  70. }
  71. }
  72. $lists = Db::name('temperature_device')
  73. ->where($map)
  74. ->column('id');
  75. if(empty($lists)){
  76. return[];
  77. }else{
  78. $groupId = Db::name('group_device')
  79. ->where('org_id',$orgId)
  80. ->where('device_id','in',$lists)
  81. ->column('group_id');
  82. if(empty($groupId)) return [];
  83. $iid = array_unique($groupId);
  84. $gList = Db::name('group')
  85. ->where('id','in',$iid)
  86. ->field('id,title')
  87. ->page($page, $size)
  88. ->select();
  89. return $gList;
  90. }
  91. }
  92. public function getAllList($orgId){
  93. $map[] = ['org_id', '=', $orgId];
  94. $map[] = ['del', '=', 0];
  95. $ids = Db::name('group_device')
  96. ->where('org_id',$orgId)
  97. ->where('group_id','>',0)
  98. ->column('device_id');
  99. if(!empty($ids)){
  100. $map[] = ['id', 'not in', $ids];
  101. }
  102. $lists = Db::name('temperature_device')
  103. ->field('id,dev_name as title')
  104. ->where($map)
  105. ->select();
  106. return $lists?$lists:[];
  107. }
  108. public function getIdSList($orgId,$id){
  109. $map[] = ['org_id', '=', $orgId];
  110. $map[] = ['del', '=', 0];
  111. $ids = Db::name('group_device')
  112. ->where('org_id',$orgId)
  113. ->where('group_id','=',$id)
  114. ->column('device_id');
  115. if(!empty($ids)){
  116. $map[] = ['id', 'in', $ids];
  117. }
  118. $lists = Db::name('temperature_device')
  119. ->field('id,dev_name as title')
  120. ->where($map)
  121. ->select();
  122. $lists = $lists?$lists:[];
  123. $ids1 = Db::name('group_device')
  124. ->where('org_id',$orgId)
  125. ->where('group_id','>',0)
  126. ->column('device_id');
  127. $map1[] = ['org_id', '=', $orgId];
  128. $map1[] = ['del', '=', 0];
  129. if(!empty($ids1)){
  130. $map1[] = ['id', 'not in', $ids1];
  131. }
  132. $lists1 = Db::name('temperature_device')
  133. ->field('id,dev_name as title')
  134. ->where($map1)
  135. ->select();
  136. $lists1 = $lists1?$lists1:[];
  137. $lists = array_merge($lists,$lists1);
  138. return $lists;
  139. }
  140. public function updates(){
  141. $data = request()->post();
  142. $data['org_id'] = cur_org_id();
  143. $data['snaddr'] = trim($data['snaddr']);
  144. return $this->updateInfo($data,'TemperatureDevice','');
  145. }
  146. }