Dep.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Dep extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'name' => input('name','','trim'),
  13. 'level' => input('level','','trim'),
  14. 'content' => input('content','','trim'),
  15. 'org_id' => input('orgId/d',0),
  16. 'parent_id' => input('parentId/d',0),
  17. ];
  18. $result = validate('Dep')->check($data,[],'');
  19. if(true !== $result){
  20. HelpHander::error(validate('Dep')->getError());
  21. }
  22. $id = $data['id'];
  23. unset($data['id']);
  24. if($id > 0){
  25. unset($data['org_id']);
  26. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  27. }else{
  28. $maxcode = Db::name('dep')->order('id desc')->value('code');
  29. if($maxcode){
  30. $data['code'] = $maxcode + 1;
  31. }else{
  32. $data['code'] = '001';
  33. }
  34. $ret = $this->allowField(true)->save($data);
  35. }
  36. if(!$ret){
  37. HelpHander::error('操作失败');
  38. }
  39. return true;
  40. }
  41. public function info($id){
  42. $info = $this->where('id',$id)->where('del',0)->find();
  43. if(!$info){
  44. HelpHander::error('数据不存在');
  45. }
  46. return $info->toArray();
  47. }
  48. public function lists($page,$size){
  49. $lists = $this
  50. ->where('del',0)
  51. ->page($page,$size)
  52. ->order('sorts asc,id asc')
  53. ->select();
  54. return $lists?$lists->toArray():[];
  55. }
  56. // 启用
  57. public function toEnableDep($orgId){
  58. $info = $this->where('id',$orgId)->where('del',0)->find();
  59. if(!$info){
  60. HelpHander::error('组织不存在');
  61. }
  62. if($info['enable'] == 1){
  63. HelpHander::error('启用中,无需重复启用');
  64. }
  65. $ret = $this->where('id',$orgId)->setField('enable',1);
  66. if(!$ret){
  67. HelpHander::error('操作失败');
  68. }
  69. return true;
  70. }
  71. // 作废部门(包括所有下级部门和岗位)
  72. public function disableDep($depId){
  73. $info = $this->where('id',$depId)->where('del',0)->find();
  74. if(!$info){
  75. HelpHander::error('部门不存在');
  76. }
  77. if($info['enable'] == 0){
  78. HelpHander::error('部门已禁用');
  79. }
  80. $deps = $this->getDepChildren($depId);
  81. $deps[] = $depId;
  82. $this->startTrans();
  83. try{
  84. Db::name('dep')->where('id','in',$deps)->setField('enable',0);
  85. Db::name('job')->where('dep_id','in',$deps)->setField('enable',0);
  86. $this->commit();
  87. }catch (Exception $e){
  88. $this->rollback();
  89. HelpHander::error('操作失败');
  90. }
  91. return true;
  92. }
  93. /**
  94. * 获取部门及下级id集合
  95. * @param $ids
  96. * @param array $result
  97. * @return array
  98. */
  99. public function getDepChildren($ids,&$result = []){
  100. $children = Db::name('dep')
  101. ->where('del',0)
  102. ->where('enable',1)
  103. ->where('parent_id','in',$ids)
  104. ->column('id');
  105. if($children){
  106. $result = array_merge($result,$children);
  107. $this->getDepChildren($children,$result);
  108. }
  109. return $result;
  110. }
  111. // 根据部门获取部门及下级部门下的所有岗位id
  112. public function getJobByDep($depId){
  113. $deps = $this->getDepChildren([$depId]);
  114. $deps = $deps?$deps:[];
  115. $deps[] = $depId;
  116. $jobs = Db::name('job')
  117. ->where('del',0)
  118. ->where('enable',1)
  119. ->where('dep_id','in',$deps)
  120. ->column('id');
  121. return $jobs?$jobs:[];
  122. }
  123. // 指定组织下的部门列表(树状结构)
  124. public function queryDepTreeByOrg($orgId){
  125. $orgs = Db::name('org')
  126. ->where('del',0)
  127. ->where('enable',1)
  128. ->where('id',$orgId)
  129. ->order('id asc')
  130. ->find();
  131. if(!$orgs){
  132. return [];
  133. }
  134. $orgs['type'] = 1;
  135. $orgs['uniqueFlag'] = $orgs['id'].'1';
  136. // 获取所有部门
  137. $deps = Db::name('dep')
  138. ->where('del',0)
  139. ->where('enable',1)
  140. ->where('org_id',$orgId)
  141. ->order('sorts asc,id asc')
  142. ->field('id,name,code,level,content,parent_id')
  143. ->select();
  144. // 添加唯一值
  145. foreach ($deps as $k=>$v){
  146. $deps[$k]['type'] = 2;
  147. $deps[$k]['uniqueFlag'] = $v['id'].'2';
  148. }
  149. $ndeps = list_to_tree($deps, 'parent_id', $child = 'childList', 0);
  150. $orgs['childList'] = $ndeps;
  151. return [$orgs];
  152. }
  153. // 指定组织下的部门列表(树状结构)
  154. public function queryDepTree($orgId){
  155. // 获取所有部门
  156. $deps = Db::name('dep')
  157. ->where('del',0)
  158. ->where('enable',1)
  159. ->where('org_id',$orgId)
  160. ->order('sorts asc,id asc')
  161. ->field('id,name,parent_id')
  162. ->select();
  163. // 添加唯一值
  164. foreach ($deps as $k=>$v){
  165. $deps[$k]['type'] = 0;
  166. $deps[$k]['uniqueId'] = $v['id'].'0bg';
  167. }
  168. $ndeps = list_to_tree($deps, 'parent_id', $child = 'depAndJobDtos', 0);
  169. $ndeps = array_delete_char($ndeps,'parent_id','depAndJobDtos');
  170. return $ndeps;
  171. }
  172. // 部门下的部门/组织下的部门 (直属部门)
  173. public function queryDepListByOrg($id,$isOrg){
  174. if($isOrg == 1){ // 部门
  175. $lists = $this
  176. ->where('del',0)
  177. ->where('parent_id',$id)
  178. ->order('sorts asc,id asc')
  179. ->select();
  180. } else { // 组织
  181. $lists = $this
  182. ->where('del',0)
  183. ->where('parent_id',0)
  184. ->where('org_id',$id)
  185. ->order('sorts asc,id asc')
  186. ->select();
  187. }
  188. return $lists?$lists->toArray():[];
  189. }
  190. //部门岗位人员树/部门岗位树
  191. public function queryDepAndJobTree($orgId,$isDep){
  192. // 获取所有部门
  193. $deps = Db::name('dep')
  194. ->where('del',0)
  195. ->where('enable',1)
  196. ->where('org_id',$orgId)
  197. ->order('sorts asc,id asc')
  198. ->field('id,name,parent_id')
  199. ->select();
  200. // 添加唯一值
  201. foreach ($deps as $k=>$v){
  202. $deps[$k]['type'] = 0;
  203. $deps[$k]['uniqueId'] = $v['id'].'0bg';
  204. $jobs = Db::name('job')
  205. ->where('del',0)
  206. ->where('enable',1)
  207. ->where('dep_id',$v['id'])
  208. ->order('id asc')
  209. ->field('id,name,code')
  210. ->select();
  211. $jobs = $jobs?$jobs:[];
  212. foreach ($jobs as $kk=>$vv){
  213. $jobs[$kk]['type'] = 1;
  214. $jobs[$kk]['uniqueId'] = $vv['id'].'1gw';
  215. if($isDep == 1){
  216. $jobs[$kk]['name'] = '['.$vv['code'].']'.$vv['name'];
  217. }
  218. unset($jobs[$kk]['code']);
  219. if($isDep == 0){ // 查询人
  220. $user = Db::name('user_job')
  221. ->alias('uj')
  222. ->join('user u','u.id = uj.user_id')
  223. ->join('user_info ui','ui.user_id = u.id')
  224. ->where('uj.job_id',$vv['id'])
  225. ->where('u.del',0)
  226. ->where('u.enable',1)
  227. ->order('u.sorts asc,u.id asc')
  228. ->field('u.id,ui.name')
  229. ->select();
  230. $user = $user?$user:[];
  231. foreach ($user as $kkk=>$vvv){
  232. $user[$kkk]['type'] = 2;
  233. $user[$kkk]['uniqueId'] = $vvv['id'].'2ry';
  234. $user[$kkk]['depAndJobDtos'] = [];
  235. // $user[$kkk]['depId'] = $v['id'];
  236. // $user[$kkk]['depName'] = $v['name'];
  237. }
  238. $jobs[$kk]['depAndJobDtos'] = $user;
  239. }else{
  240. $jobs[$kk]['depAndJobDtos'] = [];
  241. }
  242. }
  243. $deps[$k]['depAndJobDtos'] = $jobs;
  244. }
  245. $ndeps = list_to_tree($deps, 'parent_id', $child = 'depAndJobDtos', 0);
  246. $ndeps = array_delete_char($ndeps,'parent_id','depAndJobDtos');
  247. return $ndeps;
  248. }
  249. /**
  250. * 获取用户父级部门id集合
  251. * @param $ids
  252. * @param array $result
  253. * @return array
  254. */
  255. public function getDepAndParent($ids,&$result = []){
  256. $parents = Db::name('dep')
  257. ->where('del',0)
  258. ->where('enable',1)
  259. ->where('parent_id','<>',0)
  260. ->where('id','in',$ids)
  261. ->column('parent_id');
  262. if($parents){
  263. $result = array_merge($result,$parents);
  264. $this->getDepAndParent($parents,$result);
  265. }
  266. return $result;
  267. }
  268. // 获取用户部门及上级id集合
  269. public function getUserDepIds($userId,$orgId){
  270. $depids = Db::name('user_job')
  271. ->alias('uj')
  272. ->join('job j','j.id = uj.job_id')
  273. ->where('uj.user_id',$userId)
  274. ->where('j.org_id',$orgId)
  275. ->where('j.del',0)
  276. ->where('j.enable',1)
  277. ->column('j.dep_id');
  278. $depids = $depids?$depids:[];
  279. if($depids){
  280. $parents = $this->getDepAndParent($depids);
  281. $depids = array_merge($depids,$parents);
  282. }
  283. return $depids?$depids:[];
  284. }
  285. // 获取用户部门
  286. public function getUserDep($userId,$orgId,$type=0){ // type 0=可用的部门 1=全部部门(包括删除禁用的)
  287. $map[] = ['uj.user_id','=',$userId];
  288. $map[] = ['j.org_id','=',$orgId];
  289. if($type == 0){
  290. $map[] = ['j.del','=',0];
  291. $map[] = ['j.enable','=',1];
  292. }
  293. $deps = Db::name('user_job')
  294. ->alias('uj')
  295. ->join('job j','j.id = uj.job_id')
  296. ->join('dep d','d.id = j.dep_id')
  297. ->where($map)
  298. ->field('d.id,d.name')
  299. ->select();
  300. return $deps?$deps:[];
  301. }
  302. // 根据用户和组织ID获取部门列表 app端使用
  303. public function queryUserListByOrg($userId,$orgId){
  304. $deps = Db::name('user_job')
  305. ->alias('uj')
  306. ->join('job j','j.id = uj.job_id')
  307. ->join('dep d','d.id = j.dep_id')
  308. ->join('org o','o.id = j.org_id')
  309. ->where('uj.user_id',$userId)
  310. ->where('j.org_id',$orgId)
  311. ->where('j.del',0)
  312. ->where('j.enable',1)
  313. ->field('j.org_id,o.name as orgName,d.name as depName,j.dep_id,uj.user_id')
  314. ->select();
  315. return $deps?$deps:[];
  316. }
  317. // 获取部门的所有上级部门
  318. public function getDepParent($pid,$title=''){
  319. $info = Db::name('dep')->where('id',$pid)->find();
  320. if($info){
  321. $title = $title?$info['name'].'、'.$title:$info['name'];
  322. if($info['parent_id'] > 0){
  323. return $this->getDepParent($info['parent_id'],$title);
  324. }else{
  325. return $title;
  326. }
  327. }else{
  328. return $title;
  329. }
  330. }
  331. }