放缓存

This commit is contained in:
wangxiaopeng
2024-03-29 20:17:08 +08:00
parent c3e5b0e53e
commit 549805b0fe
6 changed files with 122 additions and 11 deletions

View File

@@ -263,4 +263,14 @@ public class RedisConstant {
*/
public static final String PARTNER_APPOINTMENT_LOCK = "partner:appointment:lock:{0}";
/**
* 用户意向区域key
*/
public static final String USER_WANT_AREA_CACHE = "user_want_area_cache_";
/**
* 招商经理轮询key
*/
public static final String INVESTMENT_MANAGER_CACHE = "investment_manager_cache_";
}

View File

@@ -0,0 +1,45 @@
package com.cool.store.enums;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* describe:职位来源枚举
*
* @author zhouyiping
* @date 2020/12/30
*/
public enum RoleSourceEnum {
/**
* 职位来源
*/
CREATE("create", "自建"),
EHR("ehr", "EHR"),
SYNC("sync", "钉钉同步");
private static final Map<String, RoleSourceEnum> map = Arrays.stream(values()).collect(
Collectors.toMap(RoleSourceEnum::getCode, Function.identity()));
private String code;
private String desc;
RoleSourceEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
public static RoleSourceEnum getByCode(String code) {
return map.get(code);
}
}

View File

@@ -98,4 +98,13 @@ public class RedisConstantUtil {
return active + "_" + RedisConstant.PHONE_NUMBER + phoneNumber;
}
public String getUserWantAreaListKey(String userId) {
return active + "_" + RedisConstant.USER_WANT_AREA_CACHE + eid + ":" + userId;
}
public String getInvestmentManagerKey(Long wantShopAreaId) {
return active + "_" + RedisConstant.INVESTMENT_MANAGER_CACHE + eid + ":" + wantShopAreaId;
}
}

View File

