/** * Copyright 2018 人人开源 https://www.renren.io *

* 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. *

* 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. *

* You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package com.epmet.redis; import cn.hutool.core.bean.BeanUtil; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.dao.GridLatestDao; import com.epmet.dao.RegisterRelationDao; import com.epmet.dao.UserBaseInfoDao; import com.epmet.dao.UserWechatDao; 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.entity.UserWechatEntity; 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.List; import java.util.Map; /** * 用户基础信息 * * @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; @Autowired private UserWechatDao userWechatDao; 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 = RedisKeys.getResiUserKey(baseInfo.getUserId()); //bean to map Map map = BeanUtil.beanToMap(baseInfo, false, true); //用户缓存的有效期为七天 redisUtils.hMSet(key, map,RedisUtils.DEFAULT_EXPIRE * NumConstant.SEVEN); } public UserBaseInfoResultDTO get(String userId){ if(StringUtils.isNotBlank(userId)) { Map map = redisUtils.hGetAll(RedisKeys.getResiUserKey(userId)); if (null != map && !map.isEmpty()) { //缓存中有数据,直接返回 return BeanUtil.mapToBean(map, UserBaseInfoResultDTO.class, true); } } 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 map = redisUtils.hGetAll(RedisKeys.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())){ //如果没有首次注册网格,则没有网格名称(xx机关-xx网格)、显示昵称(xx网格-x先生/女士) RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(userId); if(null != relation && StringUtils.isNotBlank(relation.getGridId())){ baseInfo.setCustomerId(relation.getCustomerId()); BelongGridNameFormDTO gridParam = new BelongGridNameFormDTO(); gridParam.setGridId(relation.getGridId()); baseInfo.setRegisteredGridId(relation.getGridId()); Result 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; }else{ //如果没有,说明这里是陌生人 //陌生人不放入缓存,也不更新用户基础信息表 baseInfo = new UserBaseInfoResultDTO(); baseInfo.setUserId(userId); List wechatInfo = userWechatDao.selectByUserId(userId); if(null != wechatInfo && !wechatInfo.isEmpty()){ baseInfo.setNickname(wechatInfo.get(NumConstant.ZERO).getNickname()); if(StringUtils.isBlank(baseInfo.getNickname())){ baseInfo.setNickname(ModuleConstant.EMPTY_STR); } }else{ return null; } RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(userId); if(null != relation && StringUtils.isNotBlank(relation.getGridId())){ baseInfo.setCustomerId(relation.getCustomerId()); }else{ LatestGridInfoResultDTO gridLatest = gridLatestDao.selectLatestGridInfoByUserId(userId); if(null != gridLatest){ baseInfo.setCustomerId(gridLatest.getCustomerId()); } } } return baseInfo; } return null; } /** * @Description 缓存中获取用户基本信息 * @param userId * @return * @author wangc * @date 2020.07.23 14:58 **/ public UserBaseInfoResultDTO getUserInfo(String userId,String customerId){ if(StringUtils.isNotBlank(userId)){ //获取居民缓存key Map map = redisUtils.hGetAll(RedisKeys.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())){ //如果没有首次注册网格,则没有网格名称(xx机关-xx网格)、显示昵称(xx网格-x先生/女士) RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserIdAndCustomerId(userId,customerId); if(null != relation && StringUtils.isNotBlank(relation.getGridId())){ baseInfo.setCustomerId(relation.getCustomerId()); BelongGridNameFormDTO gridParam = new BelongGridNameFormDTO(); gridParam.setGridId(relation.getGridId()); baseInfo.setRegisteredGridId(relation.getGridId()); Result 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; }else{ //如果没有,说明这里是陌生人 //陌生人不放入缓存,也不更新用户基础信息表 baseInfo = new UserBaseInfoResultDTO(); baseInfo.setUserId(userId); List wechatInfo = userWechatDao.selectByUserId(userId); if(null != wechatInfo && !wechatInfo.isEmpty()){ baseInfo.setNickname(wechatInfo.get(NumConstant.ZERO).getNickname()); if(StringUtils.isBlank(baseInfo.getNickname())){ baseInfo.setNickname(ModuleConstant.EMPTY_STR); } }else{ return null; } RegisterRelationEntity relation = registerRelationDao.selectRegisteredGridIdByUserId(userId); if(null != relation && StringUtils.isNotBlank(relation.getGridId())){ baseInfo.setCustomerId(relation.getCustomerId()); }else{ LatestGridInfoResultDTO gridLatest = gridLatestDao.selectLatestGridInfoByUserId(userId); if(null != gridLatest){ baseInfo.setCustomerId(gridLatest.getCustomerId()); } } } 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); UserBaseInfoResultDTO orientCache = get(entity.getUserId()); if(null != orientCache && StringUtils.isNotBlank(orientCache.getUserId())){ //将要更改的信息赋值到原缓存中 mergeObject(currentCache,orientCache); //将原来的缓存对象赋值给要更新的缓存对象 //此时orientCache中包概括要更新的属性以及之前有切不更新的属性 currentCache = orientCache; } 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 gridResult = govOrgOpenFeignClient.getGridNameByGridId(gridParam); if(gridResult.success() && null != gridResult.getData() && StringUtils.isNotBlank(gridResult.getData().getBelongsGridName())) { String gridFullName = gridResult.getData().getBelongsGridName(); currentCache.setRegisteredGridName(gridFullName); if (StringUtils.isBlank(currentCache.getGender())) currentCache.setGender(NumConstant.ZERO_STR); if (StringUtils.isNotBlank(currentCache.getNickname())) { 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()); // } //} set(currentCache); } /** * @Description 批量清楚用户缓存信息 * @param userIds * @return * @author wangc * @date 2020.08.06 14:34 **/ public void clearUserCache(List userIds){ userIds.forEach(id ->{ if(StringUtils.isNotBlank(id)) { redisUtils.delete(RedisKeys.getResiUserKey(id)); } }); } public 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) { } } } }