Browse Source

用户缓存更新补充

dev_shibei_match
wangchao 5 years ago
parent
commit
311079bf8a
  1. 9
      epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBaseInfoDao.java
  2. 116
      epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBaseInfoRedis.java
  3. 4
      epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBaseInfoServiceImpl.java
  4. 21
      epmet-user/epmet-user-server/src/main/resources/mapper/UserBaseInfoDao.xml

9
epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBaseInfoDao.java

@ -62,4 +62,13 @@ public interface UserBaseInfoDao extends BaseDao<UserBaseInfoEntity> {
* @Date 15:20 2020-07-28
**/
ResiUserBaseInfoResultDTO selecUserBaseInfoByUserId(@Param("userId") String userId);
/**
* @Description 不覆盖更新
* @param entity
* @return
* @author wangc
* @date 2020.07.30 14:01
**/
void updateWithoutCover(UserBaseInfoEntity entity);
}

116
epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBaseInfoRedis.java

@ -20,6 +20,7 @@ package com.epmet.redis;
import cn.hutool.core.bean.BeanUtil;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.constant.UserRedisKeys;
import com.epmet.dao.GridLatestDao;
@ -30,12 +31,14 @@ import com.epmet.dto.result.BelongGridNameResultDTO;
import com.epmet.dto.result.LatestGridInfoResultDTO;
import com.epmet.dto.result.UserBaseInfoResultDTO;
import com.epmet.entity.RegisterRelationEntity;
import com.epmet.entity.UserBaseInfoEntity;
import com.epmet.feign.GovOrgOpenFeignClient;
import com.epmet.util.ModuleConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Map;
import static com.epmet.commons.tools.redis.RedisUtils.NOT_EXPIRE;
@ -127,18 +130,115 @@ public class UserBaseInfoRedis {
baseInfo.setShowName(buffer.toString());
}
}
}else{
//如果没有居民注册记录,说明当前用户是陌生人,需要从陌生人网格访问列表中查询所属的客户Id
LatestGridInfoResultDTO gridLatest =
gridLatestDao.selectLatestGridInfoByUserId(userId);
if(null != gridLatest){
baseInfo.setCustomerId(gridLatest.getCustomerId());
else{
//如果没有居民注册记录,说明当前用户是陌生人,需要从陌生人网格访问列表中查询所属的客户Id
LatestGridInfoResultDTO gridLatest =
gridLatestDao.selectLatestGridInfoByUserId(userId);
if(null != gridLatest){
baseInfo.setCustomerId(gridLatest.getCustomerId());
}
}
set(baseInfo);
return baseInfo;
}
set(baseInfo);
return baseInfo;
}
return null;
}
/**
* @Description 更新缓存
* @param entity
* @return
* @author wangc
* @date 2020.07.30 14:07
**/
public void updateUserCache(UserBaseInfoEntity entity){
if(null == entity || StringUtils.isBlank(entity.getUserId())) return;
UserBaseInfoResultDTO currentCache = ConvertUtils.sourceToTarget(entity,UserBaseInfoResultDTO.class);
RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(entity.getUserId());
//如果没有首次注册网格,则没有网格名称(xx机关-xx网格)、显示昵称(xx网格-x先生/女士)
if(null != relation && StringUtils.isNotBlank(relation.getGridId())){
currentCache.setCustomerId(relation.getCustomerId());
BelongGridNameFormDTO gridParam = new BelongGridNameFormDTO();
gridParam.setGridId(relation.getGridId());
currentCache.setRegisteredGridId(relation.getGridId());
Result<BelongGridNameResultDTO> gridResult =
govOrgOpenFeignClient.getGridNameByGridId(gridParam);
if(gridResult.success() && null != gridResult.getData()
&& StringUtils.isNotBlank(gridResult.getData().getBelongsGridName())){
String gridFullName = gridResult.getData().getBelongsGridName();
currentCache.setRegisteredGridName(gridFullName);
StringBuffer buffer = new StringBuffer(gridFullName.split(ModuleConstant.DASH)[NumConstant.ONE]).append(ModuleConstant.DASH).append(currentCache.getSurname());
switch (currentCache.getGender()) {
case NumConstant.ONE_STR:
buffer.append(ModuleConstant.RESI_USER_NICKNAME_SUFFIX_MALE);
break;
case NumConstant.TWO_STR:
buffer.append(ModuleConstant.RESI_USER_NICKNAME_SUFFIX_FEMALE);
break;
default:
buffer.append(ModuleConstant.RESI_USER_NICKNAME_SUFFIX_GENDER_UNKNOWN);
}
currentCache.setShowName(buffer.toString());
}
}else{
//如果没有居民注册记录,说明当前用户是陌生人,需要从陌生人网格访问列表中查询所属的客户Id
LatestGridInfoResultDTO gridLatest =
gridLatestDao.selectLatestGridInfoByUserId(entity.getUserId());
if(null != gridLatest){
currentCache.setCustomerId(gridLatest.getCustomerId());
}
}
//更新缓存的入口只有1.更新微信信息,entity中一定携带微信信息,2.注册居民信息,在调用此方法之前,已经查询过微信信息并赋值到entity中
//因此以下代码不需执行
//if(StringUtils.isBlank(currentCache.getNickname()) || StringUtils.isBlank(currentCache.getHeadImgUrl())){
// UserBaseInfoResultDTO wechatInfo = userBaseInfoDao.selectListByUserIdList(entity.getUserId());
// if(null != wechatInfo){
// currentCache.setNickname(wechatInfo.getNickname());
// currentCache.setGender(wechatInfo.getGender());
// currentCache.setHeadImgUrl(wechatInfo.getHeadImgUrl());
// }
//}
Map<String,Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(entity.getUserId()));
if(null != map && !map.isEmpty()) {
//缓存中有数据,直接返回
UserBaseInfoResultDTO orientCache = BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true);
mergeObject(currentCache , orientCache);
set(orientCache);
}else{
//缓存中没有该数据
set(currentCache);
}
}
public <T> void mergeObject(T origin, T destination) {
if (origin == null || destination == null)
return;
if (!origin.getClass().equals(destination.getClass()))
return;
Field[] fields = origin.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
try {
fields[i].setAccessible(true);
Object value = fields[i].get(origin);
if (null != value) {
fields[i].set(destination, value);
}
fields[i].setAccessible(false);
} catch (Exception e) {
}
}
}
}

