CleanType.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class CleanType extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model= new \app\common\model\CleanType();
  11. $this->table= $this->model->table;
  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[] = ['parent_id','=',0];
  34. $map= empty($map) ? true: $map;
  35. //数据查询
  36. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  37. //数据返回
  38. $totalCount = db($this->table)->where($map)->count();
  39. $totalPage = ceil($totalCount/$length);
  40. $result['page'] = $page;
  41. $result['total'] = $totalPage;
  42. $result['records'] = $totalCount;
  43. $result['rows'] = $lists;
  44. return json($result);
  45. }else{
  46. $this->assign('m_name','任务类型');
  47. return $this->fetch();
  48. }
  49. }
  50. public function show(){
  51. if(request()->isAjax()){
  52. //分页参数
  53. $length = input('rows',10,'intval'); //每页条数
  54. $page = input('page',1,'intval'); //第几页
  55. $start = ($page - 1) * $length; //分页开始位置
  56. //排序
  57. $sortRow = input('sidx','id','trim'); //排序列
  58. $sort = input('sord','desc','trim'); //排序方式
  59. $order = $sortRow.' '.$sort;
  60. $title = input('title','','trim');
  61. if($title){
  62. $map[] = ['title','like','%'.$title.'%'];
  63. }
  64. $enable = input('enable','','trim');
  65. if($enable != ''){
  66. $map[] = ['enable','=',$enable];
  67. }
  68. $map[] = ['del','=',0];
  69. $map[] = ['org_id','=',$this->orgId];
  70. $map[] = ['parent_id','>',0];
  71. $map= empty($map) ? true: $map;
  72. //数据查询
  73. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  74. foreach ($lists as &$v){
  75. $v['parent_title'] = db($this->table)->where('id',$v['parent_id'])->value('title');
  76. }
  77. //数据返回
  78. $totalCount = db($this->table)->where($map)->count();
  79. $totalPage = ceil($totalCount/$length);
  80. $result['page'] = $page;
  81. $result['total'] = $totalPage;
  82. $result['records'] = $totalCount;
  83. $result['rows'] = $lists;
  84. return json($result);
  85. }else{
  86. $this->assign('m_name','任务项');
  87. return $this->fetch();
  88. }
  89. }
  90. /**
  91. * 新增/编辑
  92. */
  93. public function add($id=0){
  94. if(request()->isPost()){
  95. $res = $this->model->updates();
  96. if($res){
  97. $this->success('操作成功',url('index'));
  98. }else{
  99. $this->error($this->model->getError());
  100. }
  101. }else{
  102. if($id){
  103. $info = db($this->table)->where('id',$id)->find();
  104. $this->assign('info',$info);
  105. }
  106. $type = input('type','');
  107. if($type==1){
  108. $parent =$this->model->list();
  109. $this->assign('parent_list',$parent);
  110. }
  111. $this->assign('type',$type);
  112. return $this->fetch();
  113. }
  114. }
  115. /**
  116. * 删除记录
  117. * @param int $id
  118. */
  119. public function del($id=0){
  120. if(!$id){
  121. $this->error('参数错误');
  122. }
  123. $info = db($this->table)->where('id',$id)->find();
  124. if($info['parent_id'] == 0){
  125. if(db($this->table)->where('parent_id',$id)->where('del',0)->find()){
  126. $this->error('有任务项绑定该类型,暂不能删除');
  127. }
  128. }
  129. $res = db($this->table)->where('id',$id)->setField('del',1);
  130. if($res){
  131. $this->success('删除成功');
  132. }else{
  133. $this->error('删除失败');
  134. }
  135. }
  136. /**
  137. * 改变字段值
  138. * @param int $fv
  139. * @param string $fn
  140. * @param int $fv
  141. */
  142. public function changeField($id=0,$fn='',$fv=0){
  143. if(!$fn||!$id){
  144. $this->error('参数错误');
  145. }
  146. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  147. if($res){
  148. $this->success('操作成功');
  149. }else{
  150. $this->error('操作失败');
  151. }
  152. }
  153. public function batchSort(){
  154. $data = input('data','','trim');
  155. if(!$data){
  156. $this->error('参数错误');
  157. }
  158. $data = json_decode($data,true);
  159. if(!$data){
  160. $this->error('参数错误');
  161. }
  162. Db::startTrans();
  163. try{
  164. foreach ($data as $k=>$v){
  165. Db::name('clean_type')->where('id',$v['id'])->setField('sort',$v['sort']);
  166. }
  167. Db::commit();
  168. }catch (Exception $e){
  169. Db::rollback();
  170. $this->error('操作失败');
  171. }
  172. $this->success('操作成功');
  173. }
  174. }