RepairGroup.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class RepairGroup extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->table='repair_group';
  12. $this->model= new \app\common\model\RepairGroup();
  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. $map[] = ['del','=',0];
  33. $map[] = ['org_id','=',$this->orgId];
  34. $map= empty($map) ? true: $map;
  35. //数据查询
  36. $lists = db($this->table)->where($map)->limit($start,$length)->order($order)->select();
  37. $ids = Db::name('roles')
  38. ->where('org_id',$this->orgId)
  39. ->where('parent_id',7)
  40. ->where('del',0)
  41. ->where('enable',1)
  42. ->column('id');
  43. $user = Db::name('user')
  44. ->alias('a')
  45. ->join('user_org b','a.id = b.user_id')
  46. ->join('user_roles c','a.id = c.user_id')
  47. ->where('a.del',0)
  48. ->where('a.enable',1)
  49. ->where('b.org_id',$this->orgId)
  50. ->where('c.roles_id','in',$ids)
  51. ->column('real_name','a.id');
  52. foreach ($lists as $k=>$v){
  53. $users = explode(',',$v['users']);
  54. foreach ($users as $kk=>$vv){
  55. $users[$kk] = $user[$vv];
  56. }
  57. $lists[$k]['users'] = implode(',',$users);
  58. }
  59. //数据返回
  60. $totalCount = db($this->table)->where($map)->count();
  61. $totalPage = ceil($totalCount/$length);
  62. $result['page'] = $page;
  63. $result['total'] = $totalPage;
  64. $result['records'] = $totalCount;
  65. $result['rows'] = $lists;
  66. return json($result);
  67. }else{
  68. return $this->fetch();
  69. }
  70. }
  71. /**
  72. * 新增/编辑
  73. */
  74. public function add($id=0){
  75. if(request()->isPost()){
  76. $res = $this->model->updates();
  77. if($res){
  78. $this->success('操作成功',url('index'));
  79. }else{
  80. $this->error($this->model->getError());
  81. }
  82. }else{
  83. if($id){
  84. $info =db($this->table)->where('id',$id)->find();
  85. $info['users'] = explode(',',$info['users']);
  86. $this->assign('info',$info);
  87. }
  88. $ids = Db::name('roles')
  89. ->where('org_id',$this->orgId)
  90. ->where('parent_id',7)
  91. ->where('del',0)
  92. ->where('enable',1)
  93. ->column('id');
  94. $userList = Db::name('user')
  95. ->alias('a')
  96. ->join('user_org b','a.id = b.user_id')
  97. ->join('user_roles c','a.id = c.user_id')
  98. ->where('a.del',0)
  99. ->where('a.enable',1)
  100. ->where('b.org_id',$this->orgId)
  101. ->where('c.roles_id','in',$ids)
  102. ->field('a.id,a.real_name as title')
  103. ->select();
  104. $this->assign('userList',$userList);
  105. return $this->fetch();
  106. }
  107. }
  108. /**
  109. * 删除记录
  110. * @param int $id
  111. */
  112. public function del($id=0){
  113. if(!$id){
  114. $this->error('参数错误');
  115. }
  116. $res = db($this->table)->where('id',$id)->setField('del',1);
  117. if($res){
  118. $this->success('删除成功');
  119. }else{
  120. $this->error('删除失败');
  121. }
  122. }
  123. /**
  124. * 改变字段值
  125. * @param int $fv
  126. * @param string $fn
  127. * @param int $fv
  128. */
  129. public function changeField($id=0,$fn='',$fv=0){
  130. if(!$fn||!$id){
  131. $this->error('参数错误');
  132. }
  133. $res = db($this->table)->where('id',$id)->setField($fn,$fv);
  134. if($res){
  135. $this->success('操作成功');
  136. }else{
  137. $this->error('操作失败');
  138. }
  139. }
  140. /**
  141. * 排序
  142. * @param int $id
  143. * @param int $sort
  144. */
  145. public function changeSort($id=0,$sort=0){
  146. if($id<0||$sort<0){
  147. $this->error('参数错误');
  148. }
  149. $res = db($this->table)->where('id',$id)->setField('sort',$sort);
  150. if($res){
  151. $this->success('操作成功');
  152. }else{
  153. $this->error('操作失败');
  154. }
  155. }
  156. }