123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class Dep extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'name' => input('name','','trim'),
- 'level' => input('level','','trim'),
- 'content' => input('content','','trim'),
- 'org_id' => input('orgId/d',0),
- 'parent_id' => input('parentId/d',0),
- ];
- $result = validate('Dep')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('Dep')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- if($id > 0){
- unset($data['org_id']);
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $maxcode = Db::name('dep')->order('id desc')->value('code');
- if($maxcode){
- $data['code'] = $maxcode + 1;
- }else{
- $data['code'] = '001';
- }
- $ret = $this->allowField(true)->save($data);
- }
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->where('del',0)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- return $info->toArray();
- }
- public function lists($page,$size){
- $lists = $this
- ->where('del',0)
- ->page($page,$size)
- ->order('sorts asc,id asc')
- ->select();
- return $lists?$lists->toArray():[];
- }
- // 启用
- public function toEnableDep($orgId){
- $info = $this->where('id',$orgId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('组织不存在');
- }
- if($info['enable'] == 1){
- HelpHander::error('启用中,无需重复启用');
- }
- $ret = $this->where('id',$orgId)->setField('enable',1);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 作废部门(包括所有下级部门和岗位)
- public function disableDep($depId){
- $info = $this->where('id',$depId)->where('del',0)->find();
- if(!$info){
- HelpHander::error('部门不存在');
- }
- if($info['enable'] == 0){
- HelpHander::error('部门已禁用');
- }
- $deps = $this->getDepChildren($depId);
- $deps[] = $depId;
- $this->startTrans();
- try{
- Db::name('dep')->where('id','in',$deps)->setField('enable',0);
- Db::name('job')->where('dep_id','in',$deps)->setField('enable',0);
- $this->commit();
- }catch (Exception $e){
- $this->rollback();
- HelpHander::error('操作失败');
- }
- return true;
- }
- /**
- * 获取部门及下级id集合
- * @param $ids
- * @param array $result
- * @return array
- */
- public function getDepChildren($ids,&$result = []){
- $children = Db::name('dep')
- ->where('del',0)
- ->where('enable',1)
- ->where('parent_id','in',$ids)
- ->column('id');
- if($children){
- $result = array_merge($result,$children);
- $this->getDepChildren($children,$result);
- }
- return $result;
- }
- // 根据部门获取部门及下级部门下的所有岗位id
- public function getJobByDep($depId){
- $deps = $this->getDepChildren([$depId]);
- $deps = $deps?$deps:[];
- $deps[] = $depId;
- $jobs = Db::name('job')
- ->where('del',0)
- ->where('enable',1)
- ->where('dep_id','in',$deps)
- ->column('id');
- return $jobs?$jobs:[];
- }
- // 指定组织下的部门列表(树状结构)
- public function queryDepTreeByOrg($orgId){
- $orgs = Db::name('org')
- ->where('del',0)
- ->where('enable',1)
- ->where('id',$orgId)
- ->order('id asc')
- ->find();
- if(!$orgs){
- return [];
- }
- $orgs['type'] = 1;
- $orgs['uniqueFlag'] = $orgs['id'].'1';
- // 获取所有部门
- $deps = Db::name('dep')
- ->where('del',0)
- ->where('enable',1)
- ->where('org_id',$orgId)
- ->order('sorts asc,id asc')
- ->field('id,name,code,level,content,parent_id')
- ->select();
- // 添加唯一值
- foreach ($deps as $k=>$v){
- $deps[$k]['type'] = 2;
- $deps[$k]['uniqueFlag'] = $v['id'].'2';
- }
- $ndeps = list_to_tree($deps, 'parent_id', $child = 'childList', 0);
- $orgs['childList'] = $ndeps;
- return [$orgs];
- }
- // 指定组织下的部门列表(树状结构)
- public function queryDepTree($orgId){
- // 获取所有部门
- $deps = Db::name('dep')
- ->where('del',0)
- ->where('enable',1)
- ->where('org_id',$orgId)
- ->order('sorts asc,id asc')
- ->field('id,name,parent_id')
- ->select();
- // 添加唯一值
- foreach ($deps as $k=>$v){
- $deps[$k]['type'] = 0;
- $deps[$k]['uniqueId'] = $v['id'].'0bg';
- }
- $ndeps = list_to_tree($deps, 'parent_id', $child = 'depAndJobDtos', 0);
- $ndeps = array_delete_char($ndeps,'parent_id','depAndJobDtos');
- return $ndeps;
- }
- // 部门下的部门/组织下的部门 (直属部门)
- public function queryDepListByOrg($id,$isOrg){
- if($isOrg == 1){ // 部门
- $lists = $this
- ->where('del',0)
- ->where('parent_id',$id)
- ->order('sorts asc,id asc')
- ->select();
- } else { // 组织
- $lists = $this
- ->where('del',0)
- ->where('parent_id',0)
- ->where('org_id',$id)
- ->order('sorts asc,id asc')
- ->select();
- }
- return $lists?$lists->toArray():[];
- }
- //部门岗位人员树/部门岗位树
- public function queryDepAndJobTree($orgId,$isDep){
- // 获取所有部门
- $deps = Db::name('dep')
- ->where('del',0)
- ->where('enable',1)
- ->where('org_id',$orgId)
- ->order('sorts asc,id asc')
- ->field('id,name,parent_id')
- ->select();
- // 添加唯一值
- foreach ($deps as $k=>$v){
- $deps[$k]['type'] = 0;
- $deps[$k]['uniqueId'] = $v['id'].'0bg';
- $jobs = Db::name('job')
- ->where('del',0)
- ->where('enable',1)
- ->where('dep_id',$v['id'])
- ->order('id asc')
- ->field('id,name,code')
- ->select();
- $jobs = $jobs?$jobs:[];
- foreach ($jobs as $kk=>$vv){
- $jobs[$kk]['type'] = 1;
- $jobs[$kk]['uniqueId'] = $vv['id'].'1gw';
- if($isDep == 1){
- $jobs[$kk]['name'] = '['.$vv['code'].']'.$vv['name'];
- }
- unset($jobs[$kk]['code']);
- if($isDep == 0){ // 查询人
- $user = Db::name('user_job')
- ->alias('uj')
- ->join('user u','u.id = uj.user_id')
- ->join('user_info ui','ui.user_id = u.id')
- ->where('uj.job_id',$vv['id'])
- ->where('u.del',0)
- ->where('u.enable',1)
- ->order('u.sorts asc,u.id asc')
- ->field('u.id,ui.name')
- ->select();
- $user = $user?$user:[];
- foreach ($user as $kkk=>$vvv){
- $user[$kkk]['type'] = 2;
- $user[$kkk]['uniqueId'] = $vvv['id'].'2ry';
- $user[$kkk]['depAndJobDtos'] = [];
- // $user[$kkk]['depId'] = $v['id'];
- // $user[$kkk]['depName'] = $v['name'];
- }
- $jobs[$kk]['depAndJobDtos'] = $user;
- }else{
- $jobs[$kk]['depAndJobDtos'] = [];
- }
- }
- $deps[$k]['depAndJobDtos'] = $jobs;
- }
- $ndeps = list_to_tree($deps, 'parent_id', $child = 'depAndJobDtos', 0);
- $ndeps = array_delete_char($ndeps,'parent_id','depAndJobDtos');
- return $ndeps;
- }
- /**
- * 获取用户父级部门id集合
- * @param $ids
- * @param array $result
- * @return array
- */
- public function getDepAndParent($ids,&$result = []){
- $parents = Db::name('dep')
- ->where('del',0)
- ->where('enable',1)
- ->where('parent_id','<>',0)
- ->where('id','in',$ids)
- ->column('parent_id');
- if($parents){
- $result = array_merge($result,$parents);
- $this->getDepAndParent($parents,$result);
- }
- return $result;
- }
- // 获取用户部门及上级id集合
- public function getUserDepIds($userId,$orgId){
- $depids = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->where('uj.user_id',$userId)
- ->where('j.org_id',$orgId)
- ->where('j.del',0)
- ->where('j.enable',1)
- ->column('j.dep_id');
- $depids = $depids?$depids:[];
- if($depids){
- $parents = $this->getDepAndParent($depids);
- $depids = array_merge($depids,$parents);
- }
- return $depids?$depids:[];
- }
- // 获取用户部门
- public function getUserDep($userId,$orgId,$type=0){ // type 0=可用的部门 1=全部部门(包括删除禁用的)
- $map[] = ['uj.user_id','=',$userId];
- $map[] = ['j.org_id','=',$orgId];
- if($type == 0){
- $map[] = ['j.del','=',0];
- $map[] = ['j.enable','=',1];
- }
- $deps = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->where($map)
- ->field('d.id,d.name')
- ->select();
- return $deps?$deps:[];
- }
- // 根据用户和组织ID获取部门列表 app端使用
- public function queryUserListByOrg($userId,$orgId){
- $deps = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->join('dep d','d.id = j.dep_id')
- ->join('org o','o.id = j.org_id')
- ->where('uj.user_id',$userId)
- ->where('j.org_id',$orgId)
- ->where('j.del',0)
- ->where('j.enable',1)
- ->field('j.org_id,o.name as orgName,d.name as depName,j.dep_id,uj.user_id')
- ->select();
- return $deps?$deps:[];
- }
- // 获取部门的所有上级部门
- public function getDepParent($pid,$title=''){
- $info = Db::name('dep')->where('id',$pid)->find();
- if($info){
- $title = $title?$info['name'].'、'.$title:$info['name'];
- if($info['parent_id'] > 0){
- return $this->getDepParent($info['parent_id'],$title);
- }else{
- return $title;
- }
- }else{
- return $title;
- }
- }
- }
|