对象历史功能完成

yun-zuoyi
wei.peng 5 years ago
parent 787af63537
commit 85b54f6cd6

@ -49,4 +49,7 @@ public interface ISystemInitService {
@ApiOperation(value = "重新加载Elasticsearch 客户端信息")
void reloadElasticSearchClient();
@ApiOperation(value = "对象版本记录信息")
void reloadPojoVersionPlan();
}

@ -11,4 +11,7 @@ import cn.estsh.impp.framework.base.service.ICrudService;
* @Modify:
**/
public interface ISysPojoVersionService extends ICrudService<SysPojoVersion> {
SysPojoVersion getPojoVersion(SysPojoVersion bean);
}

@ -5,7 +5,9 @@ import cn.estsh.i3plus.platform.plugin.elasticsearch.ElasticSearchPack;
import cn.estsh.i3plus.platform.plugin.elasticsearch.ElasticSearchTool;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.model.license.ImppLicense;
import cn.estsh.i3plus.pojo.platform.bean.SysLogUserLogin;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogOperate;
import cn.estsh.i3plus.pojo.platform.repository.SysLogUserLoginRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysUserRepository;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
@ -14,12 +16,15 @@ import cn.estsh.impp.framework.boot.license.ImppLicenseTool;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
/**
@ -33,12 +38,10 @@ import java.util.List;
@Api(tags = "测试Demo ")
@RequestMapping("/impp/demo")
public class DemoController extends CoreBaseController {
public static final Logger LOGGER = LoggerFactory.getLogger(DemoAuthController.class);
@Autowired
private CoreLogClearJob coreLogClearJob;
@Autowired
private SysUserRepository repository;
private SysLogUserLoginRepository repository;
@GetMapping(value = "/query")
@ -108,29 +111,26 @@ public class DemoController extends CoreBaseController {
}
}
// @GetMapping(value = "/rabbitmq")
// @ApiOperation(value = "repository 测试")
// public ResultBean imppChannel() {
// try{
//
// String message = ImppSystemTool.getServerHost() + ":" + ImppSystemTool.getServerPort()
// + ":" + ImppSystemTool.getServerPid() + "Hello ->" + System.currentTimeMillis();
// mqChannel.putMessage(message);
// Thread.sleep(100);
// return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
// } catch (ImppBusiException busExcep) {
// return ResultBean.fail(busExcep).build();
// } catch (Exception e) {
// return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
// }
// }
public static void main(String[] args)throws Exception {
ResolvableType resolvableType = ResolvableType.forClass(SysUserRepository.class).as(SysUserRepository.class);
System.out.println(resolvableType.getGeneric(0));//User
System.out.println(resolvableType.getGeneric(1));//Long
@GetMapping(value = "/bojo-version")
@ApiOperation(value = "repository 测试")
public ResultBean pojoVersion(SysLogUserLogin logUserLogin) {
try{
// LOGGER.debug("Hello World !~ ");
// SysLogUserLogin logUserLogin = new SysLogUserLogin();
// logUserLogin.setId(1000L);
// logUserLogin.setUserId(10086L);
// logUserLogin.setLogLoginName("张三");
repository.saveAll(Arrays.asList(new SysLogUserLogin[]{logUserLogin}));
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
} catch (ImppBusiException busExcep) {
return ResultBean.fail(busExcep).build();
} catch (Exception e) {
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}

@ -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());
}
}
}
}

@ -3,6 +3,7 @@ package cn.estsh.i3plus.core.apiservice.serviceimpl.base;
import cn.estsh.i3plus.core.api.iservice.base.ISystemInitService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysLocaleResourceService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysMenuService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysPojoVersionPlanService;
import cn.estsh.i3plus.core.api.iservice.busi.ISystemResourceService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
@ -21,6 +22,7 @@ import cn.estsh.impp.framework.boot.util.ImppRedis;
import cn.estsh.impp.framework.boot.util.ImppSystemTool;
import cn.estsh.impp.framework.boot.util.RedisCacheTool;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -63,6 +65,9 @@ public class SystemInitService implements ISystemInitService {
@Autowired
private ISysMenuService menuService;
@Autowired
private ISysPojoVersionPlanService pojoVersionPlanService;
@Resource(name= CommonConstWords.IMPP_REDIS_RES)
private ImppRedis redisRes;
@ -80,6 +85,10 @@ public class SystemInitService implements ISystemInitService {
reloadElasticSearchClient();
LOGGER.info("加载ElasticSearch Client Url 信息完成");
LOGGER.info("加载 对象版本控制信息");
reloadPojoVersionPlan();
LOGGER.info("加载 对象版本控制信息 信息完成");
}
@Override
@ -368,4 +377,15 @@ public class SystemInitService implements ISystemInitService {
LOGGER.info("加载ElasticSearch Client Url:{}",url);
ElasticSearchTool.init(url);
}
@Override
public void reloadPojoVersionPlan() {
List<SysPojoVersionPlan> list = pojoVersionPlanService.findAll();
if(CollectionUtils.isNotEmpty(list)){
for (SysPojoVersionPlan plan : list) {
String redisKey = CommonConstWords.REDIS_PREFIX_POJO_VERSION + ":" + plan.getSoftType() + ":" + plan.getRefClass();
redisRes.putObject(redisKey,plan,-1);
}
}
}
}

@ -1,6 +1,7 @@
package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
import cn.estsh.i3plus.core.api.iservice.busi.ISysPojoVersionService;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.jpa.dao.BaseRepository;
import cn.estsh.i3plus.pojo.platform.bean.SysPojoVersion;
import cn.estsh.i3plus.pojo.platform.repository.SysPojoVersionRepository;
@ -25,4 +26,10 @@ public class SysPojoVersionService extends CrudService<SysPojoVersion> implement
public BaseRepository<SysPojoVersion, Long> getRepository() {
return pojoVersionRDao;
}
@Override
public SysPojoVersion getPojoVersion(SysPojoVersion bean) {
return pojoVersionRDao.getByProperty(DdlPackBean.getDdlPackBean(bean));
}
}

Loading…
Cancel
Save