9 changed files with 355 additions and 0 deletions
@ -0,0 +1,22 @@ |
|||||
|
package com.epmet.controller; |
||||
|
|
||||
|
import com.epmet.service.StaffLoginLogService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 工作人员登录日志表 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2023-04-04 |
||||
|
*/ |
||||
|
@RestController |
||||
|
@RequestMapping("staffLoginLog") |
||||
|
public class StaffLoginLogController { |
||||
|
|
||||
|
@Autowired |
||||
|
private StaffLoginLogService staffLoginLogService; |
||||
|
|
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
package com.epmet.dao; |
||||
|
|
||||
|
import com.epmet.commons.mybatis.dao.BaseDao; |
||||
|
import com.epmet.entity.StaffLoginLogEntity; |
||||
|
import org.apache.ibatis.annotations.Mapper; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员登录日志表 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2023-04-04 |
||||
|
*/ |
||||
|
@Mapper |
||||
|
public interface StaffLoginLogDao extends BaseDao<StaffLoginLogEntity> { |
||||
|
|
||||
|
} |
@ -0,0 +1,73 @@ |
|||||
|
package com.epmet.entity; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.annotation.TableName; |
||||
|
|
||||
|
import com.epmet.commons.mybatis.entity.BaseEpmetEntity; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员登录日志表 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2023-04-04 |
||||
|
*/ |
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper=false) |
||||
|
@TableName("staff_login_log") |
||||
|
public class StaffLoginLogEntity extends BaseEpmetEntity { |
||||
|
|
||||
|
private static final long serialVersionUID = 1L; |
||||
|
|
||||
|
/** |
||||
|
* 客户ID |
||||
|
*/ |
||||
|
private String customerId; |
||||
|
|
||||
|
/** |
||||
|
* 工作端用户id |
||||
|
*/ |
||||
|
private String staffId; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员姓名 |
||||
|
*/ |
||||
|
private String staffName; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员手机号 |
||||
|
*/ |
||||
|
private String mobile; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员所属组织id;组织名称在customer_agency |
||||
|
*/ |
||||
|
private String agencyId; |
||||
|
|
||||
|
/** |
||||
|
* 组织级别(社区级:community, |
||||
|
乡(镇、街道)级:street, |
||||
|
区县级: district, |
||||
|
市级: city |
||||
|
省级:province) |
||||
|
*/ |
||||
|
private String agencyLevel; |
||||
|
|
||||
|
/** |
||||
|
* agency_id的直属上级;如果是根组织的用户登录,pid=0或者pid='' |
||||
|
*/ |
||||
|
private String pid; |
||||
|
|
||||
|
/** |
||||
|
* AGENCY_ID全路径,包含agency_id自身 |
||||
|
*/ |
||||
|
private String orgIdPath; |
||||
|
|
||||
|
/** |
||||
|
* 登录时间 |
||||
|
*/ |
||||
|
private Date loginTime; |
||||
|
|
||||
|
} |
@ -0,0 +1,110 @@ |
|||||
|
package com.epmet.mq.listener; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.epmet.commons.rocketmq.constants.MQUserPropertys; |
||||
|
import com.epmet.commons.rocketmq.messages.LoginMQMsg; |
||||
|
import com.epmet.commons.tools.distributedlock.DistributedLock; |
||||
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.commons.tools.redis.RedisKeys; |
||||
|
import com.epmet.commons.tools.redis.RedisUtils; |
||||
|
import com.epmet.commons.tools.utils.SpringContextUtils; |
||||
|
import com.epmet.service.StaffLoginLogService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext; |
||||
|
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus; |
||||
|
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently; |
||||
|
import org.apache.rocketmq.common.message.MessageExt; |
||||
|
import org.redisson.api.RLock; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.TimeUnit; |
||||
|
|
||||
|
/** |
||||
|
* @author wxz |
||||
|
* @Description 登录操作日志监听器 |
||||
|
|
||||
|
* @return |
||||
|
* @date 2021.06.07 16:12 |
||||
|
*/ |
||||
|
public class StaffLoginLogListener implements MessageListenerConcurrently { |
||||
|
|
||||
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
|
||||
|
private RedisUtils redisUtils; |
||||
|
|
||||
|
@Override |
||||
|
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { |
||||
|
|
||||
|
if (redisUtils == null) { |
||||
|
redisUtils = SpringContextUtils.getBean(RedisUtils.class); |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
msgs.forEach(msg -> consumeMessage(msg)); |
||||
|
} catch (Exception e) { |
||||
|
logger.error(ExceptionUtils.getErrorStackTrace(e)); |
||||
|
return ConsumeConcurrentlyStatus.RECONSUME_LATER; |
||||
|
} |
||||
|
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; |
||||
|
} |
||||
|
|
||||
|
private void consumeMessage(MessageExt messageExt) { |
||||
|
String tags = messageExt.getTags(); |
||||
|
// msg示例:
|
||||
|
// {
|
||||
|
// "userId": "5198ef9e3644c4f49457d2b551a1432e",
|
||||
|
// "appId": "数字社区登录",
|
||||
|
// "loginTime": "2023-04-04 14:05:37",
|
||||
|
// "ip": "219.146.91.110",
|
||||
|
// "fromApp": "gov",
|
||||
|
// "fromClient": "web"
|
||||
|
// }
|
||||
|
String msg = new String(messageExt.getBody()); |
||||
|
String pendingMsgLabel = messageExt.getUserProperty(MQUserPropertys.BLOCKED_MSG_LABEL); |
||||
|
logger.info("工作人员登录操作日志监听器-收到消息内容:{}", msg); |
||||
|
LoginMQMsg msgObj = JSON.parseObject(msg, LoginMQMsg.class); |
||||
|
|
||||
|
DistributedLock distributedLock = null; |
||||
|
RLock lock = null; |
||||
|
try { |
||||
|
distributedLock = SpringContextUtils.getBean(DistributedLock.class); |
||||
|
lock = distributedLock.getLock(String.format("lock:staff_login_log:%s:%s", tags, msgObj.getUserId()), |
||||
|
30L, 30L, TimeUnit.SECONDS); |
||||
|
SpringContextUtils.getBean(StaffLoginLogService.class).saveLog(msgObj.getUserId(),msgObj.getLoginTime()); |
||||
|
} catch (RenException e) { |
||||
|
// 如果是我们手动抛出的异常,说明在业务可控范围内。目前不需要MQ重试
|
||||
|
logger.error("【RocketMQ】工作人员登录添加操作日志失败:".concat(ExceptionUtils.getErrorStackTrace(e))); |
||||
|
} catch (Exception e) { |
||||
|
// 不是我们自己抛出的异常,可以让MQ重试
|
||||
|
logger.error("【RocketMQ】工作人员登录添加操作日志失败:".concat(ExceptionUtils.getErrorStackTrace(e))); |
||||
|
throw e; |
||||
|
} finally { |
||||
|
distributedLock.unLock(lock); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.isNotBlank(pendingMsgLabel)) { |
||||
|
try { |
||||
|
removePendingMqMsgCache(pendingMsgLabel); |
||||
|
} catch (Exception e) { |
||||
|
logger.error("【工作人员登录操作事件监听器】-删除mq阻塞消息缓存失败:{}", ExceptionUtils.getErrorStackTrace(e)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @description |
||||
|
* |
||||
|
* @param pendingMsgLabel |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.10.14 16:32:32 |
||||
|
*/ |
||||
|
private void removePendingMqMsgCache(String pendingMsgLabel) { |
||||
|
String key = RedisKeys.blockedMqMsgKey(pendingMsgLabel); |
||||
|
redisUtils.delete(key); |
||||
|
//logger.info("【登录操作事件监听器】删除pendingMsgLabel成功:{}", pendingMsgLabel);
|
||||
|
} |
||||
|
} |
@ -0,0 +1,22 @@ |
|||||
|
package com.epmet.service; |
||||
|
|
||||
|
import com.epmet.commons.mybatis.service.BaseService; |
||||
|
import com.epmet.entity.StaffLoginLogEntity; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员登录日志表 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2023-04-04 |
||||
|
*/ |
||||
|
public interface StaffLoginLogService extends BaseService<StaffLoginLogEntity> { |
||||
|
/** |
||||
|
* 登录,插入记录 |
||||
|
* @param staffId |
||||
|
* @param loginTime |
||||
|
*/ |
||||
|
void saveLog(String staffId, Date loginTime); |
||||
|
|
||||
|
} |
@ -0,0 +1,75 @@ |
|||||
|
package com.epmet.service.impl; |
||||
|
|
||||
|
import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; |
||||
|
import com.epmet.commons.tools.constant.NumConstant; |
||||
|
import com.epmet.commons.tools.constant.StrConstant; |
||||
|
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; |
||||
|
import com.epmet.commons.tools.redis.common.CustomerStaffRedis; |
||||
|
import com.epmet.commons.tools.utils.Result; |
||||
|
import com.epmet.commons.tools.utils.SpringContextUtils; |
||||
|
import com.epmet.dao.StaffLoginLogDao; |
||||
|
import com.epmet.dto.CustomerAgencyDTO; |
||||
|
import com.epmet.dto.CustomerStaffDTO; |
||||
|
import com.epmet.entity.StaffLoginLogEntity; |
||||
|
import com.epmet.feign.EpmetUserOpenFeignClient; |
||||
|
import com.epmet.service.CustomerAgencyService; |
||||
|
import com.epmet.service.StaffLoginLogService; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
/** |
||||
|
* 工作人员登录日志表 |
||||
|
* |
||||
|
* @author generator generator@elink-cn.com |
||||
|
* @since v1.0.0 2023-04-04 |
||||
|
*/ |
||||
|
@Service |
||||
|
public class StaffLoginLogServiceImpl extends BaseServiceImpl<StaffLoginLogDao, StaffLoginLogEntity> implements StaffLoginLogService { |
||||
|
@Autowired |
||||
|
private EpmetUserOpenFeignClient userOpenFeignClient; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 登录,插入记录 |
||||
|
* |
||||
|
* @param staffId |
||||
|
* @param loginTime |
||||
|
*/ |
||||
|
@Transactional(rollbackFor = Exception.class) |
||||
|
@Override |
||||
|
public void saveLog(String staffId, Date loginTime) { |
||||
|
CustomerStaffDTO form = new CustomerStaffDTO(); |
||||
|
form.setUserId(staffId); |
||||
|
Result<CustomerStaffDTO> result = userOpenFeignClient.getCustomerStaffInfoByUserId(form); |
||||
|
if (!result.success() || null == result.getData()) { |
||||
|
return; |
||||
|
} |
||||
|
CustomerStaffInfoCacheResult staffInfoCacheResult = CustomerStaffRedis.getStaffInfo(result.getData().getCustomerId(), staffId); |
||||
|
|
||||
|
StaffLoginLogEntity logEntity = new StaffLoginLogEntity(); |
||||
|
logEntity.setCustomerId(result.getData().getCustomerId()); |
||||
|
logEntity.setStaffId(staffId); |
||||
|
logEntity.setStaffName(staffInfoCacheResult.getRealName()); |
||||
|
logEntity.setMobile(staffInfoCacheResult.getMobile()); |
||||
|
logEntity.setAgencyId(staffInfoCacheResult.getAgencyId()); |
||||
|
CustomerAgencyDTO customerAgencyDTO = SpringContextUtils.getBean(CustomerAgencyService.class).get(staffInfoCacheResult.getAgencyId()); |
||||
|
logEntity.setAgencyLevel(customerAgencyDTO.getLevel()); |
||||
|
logEntity.setPid(customerAgencyDTO.getPid()); |
||||
|
if (StringUtils.isBlank(customerAgencyDTO.getPid()) || NumConstant.ZERO_STR.equals(customerAgencyDTO.getPid())) { |
||||
|
logEntity.setOrgIdPath(customerAgencyDTO.getId()); |
||||
|
} else { |
||||
|
logEntity.setOrgIdPath(customerAgencyDTO.getPids().concat(StrConstant.COLON).concat(customerAgencyDTO.getId())); |
||||
|
} |
||||
|
logEntity.setLoginTime(loginTime); |
||||
|
baseDao.insert(logEntity); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
|
|
||||
|
<mapper namespace="com.epmet.dao.StaffLoginLogDao"> |
||||
|
|
||||
|
<resultMap type="com.epmet.entity.StaffLoginLogEntity" id="staffLoginLogMap"> |
||||
|
<result property="id" column="ID"/> |
||||
|
<result property="customerId" column="CUSTOMER_ID"/> |
||||
|
<result property="staffId" column="STAFF_ID"/> |
||||
|
<result property="staffName" column="STAFF_NAME"/> |
||||
|
<result property="mobile" column="MOBILE"/> |
||||
|
<result property="agencyId" column="AGENCY_ID"/> |
||||
|
<result property="agencyLevel" column="AGENCY_LEVEL"/> |
||||
|
<result property="pid" column="PID"/> |
||||
|
<result property="orgIdPath" column="ORG_ID_PATH"/> |
||||
|
<result property="loginTime" column="LOGIN_TIME"/> |
||||
|
<result property="delFlag" column="DEL_FLAG"/> |
||||
|
<result property="revision" column="REVISION"/> |
||||
|
<result property="createdBy" column="CREATED_BY"/> |
||||
|
<result property="createdTime" column="CREATED_TIME"/> |
||||
|
<result property="updatedBy" column="UPDATED_BY"/> |
||||
|
<result property="updatedTime" column="UPDATED_TIME"/> |
||||
|
</resultMap> |
||||
|
|
||||
|
|
||||
|
</mapper> |
Loading…
Reference in new issue