OrgLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace app\logics;
  3. use app\model\Customer;
  4. use app\model\CustomerClue;
  5. use app\model\Employee;
  6. use app\model\Footprints;
  7. use app\model\Org;
  8. use app\model\Pool;
  9. use think\facade\Db;
  10. class OrgLogic
  11. {
  12. /*
  13. * addnode 添加组织结构节点
  14. * @params
  15. * $name string 节点名称
  16. * $level integer 层级数
  17. * $pid integer 上一级id,即父id
  18. */
  19. public static function addnode($name = '', $level = 1, $pid = 0)
  20. {
  21. $org_obj = new Org();
  22. $org_obj->save([
  23. 'name' => $name,
  24. 'level' => $level,
  25. 'pid' => $pid
  26. ]);
  27. $nowid = $org_obj->id;
  28. if ($pid > 0) {
  29. $parentnode = Org::find($pid);
  30. $nowpath = $parentnode->path . $nowid . '-';
  31. $nowinfo = $parentnode->info . '/' . $name;
  32. } else {
  33. $nowpath = '1-';
  34. $nowinfo = $name;
  35. }
  36. if ($org_obj->save(['path' => $nowpath, 'info' => $nowinfo])) {
  37. return $org_obj;
  38. } else {
  39. return false;
  40. }
  41. }
  42. /**
  43. * disnode 关闭节点。如果有子节点则全部关闭
  44. *
  45. * @params
  46. * $id integer 要关闭的组织结构节点
  47. */
  48. public static function disnode($id, &$msg)
  49. {
  50. $tragetnode = Org::find($id);
  51. $thepath = $tragetnode->path;
  52. // 查找结点下是否有员工
  53. $orgids = Org::where([['path', 'like', $thepath . '%']])->column('id');
  54. // 如果有子部门,则先删除子部门再进行该部门删除
  55. if (count($orgids) > 1) {
  56. $msg = '删除失败,尚有子部门存在';
  57. return 1;
  58. }
  59. $had = Employee::where([['org_id', 'in', $orgids], ['state', 'in', ['在职', '待审核']]])->count();
  60. if ($had) {
  61. $msg = '删除失败,尚有人员在职或待审核';
  62. return 1;
  63. }
  64. // 检测是否有资源
  65. $customerResource = Customer::where(['org_id' => $id])->count();
  66. $clueResource = CustomerClue::where(['org_id' => $id])->count();
  67. $footprintResource = Footprints::where(['org_id' => $id])->count();
  68. if ($customerResource !== 0 || $clueResource !== 0 || $footprintResource !== 0) {
  69. $msg = '该部门尚有客户资源,请选择接收部门';
  70. return 2;
  71. }
  72. $allsubnodeschangenum = Org::where([
  73. ['path', 'like', $thepath . '%']
  74. ])->delete();
  75. if ($allsubnodeschangenum > 0) {
  76. return true;
  77. } else {
  78. $msg = '删除失败';
  79. return 1;
  80. }
  81. }
  82. public static function level()
  83. {
  84. $where[] = ['status', '=', 1];
  85. $levelArr = Org::where($where)->column('level');
  86. return array_unique($levelArr);
  87. }
  88. public static function levelnodes($level)
  89. {
  90. $where[] = ['status', '=', 1];
  91. $where[] = ['level', '=', $level];
  92. $levelArr = Org::where($where)->select()->toArray();
  93. foreach ($levelArr as &$item) {
  94. $item['haspool'] = false;
  95. $item['poolname'] = '';
  96. $poolobj = Pool::where('org_id', $item['id'])->find();
  97. if ($poolobj) {
  98. $item['haspool'] = true;
  99. $item['poolname'] = $poolobj->name;
  100. }
  101. }
  102. return $levelArr;
  103. }
  104. public static function subnodes($id)
  105. {
  106. $theorg = Org::find($id);
  107. $suborgs = [];
  108. if ($theorg) {
  109. $suborgs = Org::where([['path', 'like', $theorg->path . '%'], ['status', '=', 1]])->select()->toArray();
  110. }
  111. return $suborgs;
  112. }
  113. public static function subfirstlevelnodes($id)
  114. {
  115. $suborgs = Org::where([['pid', '=', $id], ['status', '=', 1]])->select()->toArray();
  116. return $suborgs;
  117. }
  118. public static function subfirstlevelnodeswithempnum($id)
  119. {
  120. $suborgs = Org::where([['pid', '=', $id], ['status', '=', 1]])->select()->toArray();
  121. foreach ($suborgs as &$item) {
  122. $orgids = orgSubIds($item['id']);
  123. $allempnum = Employee::where([['org_id', 'in', $orgids],['state','=','在职']])->count();
  124. $item['allempnum'] = $allempnum;
  125. }
  126. //$theorg = Org::find($id);
  127. //$suborgs = [];
  128. //if ($theorg) {
  129. // $suborgs = Org::where([['path', 'like', $theorg->path . '%'],['status','=',1]])->with('employee')->select()->toArray();
  130. // foreach($suborgs as &$item){
  131. // $item['empnum'] = count($item['employee']);
  132. // }
  133. //}
  134. return $suborgs;
  135. }
  136. public static function mystruc($orgids)
  137. {
  138. $where = [
  139. ['status', '=', 1],
  140. ['id', 'in', $orgids]
  141. ];
  142. $allnodes = Org::where($where)->order('level desc, id asc')->select();
  143. self::thestruclooporigin($allnodes, $arr);
  144. return [$arr];
  145. //return json_encode([$arr]);
  146. }
  147. public static function struc($id)
  148. {
  149. $where = [
  150. ['path', 'like', $id . '-%'],
  151. ['status', '=', 1]
  152. ];
  153. $allnodes = Org::where($where)->order('level desc, id asc')->select();
  154. //查询个部门人数
  155. $count = Employee::where([['root_id','=',$id],['state','like','%在职%'], ['uid', '<>', '']])->group('org_id')->column('count(*) count','org_id');
  156. foreach ($allnodes as $k => $v) {
  157. $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0;//本部门人数
  158. $allnodes[$k]['all_count'] = 0;//包含子部门总数
  159. foreach ($allnodes as $k2 => $v2) {
  160. if (strpos($v2['path'],$v['path']) !== false) {
  161. $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']]+$allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
  162. }
  163. }
  164. // $allnodes[$k]['name'] = $allnodes[$k]['name'].' ('.$allnodes[$k]['all_count'].')';
  165. }
  166. self::thestruclooporigin($allnodes, $arr);
  167. return json_encode([$arr]);
  168. }
  169. private static function thestruclooporigin($allnodes, &$arr)
  170. {
  171. $length = count($allnodes);
  172. for ($i = 0; $i < $length; $i++) {
  173. $item = [
  174. 'title' => $allnodes[$i]->name,
  175. 'spread' => true,
  176. 'id' => $allnodes[$i]->id,
  177. 'level' => $allnodes[$i]->level,
  178. 'pid' => $allnodes[$i]->pid,
  179. 'info' => $allnodes[$i]->info,
  180. 'all_count' => $allnodes[$i]->all_count,
  181. 'org_type' => $allnodes[$i]->org_type
  182. ];
  183. if (isset($arr[$item['id']])) {
  184. $item['children'] = $arr[$item['id']];
  185. unset($arr[$item['id']]);
  186. } else {
  187. $item['children'] = [];
  188. }
  189. if ($i !== $length - 1) {
  190. $arr[$item['pid']][] = $item;
  191. } else {
  192. $arr = $item;
  193. }
  194. }
  195. }
  196. }