EnergySort.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\controller;
  3. class EnergySort 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. $sort = input('sord','asc','trim'); //排序方式
  14. $order = $sortRow.' '.$sort;
  15. $map['del'] = 0;
  16. $map['org_id'] = $this->orgId;
  17. //数据查询
  18. $lists = db('energy_sort')->where($map)->limit($start,$length)->order($order)->select();
  19. //数据返回
  20. $totalCount = db('energy_sort')->where($map)->count();
  21. $totalPage = ceil($totalCount/$length);
  22. $result['page'] = $page;
  23. $result['total'] = $totalPage;
  24. $result['records'] = $totalCount;
  25. $result['rows'] = $lists;
  26. return json($result);
  27. }else{
  28. return $this->fetch();
  29. }
  30. }
  31. /**
  32. * 新增/编辑
  33. */
  34. public function add($id=0,$pid=0){
  35. if(request()->isPost()){
  36. $res = model('energy_sort')->updates();
  37. if($res){
  38. $this->success('操作成功');
  39. }else{
  40. $this->error(model('energy_sort')->getError());
  41. }
  42. }else{
  43. $map['del'] = 0;
  44. $map['org_id'] = $this->orgId;
  45. $map['enable'] = 1;
  46. $meta_title = '新增';
  47. if($id){
  48. $info = db('energy_sort')->where($map)->where('id',$id)->find();
  49. $this->assign('info',$info);
  50. $meta_title = '编辑';
  51. }
  52. $this->assign('meta_title',$meta_title);
  53. return $this->fetch();
  54. }
  55. }
  56. /**
  57. * 删除记录
  58. * @param int $id
  59. */
  60. public function del($id=0){
  61. if(!$id){
  62. $this->error('参数错误');
  63. }
  64. $res = db('energy_sort')->where('org_id',$this->orgId)->where('id',$id)->update(['del'=>1]);
  65. if($res){
  66. $this->success('删除成功');
  67. }else{
  68. $this->error('删除失败');
  69. }
  70. }
  71. /**
  72. * 改变字段值
  73. * @param int $fv
  74. * @param string $fn
  75. * @param int $fv
  76. */
  77. public function changeField($id=0,$fn='',$fv=0){
  78. if(!$fn||!$id){
  79. $this->error('参数错误');
  80. }
  81. $res = db('energy_sort')->where('org_id',$this->orgId)->where('id',$id)->setField($fn,$fv);
  82. if($res){
  83. $this->success('操作成功');
  84. }else{
  85. $this->error('操作失败');
  86. }
  87. }
  88. /**
  89. * 排序
  90. * @param int $id
  91. * @param int $sort
  92. */
  93. public function changeSort($id=0,$sort=0){
  94. if($id<0||$sort<0){
  95. $this->error('参数错误');
  96. }
  97. $res = db('energy_sort')->where('org_id',$this->orgId)->where('id',$id)->setField('sort',$sort);
  98. if($res){
  99. $this->success('操作成功');
  100. }else{
  101. $this->error('操作失败');
  102. }
  103. }
  104. }