@@ -48,6 +48,8 @@ public interface SysRoleMapper {
*/
List<SysRoleDO> getRolesByName(@Param("roleName") String roleName);
SysRoleDO getRolesByNameAndSource(@Param("roleName") String roleName, @Param("source") String source);
/**
* 查询角色详情
* @param roleId

View File

@@ -137,6 +137,15 @@
</select>
<select id="getRolesByNameAndSource" resultType="com.cool.store.entity.SysRoleDO">
select
id as id,
role_name as roleName,
role_auth as roleAuth
from sys_role_${enterpriseId}
where role_name = #{roleName} and source = #{source}
</select>
<select id="getRole" resultType="com.cool.store.entity.SysRoleDO">
select
id as id,

View File

@@ -10,12 +10,15 @@ import com.cool.store.mapper.RegionMapper;
import com.cool.store.mapper.SysRoleMapper;
import com.cool.store.mapper.UserAuthMappingMapper;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.RedisConstantUtil;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.vo.SysRoleVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -46,6 +49,11 @@ public class UserAuthMappingServiceImpl implements UserAuthMappingService {
@Resource
private EnterpriseUserDAO enterpriseUserDAO;
@Resource
private RedisUtilPool redisUtilPool;
@Autowired
private RedisConstantUtil redisConstantUtil;
@Override
public List<UserAuthMappingDO> listUserAuthMappingByUserId(String userId) {
@@ -63,24 +71,40 @@ public class UserAuthMappingServiceImpl implements UserAuthMappingService {
*/
@Override
public EnterpriseUserDO getUserByRoleNameAndAreaId(String roleName, Long wantShopAreaId) {
if(StringUtils.isBlank(roleName) || wantShopAreaId == null){
if(StringUtils.isBlank(roleName) || Objects.isNull(wantShopAreaId)){
return null;
}
List<SysRoleDO> roleList = sysRoleMapper.getRolesByName(roleName);
if (CollectionUtils.isEmpty(roleList)) {
if(UserRoleEnum.INVESTMENT_MANAGER.getDesc().equals(roleName)){
// 找大区经理 找不到找战区经理,再找不到找大区经理?
String suitableUserId = null;
String investmentManagerKey = redisConstantUtil.getInvestmentManagerKey(wantShopAreaId);
if(UserRoleEnum.INVESTMENT_MANAGER.getDesc().equals(roleName)){
suitableUserId = redisUtilPool.rpopStr(investmentManagerKey);
if(StringUtils.isNotBlank(suitableUserId)){
EnterpriseUserDO userDO = enterpriseUserDAO.getUserInfoById(suitableUserId);
return userDO;
}
}
Long roleId = roleList.get(0).getId();
SysRoleDO sysRoleDO = sysRoleMapper.getRolesByNameAndSource(roleName, RoleSourceEnum.CREATE.getCode());
Long roleId = sysRoleDO.getId();
List<String> hasRoleUserIdList = sysRoleMapper.getPositionUserIds(Collections.singletonList(String.valueOf(roleId)));
if(UserRoleEnum.INVESTMENT_MANAGER.getDesc().equals(roleName) && CollectionUtils.isEmpty(hasRoleUserIdList)){
// 找大区经理 找不到找战区经理,再找不到找大区经理?
sysRoleDO = sysRoleMapper.getRolesByNameAndSource(UserRoleEnum.REGION_MANAGER.getDesc(), RoleSourceEnum.CREATE.getCode());
roleId = sysRoleDO.getId();
hasRoleUserIdList = sysRoleMapper.getPositionUserIds(Collections.singletonList(String.valueOf(roleId)));
}
Long warRegionId = regionAreaConfigDao.getByWantShopAreaId(wantShopAreaId);
// 查找有战区权限的人
List<String> authWarRegionUserIdList = authWarRegionUser(warRegionId);
List<String> userIds = sysRoleMapper.getPositionUserIds(Collections.singletonList(String.valueOf(roleId)));
authWarRegionUserIdList.retainAll(userIds);
List<EnterpriseUserDO> userDOList = enterpriseUserDAO.getUserInfoByUserIds(authWarRegionUserIdList);
// 用redis实现 用户列表轮询返回
return userDOList.get(0);
authWarRegionUserIdList.retainAll(hasRoleUserIdList);
if(UserRoleEnum.INVESTMENT_MANAGER.getDesc().equals(roleName)){
// 按工号排序后放入redis
redisUtilPool.listPushTail(investmentManagerKey, authWarRegionUserIdList.toArray(new String[authWarRegionUserIdList.size()]));
suitableUserId = redisUtilPool.rpopStr(investmentManagerKey);
}else {
suitableUserId = authWarRegionUserIdList.get(0);
}
EnterpriseUserDO userDO = enterpriseUserDAO.getUserInfoById(suitableUserId);
return userDO;
}
/**
@@ -90,6 +114,13 @@ public class UserAuthMappingServiceImpl implements UserAuthMappingService {
*/
@Override
public List<Long> listWantShopAreaIdByUserId(String userId) {
//如果在缓存中存在,从缓存中去取
String userWantAreaListKey = redisConstantUtil.getUserWantAreaListKey(userId);
List<String> userWantAreaList = redisUtilPool.listGetAll(userWantAreaListKey);
if (CollectionUtils.isNotEmpty(userWantAreaList)) {
List<Long> authRegionIdListFromCache = userWantAreaList.stream().map(regionId -> Long.valueOf(regionId)).collect(Collectors.toList());
return authRegionIdListFromCache;
}
List<UserAuthMappingDO> userAuthList = this.listUserAuthMappingByUserId(userId);
List<String> authRegionIds = ListUtils.emptyIfNull(userAuthList)
.stream().map(UserAuthMappingDO::getMappingId)
@@ -99,6 +130,11 @@ public class UserAuthMappingServiceImpl implements UserAuthMappingService {
List<Long> wantShopAreaIdList = ListUtils.emptyIfNull(regionAreaConfigDOList)
.stream().map(RegionAreaConfigDO::getWantShopAreaId)
.collect(Collectors.toList());
List<String> wantShopAreaIdStrList = wantShopAreaIdList.stream().map(areaId -> String.valueOf(areaId)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(wantShopAreaIdStrList)) {
redisUtilPool.listPushTail(userWantAreaListKey, wantShopAreaIdStrList.toArray(new String[wantShopAreaIdList.size()]));
redisUtilPool.expire(userWantAreaListKey, 5 * 60);
}
return wantShopAreaIdList;
}