Org.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Org 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. 'account' => input('account','','trim'),
  16. 'parent_id' => input('parentId/d',0),
  17. ];
  18. if($data['id'] > 0){
  19. $result = validate('Org')->check($data,[],'edit');
  20. }else{
  21. $result = validate('Org')->check($data,[],'add');
  22. }
  23. if(true !== $result){
  24. HelpHander::error(validate('Org')->getError());
  25. }
  26. $id = $data['id'];
  27. $account = $data['account'];
  28. unset($data['id']);
  29. unset($data['account']);
  30. $this->startTrans();
  31. try{
  32. if($id > 0){
  33. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  34. if(!$ret){
  35. HelpHander::error('操作失败');
  36. }
  37. }else{
  38. // 获取机构代码
  39. $pcode = Db::name('org')->where('id',$data['parent_id'])->value('code');
  40. $maxcode = Db::name('org')->where('parent_id',$data['parent_id'])->order('id desc')->value('code');
  41. if($maxcode){
  42. $data['code'] = $maxcode + 1;
  43. }else{
  44. $data['code'] = $pcode.'10';
  45. }
  46. $ret = $this->allowField(true)->save($data);
  47. if(!$ret){
  48. \exception('操作失败');
  49. }
  50. // 创建默认角色
  51. $roles = model('Roles')->addInitRoles($this->id);
  52. if($roles <= 0){
  53. \exception('操作失败');
  54. }
  55. // 创建用户
  56. $res = model('User')->addUserOrg($account,$roles);
  57. if(!$res){
  58. \exception('操作失败');
  59. }
  60. }
  61. $this->commit();
  62. }catch (Exception $e){
  63. $this->rollback();
  64. HelpHander::error('操作失败');
  65. }
  66. return true;
  67. }
  68. public function info($id){
  69. $info = $this->where('id',$id)->where('del',0)->find();
  70. if(!$info){
  71. HelpHander::error('数据不存在');
  72. }
  73. // 获取上级组织
  74. $pername = '';
  75. if($info['parent_id'] > 0){
  76. $pername = Db::name('org')->where('id',$info['parent_id'])->value('name');
  77. }
  78. $info['pername'] = $pername;
  79. return $info->toArray();
  80. }
  81. // 获取组织列表,只取未删除和未禁用的组织
  82. public function lists($page,$size){
  83. $lists = $this
  84. ->where('del',0)
  85. ->where('enable',1)
  86. ->page($page,$size)
  87. ->order('id asc')
  88. ->select();
  89. return $lists?$lists->toArray():[];
  90. }
  91. // 启用组织
  92. public function toEnableOrg($orgId){
  93. $info = $this->where('id',$orgId)->where('del',0)->find();
  94. if(!$info){
  95. HelpHander::error('组织不存在');
  96. }
  97. if($info['enable'] == 1){
  98. HelpHander::error('启用中,无需重复启用');
  99. }
  100. $ret = $this->where('id',$orgId)->setField('enable',1);
  101. if(!$ret){
  102. HelpHander::error('操作失败');
  103. }
  104. return true;
  105. }
  106. // 作废组织(包括所有下级组织,部门和岗位)
  107. public function disableOrg($orgId){
  108. $info = $this->where('id',$orgId)->where('del',0)->find();
  109. if(!$info){
  110. HelpHander::error('组织不存在');
  111. }
  112. if($info['enable'] == 0){
  113. HelpHander::error('组织已禁用');
  114. }
  115. $orgs = $this->getOrgChildren($orgId);
  116. $orgs[] = $orgId;
  117. $this->startTrans();
  118. try{
  119. Db::name('org')->where('id','in',$orgs)->setField('enable',0);
  120. Db::name('dep')->where('org_id','in',$orgs)->setField('enable',0);
  121. Db::name('job')->where('org_id','in',$orgs)->setField('enable',0);
  122. $this->commit();
  123. }catch (Exception $e){
  124. $this->rollback();
  125. HelpHander::error('操作失败');
  126. }
  127. return true;
  128. }
  129. // 显示直属下级机构列表
  130. public function selByOrgId($id){
  131. $lists = Db::name('org')
  132. ->alias('o')
  133. ->join('org op','o.parent_id = op.id')
  134. ->where('o.del',0)
  135. ->where('o.parent_id',$id)
  136. ->order('o.id asc')
  137. ->field('op.id,op.name,o.name as nname,o.id as nid,o.parent_id as nparent_id,o.level as nlevel,o.content as ncontent,o.enable as nenable')
  138. ->select();
  139. return $lists?$lists:[];
  140. }
  141. // 获取组织的所有下级组织id(启用状态的)
  142. public function getOrgChildren($ids,&$result = []){
  143. $children = Db::name('org')
  144. ->where('del',0)
  145. ->where('enable',1)
  146. ->where('parent_id','in',$ids)
  147. ->column('id');
  148. if($children){
  149. $result = array_merge($result,$children);
  150. $this->getOrgChildren($children,$result);
  151. }
  152. return $result;
  153. }
  154. // 根据用户ID获取组织列表
  155. public function queryOrgListByUserId($userId){
  156. $list = Db::name('user_roles')
  157. ->alias('ur')
  158. ->join('roles r','r.id = ur.roles_id')
  159. ->join('org o','o.id = r.org_id')
  160. ->where('r.type',3)
  161. ->where('ur.user_id',$userId)
  162. ->field('o.id,o.name')
  163. ->select();
  164. return $list?$list:[];
  165. }
  166. }