PatrolForm.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class PatrolForm extends Auth
  6. {
  7. public function __construct(App $app = null) {
  8. parent::__construct($app);
  9. $this->model= new \app\common\model\PatrolForm();
  10. $this->table= $this->model->table;
  11. }
  12. public function index($mode=1){
  13. if(request()->isAjax()){
  14. //分页参数
  15. $length = input('rows',10,'intval'); //每页条数
  16. $page = input('page',1,'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx','id','trim'); //排序列
  20. $sort = input('sord','desc','trim'); //排序方式
  21. $order = $sortRow.' '.$sort;
  22. $title = input('title','','trim');
  23. if($title){
  24. $map[] = ['title','like','%'.$title.'%'];
  25. }
  26. $enable = input('enable','','trim');
  27. if($enable != ''){
  28. $map[] = ['enable','=',$enable];
  29. }
  30. $cate_id = input('cate_id','','trim');
  31. if($cate_id != ''){
  32. $map[] = ['cate_id','=',$cate_id];
  33. }
  34. $map[] = ['del','=',0];
  35. $map[] = ['patrol_mode','=',$mode];
  36. $map[] = ['org_id','=',$this->orgId];
  37. $map= empty($map) ? true: $map;
  38. //数据查询
  39. $lists = db($this->table)->where($map)->limit($start,$length)
  40. ->order(['sort'=>'asc','id'=>'desc'])
  41. ->select();
  42. foreach ($lists as $k=>$v){
  43. $lists[$k]['type'] = $this->model->type[$v['type']];
  44. $lists[$k]['cateName'] = $v['cate_id']?implode(',',Db::name('patrol_cate')
  45. ->where('id','in',explode(',',$v['cate_id']))
  46. ->column('title')):'';
  47. }
  48. //数据返回
  49. $totalCount = db($this->table)->where($map)->count();
  50. $totalPage = ceil($totalCount/$length);
  51. $result['page'] = $page;
  52. $result['total'] = $totalPage;
  53. $result['records'] = $totalCount;
  54. $result['rows'] = $lists;
  55. return json($result);
  56. }else{
  57. $this->assign('m_name',(new \app\common\model\PatrolAddrForm())->getModeTitle($mode).'检查项');
  58. $this->assign('mode',$mode);
  59. $cate = model('PatrolCate')->getByModeList($mode);
  60. $this->assign('cate',$cate);
  61. return $this->fetch();
  62. }
  63. }
  64. /**
  65. * 新增/编辑
  66. */
  67. public function add($id=0){
  68. $mode =input('mode',1,'trim');
  69. if(request()->isPost()){
  70. $res = $this->model->updates();
  71. if($res){
  72. $this->success('操作成功',url('index',['mode'=>$mode]));
  73. }else{
  74. $this->error($this->model->getError());
  75. }
  76. }else{
  77. if($id){
  78. $info =db($this->table)->where('id',$id)->find();
  79. $info['cate_id'] = $info['cate_id']?explode(',',$info['cate_id']):[];
  80. $this->assign('info',$info);
  81. }
  82. $cate = model('PatrolCate')->getByModeList($mode);
  83. $this->assign('type',$this->model->type);
  84. $this->assign('mode',$mode);
  85. $this->assign('cate',$cate);
  86. return $this->fetch();
  87. }
  88. }
  89. /**
  90. * 删除记录
  91. * @param int $id
  92. */
  93. public function del($id=0){
  94. if(!$id){
  95. $this->error('参数错误');
  96. }
  97. $res = db($this->table)->where('id',$id)->setField('del',1);
  98. if($res){
  99. $this->success('删除成功');
  100. }else{
  101. $this->error('删除失败');
  102. }
  103. }
  104. /**
  105. * 改变字段值
  106. * @param int $fv
  107. * @param string $fn
  108. * @param int $fv
  109. */
  110. public function changeField($id=0,$fn='',$fv=0){
  111. if(!$fn||!$id){
  112. $this->error('参数错误');
  113. }
  114. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  115. if($res){
  116. $this->success('操作成功');
  117. }else{
  118. $this->error('操作失败');
  119. }
  120. }
  121. public function getByCate(){
  122. $cate_id = input('id',[]);
  123. $mode = input('mode',0);
  124. if(empty($cate_id)){
  125. $this->success('操作成功','',[]);
  126. }
  127. $where = '';
  128. foreach ($cate_id as $k=>$v){
  129. //$map[]=['','exp',Db::raw("FIND_IN_SET($v,cate_id)")];
  130. if($k>0){
  131. $where.='or ';
  132. }
  133. $where.='FIND_IN_SET('.$v.',cate_id)';
  134. }
  135. //
  136. // if($mode==4){
  137. // $map[] = ['cate_id','=',$cate_id];
  138. // }
  139. $list = Db::name('patrol_addr_form')
  140. ->where('org_id',$this->orgId)
  141. ->where('del',0)
  142. ->where('enable',1)
  143. ->whereRaw($where)
  144. ->select();
  145. $this->success('操作成功','',$list);
  146. }
  147. public function getByCateFrom(){
  148. $cate_id = input('id',[]);
  149. $mode = input('mode',0);
  150. if(empty($cate_id)){
  151. $this->success('操作成功','',[]);
  152. }
  153. $where = '';
  154. foreach ($cate_id as $k=>$v){
  155. //$map[]=['','exp',Db::raw("FIND_IN_SET($v,cate_id)")];
  156. if($k>0){
  157. $where.='or ';
  158. }
  159. $where.='FIND_IN_SET('.$v.',cate_id)';
  160. }
  161. //
  162. // if($mode==4){
  163. // $map[] = ['cate_id','=',$cate_id];
  164. // }
  165. $list = Db::name('patrol_form')
  166. ->where('org_id',$this->orgId)
  167. ->where('del',0)
  168. ->where('enable',1)
  169. //->where($map)
  170. ->whereRaw($where)
  171. ->order(['sort'=>'asc','id'=>'desc'])
  172. ->select();
  173. $this->success('操作成功','',$list);
  174. }
  175. }