EnergyDeviceElectricity.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\helper\Str;
  5. class EnergyDeviceElectricity extends Auth
  6. {
  7. public function index(){
  8. $tree = model('EnergyDeviceElectricity')->showAllTree();
  9. $this->traverseArray($tree);
  10. // halt($tree);
  11. $this->assign('tree',$tree);
  12. return $this->fetch();
  13. }
  14. /**
  15. * 新增/编辑
  16. */
  17. public function add($pid=0,$t=0,$id=0){
  18. if(request()->isPost()){
  19. $res = model('EnergyDeviceElectricity')->updates();
  20. if($res){
  21. $this->success('操作成功',url('index'));
  22. }else{
  23. $this->error(model('EnergyDeviceElectricity')->getError());
  24. }
  25. }else{
  26. $meta_title = '新增';
  27. if($id){
  28. $info = db('energy_device_electricity')->where('org_id',$this->orgId)->where('id',$id)->find();
  29. $this->assign('info',$info);
  30. $info['s'] = $info['share'] ? (string)$info['share'] : '';
  31. $meta_title = '编辑';
  32. }
  33. $map[] = ['del','=',0];
  34. $map[]= ['enable','=',1];
  35. $map[]= ['org_id','=',$this->orgId];
  36. $alists = db('energy_area')
  37. ->field('id,title')
  38. ->where($map)
  39. ->where('pid',0)
  40. ->select();
  41. foreach ($alists as $k=>$v) {
  42. $alists[$k]['children'] = db('energy_area')
  43. ->field('id,title')
  44. ->where('pid', $v['id'])
  45. ->where($map)
  46. ->select();
  47. foreach ($alists[$k]['children'] as $kk => $vv) {
  48. $alists[$k]['children'][$kk]['children'] = db('energy_area')
  49. ->field('id,title')
  50. ->where('pid', $vv['id'])
  51. ->where($map)
  52. ->select();
  53. }
  54. }
  55. $dlists = db('energy_dep')
  56. ->field('id,name')
  57. ->where($map)
  58. ->select();
  59. $tlists = db('energy_sort')
  60. ->field('id,name')
  61. ->where($map)
  62. ->select();
  63. $this->assign('alist',$alists);
  64. $this->assign('dlist',$dlists);
  65. $this->assign('tlist',$tlists);
  66. $this->assign('pid',$pid);
  67. $this->assign('t',$t);
  68. $this->assign('meta_title',$meta_title);
  69. return $this->fetch();
  70. }
  71. }
  72. public function changeEnable($id=0,$enable=0){
  73. if($id <= 0){
  74. $this->error('参数错误');
  75. }
  76. $ret = Db::name('energy_device_electricity')->where('id',$id)->setField('enable',$enable);
  77. if(!$ret){
  78. $this->error('操作失败');
  79. }
  80. $this->success('操作成功');
  81. }
  82. /**
  83. * 删除记录
  84. * @param int $id
  85. */
  86. public function del($id=0){
  87. if(!$id){
  88. $this->error('参数错误');
  89. }
  90. // 检查是否有子级
  91. $ret = Db::name('energy_device_electricity')->where('parent_id',$id)->where('del',0)->find();
  92. if($ret){
  93. $this->error('有子电表不能删除');
  94. }
  95. $res = Db::name('energy_device_electricity')->where('id',$id)->setField('del',1);
  96. if($res){
  97. $this->success('删除成功');
  98. }else{
  99. $this->error('删除失败');
  100. }
  101. }
  102. /**
  103. * 循环tree给字段重新赋值
  104. */
  105. public function traverseArray(&$array) {
  106. foreach ($array as &$item) {
  107. // 修改type_e的值
  108. if (isset($item['type_e'])) {
  109. $item['type_e'] = Db::name('energy_sort')->where('id',$item['type_e'])->value('name');
  110. }
  111. // 如果存在children,递归调用
  112. if (isset($item['children']) && is_array($item['children'])) {
  113. $this->traverseArray($item['children']);
  114. }
  115. }
  116. }
  117. public function getDep() {
  118. $data = $this->request->param();
  119. $value = $data['param'];
  120. if ($value){
  121. $map[] = ['address','like','%'.$value.'%'];
  122. }
  123. $map[] = ['del','=',0];
  124. $map[]= ['enable','=',1];
  125. $map[]= ['org_id','=',$this->orgId];
  126. $lists = db('energy_dep')
  127. ->field('id,name')
  128. ->where($map)
  129. ->select();
  130. return json(['data'=>$lists]);
  131. }
  132. }