|
@@ -1,15 +1,22 @@
|
|
package com.persagy.dc.digital.handler;
|
|
package com.persagy.dc.digital.handler;
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.persagy.dc.amqp.handler.AbstractRabbitHandlerTemplate;
|
|
import com.persagy.dc.amqp.handler.AbstractRabbitHandlerTemplate;
|
|
|
|
+import com.persagy.dc.common.constant.CommonConstant;
|
|
|
|
+import com.persagy.dc.common.context.AppContext;
|
|
import com.persagy.dc.common.exception.BusinessException;
|
|
import com.persagy.dc.common.exception.BusinessException;
|
|
-import com.persagy.dc.digital.config.ProjectMessageConfig;
|
|
|
|
|
|
+import com.persagy.dc.digital.config.DigitalRabbitConfig;
|
|
import com.persagy.dc.digital.entity.ObjectDigital;
|
|
import com.persagy.dc.digital.entity.ObjectDigital;
|
|
|
|
+import com.persagy.dc.digital.service.IObjectDigitalService;
|
|
|
|
+import com.persagy.dc.mybatis.helper.DynamicDataSourceHelper;
|
|
import com.rabbitmq.client.Channel;
|
|
import com.rabbitmq.client.Channel;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.amqp.core.Message;
|
|
import org.springframework.amqp.core.Message;
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
|
@@ -19,11 +26,14 @@ import java.io.IOException;
|
|
* @date 2021-06-25
|
|
* @date 2021-06-25
|
|
*/
|
|
*/
|
|
@Slf4j
|
|
@Slf4j
|
|
-@RabbitListener(queues = ProjectMessageConfig.PROJECT_QUEUE)
|
|
|
|
|
|
+@Component
|
|
|
|
+@RabbitListener(queues = DigitalRabbitConfig.PROJECT_QUEUE)
|
|
public class ProjectMessageHandler extends AbstractRabbitHandlerTemplate {
|
|
public class ProjectMessageHandler extends AbstractRabbitHandlerTemplate {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private ObjectMapper objectMapper;
|
|
private ObjectMapper objectMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IObjectDigitalService digitalService;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 消费
|
|
* 消费
|
|
@@ -35,10 +45,35 @@ public class ProjectMessageHandler extends AbstractRabbitHandlerTemplate {
|
|
protected void handler(String message, Channel channel, Message vo) {
|
|
protected void handler(String message, Channel channel, Message vo) {
|
|
try {
|
|
try {
|
|
ProjectMessageVO msgVO = objectMapper.readValue(message, ProjectMessageVO.class);
|
|
ProjectMessageVO msgVO = objectMapper.readValue(message, ProjectMessageVO.class);
|
|
|
|
+ initContextInfo(msgVO);
|
|
ObjectDigital project = msgVO.toObjectDigital();
|
|
ObjectDigital project = msgVO.toObjectDigital();
|
|
|
|
+ // 幂等处理 - 检查是否已经存在
|
|
|
|
+ ObjectDigital dbVO = digitalService.load(project.getId());
|
|
|
|
+ if(dbVO == null) {
|
|
|
|
+ digitalService.insert(CollUtil.newArrayList(project));
|
|
|
|
+ } else {
|
|
|
|
+ digitalService.update(CollUtil.newArrayList(project));
|
|
|
|
+ }
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
throw new BusinessException(e.getMessage(), e);
|
|
throw new BusinessException(e.getMessage(), e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 初始化上下文,切换数据源
|
|
|
|
+ * @param vo
|
|
|
|
+ */
|
|
|
|
+ private void initContextInfo(ProjectMessageVO vo) {
|
|
|
|
+ AppContext.getContext().setGroupCode(vo.getGroupCode());
|
|
|
|
+ AppContext.getContext().setProjectId(vo.getProjectId());
|
|
|
|
+ AppContext.getContext().setAppId("BOSS");
|
|
|
|
+ String userId = vo.getUpdateUser();
|
|
|
|
+ // 无用户时,默认为默认系统用户
|
|
|
|
+ if(StrUtil.isBlank(userId)) {
|
|
|
|
+ userId = CommonConstant.DEFAULT_ID;
|
|
|
|
+ }
|
|
|
|
+ AppContext.getContext().setAccountId(userId);
|
|
|
|
+ DynamicDataSourceHelper.loadDataSource();
|
|
|
|
+ }
|
|
}
|
|
}
|