123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace app\admin\controller;
- use app\common\util\ExcelUtil;
- use Overtrue\Pinyin\Pinyin;
- use think\Db;
- use think\Exception;
- use tools\Phptree;
- class UserSorts extends Auth
- {
- public function index(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','u.id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['u.enable','=',$enable];
- }
- $depId = input('depId','','trim');
- if($depId != ''){
- // $map[] = ['ud.dep_id','=',$depId];
- $userDepId = Db::name('user_dep')
- ->where('dep_id',$depId)
- ->column('user_id');
- $map[] = ['u.id','in',$userDepId];
- }
- $worker = input('worker','','trim');
- if($worker != ''){
- $map[] = ['u.worker','=',$worker];
- }
- $rolesId = Db::name('roles')
- ->where('parent_id',7)
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->column('id');
- $userRolesId = Db::name('user_roles')
- ->where('roles_id','in',$rolesId)
- ->column('user_id');
- $map[] = ['u.id','in',$userRolesId];
- $map[] = ['u.del','=',0];
- $map[] = ['u.type','=',0];
- $map[] = ['uo.org_id','=',$this->orgId];
- $map[] = ['u.enable','=',1];
- $map= empty($map) ? true: $map;
- $lists = db('user')
- ->alias('u')
- ->join('user_org uo','u.id=uo.user_id')
- ->where($map)
- ->field('u.*,uo.org_id')
- ->limit($start,$length)
- ->order(['u.sorts'=>'desc','u.id'=>'asc'])
- ->select();
- $cache = cache("org_user_sort_list_".$this->orgId);
- $lastUser = Db::name('order_auto_send_log')
- ->where('org_id',$this->orgId)
- ->where('type',0)
- ->order('id','desc')
- ->value('user_id');
- foreach ($lists as $k=>$v){
- $lists[$k]['dep_name'] = Db::name('user_dep')
- ->alias('ud')
- ->join('dep d','d.id=ud.dep_id')
- ->where('ud.user_id',$v['id'])
- ->where('d.org_id',$v['org_id'])
- ->value('d.title');
- $lists[$k]['type_name'] =$v['type_id']>0?Db::name('order_type')
- ->where('id',$v['type_id'])
- ->value('title'):"";
- $lists[$k]['roles_name'] = Db::name('user_roles')
- ->alias('ur')
- ->join('roles r','r.id=ur.roles_id')
- ->where('ur.user_id',$v['id'])
- ->where('r.org_id',$v['org_id'])
- ->value('r.name');
- $lists[$k]['old_sorts'] = 0;
- if($cache){
- foreach ($cache as $kk=>$vv){
- if($vv['id']==$v['id']){
- $lists[$k]['old_sorts'] = $vv['sorts'];
- }
- }
- }
- $lists[$k]['is_last'] = false;
- if($v['id']==$lastUser){
- $lists[$k]['is_last'] = true;
- }
- }
- //数据返回
- $totalCount = db('user')
- ->alias('u')
- ->join('user_org uo','uo.user_id = u.id')
- ->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $deps = model('Dep')->getList();
- $this->assign('deps',$deps);
- return $this->fetch();
- }
- }
- public function batchSorts(){
- $data = input('data','','trim');
- if(!$data){
- $this->error('参数错误');
- }
- $data = json_decode($data,true);
- if(!$data){
- $this->error('参数错误');
- }
- $oldSort = cache("org_user_sort_".$this->orgId);
- $lastUser = Db::name('order_auto_send_log')
- ->where('org_id',$this->orgId)
- ->where('type',0)
- ->order('id','desc')
- ->value('user_id');
- if(empty($oldSort) && !empty($lastUser) && !cache("auto_send_end_".$this->orgId)){
- cache("org_user_sort_".$this->orgId,get_sort_user($this->orgId,1));
- cache("org_user_sort_list_".$this->orgId,get_sort_user_list($this->orgId,1));
- }
- Db::startTrans();
- try{
- foreach ($data as $k=>$v){
- Db::name('user')->where('id',$v['id'])->setField('sorts',$v['sort']);
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error('操作失败');
- }
- $this->success('操作成功');
- }
- public function changeField($id=0,$fn='',$fv=0){
- if(!$fn||!$id){
- $this->error('参数错误');
- }
- $info = Db::name('user')->where('id',$id)->where('del',0)->find();
- if(!$info){
- $this->error('记录不存在');
- }
- $res = db('user')->where('id',$id)->setField($fn,$fv);
- if($res){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function remove(){
- cache('org_user_sort_'.$this->orgId,null);
- cache('auto_send_end_'.$this->orgId,null);
- cache('org_user_sort_list_'.$this->orgId,null);
- Db::name('order_auto_send_log')
- ->where('org_id',$this->orgId)
- ->delete();
- Db::name('order_auto_send_area')
- ->where('org_id',$this->orgId)
- ->update(['last_user'=>""]);
- $this->success('操作成功');
- }
- }
|