UserGroup.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class UserGroup extends Auth
  5. {
  6. public function index(){
  7. if(request()->isAjax()){
  8. //分页参数
  9. $length = input('rows',10,'intval'); //每页条数
  10. $page = input('page',1,'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx','id','trim'); //排序列
  14. $sort = input('sord','desc','trim'); //排序方式
  15. $order = $sortRow.' '.$sort;
  16. $title = input('title','','trim');
  17. if($title){
  18. $map[] = ['title','like','%'.$title.'%'];
  19. }
  20. $enable = input('enable','','trim');
  21. if($enable != ''){
  22. $map[] =['enable','=',$enable];
  23. }
  24. $map[] = ['org_id','=',$this->orgId];
  25. $map= empty($map) ? true: $map;
  26. //数据查询
  27. $lists = Db::name('user_group')
  28. ->where($map)
  29. ->limit($start,$length)
  30. ->order($order)->select();
  31. foreach ($lists as $k=>$v){
  32. $lists[$k]['nums'] = Db::name('user_group_user')
  33. ->where('org_id',$this->orgId)
  34. ->where('group_id',$v['id'])
  35. ->count();
  36. }
  37. //数据返回
  38. $totalCount = Db::name('user_group')->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('meta_title','运送员分组列表');
  47. return $this->fetch();
  48. }
  49. }
  50. /**
  51. * 新增/编辑
  52. */
  53. public function add($id=0){
  54. if(request()->isPost()){
  55. $model = new \app\common\model\UserGroup();
  56. $post = request()->post();
  57. $data = [
  58. 'title'=>$post['title'],
  59. 'enable'=>$post['enable'],
  60. 'org_id'=>$this->orgId,
  61. 'id'=>$id
  62. ];
  63. $validate = new \app\common\validate\UserGroup();
  64. $result = $validate->scene('')->check($data,[]);
  65. if(true !== $result){
  66. $this->error($validate->getError());
  67. }
  68. unset($data['id']);
  69. if($id <=0){
  70. $data['create_time'] = date('Y-m-d H:i:s');
  71. $res = Db::name('user_group')
  72. ->insertGetId($data);
  73. $id = $res;
  74. }else{
  75. $data['update_time'] = date('Y-m-d H:i:s');
  76. $res = Db::name('user_group')
  77. ->where('id',$id)
  78. ->update($data);
  79. }
  80. if($res){
  81. Db::name('user_group_user')
  82. ->where('org_id',$this->orgId)
  83. ->where('group_id',$id)
  84. ->delete();
  85. $data = request()->post();
  86. if(isset($data['user_id']) && !empty($data['user_id'])){
  87. $ids = explode(',',$data['user_id']);
  88. $a = [];
  89. foreach ($ids as $k=>$v){
  90. $a[] = [
  91. 'org_id'=>$this->orgId,
  92. 'user_id'=>$v,
  93. 'group_id'=>$id,
  94. ];
  95. }
  96. Db::name('user_group_user')
  97. ->insertAll($a);
  98. }
  99. $this->success('操作成功',url('index'));
  100. }else{
  101. $this->error($model->getError());
  102. }
  103. }else{
  104. $meta_title = '新增分组';
  105. if($id){
  106. $info = Db::name('user_group')->where('id',$id)->find();
  107. $info['user_group'] = Db::name('user_group_user')
  108. ->where('org_id',$this->orgId)
  109. ->where('group_id',$id)
  110. ->column('user_id');
  111. foreach ($info['user_group'] as $k=>$v){
  112. $info['user_group'][$k] = (string)$v;
  113. }
  114. $this->assign('info',$info);
  115. $meta_title = '编辑分组';
  116. $device = $this->getUser($id);
  117. }else{
  118. $device = $this->getUser();
  119. }
  120. $this->assign('meta_title',$meta_title);
  121. $this->assign('user',$device);
  122. return $this->fetch();
  123. }
  124. }
  125. public function getUser($uid=[]){
  126. $user = (new \app\common\model\WorkTypeMode())->getRolesUser(3, $this->orgId);
  127. $newUser = [];
  128. foreach ($user as $k => $v) {
  129. foreach ($v['user'] as $k1 => $v1) {
  130. $newUser[] = $v1['id'];
  131. }
  132. }
  133. $map8[] = ['id','in',$newUser];
  134. $map1 = [];
  135. // if($uid){
  136. // $map8[] = ['id','in',$uid];
  137. // }
  138. $lists = db('user')
  139. ->where($map8)
  140. ->field('id,real_name as title')
  141. ->order('id','desc')
  142. ->select();
  143. return $lists;
  144. }
  145. /**
  146. * 删除记录
  147. * @param int $id
  148. */
  149. public function del($id=0){
  150. if(!$id){
  151. $this->error('参数错误');
  152. }
  153. $ret = Db::name('user_group_user')->where('group_id',$id)->find();
  154. if($ret){
  155. $this->error('分组已被绑定,无法删除');
  156. }
  157. $res = Db::name('user_group')->where('id',$id)->delete();
  158. if($res){
  159. $this->success('删除成功');
  160. }else{
  161. $this->error('删除失败');
  162. }
  163. }
  164. /**
  165. * 改变字段值
  166. * @param int $fv
  167. * @param string $fn
  168. * @param int $fv
  169. */
  170. public function changeField($id=0,$fn='',$fv=0){
  171. if(!$fn||!$id){
  172. $this->error('参数错误');
  173. }
  174. $res = Db::name('user_group')->where('id',$id)->update([$fn => $fv]);
  175. if($res){
  176. $this->success('操作成功');
  177. }else{
  178. $this->error('操作失败');
  179. }
  180. }
  181. public function detail_auth($id){
  182. $info = Db::name('user_group_user')
  183. ->where('group_id', $id)->column('user_id');
  184. $d = Db::name('user')
  185. ->where('id','in',$info)
  186. ->select();
  187. $this->assign('list',$d);
  188. return $this->fetch();
  189. }
  190. }