EnergyDep.php 4.4 KB

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