4
epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBaseInfoServiceImpl.java

@ -225,11 +225,13 @@ public class UserBaseInfoServiceImpl extends BaseServiceImpl<UserBaseInfoDao, Us
if(null != userBaseInfoResultDTO && StringUtils.isNotBlank(userBaseInfoResultDTO.getId())){
//更新
baseInfo.setId(userBaseInfoResultDTO.getId());
updateById(baseInfo);
baseDao.updateWithoutCover(baseInfo);
}else{
//新增
insert(baseInfo);
}
//更新缓存
userBaseInfoRedis.updateUserCache(baseInfo);
}
/**

21
epmet-user/epmet-user-server/src/main/resources/mapper/UserBaseInfoDao.xml

@ -92,4 +92,25 @@
WHERE DEL_FLAG = '0'
AND USER_ID = #{userId}
</select>
<update id="updateWithoutCover" parameterType="com.epmet.entity.UserBaseInfoEntity">
UPDATE user_base_info
<set>
<if test ='null != mobile and "" != mobile'>mobile = #{mobile},</if>
<if test ='null != surname and "" != surname'>surname = #{surname},</if>
<if test ='null != name and "" != name'>name = #{name},</if>
<if test ='null != realName and "" != realName'>real_name = #{realName},</if>
<if test ='null != idNum and "" != idNum'>id_num = #{idNum},</if>
<if test ='null != gender and "" != gender'>gender = #{gender},</if>
<if test ='null != street and "" != street'>street = #{street},</if>
<if test ='null != district and "" != district'>district = #{district},</if>
<if test ='null != buildingAddress and "" != buildingAddress'>building_address = #{buildingAddress},</if>
<if test ='null != nickname and "" != nickname'>nickname = #{nickname},</if>
<if test ='null != headImgUrl and "" != headImgUrl'>head_img_url = #{headImgUrl},</if>
<if test ='null != delFlag and "" != delFlag'>del_flag = #{delFlag},</if>
updated_by = #{updatedBy},
updated_time = now()
</set>
WHERE id = #{id}
</update>
</mapper>

Loading…
Cancel
Save