fix:优化行程规划分页查询的逻辑
This commit is contained in:
parent
c1ef9fa1db
commit
60dc853b57
|
@ -163,7 +163,7 @@ public class LoginController {
|
|||
CompletableFuture.runAsync(() -> {
|
||||
Command.of(UserLoginCommand.class).user(user).sendToQueue();
|
||||
user.setSyncTime(LocalDateTime.now());
|
||||
userRepository.save(user);
|
||||
userRepository.saveAndUpdateCache(user);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ import java.time.LocalDate;
|
|||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -159,9 +160,12 @@ public class OrderQuery {
|
|||
|
||||
int total = routeOrders.size();
|
||||
routeApprovalDomainService.checkApprovalPermissions(routeOrders);
|
||||
// Set<String> employeeNoSet = routeOrders.stream().filter(it -> it.getApproveOrderNo() != null)
|
||||
// .flatMap(it -> Stream.of(it.getUserId(), it.getApproveOrderNo().getCreator()))
|
||||
// .collect(Collectors.toSet());
|
||||
Set<String> employeeNoSet = routeOrders.stream().filter(it -> it.getApproveOrderNo() != null)
|
||||
.flatMap(it -> Stream.of(it.getUserId(), it.getApproveOrderNo().getCreator()))
|
||||
.collect(Collectors.toSet());
|
||||
Map<String, User> userMap = userRepository.findByEmployeeNoIn(employeeNoSet)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(User::getEmployeeNo, Function.identity()));
|
||||
List<RouteOrderPageRes> orders = routeOrders
|
||||
.stream()
|
||||
.sorted(statusComparator.thenComparing(RouteOrder::getUpdateTime).reversed())
|
||||
|
@ -171,7 +175,7 @@ public class OrderQuery {
|
|||
if (legDomainService.checkLocation(routeOrder.getLegItems())) {
|
||||
routeRepository.reloadRouteOrderInCache(routeOrder);
|
||||
}
|
||||
User user = userRepository.findByUserEmployeeNo(routeOrder.getUserId());
|
||||
User user = userMap.get(routeOrder.getUserId());
|
||||
return getRouteOrderPageRes(routeOrder, user);
|
||||
})
|
||||
.toList();
|
||||
|
|
|
@ -15,6 +15,7 @@ import com.chint.infrastructure.echo_framework.command.Command;
|
|||
import com.chint.infrastructure.util.BaseContext;
|
||||
import com.chint.infrastructure.util.StringCheck;
|
||||
import com.chint.interfaces.rest.data_center.user.dto.UserDataDTO;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -57,6 +58,7 @@ public class User implements Serializable {
|
|||
private String gender;
|
||||
private String phoneNumber;
|
||||
private String password;
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime syncTime;
|
||||
private Integer status;
|
||||
@Transient
|
||||
|
|
|
@ -3,16 +3,17 @@ package com.chint.domain.repository;
|
|||
import com.chint.domain.aggregates.user.User;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public interface UserRepository {
|
||||
User findById(Long id);
|
||||
Optional<User> findById(Long id);
|
||||
|
||||
User findByUserEmployeeNo(String employeeNo);
|
||||
|
||||
List<User> findByEmployeeNoIn(Set<String> employeeNoSet);
|
||||
|
||||
User save(User user);
|
||||
User saveAndUpdateCache(User user);
|
||||
|
||||
void saveAll(List<User> userList);
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ public class AuthenticateServiceImpl implements AuthenticateService {
|
|||
//如果数据库不存在该用户,需要通过sf信息进行查询并保存到数据库
|
||||
newUser.setCompanyCode(userLoginParam.getCompanyCode());
|
||||
httpRequest.loadUserInfo(newUser);
|
||||
userRepository.save(newUser);
|
||||
userRepository.saveAndUpdateCache(newUser);
|
||||
newUser.setUserLoginParam(userLoginParam);
|
||||
return getUserLoginResult(newUser);
|
||||
}
|
||||
|
|
|
@ -64,12 +64,10 @@ public class LYOrderRecordDomainService {
|
|||
|
||||
|
||||
@ListenTo(command = "LYOrderRecordGenerateCommand", order = 0)
|
||||
@Transactional
|
||||
public void generateFlightRecord(LYOrderRecordGenerateCommand command) {
|
||||
int count = 0;//初始化变量
|
||||
int pageSize = 100; // 假设每页大小为10
|
||||
int LoopNum;//需要的循环数
|
||||
ArrayList<LyOrderFlightRecord> recordsTotal = new ArrayList<>();
|
||||
do {
|
||||
PageRequest pageResult = PageRequest.of(count, pageSize);
|
||||
Page<LyOrderFlightRecord> lyOrderFlightRecordPage = jdbcLyOrderFlightRecord.findAllByStatusIsNull(pageResult);
|
||||
|
@ -77,34 +75,31 @@ public class LYOrderRecordDomainService {
|
|||
LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整
|
||||
//数据
|
||||
List<LyOrderFlightRecord> lyOrderFlightRecordPageList = lyOrderFlightRecordPage.toList();
|
||||
recordsTotal.addAll(lyOrderFlightRecordPageList);
|
||||
List<String> routeOrderNoList = lyOrderFlightRecordPageList
|
||||
.stream()
|
||||
.map(LyOrderFlightRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderFlightRecord> orderFlightRecordList = lyOrderFlightRecordPageList
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createFlightOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveFlightOrderRecordBatch(orderFlightRecordList);
|
||||
jdbcLyOrderFlightRecord.updateStatusByNullStatus();
|
||||
count++;
|
||||
} while (count < LoopNum);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = recordsTotal
|
||||
.stream()
|
||||
.map(LyOrderFlightRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderFlightRecord> orderFlightRecordList = recordsTotal
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createFlightOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveFlightOrderRecordBatch(orderFlightRecordList);
|
||||
jdbcLyOrderFlightRecord.updateStatusByNullStatus();
|
||||
}
|
||||
|
||||
|
||||
@ListenTo(command = "LYOrderRecordGenerateCommand", order = 1)
|
||||
@Transactional
|
||||
public void generateHotelRecord(LYOrderRecordGenerateCommand command) {
|
||||
int count = 0;//初始化变量
|
||||
int pageSize = 100; // 假设每页大小为10
|
||||
int LoopNum;//需要的循环数
|
||||
ArrayList<LyOrderHotelRecord> recordsTotal = new ArrayList<>();
|
||||
do {
|
||||
PageRequest pageResult = PageRequest.of(count, pageSize);
|
||||
Page<LyOrderHotelRecord> lyOrderHotelRecordPage = jdbcLyOrderHotelRecord.findAllByStatusIsNull(pageResult);
|
||||
|
@ -112,34 +107,32 @@ public class LYOrderRecordDomainService {
|
|||
LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整
|
||||
//数据
|
||||
List<LyOrderHotelRecord> lyOrderHotelRecordPageList = lyOrderHotelRecordPage.toList();
|
||||
recordsTotal.addAll(lyOrderHotelRecordPageList);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = lyOrderHotelRecordPageList
|
||||
.stream()
|
||||
.map(LyOrderHotelRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderHotelRecord> orderHotelRecordList = lyOrderHotelRecordPageList
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createHotelOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveHotelOrderRecordBatch(orderHotelRecordList);
|
||||
jdbcLyOrderHotelRecord.updateStatusByNullStatus();
|
||||
count++;
|
||||
} while (count < LoopNum);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = recordsTotal
|
||||
.stream()
|
||||
.map(LyOrderHotelRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderHotelRecord> orderHotelRecordList = recordsTotal
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createHotelOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveHotelOrderRecordBatch(orderHotelRecordList);
|
||||
jdbcLyOrderHotelRecord.updateStatusByNullStatus();
|
||||
}
|
||||
|
||||
|
||||
@ListenTo(command = "LYOrderRecordGenerateCommand", order = 2)
|
||||
@Transactional
|
||||
public void generateTrainRecord(LYOrderRecordGenerateCommand command) {
|
||||
int count = 0;//初始化变量
|
||||
int pageSize = 100; // 假设每页大小为10
|
||||
int LoopNum;//需要的循环数
|
||||
ArrayList<LyOrderTrainRecord> recordsTotal = new ArrayList<>();
|
||||
do {
|
||||
PageRequest pageResult = PageRequest.of(count, pageSize);
|
||||
Page<LyOrderTrainRecord> lyOrderTrainRecordPage = jdbcLyOrderTrainRecord.findAllByStatusIsNull(pageResult);
|
||||
|
@ -147,34 +140,32 @@ public class LYOrderRecordDomainService {
|
|||
LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整
|
||||
//数据
|
||||
List<LyOrderTrainRecord> lyOrderTrainRecordPageList = lyOrderTrainRecordPage.toList();
|
||||
recordsTotal.addAll(lyOrderTrainRecordPageList);
|
||||
List<String> routeOrderNoList = lyOrderTrainRecordPageList
|
||||
.stream()
|
||||
.map(LyOrderTrainRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderTrainRecord> orderTrainRecordList = lyOrderTrainRecordPageList
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createTrainOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveTrainOrderRecordBatch(orderTrainRecordList);
|
||||
jdbcLyOrderTrainRecord.updateStatusByNullStatus();
|
||||
count++;
|
||||
} while (count < LoopNum);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = recordsTotal
|
||||
.stream()
|
||||
.map(LyOrderTrainRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderTrainRecord> orderTrainRecordList = recordsTotal
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createTrainOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveTrainOrderRecordBatch(orderTrainRecordList);
|
||||
jdbcLyOrderTrainRecord.updateStatusByNullStatus();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ListenTo(command = "LYOrderRecordGenerateCommand", order = 3)
|
||||
@Transactional
|
||||
public void generateCarRecord(LYOrderRecordGenerateCommand command) {
|
||||
int count = 0;//初始化变量
|
||||
int pageSize = 100; // 假设每页大小为10
|
||||
int LoopNum;//需要的循环数
|
||||
ArrayList<LyOrderCarRecord> recordsTotal = new ArrayList<>();
|
||||
do {
|
||||
PageRequest pageResult = PageRequest.of(count, pageSize);
|
||||
Page<LyOrderCarRecord> lyOrderCarRecordPage = jdbcLyOrderCarRecord.findAllByStatusIsNull(pageResult);
|
||||
|
@ -182,24 +173,24 @@ public class LYOrderRecordDomainService {
|
|||
LoopNum = (int) Math.ceil((double) totalElements / pageSize); // 计算总页数并向上取整
|
||||
//数据
|
||||
List<LyOrderCarRecord> lyOrderCarRecordPageList = lyOrderCarRecordPage.toList();
|
||||
recordsTotal.addAll(lyOrderCarRecordPageList);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = lyOrderCarRecordPageList
|
||||
.stream()
|
||||
.map(LyOrderCarRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderCarRecord> orderCarRecordList = lyOrderCarRecordPageList
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createCarOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveCarOrderRecordBatch(orderCarRecordList);
|
||||
jdbcLyOrderCarRecord.updateStatusByNullStatus();
|
||||
count++;
|
||||
} while (count < LoopNum);
|
||||
//修改数据,将状态为null的设置为1
|
||||
List<String> routeOrderNoList = recordsTotal
|
||||
.stream()
|
||||
.map(LyOrderCarRecord::getOutApplayOrderNo)
|
||||
.distinct()
|
||||
.toList();
|
||||
List<RouteOrder> routeOrderList = routeRequestDomainService.getRouteOrder(routeOrderNoList);
|
||||
Map<String, Optional<RouteOrder>> collect = routeOrderNoList.parallelStream().collect(Collectors.toMap(Function.identity(),
|
||||
it -> routeOrderList.stream().filter(routeOrder ->routeOrder != null && routeOrder.checkIfMine(it)).findFirst()));
|
||||
List<OrderCarRecord> orderCarRecordList = recordsTotal
|
||||
.stream()
|
||||
.map(it -> lyOrderRecordExtensionFactory.createCarOrderRecord(it, collect.get(it.getOutApplayOrderNo())))
|
||||
.toList();
|
||||
orderDetailDomainService.saveCarOrderRecordBatch(orderCarRecordList);
|
||||
jdbcLyOrderCarRecord.updateStatusByNullStatus();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,66 +1,183 @@
|
|||
package com.chint.infrastructure.echo_framework.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class AbstractGenericRepository<T, ID> {
|
||||
|
||||
protected final GenericCacheRepository<T, ID> cacheRepository;
|
||||
protected final JdbcGenericRepository<T, ID> jdbcRepository;
|
||||
protected final String cachePrefix;
|
||||
protected final Map<String, Function<Set<String>, List<T>>> fieldQueryMap = new HashMap<>();
|
||||
|
||||
protected AbstractGenericRepository(GenericCacheRepository<T, ID> cacheRepository,
|
||||
JdbcGenericRepository<T, ID> jdbcRepository,
|
||||
String cachePrefix) {
|
||||
|
||||
public AbstractGenericRepository<T, ID> addFieldQueryMap(String fieldPrefix,
|
||||
Function<Set<String>, List<T>> fieldQuery) {
|
||||
fieldQueryMap.put(fieldPrefix, fieldQuery);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected AbstractGenericRepository(GenericCacheRepository<T, ID> cacheRepository) {
|
||||
this.cacheRepository = cacheRepository;
|
||||
this.jdbcRepository = jdbcRepository;
|
||||
this.cachePrefix = cachePrefix;
|
||||
this.cachePrefix = getPrefix();
|
||||
}
|
||||
|
||||
public T save(T entity) {
|
||||
T savedEntity = jdbcRepository.save(entity);
|
||||
updateCache(savedEntity);
|
||||
return savedEntity;
|
||||
public T saveAndUpdateCache(T entity) {
|
||||
save(entity);
|
||||
updateCache(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public T findById(ID id) {
|
||||
T entity = cacheRepository.findFromCache(cachePrefix, id);
|
||||
if (entity == null) {
|
||||
Optional<T> optionalEntity = jdbcRepository.findById(id);
|
||||
public abstract T save(T entity);
|
||||
|
||||
public Optional<T> findById(ID id, Function<ID, Optional<T>> function) {
|
||||
Optional<T> entity = Optional.ofNullable(cacheRepository.findFromCache(cachePrefix, id));
|
||||
if (entity.isEmpty()) {
|
||||
Optional<T> optionalEntity = function.apply(id);
|
||||
if (optionalEntity.isPresent()) {
|
||||
entity = optionalEntity.get();
|
||||
T finalEntity = entity;
|
||||
T finalEntity = optionalEntity.get();
|
||||
CompletableFuture.runAsync(() -> updateCache(finalEntity));
|
||||
}
|
||||
return optionalEntity;
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public <V> T findByField(Function<T, V> fieldGetter, String fieldPrefix) {
|
||||
ID id = cacheRepository.findIdFromCache(fieldPrefix, fieldGetter.toString());
|
||||
if (id == null) {
|
||||
T entity = findByFieldFromDatabase(fieldGetter);
|
||||
if (entity != null) {
|
||||
CompletableFuture.runAsync(() -> updateCache(entity));
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
return findById(id);
|
||||
|
||||
private Optional<T> findEntity(String fieldPrefix, String fieldValue) {
|
||||
return fieldQueryMap.get(fieldPrefix).apply(Set.of(fieldValue)).stream().findFirst();
|
||||
}
|
||||
|
||||
private Optional<T> findByFieldCommon(String fieldPrefix,
|
||||
String fieldValue, Supplier<ID> idSupplier,
|
||||
Supplier<Optional<T>> entitySupplier,
|
||||
Function<ID, Optional<T>> function) {
|
||||
ID id = idSupplier.get();
|
||||
if (id == null) {
|
||||
Optional<T> first = entitySupplier.get();
|
||||
if (first.isPresent()) {
|
||||
T entity = first.get();
|
||||
CompletableFuture.runAsync(() -> {
|
||||
updateCache(entity);
|
||||
updateFieldCache(fieldPrefix, fieldValue, getPrimaryKeys(entity));
|
||||
});
|
||||
}
|
||||
return first;
|
||||
}
|
||||
return function.apply(id);
|
||||
}
|
||||
|
||||
public <V> Optional<T> findByField(String fieldPrefix, String fieldValue) {
|
||||
return findByField(fieldPrefix, fieldValue, this::findByIdFromDB);
|
||||
}
|
||||
|
||||
// public <V> Optional<T> findByField(String fieldPrefix, Function<T, V> fieldGetter, Function<ID, Optional<T>> function) {
|
||||
// return findByFieldCommon(
|
||||
// fieldPrefix, fieldGetter.apply()
|
||||
// () -> cacheRepository.findIdFromCache(cachePrefix, fieldPrefix, fieldGetter.toString()),
|
||||
// () -> findEntity(fieldPrefix, fieldGetter.toString()), (id) -> findById(id, function);
|
||||
// );
|
||||
// }
|
||||
|
||||
public Optional<T> findByField(String fieldPrefix, String fieldValue, Function<ID, Optional<T>> function) {
|
||||
return findByFieldCommon(
|
||||
fieldPrefix, fieldValue,
|
||||
() -> cacheRepository.findIdFromCache(cachePrefix, fieldPrefix, fieldValue),
|
||||
() -> findEntity(fieldPrefix, fieldValue),
|
||||
(id) -> findById(id, function)
|
||||
);
|
||||
}
|
||||
|
||||
public List<T> findByIdsIn(Set<ID> ids) {
|
||||
// Step 1: Get data from cache
|
||||
List<T> inCacheList = cacheRepository.findBatchByIds(cachePrefix, ids);
|
||||
|
||||
// Step 2: Identify cached IDs and missing IDs
|
||||
List<ID> cachedIds = inCacheList.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(this::getPrimaryKeys)
|
||||
.map(Object::toString)
|
||||
.map(id -> (ID) id)
|
||||
.toList();
|
||||
List<ID> missingIds = ids.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(id -> !cachedIds.contains(id))
|
||||
.toList();
|
||||
|
||||
// Step 3: Get data from database for missing IDs and update cache
|
||||
if (!missingIds.isEmpty()) {
|
||||
List<T> fetchedList = findByIdsInFromDB(new HashSet<>(missingIds));
|
||||
inCacheList.addAll(fetchedList);
|
||||
CompletableFuture.runAsync(() -> fetchedList.forEach(this::updateCache));
|
||||
}
|
||||
|
||||
// Step 4: Return the complete list
|
||||
return inCacheList;
|
||||
}
|
||||
|
||||
public List<T> findByFieldsIn(String fieldPrefix,
|
||||
Set<String> fieldValues,
|
||||
Function<T, String> fieldGetter,
|
||||
Function<Set<String>, List<T>> fieldQueryMethod) {
|
||||
|
||||
// Step 1: Get IDs from cache
|
||||
Set<ID> ids = new HashSet<>(cacheRepository.findIdsFromCache(cachePrefix, fieldPrefix, fieldValues));
|
||||
|
||||
// Step 2: Get data from cache using IDs
|
||||
List<T> inCacheList = cacheRepository.findBatchByIds(cachePrefix, ids);
|
||||
|
||||
// Step 3: Identify cached IDs and missing IDs
|
||||
List<String> cachedFields = inCacheList.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(fieldGetter)
|
||||
.toList();
|
||||
|
||||
Set<String> missingFields = fieldValues.stream()
|
||||
.filter(field -> !cachedFields.contains(field))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// Step 4: Get data from database for missing fields and update cache
|
||||
if (!missingFields.isEmpty()) {
|
||||
List<T> fetchedList = fieldQueryMethod.apply(missingFields);
|
||||
inCacheList.addAll(fetchedList);
|
||||
CompletableFuture.runAsync(() -> {
|
||||
fetchedList.forEach(this::updateCache);
|
||||
fetchedList.forEach(it -> updateFieldCache(fieldPrefix, fieldGetter.apply(it), getPrimaryKeys(it)));
|
||||
});
|
||||
}
|
||||
|
||||
// Step 5: Return the complete list
|
||||
return inCacheList;
|
||||
}
|
||||
|
||||
public List<T> findByFieldsIn(String fieldPrefix,
|
||||
Set<String> fieldValues,
|
||||
Function<T, String> fieldGetter) {
|
||||
return findByFieldsIn(fieldPrefix, fieldValues, fieldGetter, fieldQueryMap.get(fieldPrefix));
|
||||
}
|
||||
|
||||
protected abstract Optional<T> findByIdFromDB(ID id);
|
||||
|
||||
protected abstract List<T> findByIdsInFromDB(Set<ID> ids);
|
||||
|
||||
//通过主键实现该方法
|
||||
protected abstract ID getPrimaryKeys(T entity);
|
||||
|
||||
protected abstract String getPrefix();
|
||||
|
||||
protected abstract <V> T findByFieldFromDatabase(V fieldValue);
|
||||
public void updateCache(T entity) {
|
||||
beforeUpdateCache();
|
||||
cacheRepository.cacheEvict(cachePrefix, getPrimaryKeys(entity));
|
||||
cacheRepository.cache(cachePrefix, getPrimaryKeys(entity), entity);
|
||||
}
|
||||
|
||||
protected abstract List<T> findByFieldInFromDatabase(List<String> fieldValueList);
|
||||
|
||||
protected abstract void updateCache(T entity);
|
||||
protected abstract String beforeUpdateCache();
|
||||
|
||||
protected void updateFieldCache(String fieldName, String fieldValue, ID id) {
|
||||
cacheRepository.cacheEvictFieldToId(fieldName, fieldValue);
|
||||
cacheRepository.cacheFieldToId(fieldName, fieldValue, id);
|
||||
cacheRepository.cacheEvictFieldToId(cachePrefix, fieldName, fieldValue);
|
||||
cacheRepository.cacheFieldToId(cachePrefix, fieldName, fieldValue, id);
|
||||
}
|
||||
|
||||
protected String getFieldPrefix(String fieldName) {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package com.chint.infrastructure.echo_framework.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
||||
public class GenericCacheRepository<T, ID> {
|
||||
|
@ -13,6 +14,7 @@ public class GenericCacheRepository<T, ID> {
|
|||
private final RedisTemplate<String, ID> redisTemplateId;
|
||||
private final Class<T> entityType;
|
||||
private final Class<ID> idType;
|
||||
private final Duration cacheExpiration; // 添加缓存失效时间
|
||||
|
||||
public GenericCacheRepository(RedisTemplate<String, T> redisTemplate,
|
||||
RedisTemplate<String, ID> redisTemplateId,
|
||||
|
@ -22,15 +24,20 @@ public class GenericCacheRepository<T, ID> {
|
|||
this.redisTemplateId = redisTemplateId;
|
||||
this.entityType = entityType;
|
||||
this.idType = idType;
|
||||
this.cacheExpiration = Duration.ofHours(6L);
|
||||
}
|
||||
|
||||
private String getKey(String prefix, Object value) {
|
||||
return prefix + "::" + value;
|
||||
}
|
||||
|
||||
private String getKey(String prefix, String fieldPrefix, Object value) {
|
||||
return prefix + "::" + fieldPrefix + "::" + value;
|
||||
}
|
||||
|
||||
public T cache(String prefix, ID id, T entity) {
|
||||
String key = getKey(prefix, id);
|
||||
redisTemplate.opsForValue().set(key, entity);
|
||||
redisTemplate.opsForValue().set(key, entity, cacheExpiration);
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
@ -44,29 +51,48 @@ public class GenericCacheRepository<T, ID> {
|
|||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public List<T> cacheBatchByIds(String prefix, List<ID> ids) {
|
||||
public List<T> findBatchByIds(String prefix, Set<ID> ids) {
|
||||
List<String> keys = ids.stream().map(id -> getKey(prefix, id)).toList();
|
||||
return redisTemplate.opsForValue().multiGet(keys);
|
||||
}
|
||||
|
||||
public ID cacheFieldToId(String fieldPrefix, String fieldValue, ID id) {
|
||||
String key = getKey(fieldPrefix, fieldValue);
|
||||
redisTemplateId.opsForValue().set(key, id);
|
||||
public ID cacheFieldToId(String prefix, String fieldPrefix,
|
||||
String fieldValue, ID id) {
|
||||
String key = getKey(prefix, fieldPrefix, fieldValue);
|
||||
redisTemplateId.opsForValue().set(key, id, cacheExpiration);
|
||||
return id;
|
||||
}
|
||||
|
||||
public ID findIdFromCache(String fieldPrefix, String fieldValue) {
|
||||
String key = getKey(fieldPrefix, fieldValue);
|
||||
public void cacheEvictFieldToId(String prefix, String fieldPrefix, String fieldValue) {
|
||||
String key = getKey(prefix, fieldPrefix, fieldValue);
|
||||
redisTemplateId.delete(key);
|
||||
}
|
||||
|
||||
public void cacheFieldsToIdsBatch(String prefix, String fieldPrefix,
|
||||
Map<String, ID> fieldValuesToIds) {
|
||||
fieldValuesToIds.forEach((fieldValue, id) -> {
|
||||
String redisKey = getKey(prefix, fieldPrefix, fieldValue);
|
||||
// 将ID存储到Redis列表中,假设列表中每个位置存储一个ID对象
|
||||
redisTemplateId.opsForList().rightPush(redisKey, id);
|
||||
redisTemplateId.expire(redisKey, cacheExpiration);
|
||||
});
|
||||
}
|
||||
|
||||
public void cacheEvictFieldsToIdsBatch(String prefix, String fieldPrefix, Set<String> fieldValues) {
|
||||
List<String> keys = fieldValues.stream().map(it -> getKey(prefix, fieldPrefix, it)).toList();
|
||||
redisTemplateId.delete(keys);
|
||||
}
|
||||
|
||||
public ID findIdFromCache(String prefix, String fieldPrefix,
|
||||
String fieldValue) {
|
||||
String key = getKey(prefix, fieldPrefix, fieldValue);
|
||||
return redisTemplateId.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public List<ID> findIdsFromCache(String fieldPrefix, List<String> fieldValueList) {
|
||||
List<String> keys = fieldValueList.stream().map(fieldValue -> getKey(fieldPrefix, fieldValue)).toList();
|
||||
public List<ID> findIdsFromCache(String prefix, String fieldPrefix,
|
||||
Set<String> fieldValueList) {
|
||||
List<String> keys = fieldValueList.stream().map(fieldValue -> getKey(prefix, fieldPrefix, fieldValue))
|
||||
.toList();
|
||||
return redisTemplateId.opsForValue().multiGet(keys);
|
||||
}
|
||||
|
||||
public void cacheEvictFieldToId(String fieldPrefix, String fieldValue) {
|
||||
String key = getKey(fieldPrefix, fieldValue);
|
||||
redisTemplateId.delete(key);
|
||||
}
|
||||
}
|
|
@ -8,9 +8,4 @@ import java.util.Optional;
|
|||
|
||||
@Repository
|
||||
public interface JdbcGenericRepository<T, ID> extends CrudRepository<T, ID> {
|
||||
Optional<T> findById(ID id);
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
List<T> findByIdIn(List<ID> ids);
|
||||
}
|
|
@ -230,7 +230,7 @@ public class RouteRepositoryImpl implements RouteRepository {
|
|||
routeOrder.setUpdateTime(LocalDateTime.now());
|
||||
}
|
||||
String redisKey = "TemporaryRouteOrder:" + approveOrderNo.getSysCode() + "-" + approveOrderNo.getFakeOrderNo();
|
||||
redisTemplate.opsForValue().set(redisKey, routeOrder, 7L, TimeUnit.DAYS);
|
||||
redisTemplate.opsForValue().set(redisKey, routeOrder, 15L, TimeUnit.DAYS);
|
||||
return routeOrder;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,51 +1,105 @@
|
|||
package com.chint.infrastructure.repository;
|
||||
|
||||
import com.chint.domain.aggregates.user.User;
|
||||
import com.chint.domain.exceptions.CommandException;
|
||||
import com.chint.domain.repository.UserRepository;
|
||||
import com.chint.infrastructure.echo_framework.repository.AbstractGenericRepository;
|
||||
import com.chint.infrastructure.echo_framework.repository.GenericCacheRepository;
|
||||
import com.chint.infrastructure.repository.cache.CacheUserRepository;
|
||||
import com.chint.infrastructure.repository.jdbc.JdbcUserRepository;
|
||||
import com.chint.interfaces.rest.base.PostRequest;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Repository
|
||||
public class UserRepositoryImpl implements UserRepository {
|
||||
public class UserRepositoryImpl extends AbstractGenericRepository<User, Long> implements UserRepository {
|
||||
|
||||
@Autowired
|
||||
private JdbcUserRepository jdbcUserRepository;
|
||||
|
||||
@Override
|
||||
public User findById(Long id) {
|
||||
return null;
|
||||
@Autowired
|
||||
private CacheUserRepository cacheUserRepository;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, User> redisTemplate;
|
||||
@Autowired
|
||||
private RedisTemplate<String, Long> redisTemplateId;
|
||||
@Autowired
|
||||
private PostRequest postRequest;
|
||||
|
||||
@Autowired
|
||||
public UserRepositoryImpl(JdbcUserRepository jdbcUserRepository,
|
||||
CacheUserRepository cacheUserRepository,
|
||||
RedisTemplate<String, User> redisTemplate,
|
||||
RedisTemplate<String, Long> redisTemplateId) {
|
||||
super(new GenericCacheRepository<>(redisTemplate, redisTemplateId, User.class, Long.class));
|
||||
this.jdbcUserRepository = jdbcUserRepository;
|
||||
this.cacheUserRepository = cacheUserRepository;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.addFieldQueryMap("EmployeeNo", (set) -> jdbcUserRepository.findByEmployeeNoIn(set));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Optional<User> findById(Long id) {
|
||||
return findById(id, (userId) -> jdbcUserRepository.findById(userId));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<User> findByIdsInFromDB(Set<Long> longs) {
|
||||
return jdbcUserRepository.findByUserIdIn(longs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Long getPrimaryKeys(User entity) {
|
||||
return entity.getUserId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPrefix() {
|
||||
return "User";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String beforeUpdateCache() {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@Cacheable(value = "UserEmployeeNoCache", key = "#employeeNo")
|
||||
@Override
|
||||
public User findByUserEmployeeNo(String employeeNo) {
|
||||
return jdbcUserRepository.findByEmployeeNo(employeeNo);
|
||||
return this.findByField("EmployeeNo", employeeNo)
|
||||
.orElseThrow(() -> new CommandException("Employee Not Found"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findByEmployeeNoIn(Set<String> employeeNoSet) {
|
||||
return List.of();
|
||||
return this.findByFieldsIn("EmployeeNo", employeeNoSet,
|
||||
User::getEmployeeNo,
|
||||
(set) -> jdbcUserRepository.findByEmployeeNoIn(employeeNoSet));
|
||||
}
|
||||
|
||||
|
||||
@CacheEvict(value = "UserEmployeeNoCache", key = "#user.employeeNo")
|
||||
@Override
|
||||
public User save(User user) {
|
||||
User byEmployeeNo = jdbcUserRepository.findByEmployeeNo(user.getEmployeeNo());
|
||||
if (byEmployeeNo != null) {
|
||||
user.setUserId(byEmployeeNo.getUserId());
|
||||
}
|
||||
return jdbcUserRepository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<User> findByIdFromDB(Long aLong) {
|
||||
return jdbcUserRepository.findById(aLong);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void saveAll(List<User> userList) {
|
||||
jdbcUserRepository.saveAll(userList);
|
||||
|
|
10
src/main/java/com/chint/infrastructure/repository/cache/CacheUserRepository.java
vendored
Normal file
10
src/main/java/com/chint/infrastructure/repository/cache/CacheUserRepository.java
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
package com.chint.infrastructure.repository.cache;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class CacheUserRepository {
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -6,12 +6,17 @@ import org.springframework.data.domain.Pageable;
|
|||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface JdbcUserRepository extends CrudRepository<User, Long> {
|
||||
User findByEmployeeNo(String employeeNo);
|
||||
|
||||
List<User> findByEmployeeNoIn(Collection<String> employeeNo);
|
||||
|
||||
List<User> findByUserIdIn(Collection<Long> userId);
|
||||
|
||||
List<User> findByStatus(Integer status);
|
||||
|
||||
List<User> findByStatusIsNull();
|
||||
|
|
|
@ -111,7 +111,7 @@ public class BatchUserWorker {
|
|||
} else {
|
||||
user.setStatus(1);
|
||||
}
|
||||
userRepository.save(user);
|
||||
userRepository.saveAndUpdateCache(user);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -228,7 +228,7 @@ public class BatchUserWorker {
|
|||
cTripUserSaveRequest.saveUserToCTrip(user);
|
||||
// 发送保存用户同步时间
|
||||
user.setSyncTime(LocalDateTime.now());
|
||||
userRepository.save(user);
|
||||
userRepository.saveAndUpdateCache(user);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -187,7 +187,7 @@ public class UserHttpRequestImpl implements UserHttpRequest {
|
|||
.ifPresent(it -> {
|
||||
User byUserEmployeeNo = userRepository.findByUserEmployeeNo(user.getEmployeeNo());
|
||||
if (byUserEmployeeNo == null) {
|
||||
userRepository.save(
|
||||
userRepository.saveAndUpdateCache(
|
||||
getDataFromUserSFData(user, it)
|
||||
);
|
||||
}
|
||||
|
@ -224,7 +224,7 @@ public class UserHttpRequestImpl implements UserHttpRequest {
|
|||
getDataFromUserSFData(user, userDataDTOList.get(0));
|
||||
}
|
||||
|
||||
return userRepository.save(user);
|
||||
return userRepository.saveAndUpdateCache(user);
|
||||
}
|
||||
|
||||
public String belongDeport(String employeeNo, String accountCompanyName) {
|
||||
|
@ -248,7 +248,7 @@ public class UserHttpRequestImpl implements UserHttpRequest {
|
|||
List<UserDepartmentInfo> userDepartmentInfos = userDepartmentInfoList.stream().distinct().toList();
|
||||
if (userDepartmentInfos.size() < userDepartmentInfoList.size()) {
|
||||
user.setUserDepartmentInfoList(userDepartmentInfos);
|
||||
userRepository.save(user);
|
||||
userRepository.saveAndUpdateCache(user);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -250,7 +250,7 @@ public class CTripTest {
|
|||
@Test
|
||||
void search() {
|
||||
BaseContext.setCurrentUser(user);
|
||||
SearchOrderResponse response = orderSearchRequest.searchOrderResponseByOrderId("32055893290");
|
||||
SearchOrderResponse response = orderSearchRequest.searchOrderResponseByOrderId("33303376379");
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.chint;
|
|||
|
||||
|
||||
import com.chint.domain.aggregates.order.RouteOrder;
|
||||
import com.chint.domain.aggregates.user.User;
|
||||
import com.chint.domain.repository.UserRepository;
|
||||
import com.chint.infrastructure.cache.RouteCacheManage;
|
||||
import com.chint.infrastructure.cache.RouteCacheService;
|
||||
import com.chint.interfaces.rest.ctrip.CTripLocationHttpRequest;
|
||||
|
@ -12,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@SpringBootTest
|
||||
public class CacheTest {
|
||||
|
@ -25,6 +28,11 @@ public class CacheTest {
|
|||
@Autowired
|
||||
private RouteCacheManage routeCacheManage;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private Gson gson = new Gson();
|
||||
|
||||
// @Test
|
||||
public void getAllRouteOrderById(){
|
||||
List<String> keys = List.of("RouteOrders::900","RouteOrders::918");
|
||||
|
@ -51,4 +59,18 @@ public class CacheTest {
|
|||
public void cancelCache(){
|
||||
routeCacheService.invalidateRouteCache(null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void findUserByEmployeeNo(){
|
||||
User byUserEmployeeNo = userRepository.findByUserEmployeeNo("230615020");
|
||||
System.out.println(byUserEmployeeNo);
|
||||
|
||||
System.out.println(userRepository.findByEmployeeNoIn(Set.of("230615020", "231116011")));
|
||||
|
||||
System.out.println(userRepository.findById(1L));
|
||||
byUserEmployeeNo.setPassword("123123123123123213123");
|
||||
userRepository.saveAndUpdateCache(byUserEmployeeNo);
|
||||
System.out.println(userRepository.findById(1L));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -324,10 +324,10 @@ class RouteApplicationTests {
|
|||
|
||||
@Test
|
||||
void loginSignProd() {
|
||||
String sfno = "210506025";
|
||||
String syscode = "ANFSSC";
|
||||
String billcode = "CLS24062700018";
|
||||
String companycode = "正泰安能数字能源(浙江)股份有限公司";
|
||||
String sfno = "231026051";
|
||||
String syscode = "FSSC";
|
||||
String billcode = "CLSQ240225000099";
|
||||
String companycode = "正泰集团股份有限公司";
|
||||
String timespan = "1708908662738";
|
||||
String key = "ZhengTaiRoute";
|
||||
String s = Digest.md5(sfno + syscode + billcode + companycode + key + timespan);
|
||||
|
|
Loading…
Reference in New Issue