对象历史功能完成
parent
787af63537
commit
85b54f6cd6
@ -0,0 +1,169 @@
|
||||
package cn.estsh.i3plus.core.apiservice.mq;
|
||||
|
||||
import cn.estsh.i3plus.core.api.iservice.busi.ISysPojoVersionDetailService;
|
||||
import cn.estsh.i3plus.core.api.iservice.busi.ISysPojoVersionService;
|
||||
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
|
||||
import cn.estsh.i3plus.platform.common.tool.JsonUtilTool;
|
||||
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
|
||||
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
|
||||
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
||||
import cn.estsh.i3plus.pojo.platform.bean.SysPojoVersion;
|
||||
import cn.estsh.i3plus.pojo.platform.bean.SysPojoVersionDetail;
|
||||
import cn.estsh.impp.framework.boot.util.ImppRedis;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.MapDifference;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.estsh.i3plus.platform.common.util.PlatformConstWords.QUEUE_IMPP_POJO_VERSION;
|
||||
|
||||
/**
|
||||
* @Description :
|
||||
* 对象版本记录
|
||||
* @Reference :
|
||||
* @Author : wei.peng
|
||||
* @CreateDate : 20-4-26 下午2:32
|
||||
* @Modify:
|
||||
**/
|
||||
@Component
|
||||
@ConditionalOnExpression("'${impp.mq.queue.letter}' == 'true'")
|
||||
public class PojoVersionQueueReceiver {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PojoVersionQueueReceiver.class);
|
||||
|
||||
@Autowired
|
||||
private ISysPojoVersionService pojoVersionService;
|
||||
|
||||
@Autowired
|
||||
private ISysPojoVersionDetailService pojoVersionDetailService;
|
||||
|
||||
@Autowired
|
||||
private SnowflakeIdMaker snowflakeIdMaker;
|
||||
|
||||
@Resource(name= CommonConstWords.IMPP_REDIS_RES)
|
||||
private ImppRedis redisRes;
|
||||
|
||||
/**
|
||||
* QUEUE_SWEB_NOTICE 队列
|
||||
* @return 队列
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean
|
||||
public Queue getQueueSwebNoticeQueue(){
|
||||
return new Queue(QUEUE_IMPP_POJO_VERSION);
|
||||
}
|
||||
|
||||
@RabbitListener(queues = QUEUE_IMPP_POJO_VERSION)
|
||||
public void processImppMessage(Channel channel, Message message) {
|
||||
boolean isNack = false;
|
||||
try {
|
||||
byte[] messageBody = message.getBody();
|
||||
if(messageBody != null && messageBody.length > 0){
|
||||
String msg = new String(messageBody);
|
||||
if(StringUtils.isNotBlank(msg)){
|
||||
// 当前操作数据
|
||||
SysPojoVersion afterVersion = JSON.parseObject(msg, SysPojoVersion.class);
|
||||
|
||||
// 是否进行版本控制
|
||||
Object redisValue = redisRes.getObject(CommonConstWords.REDIS_PREFIX_POJO_VERSION + ":" + afterVersion.getSoftType() + ":" + afterVersion.getRefClass());
|
||||
if(Objects.nonNull(redisValue)){
|
||||
Map<String,Object> afterMap = JsonUtilTool.decode(afterVersion.getBean(), Map.class);
|
||||
SysPojoVersionDetail afterVersionDetail = null;
|
||||
|
||||
// 已存在数据
|
||||
SysPojoVersion beforeVersion = null;
|
||||
SysPojoVersionDetail beforeVersionDetail = null;
|
||||
Map<String,Object> beforeMap = new HashMap<>();
|
||||
|
||||
Integer versionNo; // 对象版本
|
||||
CommonEnumUtil.DAO_OPERATE_TYPE operateType = CommonEnumUtil.DAO_OPERATE_TYPE.INSERT; // 操作类型
|
||||
Map<String, MapDifference.ValueDifference<Object>> differing; // 版本对比
|
||||
String pojoCompare = null;
|
||||
String userName = StringUtils.isNotBlank(afterVersion.getModifyUser()) ? afterVersion.getModifyUser() : afterVersion.getCreateUser();
|
||||
|
||||
if(!"insert".equals(afterVersion.getVersionMethodName())){ // 不是新增数据的时候则需要查询历史记录
|
||||
// 已存在数据封装
|
||||
if(afterVersion != null){
|
||||
afterVersion.setOrderByParam("createDatetime");
|
||||
afterVersion.setAscOrDesc(CommonEnumUtil.ASC_OR_DESC.DESC.getValue());
|
||||
beforeVersion = pojoVersionService.getPojoVersion(afterVersion);
|
||||
if(Objects.nonNull(beforeVersion)){
|
||||
beforeVersionDetail = pojoVersionDetailService.get(beforeVersion.getPojoDetailId());
|
||||
if(Objects.nonNull(beforeVersionDetail)){
|
||||
beforeMap.putAll(JsonUtilTool.decode(beforeVersionDetail.getPojoAfter(), Map.class));
|
||||
}
|
||||
}
|
||||
operateType = CommonEnumUtil.DAO_OPERATE_TYPE.UPDATE;
|
||||
}
|
||||
}
|
||||
|
||||
// 不保存字段 数据剔除
|
||||
for (String key : CommonConstWords.POJO_VERSION_SAVE_ATTR_REMOVE) {
|
||||
afterMap.remove(key);
|
||||
}
|
||||
|
||||
|
||||
differing = Maps.difference(beforeMap, afterMap).entriesDiffering();
|
||||
// 忽略版本控制属性
|
||||
if(differing != null ){
|
||||
for (String key : CommonConstWords.POJO_VERSION_ATTR_INGNORE) {
|
||||
if(differing.containsKey(key)){
|
||||
differing.remove(key);
|
||||
}
|
||||
}
|
||||
pojoCompare = differing.toString();
|
||||
}
|
||||
|
||||
versionNo = Objects.nonNull(beforeVersion) ? beforeVersion.getVersionNo() : 0;
|
||||
afterVersion.setVersionNo(++versionNo);
|
||||
|
||||
afterVersion.setId(snowflakeIdMaker.nextId());
|
||||
afterVersionDetail = new SysPojoVersionDetail();
|
||||
afterVersionDetail.setId(snowflakeIdMaker.nextId());
|
||||
afterVersionDetail.setPojoVersionId(afterVersion.getId());
|
||||
afterVersionDetail.setPojoBefore(JSON.toJSONString(beforeMap));
|
||||
afterVersionDetail.setPojoAfter(JSON.toJSONString(afterMap));
|
||||
afterVersionDetail.setPojoCompare(pojoCompare);
|
||||
ConvertBean.serviceModelInitialize(afterVersionDetail,userName);
|
||||
pojoVersionDetailService.insert(afterVersionDetail);
|
||||
|
||||
afterVersion.setOperateType(operateType.getValue());
|
||||
afterVersion.setVersionNo(versionNo);
|
||||
afterVersion.setPojoCompare(pojoCompare);
|
||||
afterVersion.setPojoDetailId(afterVersionDetail.getId());
|
||||
ConvertBean.serviceModelInitialize(afterVersion,userName);
|
||||
pojoVersionService.insert(afterVersion);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
if(isNack){
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
}else{
|
||||
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
|
||||
}
|
||||
}catch (Exception e){
|
||||
LOGGER.error("Pojo Version MQ ACK Error Message :{}",e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue