DelayReason.php 4.0 KB

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