You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							244 lines
						
					
					
						
							10 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							244 lines
						
					
					
						
							10 KiB
						
					
					
				| /** | |
|  * Copyright 2018 人人开源 https://www.renren.io | |
|  * <p> | |
|  * This program is free software: you can redistribute it and/or modify | |
|  * it under the terms of the GNU General Public License as published by | |
|  * the Free Software Foundation, either version 3 of the License, or | |
|  * (at your option) any later version. | |
|  * <p> | |
|  * This program is distributed in the hope that it will be useful, | |
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | |
|  * GNU General Public License for more details. | |
|  * <p> | |
|  * You should have received a copy of the GNU General Public License | |
|  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | |
|  */ | |
| 
 | |
| 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; | |
| import com.epmet.dao.RegisterRelationDao; | |
| import com.epmet.dao.UserBaseInfoDao; | |
| import com.epmet.dto.form.BelongGridNameFormDTO; | |
| 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; | |
| 
 | |
| /** | |
|  * 用户基础信息 | |
|  * | |
|  * @author generator generator@elink-cn.com | |
|  * @since v1.0.0 2020-07-19 | |
|  */ | |
| @Component | |
| public class UserBaseInfoRedis { | |
|     @Autowired | |
|     private RedisUtils redisUtils; | |
|     @Autowired | |
|     private UserBaseInfoDao userBaseInfoDao; | |
|     @Autowired | |
|     private RegisterRelationDao registerRelationDao; | |
|     @Autowired | |
|     private GovOrgOpenFeignClient govOrgOpenFeignClient; | |
|     @Autowired | |
|     private GridLatestDao gridLatestDao; | |
| 
 | |
|     public void delete(Object[] ids) { | |
| 
 | |
|     } | |
| 
 | |
|     /** | |
|      * @Description 将用户基本信息放入缓存 | |
|      * @param baseInfo | |
|      * @return | |
|      * @author wangc | |
|      * @date 2020.07.23 14:43 | |
|     **/ | |
|     public void set(UserBaseInfoResultDTO baseInfo){ | |
|         if(StringUtils.isBlank(baseInfo.getUserId())) return; | |
|         String key = UserRedisKeys.getResiUserKey(baseInfo.getUserId()); | |
|         //bean to map | |
|         Map<String, Object> map = BeanUtil.beanToMap(baseInfo, false, true); | |
|         redisUtils.hMSet(key, map,NOT_EXPIRE); | |
|     } | |
| 
 | |
|     public String get(String id){ | |
|         return null; | |
|     } | |
| 
 | |
|     /** | |
|      * @Description 缓存中获取用户基本信息 | |
|      * @param userId | |
|      * @return | |
|      * @author wangc | |
|      * @date 2020.07.23 14:58 | |
|     **/ | |
|     public UserBaseInfoResultDTO getUserInfo(String userId){ | |
|         if(StringUtils.isNotBlank(userId)){ | |
|             //获取居民缓存key | |
|             Map<String,Object> map = redisUtils.hGetAll(UserRedisKeys.getResiUserKey(userId)); | |
|             if(null != map && !map.isEmpty()) { | |
|                 //缓存中有数据,直接返回 | |
|                 return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true); | |
|             } | |
|             //缓存中没有数据,先查数据库,放入缓存后再返回 | |
|             UserBaseInfoResultDTO baseInfo = userBaseInfoDao.selectListByUserIdList(userId); | |
|             if(null != baseInfo && StringUtils.isNotBlank(baseInfo.getId())){ | |
|                 RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(userId); | |
|                 //如果没有首次注册网格,则没有网格名称(xx机关-xx网格)、显示昵称(xx网格-x先生/女士) | |
|                 if(null != relation && StringUtils.isNotBlank(relation.getGridId())){ | |
|                     baseInfo.setCustomerId(relation.getCustomerId()); | |
|                     BelongGridNameFormDTO gridParam = new BelongGridNameFormDTO(); | |
|                     gridParam.setGridId(relation.getGridId()); | |
|                     baseInfo.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(); | |
|                         baseInfo.setRegisteredGridName(gridFullName); | |
|                         StringBuffer buffer = new StringBuffer(gridFullName.split(ModuleConstant.DASH)[NumConstant.ONE]).append(ModuleConstant.DASH).append(baseInfo.getSurname()); | |
|                         switch (baseInfo.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); | |
|                         } | |
|                         baseInfo.setShowName(buffer.toString()); | |
|                     } | |
|                 } | |
|                 else{ | |
|                     //如果没有居民注册记录,说明当前用户是陌生人,需要从陌生人网格访问列表中查询所属的客户Id | |
|                     LatestGridInfoResultDTO gridLatest = | |
|                             gridLatestDao.selectLatestGridInfoByUserId(userId); | |
|                     if(null != gridLatest){ | |
|                         baseInfo.setCustomerId(gridLatest.getCustomerId()); | |
|                     } | |
|                 } | |
|                 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) { | |
| 
 | |
|             } | |
|         } | |
|     } | |
| } |