UserSorts.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use Overtrue\Pinyin\Pinyin;
  5. use think\Db;
  6. use think\Exception;
  7. use tools\Phptree;
  8. class UserSorts extends Auth
  9. {
  10. public function index(){
  11. if(request()->isAjax()){
  12. //分页参数
  13. $length = input('rows',10,'intval'); //每页条数
  14. $page = input('page',1,'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. //排序
  17. $sortRow = input('sidx','u.id','trim'); //排序列
  18. $sort = input('sord','desc','trim'); //排序方式
  19. $order = $sortRow.' '.$sort;
  20. $title = input('title','','trim');
  21. if($title){
  22. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  23. }
  24. $enable = input('enable','','trim');
  25. if($enable != ''){
  26. $map[] = ['u.enable','=',$enable];
  27. }
  28. $depId = input('depId','','trim');
  29. if($depId != ''){
  30. // $map[] = ['ud.dep_id','=',$depId];
  31. $userDepId = Db::name('user_dep')
  32. ->where('dep_id',$depId)
  33. ->column('user_id');
  34. $map[] = ['u.id','in',$userDepId];
  35. }
  36. $worker = input('worker','','trim');
  37. if($worker != ''){
  38. $map[] = ['u.worker','=',$worker];
  39. }
  40. $rolesId = Db::name('roles')
  41. ->where('parent_id',7)
  42. ->where('org_id',$this->orgId)
  43. ->where('del',0)
  44. ->column('id');
  45. $userRolesId = Db::name('user_roles')
  46. ->where('roles_id','in',$rolesId)
  47. ->column('user_id');
  48. $map[] = ['u.id','in',$userRolesId];
  49. $map[] = ['u.del','=',0];
  50. $map[] = ['u.type','=',0];
  51. $map[] = ['uo.org_id','=',$this->orgId];
  52. $map[] = ['u.enable','=',1];
  53. $map= empty($map) ? true: $map;
  54. $lists = db('user')
  55. ->alias('u')
  56. ->join('user_org uo','u.id=uo.user_id')
  57. ->where($map)
  58. ->field('u.*,uo.org_id')
  59. ->limit($start,$length)
  60. ->order(['u.sorts'=>'desc','u.id'=>'asc'])
  61. ->select();
  62. $cache = cache("org_user_sort_list_".$this->orgId);
  63. $lastUser = Db::name('order_auto_send_log')
  64. ->where('org_id',$this->orgId)
  65. ->where('type',0)
  66. ->order('id','desc')
  67. ->value('user_id');
  68. foreach ($lists as $k=>$v){
  69. $lists[$k]['dep_name'] = Db::name('user_dep')
  70. ->alias('ud')
  71. ->join('dep d','d.id=ud.dep_id')
  72. ->where('ud.user_id',$v['id'])
  73. ->where('d.org_id',$v['org_id'])
  74. ->value('d.title');
  75. $lists[$k]['type_name'] =$v['type_id']>0?Db::name('order_type')
  76. ->where('id',$v['type_id'])
  77. ->value('title'):"";
  78. $lists[$k]['roles_name'] = Db::name('user_roles')
  79. ->alias('ur')
  80. ->join('roles r','r.id=ur.roles_id')
  81. ->where('ur.user_id',$v['id'])
  82. ->where('r.org_id',$v['org_id'])
  83. ->value('r.name');
  84. $lists[$k]['old_sorts'] = 0;
  85. if($cache){
  86. foreach ($cache as $kk=>$vv){
  87. if($vv['id']==$v['id']){
  88. $lists[$k]['old_sorts'] = $vv['sorts'];
  89. }
  90. }
  91. }
  92. $lists[$k]['is_last'] = false;
  93. if($v['id']==$lastUser){
  94. $lists[$k]['is_last'] = true;
  95. }
  96. }
  97. //数据返回
  98. $totalCount = db('user')
  99. ->alias('u')
  100. ->join('user_org uo','uo.user_id = u.id')
  101. ->where($map)->count();
  102. $totalPage = ceil($totalCount/$length);
  103. $result['page'] = $page;
  104. $result['total'] = $totalPage;
  105. $result['records'] = $totalCount;
  106. $result['rows'] = $lists;
  107. return json($result);
  108. }else{
  109. $deps = model('Dep')->getList();
  110. $this->assign('deps',$deps);
  111. return $this->fetch();
  112. }
  113. }
  114. public function batchSorts(){
  115. $data = input('data','','trim');
  116. if(!$data){
  117. $this->error('参数错误');
  118. }
  119. $data = json_decode($data,true);
  120. if(!$data){
  121. $this->error('参数错误');
  122. }
  123. $oldSort = cache("org_user_sort_".$this->orgId);
  124. $lastUser = Db::name('order_auto_send_log')
  125. ->where('org_id',$this->orgId)
  126. ->where('type',0)
  127. ->order('id','desc')
  128. ->value('user_id');
  129. if(empty($oldSort) && !empty($lastUser) && !cache("auto_send_end_".$this->orgId)){
  130. cache("org_user_sort_".$this->orgId,get_sort_user($this->orgId,1));
  131. cache("org_user_sort_list_".$this->orgId,get_sort_user_list($this->orgId,1));
  132. }
  133. Db::startTrans();
  134. try{
  135. foreach ($data as $k=>$v){
  136. Db::name('user')->where('id',$v['id'])->setField('sorts',$v['sort']);
  137. }
  138. Db::commit();
  139. }catch (Exception $e){
  140. Db::rollback();
  141. $this->error('操作失败');
  142. }
  143. $this->success('操作成功');
  144. }
  145. public function changeField($id=0,$fn='',$fv=0){
  146. if(!$fn||!$id){
  147. $this->error('参数错误');
  148. }
  149. $info = Db::name('user')->where('id',$id)->where('del',0)->find();
  150. if(!$info){
  151. $this->error('记录不存在');
  152. }
  153. $res = db('user')->where('id',$id)->setField($fn,$fv);
  154. if($res){
  155. $this->success('操作成功');
  156. }else{
  157. $this->error('操作失败');
  158. }
  159. }
  160. public function remove(){
  161. cache('org_user_sort_'.$this->orgId,null);
  162. cache('auto_send_end_'.$this->orgId,null);
  163. cache('org_user_sort_list_'.$this->orgId,null);
  164. Db::name('order_auto_send_log')
  165. ->where('org_id',$this->orgId)
  166. ->delete();
  167. Db::name('order_auto_send_area')
  168. ->where('org_id',$this->orgId)
  169. ->update(['last_user'=>""]);
  170. $this->success('操作成功');
  171. }
  172. }