showAllTree(); $this->traverseArray($tree); // halt($tree); $this->assign('tree',$tree); return $this->fetch(); } /** * 新增/编辑 */ public function add($pid=0,$t=0,$id=0){ if(request()->isPost()){ $res = model('EnergyDeviceElectricity')->updates(); if($res){ $this->success('操作成功',url('index')); }else{ $this->error(model('EnergyDeviceElectricity')->getError()); } }else{ $meta_title = '新增'; if($id){ $info = db('energy_device_electricity')->where('org_id',$this->orgId)->where('id',$id)->find(); $this->assign('info',$info); $info['s'] = $info['share'] ? (string)$info['share'] : ''; $meta_title = '编辑'; } $map[] = ['del','=',0]; $map[]= ['enable','=',1]; $map[]= ['org_id','=',$this->orgId]; $alists = db('energy_area') ->field('id,title') ->where($map) ->where('pid',0) ->select(); foreach ($alists as $k=>$v) { $alists[$k]['children'] = db('energy_area') ->field('id,title') ->where('pid', $v['id']) ->where($map) ->select(); foreach ($alists[$k]['children'] as $kk => $vv) { $alists[$k]['children'][$kk]['children'] = db('energy_area') ->field('id,title') ->where('pid', $vv['id']) ->where($map) ->select(); } } $dlists = db('energy_dep') ->field('id,name') ->where($map) ->select(); $tlists = db('energy_sort') ->field('id,name') ->where($map) ->select(); $this->assign('alist',$alists); $this->assign('dlist',$dlists); $this->assign('tlist',$tlists); $this->assign('pid',$pid); $this->assign('t',$t); $this->assign('meta_title',$meta_title); return $this->fetch(); } } public function changeEnable($id=0,$enable=0){ if($id <= 0){ $this->error('参数错误'); } $ret = Db::name('energy_device_electricity')->where('id',$id)->setField('enable',$enable); if(!$ret){ $this->error('操作失败'); } $this->success('操作成功'); } /** * 删除记录 * @param int $id */ public function del($id=0){ if(!$id){ $this->error('参数错误'); } // 检查是否有子级 $ret = Db::name('energy_device_electricity')->where('parent_id',$id)->where('del',0)->find(); if($ret){ $this->error('有子电表不能删除'); } $res = Db::name('energy_device_electricity')->where('id',$id)->setField('del',1); if($res){ $this->success('删除成功'); }else{ $this->error('删除失败'); } } /** * 循环tree给字段重新赋值 */ public function traverseArray(&$array) { foreach ($array as &$item) { // 修改type_e的值 if (isset($item['type_e'])) { $item['type_e'] = Db::name('energy_sort')->where('id',$item['type_e'])->value('name'); } // 如果存在children,递归调用 if (isset($item['children']) && is_array($item['children'])) { $this->traverseArray($item['children']); } } } public function getDep() { $data = $this->request->param(); $value = $data['param']; if ($value){ $map[] = ['address','like','%'.$value.'%']; } $map[] = ['del','=',0]; $map[]= ['enable','=',1]; $map[]= ['org_id','=',$this->orgId]; $lists = db('energy_dep') ->field('id,name') ->where($map) ->select(); return json(['data'=>$lists]); } }