0
0

DeviceForm.php 4.3 KB

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