Group.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class Group extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['title','like','%'.$title.'%'];
  19. }
  20. $enable = input('enable','','trim');
  21. if($enable != ''){
  22. $map[] =['enable','=',$enable];
  23. }
  24. $map[] = ['org_id','=',$this->orgId];
  25. $map= empty($map) ? true: $map;
  26. //数据查询
  27. $lists = Db::name('group')
  28. ->where($map)
  29. ->limit($start,$length)
  30. ->order($order)->select();
  31. foreach ($lists as $k=>$v){
  32. $lists[$k]['nums'] = Db::name('group_device')
  33. ->where('org_id',$this->orgId)
  34. ->where('group_id',$v['id'])
  35. ->count();
  36. }
  37. //数据返回
  38. $totalCount = Db::name('group')->where($map)->count();
  39. $totalPage = ceil($totalCount/$length);
  40. $result['page'] = $page;
  41. $result['total'] = $totalPage;
  42. $result['records'] = $totalCount;
  43. $result['rows'] = $lists;
  44. return json($result);
  45. }else{
  46. $this->assign('meta_title','分组列表');
  47. return $this->fetch();
  48. }
  49. }
  50. /**
  51. * 新增/编辑
  52. */
  53. public function add($id=0){
  54. if(request()->isPost()){
  55. $model = new \app\common\model\Group();
  56. $post = request()->post();
  57. $data = [
  58. 'title'=>$post['title'],
  59. 'enable'=>$post['enable'],
  60. 'org_id'=>$this->orgId,
  61. 'id'=>$id
  62. ];
  63. $validate = new \app\common\validate\Group();
  64. $result = $validate->scene('')->check($data,[]);
  65. if(true !== $result){
  66. $this->error($validate->getError());
  67. }
  68. unset($data['id']);
  69. if($id <=0){
  70. $data['create_time'] = date('Y-m-d H:i:s');
  71. $res = Db::name('group')
  72. ->insertGetId($data);
  73. $id = $res;
  74. }else{
  75. $data['update_time'] = date('Y-m-d H:i:s');
  76. $res = Db::name('group')
  77. ->where('id',$id)
  78. ->update($data);
  79. }
  80. if($res){
  81. Db::name('group_device')
  82. ->where('org_id',$this->orgId)
  83. ->where('group_id',$id)
  84. ->delete();
  85. $data = request()->post();
  86. if(isset($data['device_id']) && !empty($data['device_id'])){
  87. $ids = explode(',',$data['device_id']);
  88. $a = [];
  89. foreach ($ids as $k=>$v){
  90. $a[] = [
  91. 'org_id'=>$this->orgId,
  92. 'device_id'=>$v,
  93. 'group_id'=>$id,
  94. ];
  95. }
  96. Db::name('group_device')
  97. ->insertAll($a);
  98. }
  99. $this->success('操作成功',url('index'));
  100. }else{
  101. $this->error($model->getError());
  102. }
  103. }else{
  104. $meta_title = '新增分组';
  105. if($id){
  106. $info = Db::name('group')->where('id',$id)->find();
  107. $info['group_device'] = Db::name('group_device')
  108. ->where('org_id',$this->orgId)
  109. ->where('group_id',$id)
  110. ->column('device_id');
  111. $this->assign('info',$info);
  112. $meta_title = '编辑分组';
  113. $device = (new \app\common\model\TemperatureDevice())->getIdSList($this->orgId,$id);
  114. }else{
  115. $device = (new \app\common\model\TemperatureDevice())->getAllList($this->orgId);
  116. }
  117. $this->assign('meta_title',$meta_title);
  118. $this->assign('device',$device);
  119. return $this->fetch();
  120. }
  121. }
  122. /**
  123. * 删除记录
  124. * @param int $id
  125. */
  126. public function del($id=0){
  127. if(!$id){
  128. $this->error('参数错误');
  129. }
  130. $ret = Db::name('temperature_device')->where('group_id',$id)->find();
  131. if($ret){
  132. $this->error('分组已被绑定,无法删除');
  133. }
  134. $res = Db::name('group')->where('id',$id)->delete();
  135. if($res){
  136. $this->success('删除成功');
  137. }else{
  138. $this->error('删除失败');
  139. }
  140. }
  141. /**
  142. * 改变字段值
  143. * @param int $fv
  144. * @param string $fn
  145. * @param int $fv
  146. */
  147. public function changeField($id=0,$fn='',$fv=0){
  148. if(!$fn||!$id){
  149. $this->error('参数错误');
  150. }
  151. $res = Db::name('group')->where('id',$id)->update([$fn => $fv]);
  152. if($res){
  153. $this->success('操作成功');
  154. }else{
  155. $this->error('操作失败');
  156. }
  157. }
  158. public function detail_auth($id){
  159. $info = Db::name('group_device')
  160. ->where('group_id', $id)->column('device_id');
  161. $d = Db::name('temperature_device')
  162. ->where('id','in',$info)
  163. ->select();
  164. $this->assign('list',$d);
  165. return $this->fetch();
  166. }
  167. }