StaffService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\common\services;
  11. use app\backend\modules\member\models\Member;
  12. use business\common\models\Department;
  13. use business\common\models\DepartmentStaff;
  14. use business\common\models\Staff;
  15. use business\common\models\StaffTicket;
  16. use Exception;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Redis;
  19. use Yunshop\WorkWechat\common\models\Employee;
  20. class StaffService
  21. {
  22. /*
  23. * 校验成员管理权限
  24. */
  25. public static function checkManageRightByKey($function, $staff_id, $department_id_list)
  26. {
  27. $function_arr = [
  28. 'updateStaff' => '编辑',
  29. 'createStaff' => '创建',
  30. 'deleteStaff' => '禁用'
  31. ];
  32. $right = BusinessService::checkBusinessRight();
  33. if ($right['identity'] > 1) {
  34. return ['result' => 1, 'msg' => '管理员权限无需校验'];
  35. }
  36. if (!$right_department_id = $right['route_department_id'][$function]) {
  37. return ['result' => 0, 'msg' => '无权' . $function_arr[$function] . '员工'];
  38. }
  39. $isset_department_id = DepartmentStaff::where('staff_id', $staff_id)->pluck('department_id')->toArray();
  40. $department_list = array_column(Department::business()->get()->toArray(), null, 'id');
  41. if ($function == 'updateStaff' || $function == 'deleteStaff') {
  42. if (empty(array_intersect($right_department_id, $isset_department_id))) {
  43. return ['result' => 0, 'msg' => '无权' . $function_arr[$function] . '当前员工'];
  44. }
  45. }
  46. if (($function == 'updateStaff' && request()->is_edit == 1) || $function == 'createStaff') {
  47. $add_department_id = array_diff($department_id_list, $isset_department_id);
  48. $delete_department_id = array_diff($isset_department_id, $department_id_list);
  49. if ($add_diff_id = array_diff($add_department_id, $right_department_id)) {
  50. foreach ($add_diff_id as $v) {
  51. if (!$department_list[$v]) {
  52. continue;
  53. } else {
  54. return ['result' => 0, 'msg' => '无权将当前员工加入' . $department_list[$v]['name']];
  55. }
  56. }
  57. }
  58. if ($delete_diff_id = array_diff($delete_department_id, $right_department_id)) {
  59. foreach ($delete_diff_id as $v) {
  60. if (!$department_list[$v]) {
  61. continue;
  62. } else {
  63. return ['result' => 0, 'msg' => '无权将当前员工移出' . $department_list[$v]['name']];
  64. }
  65. }
  66. }
  67. }
  68. return ['result' => 1, 'msg' => '校验通过'];
  69. }
  70. /*
  71. * 禁用/解禁部门成员
  72. */
  73. public static function deleteStaff($staff_id, $disabled = 1)
  74. {
  75. try {
  76. if (!$staff_id) {
  77. throw new Exception('请选择要禁用的员工');
  78. }
  79. $where[] = is_array($staff_id) ? [function ($query) use ($staff_id) {
  80. $query->whereIn('id', $staff_id);
  81. }] : ['id', $staff_id];
  82. $count = is_array($staff_id) ? count($staff_id) : 1;
  83. $staff = Staff::business()->where($where)->get();
  84. $clean_member_id = [];
  85. if ($staff->count() != $count) {
  86. throw new Exception('员工不存在');
  87. }
  88. DB::beginTransaction();
  89. $staff->each(function ($v) use ($disabled, &$clean_member_id) {
  90. if ($v->uid) $clean_member_id[] = $v->uid;
  91. Staff::where('id', $v->id)->update(['disabled' => $disabled]);
  92. });
  93. } catch (Exception $e) {
  94. DB::rollBack();
  95. return self::returnArr(0, $e->getMessage());
  96. }
  97. DB::commit();
  98. if ($clean_member_id) {
  99. BusinessService::flush(0, $clean_member_id); //清除会员缓存
  100. }
  101. return self::returnArr(1, '禁用成功');
  102. }
  103. /*
  104. * 设置部门领导
  105. */
  106. public static function setStaffLeader($department_id, $staff_id = [])
  107. {
  108. try {
  109. if (!$department = Department::business()->find($department_id)) {
  110. throw new Exception('部门不存在');
  111. }
  112. $where = [
  113. ['department_id', $department->id],
  114. ['is_leader', 1]
  115. ];
  116. if ($staff_id) {
  117. $staff_arr = DepartmentStaff::business()->where('department_id', $department->id)
  118. ->whereIn('staff_id', $staff_id)
  119. ->with('hasOneStaff')
  120. ->groupBy('staff_id', 'department_id')
  121. ->get()->toArray();
  122. if (count($staff_arr) != count($staff_id)) {
  123. throw new Exception('存在员工未加入此部门');
  124. }
  125. $where[] = [function ($query) use ($staff_id) {
  126. $query->whereNotIn('staff_id', $staff_id);
  127. }];
  128. }
  129. $msg = [];
  130. if ($department->wechat_department_id && SettingService::EnabledQyWx()) {
  131. $delete_staff = DepartmentStaff::business()->where($where)->with('hasOneStaff')->get()->toArray();
  132. if ($delete_staff) {
  133. foreach ($delete_staff as $v) {
  134. if (empty($v['has_one_staff']['status'])) {
  135. continue;
  136. }
  137. DB::beginTransaction();
  138. DepartmentStaff::where('id', $v['id'])->update(['is_leader' => 0]);
  139. $res = self::pushStaff($v['staff_id']);
  140. if (!$res['result']) {
  141. DB::rollBack();
  142. $msg[] = '员工' . $v['has_one_staff']['name'] . '企业微信推送,' . $res['msg'];
  143. continue;
  144. }
  145. DB::commit();
  146. }
  147. }
  148. foreach ($staff_arr as $v) {
  149. if (empty($v['has_one_staff']['status'])) {
  150. $msg[] = "员工{$v['has_one_staff']['name']}未与企业微信同步";
  151. continue;
  152. }
  153. DB::beginTransaction();
  154. DepartmentStaff::where('id', $v['id'])->update(['is_leader' => 1]);
  155. $res = self::pushStaff($v['staff_id']);
  156. if (!$res['result']) {
  157. DB::rollBack();
  158. $msg[] = '员工' . $v['has_one_staff']['name'] . '企业微信推送,' . $res['msg'];
  159. continue;
  160. }
  161. DB::commit();
  162. }
  163. if ($msg) {
  164. throw new Exception(implode(',', $msg));
  165. }
  166. } else {
  167. DepartmentStaff::business()->where($where)->update(['is_leader' => 0]);
  168. DepartmentStaff::business()->whereIn('staff_id', $staff_id)->where('department_id', $department->id)->update(['is_leader' => 1]);
  169. }
  170. } catch (Exception $e) {
  171. return self::returnArr(0, $e->getMessage());
  172. }
  173. BusinessService::flush(SettingService::getBusinessId()); //清除企业缓存
  174. return self::returnArr(1, '设置部门领导成功');
  175. }
  176. /*
  177. * 创建/编辑 员工
  178. */
  179. public static function changeStaff($data)
  180. {
  181. DB::beginTransaction();
  182. try {
  183. $business_id = SettingService::getBusinessId();
  184. if (!$data->mobile || !$data->name) {
  185. throw new Exception('手机、姓名不能为空');
  186. }
  187. $staff = null;
  188. if ($data->id && (!$staff = Staff::business()->find($data->id))) {
  189. throw new Exception('员工不存在');
  190. }
  191. if ($data->uid) {
  192. if (!$member = Member::uniacid()->find($data->uid)) {
  193. throw new Exception('系统会员不存在');
  194. }
  195. if (!$member->mobile) {
  196. throw new Exception('系统会员未绑定手机号');
  197. }
  198. if ($data->mobile != $member->mobile) {
  199. throw new Exception('系统会员手机号与员工手机号不一致');
  200. }
  201. }
  202. /*判断是否有手机号或绑定系统会员冲突的员工*/
  203. $where = [
  204. ['business_id', $business_id],
  205. [function ($query) use ($member) {
  206. $query->where('uid', $member->uid)->orWhere('mobile', $member->mobile);
  207. }],
  208. ];
  209. if ($staff) {
  210. $where[] = ['id', '<>', $staff->id];
  211. }
  212. $isset_staff = Staff::uniacid()->where($where)->first();
  213. if ($isset_staff) {
  214. throw new Exception($isset_staff->uid == $member->uid ? '系统会员已经绑定其他员工' : '系统会员手机号与已存在员工重复');
  215. }
  216. /*判断是否有手机号或绑定系统会员冲突的员工*/
  217. $name = trim($data->name);
  218. $user_id = $staff ? $staff->user_id : self::overTrue($name, 21) . '_' . time();
  219. $update_data = [
  220. 'user_id' => $user_id,
  221. 'name' => $name ?: '',
  222. 'uid' => $member->uid,
  223. 'mobile' => $member->mobile ?: '',
  224. 'position' => trim($data->position) ?: '',
  225. 'gender' => in_array($data->gender, [0, 1, 2]) ?: 0,
  226. 'telephone' => trim($data->telephone) ?: '',
  227. 'email' => trim($data->email) ?: '',
  228. 'avatar' => $data->avatar ?: '',
  229. 'alias' => $data->alias ?: '',
  230. 'address' => $data->address ?: '',
  231. 'open_userid' => $business_id . '_' . $user_id,
  232. ];
  233. if ($staff) {
  234. if (SettingService::EnabledQyWx() && !in_array($staff->status, [0, 4]) && $staff->mobile != $update_data['mobile']) {
  235. throw new Exception('该成员已激活企业微信,需自行修改手机号');
  236. }
  237. $staff->fill($update_data);
  238. $staff->save();
  239. } else {
  240. $staff = Staff::create(array_merge($update_data, [
  241. 'uniacid' => \YunShop::app()->uniacid,
  242. 'business_id' => $business_id,
  243. ]));
  244. }
  245. if (!$department_id = is_array($data->department_id) && $data->department_id ? $data->department_id : []) {
  246. throw new Exception('请选择成员所属部门');
  247. }
  248. $department_list = Department::business()->get();
  249. if ($department_list->whereIn('id', $department_id)->isEmpty()) {
  250. throw new Exception('请选择有效部门');
  251. }
  252. $department_staff_list = DepartmentStaff::uniacid()->where('staff_id', $staff->id)->get()->toArray();
  253. $department_list = $department_list->isNotEmpty() ? array_column($department_list->toArray(), null, 'id') : [];
  254. $department_staff_list = $department_staff_list ? array_column($department_staff_list, null, 'department_id') : [];
  255. foreach ($department_id as $k => $v) {
  256. if (!$this_department = $department_list[$v]) {
  257. throw new Exception('部门不存在');
  258. }
  259. if (!$this_department_staff = $department_staff_list[$this_department['id']]) {
  260. DepartmentStaff::create([
  261. 'uniacid' => \YunShop::app()->uniacid,
  262. 'staff_id' => $staff->id,
  263. 'business_id' => $business_id,
  264. 'department_id' => $this_department['id'],
  265. 'is_leader' => 0,
  266. 'sort' => 1,
  267. ]);
  268. }
  269. }
  270. foreach ($department_staff_list as $v) {
  271. if (!in_array($v['department_id'], $department_id) || !isset($department_list[$v['department_id']])) {
  272. DepartmentStaff::where('id', $v['id'])->delete();
  273. }
  274. }
  275. //企业微信开启 并且 员工不是未关联且被禁用 时,进行同步
  276. if (SettingService::EnabledQyWx() && ($staff->disabled != 1)) {
  277. $push_res = self::pushStaff($staff->id);
  278. if (!$push_res['result']) {
  279. throw new Exception($push_res['msg']);
  280. }
  281. if ($staff->status == 0) {
  282. $staff->status = 4;
  283. $staff->save();
  284. }
  285. }
  286. } catch (Exception $e) {
  287. DB::rollBack();
  288. return self::returnArr(0, $e->getMessage());
  289. }
  290. DB::commit();
  291. if ($staff->uid) {
  292. BusinessService::flush(0, $staff->uid);
  293. }
  294. return self::returnArr(1, '成功', ['staff' => $staff]);
  295. }
  296. /*
  297. * 推送成员信息到企业微信
  298. */
  299. public static function pushStaff($staff_id)
  300. {
  301. try {
  302. if (!SettingService::EnabledQyWx()) {
  303. throw new Exception('未开启企业微信同步');
  304. }
  305. $staff = Staff::business()
  306. ->with(['hasManyDepartmentStaff' => function ($query) {
  307. $query->with('hasOneDepartment');
  308. }])->find($staff_id);
  309. if (!$staff) {
  310. throw new Exception('员工不存在');
  311. }
  312. $department_id_arr = [];
  313. $department_order_arr = [];
  314. $department_leader_arr = [];
  315. if ($staff->hasManyDepartmentStaff->isNotEmpty()) {
  316. $staff->hasManyDepartmentStaff->each(function ($v) use (&$department_id_arr, &$department_order_arr, &$department_leader_arr) {
  317. if ($v->hasOneDepartment->wechat_department_id) {
  318. $department_id_arr[] = $v->hasOneDepartment->wechat_department_id;
  319. $department_order_arr[] = intval($v->order) ?: 1;
  320. $department_leader_arr[] = $v->is_leader ? 1 : 0;
  321. }
  322. });
  323. }
  324. $request_data = Staff::getQyWxStaff($staff, $department_id_arr, $department_order_arr, $department_leader_arr);
  325. $res = (new QyWechatRequestService())->request($staff->status == 0 ? 'createMember' : 'updateMember', $request_data);
  326. if (!$res['result']) {
  327. throw new Exception($res['msg']);
  328. }
  329. } catch (Exception $e) {
  330. return self::returnArr(0, $e->getMessage());
  331. }
  332. return self::returnArr(1, '推送成员成功');
  333. }
  334. /*
  335. * 从企业微信同步成员
  336. */
  337. public static function refreshStaff($department_id = 0, $staff_id = 0, $wechat_user_id = '')
  338. {
  339. if (!$business_id = SettingService::getBusinessId()) {
  340. return self::returnArr(0, '请先选择要管理的企业');
  341. }
  342. $res = self::getWechatStaffList($department_id, $staff_id, $wechat_user_id);
  343. if (!$res['result']) {
  344. return $res;
  345. }
  346. $staff_list = $res['data'];
  347. $wechat_department = Department::getWechatBusinessDepartment($business_id); //获取该企业已经关联企业微信部门
  348. //获取员工与企业微信部门的关联信息
  349. $department_staff_list = $wechat_department->isNotEmpty() ? DepartmentStaff::uniacid()->select('staff_id', 'department_id', 'is_leader')->whereIn('department_id', array_column($wechat_department->toArray(), 'id'))->get() : collect((object)[]);
  350. $department_staff_list = $department_staff_list->map(function ($v) use ($wechat_department) {
  351. if ($this_department_staff = $wechat_department->where('id', $v->department_id)->first()) {
  352. $v->wechat_department_id = $this_department_staff->wechat_department_id;
  353. }
  354. return $v;
  355. });
  356. $user_id_list = [];
  357. // $isset_staff = Staff::business()->get(); //获取该企业已经存在的员工
  358. if ($staff_list) {
  359. $insert_data = [];
  360. $mobile_list = Member::uniacid()->select('uid', 'mobile')->whereIn('mobile', array_values(array_filter(array_column($staff_list, 'mobile'))))->get()->toArray();
  361. $mobile_list = $mobile_list ? array_column($mobile_list, 'uid', 'mobile') : [];
  362. $time = time();
  363. $extra_columns = ['address','mobile','email'];
  364. foreach ($staff_list as $k => $v) {
  365. $user_id_list[] = $v['userid'];
  366. $form_data = self::formWechatStaff($business_id, $v);
  367. if ($mobile_list[$v['mobile']]) {
  368. $form_data['uid'] = $mobile_list[$v['mobile']];
  369. }
  370. foreach ($extra_columns as $ec){
  371. if ($this_staff->$ec && !$v->$ec){
  372. unset($form_data[$ec]);
  373. }
  374. }
  375. if ($this_staff = Staff::business()->where('user_id', $v['userid'])->first()) { //如果存在user_id相同的员工
  376. if (self::checkRepeatStaff($form_data['uid'], $this_staff->id)) {
  377. $form_data['uid'] = 0;
  378. }
  379. $this_staff->fill($form_data);
  380. $this_staff->save();
  381. } elseif ($v['mobile'] && $this_staff = Staff::business()->where('mobile', $v['mobile'])->first()) { //如果存在手机号相同的员工
  382. if (self::checkRepeatStaff($form_data['uid'], $this_staff->id)) {
  383. $form_data['uid'] = 0;
  384. }
  385. $this_staff->fill($form_data);
  386. $this_staff->save();
  387. } else { //否则新建员工
  388. foreach ($extra_columns as $ec) {
  389. $form_data[$ec] = $form_data[$ec] ?: '';
  390. }
  391. $form_data['mobile'] = $form_data['mobile'] ?: '';
  392. $form_data['email'] = $form_data['email'] ?: '';
  393. $key = 'create_staff_' . $form_data['business_id'] . '_' . $form_data['user_id'];
  394. if (Redis::setnx($key, 1)) {
  395. Redis::expire($key, 10);
  396. } else {
  397. continue;
  398. }
  399. $form_data['uniacid'] = \YunShop::app()->uniacid;
  400. $form_data['uid'] = self::getConnectionMember($form_data, 1);
  401. $form_data['mobile'] = $form_data['mobile'] ? : '';
  402. $this_staff = Staff::create($form_data);
  403. }
  404. $delete_department_id = [];
  405. //获取该员工的部门关联
  406. $this_department_staff_list = $department_staff_list->isNotEmpty() ? $department_staff_list->where('staff_id', $this_staff->id)->toArray() : [];
  407. /*如果存在本地与微信部门关联,但返回的信息中不存在的,删除关联*/
  408. foreach ($this_department_staff_list as $vv) {
  409. if (in_array($vv['wechat_department_id'], $v['department'])) {
  410. continue;
  411. }
  412. if ($this_delete_department = $wechat_department->where('wechat_department_id', $vv['wechat_department_id'])->first()) {
  413. $delete_department_id[] = $this_delete_department->id;
  414. }
  415. }
  416. if ($delete_department_id) {
  417. DepartmentStaff::where('staff_id', $this_staff->id)->whereIn('department_id', $delete_department_id)->delete();
  418. }
  419. /*如果存在本地与微信部门关联,但返回的信息中不存在的,删除关联*/
  420. //获取该员工本地已存在的微信部门关联
  421. $this_isset_department_id = $this_department_staff_list ? array_column($this_department_staff_list, null, 'wechat_department_id') : [];
  422. /*如果员工信息中存在 本地存在的微信部门 并且 未与员工关联的部门 ,新增关联*/
  423. foreach ($v['department'] as $kk => $vv) {
  424. if (in_array($vv, array_keys($this_isset_department_id))) {
  425. if ($this_isset_department_id[$vv]['is_leader'] != $v['is_leader_in_dept'][$kk]) {
  426. DepartmentStaff::uniacid()->where('department_id', $this_isset_department_id[$vv]['department_id'])->where('staff_id', $this_staff->id)->update(['is_leader' => $v['is_leader_in_dept'][$kk] ? 1 : 0]);
  427. }
  428. continue;
  429. }
  430. if ($this_insert_department = $wechat_department->where('wechat_department_id', $vv)->first()) {
  431. $insert_data[] = [
  432. 'uniacid' => \YunShop::app()->uniacid,
  433. 'business_id' => $business_id,
  434. 'staff_id' => $this_staff->id,
  435. 'department_id' => $this_insert_department->id,
  436. 'is_leader' => $v['is_leader_in_dept'][$kk] ? 1 : 0,
  437. 'sort' => intval($v['order'][$kk]),
  438. 'created_at' => $time,
  439. 'updated_at' => $time,
  440. ];
  441. }
  442. }
  443. /*如果员工信息中存在 本地存在的微信部门 并且 未与员工关联的部门 ,新增关联*/
  444. }
  445. if ($insert_data) {
  446. $insert_data = array_chunk($insert_data, 2000);
  447. foreach ($insert_data as $v) {
  448. DepartmentStaff::insert($v);
  449. }
  450. }
  451. }
  452. self::cleanWechatDepartemntStaff($department_id, $user_id_list); //清除无关联员工
  453. BusinessService::flush(SettingService::getBusinessId()); //清除企业缓存
  454. return self::returnArr(1, '同步企业微信员工成功');
  455. }
  456. /*
  457. * 检测是否有重复绑定商城会员
  458. */
  459. public static function checkRepeatStaff($uid, $expect_staff_id)
  460. {
  461. if ($uid == 0) return false;
  462. return Staff::business()->where('uid', $uid)->where('id', '<>', $expect_staff_id)->first();
  463. }
  464. /*
  465. * 清除无关企业微信部门员工
  466. */
  467. public static function cleanWechatDepartemntStaff($department_id, $user_id_list)
  468. {
  469. if (!$department_id) return;
  470. if (!$staff_id = DepartmentStaff::where('department_id', $department_id)->groupBy('staff_id')->pluck('staff_id')->toArray()) return;
  471. // $sub_department_id = Department::getAllDepartmentSubId([$department_id]);
  472. $where = [
  473. ['uniacid', \YunShop::app()->uniacid],
  474. ['business_id', SettingService::getBusinessId()],
  475. ['status', '<>', 0],
  476. [function ($query) use ($staff_id) {
  477. $query->whereIn('id', $staff_id);
  478. }]
  479. ];
  480. if ($user_id_list) {
  481. $where[] = [function ($query) use ($user_id_list) {
  482. $query->whereNotIn('user_id', $user_id_list);
  483. }];
  484. }
  485. $staff_id_list = Staff::where($where)->pluck('id')->toArray();
  486. if ($staff_id_list) { // 禁用会员中关联了部门且关联了企业微信,但企业微信返回数据中不存在的员工
  487. DepartmentStaff::whereIn('staff_id', $staff_id_list)->where('department_id', $department_id)->delete();
  488. $isset_staff_id_list = DepartmentStaff::whereIn('staff_id', $staff_id_list)->groupBy('staff_id')->pluck('staff_id')->toArray();
  489. if ($delete_staff_id = array_diff($staff_id_list, $isset_staff_id_list)) { //禁用无部门归属的员工
  490. Staff::whereIn('id', $delete_staff_id)->update(['disabled' => 1]);
  491. }
  492. }
  493. }
  494. /*
  495. * 获取微信员工列表
  496. */
  497. public static function getWechatStaffList($department_id, $staff_id, $wechat_user_id)
  498. {
  499. $class = new QyWechatRequestService();
  500. if ($department_id) { //更新全部员工
  501. $department = Department::business()->find($department_id);
  502. if (!$department->wechat_department_id) {
  503. return self::returnArr(0, '该部门未与企业微信关联');
  504. }
  505. $res = $class->request('getStaffDetailList', ['department_id' => $department->wechat_department_id]);
  506. if (!$res['result']) {
  507. return self::returnArr(0, $res['msg']);
  508. }
  509. $staff_list = $res['data']['userlist'];
  510. } elseif ($staff_id) { //更新指定id员工
  511. if (!$staff = Staff::business()->find($staff_id)) {
  512. return self::returnArr(0, '员工不存在');
  513. }
  514. $res = $class->request('getStaffDetail', ['userid' => $staff->user_id]);
  515. if (!$res['result']) {
  516. return self::returnArr(0, $res['msg']);
  517. }
  518. $staff_list = [$res['data']];
  519. } elseif ($wechat_user_id) { //更新指定userid员工
  520. $res = $class->request('getStaffDetail', ['userid' => $wechat_user_id]);
  521. if (!$res['result']) {
  522. return self::returnArr(0, $res['msg']);
  523. }
  524. $staff_list = [$res['data']];
  525. } else {
  526. return self::returnArr(0, '请选择要同步的部门或员工');
  527. }
  528. return self::returnArr(1, '成功', $staff_list);
  529. }
  530. /*
  531. * 获取可能跟员工相关联的商城会员
  532. */
  533. public static function getConnectionMember($wechat_staff, $check_isset = 1)
  534. {
  535. $uid = 0;
  536. if (app('plugins')->isEnabled('work-wechat')) { //判断旧雇员表是否有相关数据
  537. $old_staff = Employee::uniacid()->with('hasOneMember')
  538. ->where('crop_id', SettingService::getBusinessId())
  539. ->where('userid', $wechat_staff['user_id'])
  540. ->first();
  541. if ($old_staff->hasOneMember->uid) {
  542. $uid = $old_staff->hasOneMember->uid;
  543. }
  544. }
  545. if (!$uid && $wechat_staff['mobile']) { //判断商城会员是否有手机号关联会员
  546. if ($member = Member::uniacid()->where('mobile', $wechat_staff['mobile'])->first()) $uid = $member->uid;
  547. }
  548. if ($uid && $check_isset) { //判断是否有重复绑定商城会员的员工
  549. if (Staff::business()->where('uid', $uid)->first()) $uid = 0;
  550. }
  551. return $uid;
  552. }
  553. /*
  554. * 组装企业微信员工数据
  555. */
  556. public static function formWechatStaff($business_id, $v)
  557. {;
  558. //企业微信隐私数据限制
  559. if (!isset($v['mobile']) && ($ticket = StaffTicket::uniacid()->business($business_id)->userId($v['userid'])->orderBy('id', 'DESC')->first())) {
  560. $res = (new QyWechatRequestService($business_id))->request('getUserDetail', ['user_ticket' => $ticket->ticket]);
  561. if ($res['result']) {
  562. $columns = ['gender', 'avatar', 'qr_code', 'mobile', 'email', 'address'];
  563. foreach ($columns as $column) {
  564. if ($res['data'][$column]) {
  565. $v[$column] = $res['data'][$column];
  566. }
  567. }
  568. }
  569. }
  570. return [
  571. 'business_id' => $business_id,
  572. 'user_id' => $v['userid'],
  573. 'name' => $v['name'],
  574. 'mobile' => $v['mobile'],
  575. 'position' => $v['position'] ?: '',
  576. 'gender' => $v['gender'] ?: 0,
  577. 'telephone' => $v['telephone'] ?: '',
  578. 'email' => $v['email'] ?: '',
  579. 'avatar' => $v['avatar'] ?: '',
  580. 'alias' => $v['alias'] ?: '',
  581. 'status' => $v['status'],
  582. 'qr_code' => $v['qr_code'] ?: '',
  583. 'address' => $v['address'] ?: '',
  584. 'open_userid' => $business_id . '_' . $v['userid'],
  585. 'main_department' => $v['main_department'] ?: '',
  586. ];
  587. }
  588. /*
  589. * 为员工列表添加权限数据
  590. */
  591. public static function addStaffPremission(&$data)
  592. {
  593. $right_arr = BusinessService::checkBusinessRight();
  594. $function_arr = ['updateStaff', 'deleteStaff'];
  595. if ($right_arr['identity'] < 2) {
  596. $department_staff = DepartmentStaff::whereIn('staff_id', array_column($data, 'staff_id'))->get();
  597. }
  598. // $array = ['']
  599. foreach ($data as &$v) {
  600. $v['premission']['setAuth'] = $right_arr['identity'] < 2 ? 0 : 1;
  601. foreach ($function_arr as $vv) {
  602. if ($right_arr['identity'] > 1) {
  603. $v['premission'][$vv] = 1;
  604. } elseif (!$right_arr['route_department_id'][$vv]) {
  605. $v['premission'][$vv] = 0;
  606. } elseif ($department_staff->where('staff_id', $v['staff_id'])->whereIn('department_id', $right_arr['route_department_id'][$vv])->isNotEmpty()) {
  607. $v['premission'][$vv] = 1;
  608. } else {
  609. $v['premission'][$vv] = 0;
  610. }
  611. }
  612. }
  613. }
  614. /*
  615. * 获取员工列表
  616. */
  617. public static function getStaffList($department_id, $data = [])
  618. {
  619. $where = [];
  620. if (is_array($department_id)) {
  621. $where[] = [function ($query) use ($department_id) {
  622. $query->whereIn('department_id', $department_id);
  623. }];
  624. } else {
  625. $where[] = ['department_id', $department_id];
  626. }
  627. $staff_where = [];
  628. if ($kwd = $data['kwd']) {
  629. $staff_where[] = [function ($q_kwd) use ($kwd) {
  630. $q_kwd->where('name', 'like', "%{$kwd}%")->orWhere('mobile', 'like', "%{$kwd}%");
  631. }];
  632. }
  633. if ($id = $data['id']) {
  634. $staff_where[] = ['id', $id];
  635. }
  636. if ($data['status'] || $data['status'] === 0 || $data['status'] === '0') {
  637. $staff_where[] = ['status', $data['status']];
  638. }
  639. if ($data['disabled'] === 0 || $data['disabled'] === '0') {
  640. $staff_where[] = ['disabled', 0];
  641. } elseif ($data['disabled'] == 1) {
  642. $staff_where[] = ['disabled', 1];
  643. }
  644. if ($staff_where) {
  645. $staff_id_list = Staff::business()->where($staff_where)->pluck('id')->toArray() ?: [-1];
  646. $where[] = [function ($query) use ($staff_id_list) {
  647. $query->whereIn('staff_id', $staff_id_list);
  648. }];
  649. }
  650. $list = DepartmentStaff::business()->where($where)->with(['hasOneStaff' => function ($query) use ($data) {
  651. $query->with(['hasOneMember' => function ($query1) {
  652. $query1->select('uid', 'avatar', 'nickname', 'realname');
  653. }]);
  654. }])->groupBy('staff_id')->select('id', 'staff_id', 'department_id', 'is_leader');
  655. return $list;
  656. }
  657. /*
  658. * 将中文转换成拼音字符串
  659. */
  660. public static function overTrue($string, $length = 0)
  661. {
  662. $arr = (new \Overtrue\Pinyin\Pinyin())->convert($string);
  663. $return_string = '';
  664. foreach ($arr as $v) {
  665. $return_string .= strtoupper($v);
  666. }
  667. return $length ? substr($return_string, 0, $length) : $return_string;
  668. }
  669. public static function returnArr($result, $msg = '', $data = [])
  670. {
  671. return ['result' => $result, 'msg' => $msg, 'data' => $data];
  672. }
  673. }