数据同步相关代码

cmt1.0
dearlin 2023-12-17 15:44:38 +08:00
parent 7073565e0b
commit 9757d08f69
6 changed files with 278 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package com.zcloud.aspect;
import java.lang.annotation.*;
/**
* @Description:
* @Author: dearLin
* @Date: 2023/3/8
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DockAnnotation {
// 是否有额外数据
boolean isAdd() default false;
//是否有附件
boolean hasAnnex() default false;
}

View File

@ -0,0 +1,146 @@
package com.zcloud.aspect;
import com.alibaba.fastjson.JSON;
import com.zcloud.dto.TenCorpDto;
import com.zcloud.entity.PageData;
import com.zcloud.service.docking.DockingRelationService;
import com.zcloud.service.mq.SendMessageService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@Component
@Aspect
public class DockingAspect {
@Autowired
private SendMessageService sendMessageService;
@Autowired
private DockingRelationService dockingRelationService;
@Pointcut("@annotation(com.zcloud.aspect.DockAnnotation)")
public void pointcut() {}
public static Map<String, String> dockingRelationMap = new HashMap();
// 增强逻辑:在切点方法执行前打印日志
@Around("pointcut()")
public Object beforeApi(ProceedingJoinPoint joinPoint) throws Throwable {
Map<String,String> proceed = (HashMap<String,String>)joinPoint.proceed();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
try {
HttpServletRequest request = getRequest();
//获取资源映射路径servlet路径
String servletPath = request.getServletPath();
//验证该数据是否需要对接
System.out.println("------------------------切面方法执行------------------------");
//将路径换为对应的访问路径
/*servletPath=dockingRelationMap.get(servletPath);
//完整访问路径(访问港务局的路径,不是被访问的路径)
String fullPath = gwjUrl+servletPath;
System.out.println("完整路径:"+fullPath);*/
//获取所有访问参数
PageData pageData = getPageData();
DockAnnotation annotation = method.getAnnotation(DockAnnotation.class);
if (annotation != null) {
if (annotation.isAdd()){
if (Tools.notEmpty(proceed.get("dockData"))){
pageData.put("dockData",proceed.get("dockData"));
}
}else if (annotation.hasAnnex()) {
//查找附件
String dockDataJson = pageData.getString("dockData");
if (Tools.notEmpty(dockDataJson)){
Map<String,String> dockData = (Map<String,String>) JSON.parse(dockDataJson);
String img = dockData.get("img");
for (String imgurl : img.split(",")) {
//获取附件名称
String[] split = imgurl.split("/");
String filename = split[split.length - 1];
PageData imgpd = new PageData();
imgpd.put("imgUrl",imgurl);
imgpd.put("filename",filename);
imgpd.put("url","/file/saveImg");
imgpd.put("TARGETURL","https://qgqy.qhdsafety.com/file/");
imgpd.put("ORIGINURL","https://qgqy.qhdsafety.com/file/");
TenCorpDto tenCorpDto = new TenCorpDto();
tenCorpDto.setMessage("一条新增消息");
tenCorpDto.setData(imgpd);
tenCorpDto.setId(UuidUtil.get32UUID());
tenCorpDto.setTopic("docking");
tenCorpDto.setProducer_name("qa-prevention-czks");
sendMessageService.sendMessage(tenCorpDto);
}
}
}
}
PageData login_user = new PageData();
login_user.put("USER_ID",Jurisdiction.getUSER_ID());
login_user.put("USERNAME",Jurisdiction.getUsername());
login_user.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
login_user.put("DEPARTMENT_ID",Jurisdiction.getDEPARTMENT_ID());
login_user.put("NAME",Jurisdiction.getName());
login_user.put("ISSUPERVISE",Jurisdiction.getISSUPERVISE());
login_user.put("POST_ID",Jurisdiction.getPOST_ID());
pageData.put("LOGIN_USER",login_user);
pageData.put("url",servletPath);
/*//连接redis
Jedis jedis = new Jedis(host,port);
//测试是否连接成功
String ping = jedis.ping();
System.out.println(ping);
//切换数据库
String select = jedis.select(0);
System.out.println(select);
//存储数据至redis
String rq = JSON.toJSONString(map);
jedis.zadd("request",0,rq);
System.out.println(rq);*/
TenCorpDto tenCorpDto = new TenCorpDto();
tenCorpDto.setMessage("一条新增消息");
tenCorpDto.setData(pageData);
tenCorpDto.setId(UuidUtil.get32UUID());
tenCorpDto.setTopic("docking");
tenCorpDto.setProducer_name("qa-prevention-czks");
sendMessageService.sendMessage(tenCorpDto);
System.out.println("------------------------方法执行完毕------------------------");
} catch (Exception e) {
throw new RuntimeException(e);
}
return proceed;
}
@PostConstruct
private void initDockingRelationship() throws Exception {
dockingRelationMap = dockingRelationService.listAll(null);
}
private PageData getPageData() {
return new PageData(this.getRequest());
}
/**
* request
* @return
*/
private HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return request;
}
}

View File

@ -0,0 +1,16 @@
package com.zcloud.mapper.datasource.docking;
import com.zcloud.entity.PageData;
import java.util.List;
public interface DockingRelationMapper {
/**()
* @param pd
* @throws Exception
*/
List<PageData> listAll(PageData pd);
}

View File

@ -0,0 +1,22 @@
package com.zcloud.service.docking;
import com.zcloud.entity.PageData;
import java.util.Map;
/**
*
* luoxiaobao
* 2020-12-24
* www.zcloudchina.com
*/
public interface DockingRelationService {
/**()
* @param pd
* @throws Exception
*/
public Map<String,String> listAll(PageData pd)throws Exception;
}

View File

@ -0,0 +1,43 @@
package com.zcloud.service.docking.impl;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.docking.DockingRelationMapper;
import com.zcloud.service.docking.DockingRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* luoxiaobao
* 2020-12-24
* www.zcloudchina.com
*/
@Service
@Transactional //开启事物
public class DockingRelationServiceImpl implements DockingRelationService {
@Autowired
private DockingRelationMapper dockingRelationMapper;
/**()
* @param pd
* @throws Exception
*/
public Map<String,String> listAll(PageData pd)throws Exception{
HashMap<String, String> relationMap = new HashMap<>();
List<PageData> relationList = dockingRelationMapper.listAll(pd);
for (PageData pageData : relationList) {
String originurl = pageData.getString("ORIGINURL");
String targeturl = pageData.getString("TARGETURL");
relationMap.put(originurl,targeturl);
}
return relationMap;
}
}

View File

@ -0,0 +1,33 @@
<?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.zcloud.mapper.datasource.docking.DockingRelationMapper">
<!--表名 -->
<sql id="tableName">
SYS_DOCKING_RELATION
</sql>
<sql id="Field">
f.DOCKING_RRELATION_ID,
f.ORIGINURL,
f.TARGETURL
</sql>
<!-- 字段值 -->
<sql id="FieldValue">
#{DOCKING_RRELATION_ID},
#{ORIGINURL},
#{TARGETURL}
</sql>
<!-- 列表(全部) -->
<select id="listAll" parameterType="pd" resultType="pd">
select
<include refid="Field"></include>
from
<include refid="tableName"></include> f
where 1=1
</select>
</mapper>