企微短剧业务系统

CorpService.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Service;
  3. use App\Models\AuthorizeCorp;
  4. use App\Models\CorpUserScaleWarningConf;
  5. use App\Models\CustomerDetails;
  6. use App\Models\System\AdminManageCorp;
  7. use App\Models\System\Users;
  8. use App\RedisModel;
  9. class CorpService
  10. {
  11. /**
  12. * 获取登陆账户可管理的企业主体
  13. * @param $is_system_admin
  14. * @param $keyword
  15. * @param $adminId
  16. * @param $getUserScale
  17. * @param $page
  18. * @param $pageSize
  19. * @return array
  20. */
  21. public static function getCorpList($is_system_admin, $keyword, $adminId, $getUserScale, $page, $pageSize, $active)
  22. {
  23. $only_manage_corp_id_arr = false; //只查看特定的主体id,false代表不限
  24. /**非系统管理员查看受分配的主体列表**/
  25. if($is_system_admin != Users::SYSTEM_ADMIN){
  26. $only_manage_corp_id_arr = AdminManageCorp::query()
  27. ->where("sys_user_id",$adminId)
  28. ->where("is_delete",0)
  29. ->pluck("corpid")->toArray();
  30. }
  31. # 获取已授权账号列表
  32. list($list, $total) = AuthorizeCorp::getCorpList($keyword, $page, $pageSize, $active, $only_manage_corp_id_arr);
  33. if($getUserScale) {
  34. # 提取企微ID,批量查询用户规模
  35. $corpIdList = array_column($list->toArray(), 'corpid');
  36. $corpUserScaleList = CorpService::getMultipleCorpUserScaleFromRds($corpIdList);
  37. foreach ($list as $item) {
  38. $userScale = $corpUserScaleList[$item->corpid] ?? 0;
  39. $item->user_scale = $userScale;
  40. }
  41. }
  42. return [$list, $total];
  43. }
  44. /**
  45. * 获取用户规模
  46. * @param $corpId
  47. * @return numeric
  48. */
  49. public static function getCorpUserScale($corpId) {
  50. if(empty($corpId)) {
  51. return 0;
  52. }
  53. return CustomerDetails::getCorpUserScale($corpId);
  54. }
  55. /**
  56. * 设置用户规模
  57. * @param $corpId
  58. * @param $userScale
  59. * @return void
  60. */
  61. public static function setCorpUserScale($corpId, $userScale) {
  62. $redisKey = 'Playlet::corpUserScale' . '-' . $corpId;
  63. return RedisModel::set($redisKey, $userScale);
  64. }
  65. public static function getMultipleCorpUserScaleFromRds($corpIdList) {
  66. $result = RedisModel::get(CorpUserScaleWarningConf::CORP_USER_SCALE);
  67. if(empty($result)) return self::getMultipleCorpUserScale($corpIdList);
  68. return json_decode($result, 1);
  69. }
  70. public static function getMultipleCorpUserScale($corpIdList) {
  71. # 对相同取模的ID进行合并处理
  72. $idList = [];
  73. foreach($corpIdList as $corpid) {
  74. $suffix = get_hash_code($corpid, 50);
  75. $idList[$suffix][] = $corpid;
  76. }
  77. $result = [];
  78. foreach($idList as $suffix => $item) {
  79. $res = CustomerDetails::suffix($suffix)->whereIn('corpid', $item)->selectRaw('corpid, count(external_userid) as count')
  80. ->groupBy('corpid')->get();
  81. foreach ($res as $value) {
  82. $result[$value->corpid] = $value->count;
  83. }
  84. }
  85. return $result;
  86. }
  87. }