123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Service;
- use App\Models\AuthorizeCorp;
- use App\Models\CorpUserScaleWarningConf;
- use App\Models\CustomerDetails;
- use App\Models\System\AdminManageCorp;
- use App\Models\System\Users;
- use App\RedisModel;
- class CorpService
- {
- /**
- * 获取登陆账户可管理的企业主体
- * @param $is_system_admin
- * @param $keyword
- * @param $adminId
- * @param $getUserScale
- * @param $page
- * @param $pageSize
- * @return array
- */
- public static function getCorpList($is_system_admin, $keyword, $adminId, $getUserScale, $page, $pageSize, $active)
- {
- $only_manage_corp_id_arr = false; //只查看特定的主体id,false代表不限
- /**非系统管理员查看受分配的主体列表**/
- if($is_system_admin != Users::SYSTEM_ADMIN){
- $only_manage_corp_id_arr = AdminManageCorp::query()
- ->where("sys_user_id",$adminId)
- ->where("is_delete",0)
- ->pluck("corpid")->toArray();
- }
- # 获取已授权账号列表
- list($list, $total) = AuthorizeCorp::getCorpList($keyword, $page, $pageSize, $active, $only_manage_corp_id_arr);
- if($getUserScale) {
- # 提取企微ID,批量查询用户规模
- $corpIdList = array_column($list->toArray(), 'corpid');
- $corpUserScaleList = CorpService::getMultipleCorpUserScaleFromRds($corpIdList);
- foreach ($list as $item) {
- $userScale = $corpUserScaleList[$item->corpid] ?? 0;
- $item->user_scale = $userScale;
- }
- }
- return [$list, $total];
- }
- /**
- * 获取用户规模
- * @param $corpId
- * @return numeric
- */
- public static function getCorpUserScale($corpId) {
- if(empty($corpId)) {
- return 0;
- }
- return CustomerDetails::getCorpUserScale($corpId);
- }
- /**
- * 设置用户规模
- * @param $corpId
- * @param $userScale
- * @return void
- */
- public static function setCorpUserScale($corpId, $userScale) {
- $redisKey = 'Playlet::corpUserScale' . '-' . $corpId;
- return RedisModel::set($redisKey, $userScale);
- }
- public static function getMultipleCorpUserScaleFromRds($corpIdList) {
- $result = RedisModel::get(CorpUserScaleWarningConf::CORP_USER_SCALE);
- if(empty($result)) return self::getMultipleCorpUserScale($corpIdList);
- return json_decode($result, 1);
- }
- public static function getMultipleCorpUserScale($corpIdList) {
- # 对相同取模的ID进行合并处理
- $idList = [];
- foreach($corpIdList as $corpid) {
- $suffix = get_hash_code($corpid, 50);
- $idList[$suffix][] = $corpid;
- }
- $result = [];
- foreach($idList as $suffix => $item) {
- $res = CustomerDetails::suffix($suffix)->whereIn('corpid', $item)->selectRaw('corpid, count(external_userid) as count')
- ->groupBy('corpid')->get();
- foreach ($res as $value) {
- $result[$value->corpid] = $value->count;
- }
- }
- return $result;
- }
- }
|