springboot aop 改写jar包中的某些执行逻辑
·
背景:改写jar包中部分执行逻辑,又不想整个复写
package ies.aspect;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import groovy.util.logging.Slf4j;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import static org.reflections.Reflections.log;
/**
* 针对区域展示要求修改代码
*/
@Slf4j
@Aspect
@Component
public class DisclosureAspect {
private static final Logger log = LoggerFactory.getLogger(InfoDisclosureServiceImpl.class);
@Autowired
private BaseOrganizationService baseOrganizationService;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private InfoDisclosureFileRepository disclosureFileRepository;
@Autowired
private InfoDisclosureReadRecordRepository infoDisclosureReadRecordRepository;
@Autowired
private InfoDisclosureFileService infoDisclosureFileService;
private static String LOAD_CENTER = "四川电力负荷管理中心";
private static String TRADE_CENTER = "四川电力交易中心";
private static String DISPATCH_CENTER = "四川电力调度控制中心";
private static String LOAD_CENTER_ID = "SICHUAN_LOAD_CENTER_ID";
private static String TRADE_CENTER_ID = "SICHUAN_TRADE_CENTER_ID";
private static String DISPATCH_CENTER_ID = "SICHUAN_DISPATCH_CENTER_ID";
private static String NANFANG_TRADE_CENTER_ID = "1428617215925608497";
@Value("${geekbidder.disclosure.unread.start:2024-09-05}")
private String showUnReadStartDate;
private Map<String, Object> obj2Map(Object obj) {
return this.objectMapper.convertValue(obj, new TypeReference<Map<String, Object>>() {
});
}
@Pointcut("execution(* cn.com.gb.infodisclosure.service.impl.InfoDisclosureServiceImpl.getDisclosureFiles(..))")
public void getDisclosureFilesPointcut() {}
@Pointcut("execution(* cn.com.gb.infodisclosure.service.impl.InfoDisclosureServiceImpl.getDisclosureFileRecords(..))")
public void getDisclosureFileRecords() {}
@Pointcut("execution(* cn.com.gb.infodisclosure.service.impl.InfoDisclosureServiceImpl.getDisclosureUnReadFile(..))")
public void getDisclosureUnReadFile() {}
@Around(value = "getDisclosureFilesPointcut() && args(orgId, menuId, tabName)", argNames = "joinPoint,orgId,menuId,tabName")
public Object aroundGetDisclosureFiles(ProceedingJoinPoint joinPoint, String orgId, String menuId, String tabName) {
// 不执行原方法,而是调用自定义方法
log.info("拦截到getDisclosureFiles方法调用,参数: orgId={}, menuId={}, tabName={}", orgId, menuId, tabName);
// 不执行原方法,而是调用电力交易中心数据接口数据获取接口
List<Organization> orgs = this.baseOrganizationService.getInnerChildrenByOrgId(orgId);
if (CollectionUtils.isEmpty(orgs)) {
return Collections.emptyList();
} else {
// orgs = this.organizationService.filterOrgByPermission(orgs);
List<Map<String, Object>> res = new ArrayList();
// List<InfoDisclosureFileEntity> entities = Lists.newArrayList();
Map<String, Object> loadCenter = new HashMap();
loadCenter.put("name", LOAD_CENTER);
loadCenter.put("id", LOAD_CENTER_ID);
Map<String, Object> tradeCenter = new HashMap();
tradeCenter.put("name", TRADE_CENTER);
tradeCenter.put("id", TRADE_CENTER_ID);
Map<String, Object> dispatchCenter = new HashMap();
dispatchCenter.put("name", DISPATCH_CENTER);
dispatchCenter.put("id", DISPATCH_CENTER_ID);
List<String> subjectIds = new ArrayList();
subjectIds.add(LOAD_CENTER_ID);
subjectIds.add(TRADE_CENTER_ID);
subjectIds.add(DISPATCH_CENTER_ID);
res.add(0, dispatchCenter);
res.add(0, tradeCenter);
res.add(0, loadCenter);
List<InfoDisclosureFileEntity> fileList =
this.disclosureFileRepository.getFilesBySubjectIds(subjectIds);
fileList.forEach((entity) -> {
Map<String, Object> map = this.obj2Map(entity);
map.put("parentId", entity.getSubjectId());
map.put("name", entity.getFileName());
res.add(map);
});
return res;
}
}
@Around(value = "getDisclosureFileRecords() && args(orgId, start, end)", argNames = "joinPoint,orgId,start,end")
public Object aroundGetDisclosureFileRecords(ProceedingJoinPoint joinPoint, String orgId, LocalDate start, LocalDate end) throws Throwable {
List<String> sourceList = Arrays.asList(LOAD_CENTER_ID, TRADE_CENTER_ID, DISPATCH_CENTER_ID);
List<InfoDisclosureFileRecordEntity> fileRecords = this.infoDisclosureFileService.getByFileCodeAnSubjectIdAndDate(sourceList, start, end);
if (CollectionUtils.isEmpty(fileRecords)) {
return null;
} else {
List<Map<String, Object>> list = BeanHelper.map(fileRecords, this::obj2Map);
List<InfoDisclosureReadRecordEntity> readRecordEntities = this.infoDisclosureReadRecordRepository.listByDisclosureIdsAndUserId(BaseContextUtil.getUserId());
Map<String, Long> readMap = readRecordEntities.stream().collect(Collectors.groupingBy(InfoDisclosureReadRecordEntity::getDisUploadId, Collectors.counting()));
list.forEach((v) -> {
LocalDateTime createTime = v.get("createTime") != null && !StringUtils.isEmpty(v.get("createTime").toString()) ? LocalDateTime.parse(String.valueOf(v.get("createTime")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : null;
if (createTime != null && !createTime.isBefore(LocalDateTime.of(LocalDate.parse(this.showUnReadStartDate), LocalTime.MIN))) {
v.put("readStatus", readMap.getOrDefault(String.valueOf(v.get("id")), 0L) > 0L);
} else {
v.put("readStatus", true);
}
});
return list;
}
}
@Around(value = "getDisclosureUnReadFile() && args(orgId, start, end)", argNames = "joinPoint,orgId,start,end")
public Object aroundGetDisclosureUnReadFile(ProceedingJoinPoint joinPoint, String orgId, LocalDate start, LocalDate end) throws Throwable {
List<String> ids = Arrays.asList(LOAD_CENTER_ID, TRADE_CENTER_ID, DISPATCH_CENTER_ID);
List<InfoDisclosureFileRecordEntity> fileRecords = this.infoDisclosureFileService.getByFileCodeAnSubjectIdAndDate(ids, start, end).stream().filter((v) ->
v.getCreateTime()
.isAfter(LocalDateTime.of(LocalDate.parse(this.showUnReadStartDate), LocalTime.MIN))).collect(Collectors.toList());
if (CollectionUtils.isEmpty(fileRecords)) {
return Collections.emptyMap();
} else {
Map<LocalDate, Integer> res = new HashMap();
List<InfoDisclosureReadRecordEntity> readRecordEntities = this.infoDisclosureReadRecordRepository.listByDisclosureIdsAndUserId(BaseContextUtil.getUserId());
List<String> disUploadIds = readRecordEntities.stream().map(InfoDisclosureReadRecordEntity::getDisUploadId).collect(Collectors.toList());
fileRecords.stream().collect(Collectors.groupingBy(InfoDisclosureFileRecordEntity::getInfoDate)).values().forEach((vs) -> {
res.put(vs.get(0).getInfoDate(),
(int) vs.stream().filter((v) -> !disUploadIds.contains(String.valueOf(v.getId()))).count());
});
return res;
}
}
}
更多推荐



所有评论(0)