1
0

EmployeeLogic.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace app\logics;
  3. use app\model\Grant;
  4. use app\model\Org;
  5. use app\model\User;
  6. use app\model\Employee;
  7. class EmployeeLogic
  8. {
  9. /*
  10. * addemployee 添加员工
  11. * @params
  12. */
  13. public static function addemployee($data, &$msg)
  14. {
  15. $token = request()->token;
  16. $user = User::where([['id', '=', $token['uid']], ['root_id', '=', $token['root_org']]])->find();
  17. if (empty($user->phone)) {
  18. $msg = '请授权手机号信息';
  19. return false;
  20. }
  21. $employee_obj = (new Employee())->where([['uid', '=', $user->id], ['root_id', '=', $user->root_id], ['state', 'in', ['在职', '待审核']]])->findOrEmpty();
  22. if (!$employee_obj->isEmpty()) {
  23. $msg = '已邀请,无需重复提交';
  24. return false;
  25. }
  26. // 根据用户手机号同步员工信息
  27. $employee_obj = (new Employee())->where(['phone' => cypherphone($user->phone), 'root_id' => $user->root_id])->order('id desc')->findOrEmpty();
  28. $saveData = [
  29. 'uid' => $user->id,
  30. 'root_id' => $user->root_id,
  31. 'org_id' => $data['orgid'],
  32. 'role' => $data['is_manager'] == 1 ? '领导' : '员工',
  33. 'is_manager' => $data['is_manager'],
  34. 'name' => $data['name'],
  35. 'state' => '待审核',
  36. 'verified' => 0,
  37. 'recruit' => $data['recruit'],
  38. 'initials' => strtoupper(substr(hanzi2pinyin($data['name']),0,1))
  39. ];
  40. if ($employee_obj->isEmpty()) {
  41. $saveData['name'] = $data['name'];
  42. $saveData['phone'] = $user->phone;
  43. }
  44. $employee_obj->save($saveData);
  45. return $employee_obj;
  46. }
  47. public static function employeelist4resource($orgid, $condition = [], $page = 1, $limit = 10)
  48. {
  49. $theorg = Org::find($orgid);
  50. $orgids = [];
  51. if (!empty($theorg)) {
  52. $orgids = Org::where([['path', 'like', $theorg->path . '%']])->column('id');
  53. }
  54. $condition[] = ['org_id', 'in', $orgids];
  55. $condition[] = ['state', '=', '在职'];
  56. $condition[] = ['uid', '<>', 0];
  57. $list = Employee::where($condition)->with(['org', 'grant', 'user'])->order('org_id asc, is_manager desc')->page($page, $limit)->select()->toArray();
  58. return $list;
  59. }
  60. public static function employeelistfront($orgid, $condition = [], $page = 1, $limit = 10, $state = '在职')
  61. {
  62. $theorg = Org::find($orgid);
  63. $orgids = [];
  64. if (!empty($theorg)) {
  65. $where = [['path', 'like', $theorg->path . '%']];
  66. $orgids = Org::where($where)->column('id');
  67. }
  68. $condition[] = ['org_id', 'in', $orgids];
  69. //$condition[] = ['state', '<>', '离职'];
  70. $condition[] = ['uid', '<>', 0];
  71. $list = Employee::where($condition)->with(['org', 'grant', 'user'])->order('org_id asc, is_manager desc')->page($page, $limit)->select();
  72. $final_list = [];
  73. if (!empty($list)) {
  74. foreach ($list as $emp) {
  75. if ($state == '在职') {
  76. if ($emp->state == '在职' && $emp->verified == 1) {
  77. $final_list[] = $emp;
  78. }
  79. }
  80. if ($state == '待审核') {
  81. if ($emp->state == '待审核' && $emp->verified == 0) {
  82. if ($emp->org_id != $orgid) {
  83. $final_list[] = $emp;
  84. }
  85. if ($emp->org_id == $orgid && $emp->is_manager == 0) {
  86. $final_list[] = $emp;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return $final_list;
  93. }
  94. public static function employeelist($orgid, $condition = [], $page = 1, $limit = 10, $state = '在职')
  95. {
  96. $theorg = Org::find($orgid);
  97. $orgids = [];
  98. if (!empty($theorg)) {
  99. $o_where = $state == '在职' ? [['path', 'like', $theorg->path . '%']] : [['path', 'like', $theorg->path . '%'], ['path', '<>', $theorg->path]];
  100. $orgids = Org::where($o_where)->column('id');
  101. }
  102. $have_state = false;
  103. foreach ($condition as $k => $v) {
  104. if (isset($v[0]) && $v[0] == 'state'){
  105. $have_state = true;
  106. }
  107. }
  108. $condition[] = ['org_id', 'in', $orgids];
  109. $condition[] = ['uid', '<>', 0];
  110. if (!$have_state){
  111. $condition[] = ['state', '=', $state];
  112. }
  113. $list = Employee::where($condition)->with(['org', 'grant', 'user'])->order('org_id asc, is_manager desc')->page($page, $limit)->select()->toArray();
  114. return $list;
  115. }
  116. public static function count($orgid, $state = "在职", $keyword = 'null')
  117. {
  118. $request = request();
  119. // $states = $request->controller()=='Employee'&&$state=='待审核' ? '在职' : $state;
  120. $states = app('http')->getName() != 'api' && $state == '待审核' ? '在职' : $state;
  121. if ($orgid) {
  122. $theorg = Org::find($orgid);
  123. $orgids = [];
  124. if (!empty($theorg)) {
  125. $o_where = $states == '在职' ? [['path', 'like', $theorg->path . '%']] : [['path', 'like', $theorg->path . '%'], ['path', '<>', $theorg->path]];
  126. $orgids = Org::where($o_where)->column('id');
  127. }
  128. if (!empty($keyword)) {
  129. $count = Employee::where([['org_id', 'in', $orgids], ['state', '=', $state], ['uid', '<>', 0], ['name', 'like', '%' . $keyword . '%']])->count();
  130. } else {
  131. $count = Employee::where([['org_id', 'in', $orgids], ['state', '=', $state], ['uid', '<>', 0]])->count();
  132. }
  133. } else {
  134. if (!empty($keyword)) {
  135. $count = Employee::where(['state' => ['eq', $state], 'name' => ['like', '%' . $keyword . '%']])->count();
  136. } else {
  137. $count = Employee::where(['state' => ['eq', $state]])->count();
  138. }
  139. }
  140. return $count;
  141. }
  142. public static function linkgrant($employeeid, $grantid)
  143. {
  144. return Employee::where('id', $employeeid)->update(['grant_id' => $grantid]);
  145. }
  146. public static function list($condition = [], $field = '*')
  147. {
  148. return Employee::where($condition)->field($field)->select();
  149. }
  150. public static function getEmployee($openid)
  151. {
  152. // if ($openid == 'adminopenid') {
  153. // $employee = Employee::find(1);
  154. // } else {
  155. $currentUser = User::where('openid', $openid)->find();
  156. if (!$currentUser) {
  157. return false;
  158. }
  159. $employee = Employee::where('uid', $currentUser->id)->find();
  160. // }
  161. return $employee;
  162. }
  163. public static function delete($id)
  164. {
  165. $openid = session('openid', '', 'live');
  166. $currentUser = User::where('openid', $openid)->find();
  167. if (!$currentUser) {
  168. return false;
  169. }
  170. $employee = Employee::where('uid', $currentUser->id)->find();
  171. if ($id == $employee->id) {
  172. return false;
  173. } else {
  174. return Employee::where(['id' => $id])->delete();
  175. }
  176. }
  177. public static function getEmpInSameNode($handled_id)
  178. {
  179. $handled_emp = Employee::where('id', $handled_id)->find();
  180. $theorg = Org::find($handled_emp->org_id);
  181. if ($theorg->pid == 0) {
  182. $parent_org = $theorg;
  183. } else {
  184. $parent_org = Org::find($theorg->pid);
  185. }
  186. $orgids = Org::where([['path', 'like', $parent_org->path . '%']])->column('id');
  187. $target_emp_list = Employee::where([['org_id', 'in', $orgids], ['id', '<>', $handled_id], ['state', '=', '在职']])->column('id', 'name');
  188. return $target_emp_list;
  189. }
  190. }