EnergyArea.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\admin\controller;
  3. class EnergyArea extends Auth
  4. {
  5. public function index(){
  6. $pid = input('pid',0,'intval');
  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','sort','trim'); //排序列
  14. $sort = input('sord','asc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $map['pid'] = $pid;
  17. $map['del'] = 0;
  18. $map['org_id'] = $this->orgId;
  19. //数据查询
  20. $lists = db('energy_area')->where($map)->limit($start,$length)->order($order)->select();
  21. foreach ($lists as $k=>$v){
  22. if($v['pid']){
  23. $lists[$k]['ptitle'] = db('energy_area')->where('id',$v['pid'])->value('title');
  24. }else{
  25. $lists[$k]['ptitle'] = '无';
  26. }
  27. }
  28. //数据返回
  29. $totalCount = db('energy_area')->where($map)->count();
  30. $totalPage = ceil($totalCount/$length);
  31. $result['page'] = $page;
  32. $result['total'] = $totalPage;
  33. $result['records'] = $totalCount;
  34. $result['rows'] = $lists;
  35. return json($result);
  36. }else{
  37. if($pid){
  38. $ptitle = '['.db('energy_area')->where('id',$pid)->value('title').']子区域';
  39. $ppid = db('energy_area')->where('id',$pid)->value('pid');
  40. $id = db('energy_area')->where('pid',$pid)->value('id');
  41. }else{
  42. $ptitle = '院级区域';
  43. $ppid = 0;
  44. }
  45. $this->assign('meta_title',$ptitle);
  46. $this->assign('pid',$pid);
  47. $this->assign('ppid',$ppid);
  48. return $this->fetch();
  49. }
  50. }
  51. /**
  52. * 新增/编辑
  53. */
  54. public function add($id=0,$pid=0){
  55. if(request()->isPost()){
  56. $res = model('energy_area')->updates();
  57. if($res){
  58. $this->success('操作成功',url('index',array('pid'=>$pid)));
  59. }else{
  60. $this->error(model('energy_area')->getError());
  61. }
  62. }else{
  63. $map['del'] = 0;
  64. $map['org_id'] = $this->orgId;
  65. $map['enable'] = 1;
  66. $meta_title = '新增';
  67. if($id){
  68. $info = db('energy_area')->where($map)->where('id',$id)->find();
  69. $this->assign('info',$info);
  70. $meta_title = '编辑';
  71. }
  72. $list = get_menu();
  73. $this->assign('list',$list);
  74. $this->assign('pid',$pid);
  75. $this->assign('meta_title',$meta_title);
  76. return $this->fetch();
  77. }
  78. }
  79. /**
  80. * 删除记录
  81. * @param int $id
  82. */
  83. public function del($id=0){
  84. if(!$id){
  85. $this->error('参数错误');
  86. }
  87. $map['pid'] = $id;
  88. $map['del'] = 0;
  89. $subid = db('energy_area')->where('org_id',$this->orgId)->where($map)->value('id');
  90. if($subid){
  91. $this->error('有下级不能删除');
  92. }
  93. $res = db('energy_area')->where('org_id',$this->orgId)->where('id',$id)->update(['del'=>1]);
  94. if($res){
  95. $this->success('删除成功');
  96. }else{
  97. $this->error('删除失败');
  98. }
  99. }
  100. /**
  101. * 改变字段值
  102. * @param int $fv
  103. * @param string $fn
  104. * @param int $fv
  105. */
  106. public function changeField($id=0,$fn='',$fv=0){
  107. if(!$fn||!$id){
  108. $this->error('参数错误');
  109. }
  110. $res = db('energy_area')->where('org_id',$this->orgId)->where('id',$id)->setField($fn,$fv);
  111. if($res){
  112. $this->success('操作成功');
  113. }else{
  114. $this->error('操作失败');
  115. }
  116. }
  117. /**
  118. * 排序
  119. * @param int $id
  120. * @param int $sort
  121. */
  122. public function changeSort($id=0,$sort=0){
  123. if($id<0||$sort<0){
  124. $this->error('参数错误');
  125. }
  126. $res = db('energy_area')->where('id',$id)->setField('sort',$sort);
  127. if($res){
  128. $this->success('操作成功');
  129. }else{
  130. $this->error('操作失败');
  131. }
  132. }
  133. }