点位导入、容器导入
parent
654d97276a
commit
26e11156c5
|
|
@ -75,6 +75,8 @@ public class PointController {
|
||||||
|
|
||||||
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||||
|
|
||||||
|
private final RedisObjectUtils redisObjectUtils;
|
||||||
|
|
||||||
@Log("导出数据")
|
@Log("导出数据")
|
||||||
@ApiOperation("导出数据")
|
@ApiOperation("导出数据")
|
||||||
@GetMapping(value = "/download")
|
@GetMapping(value = "/download")
|
||||||
|
|
@ -97,36 +99,14 @@ public class PointController {
|
||||||
ExcelReader reader = ExcelUtil.getReader(file);
|
ExcelReader reader = ExcelUtil.getReader(file);
|
||||||
List<Map<String, Object>> readAll = reader.readAll();
|
List<Map<String, Object>> readAll = reader.readAll();
|
||||||
try {
|
try {
|
||||||
Set<String> areaCodes = new HashSet<>();//库区集合
|
|
||||||
Set<String> pointCodes = new HashSet<>();//点位集合
|
Set<String> pointCodes = new HashSet<>();//点位集合
|
||||||
for (int i = 0; i < readAll.size(); i++) {
|
for (Map<String, Object> record : readAll) {
|
||||||
String areaCode = readAll.get(i).get("库区").toString().trim();
|
String pointCode = record.get("编码").toString().trim();
|
||||||
String pointCode = readAll.get(i).get("编码").toString().trim();
|
|
||||||
if (!StringUtils.isEmpty(areaCode)) {
|
|
||||||
areaCodes.add(areaCode);
|
|
||||||
}
|
|
||||||
if (!StringUtils.isEmpty(pointCode)) {
|
if (!StringUtils.isEmpty(pointCode)) {
|
||||||
pointCodes.add(pointCode);
|
pointCodes.add(pointCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Map<String, Area> existingArea = new HashMap<>();
|
|
||||||
for (String areaCode : areaCodes) {
|
|
||||||
Area area = areaService.findByCode(areaCode);
|
|
||||||
if (area == null) {
|
|
||||||
return badRequest("系统不存在该库区:" + areaCode);
|
|
||||||
}
|
|
||||||
existingArea.put(areaCode, area);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
Map<String, Area> existingArea = areaCodes.stream()
|
|
||||||
.map(areaCode -> {
|
|
||||||
Area area = areaService.findByCode(areaCode);
|
|
||||||
if (area == null) {
|
|
||||||
throw new IllegalArgumentException("系统不存在该库区:" + areaCode);
|
|
||||||
}
|
|
||||||
return new AbstractMap.SimpleEntry<>(areaCode, area);
|
|
||||||
})
|
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
|
||||||
|
|
||||||
//获取已存在的点位
|
//获取已存在的点位
|
||||||
Map<String, Point> existingPoint = pointService.findByCodes(pointCodes);
|
Map<String, Point> existingPoint = pointService.findByCodes(pointCodes);
|
||||||
|
|
@ -134,34 +114,36 @@ public class PointController {
|
||||||
|
|
||||||
List<Point> pointsToCreate = new ArrayList<>();//新增点位集合
|
List<Point> pointsToCreate = new ArrayList<>();//新增点位集合
|
||||||
List<Point> pointsToUpdate = new ArrayList<>();//修改点位集合
|
List<Point> pointsToUpdate = new ArrayList<>();//修改点位集合
|
||||||
for (int i = 0; i < readAll.size(); i++) {
|
for (Map<String, Object> record : readAll) {
|
||||||
String pointCode = readAll.get(i).get("编码").toString().trim();
|
String pointCode = record.get("编码").toString().trim();
|
||||||
String areaCode = readAll.get(i).get("库区").toString().trim();
|
String areaCode = record.get("库区").toString().trim();
|
||||||
Area area = existingArea.get(areaCode);
|
Area area = redisObjectUtils.getObjectFromCache(areaCode, () -> areaService.findByCode(areaCode), areaCode + " 系统无此库区!");
|
||||||
//判断是否已存在点位
|
//判断是否已存在点位
|
||||||
if (existingPoint.containsKey(pointCode)) {
|
if (existingPoint.containsKey(pointCode)) {
|
||||||
Point point = existingPoint.get(pointCode);
|
Point point = existingPoint.get(pointCode);
|
||||||
//更新点位
|
//更新点位
|
||||||
pointsToUpdate.add(updatePoint(point, area, dept, readAll, i));
|
pointsToUpdate.add(updatePoint(point, area, dept, record));
|
||||||
} else {
|
} else {
|
||||||
//新增点位
|
//新增点位
|
||||||
pointsToCreate.add(createPoint(area, dept, readAll, i));
|
pointsToCreate.add(createPoint(area, dept, record));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture<Void> createFuture = CompletableFuture.runAsync(() -> {
|
||||||
//批量新增Mo票
|
//批量新增Mo票
|
||||||
if (!pointsToCreate.isEmpty()) {
|
if (!pointsToCreate.isEmpty()) {
|
||||||
batchCreateOrUpdate.batchCreate(pointsToCreate);
|
batchCreateOrUpdate.batchCreate(pointsToCreate);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> {
|
||||||
//批量更新Mo票
|
//批量更新Mo票
|
||||||
if (!pointsToUpdate.isEmpty()) {
|
if (!pointsToUpdate.isEmpty()) {
|
||||||
batchCreateOrUpdate.batchUpdate(pointsToUpdate);
|
batchCreateOrUpdate.batchUpdate(pointsToUpdate);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// 等待所有操作完成
|
||||||
|
CompletableFuture.allOf(createFuture, updateFuture).join();
|
||||||
return successRequest("导入成功:" + " 新增(" + pointsToCreate.size() + ")修改(" + pointsToUpdate.size() + ")", "");
|
return successRequest("导入成功:" + " 新增(" + pointsToCreate.size() + ")修改(" + pointsToUpdate.size() + ")", "");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return badRequest("导入失败:" + e.getMessage());
|
return badRequest("导入失败:" + e.getMessage());
|
||||||
|
|
@ -169,11 +151,11 @@ public class PointController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Point updatePoint(Point point, Area area, Dept dept, List<Map<String, Object>> readAll, int i) {
|
private Point updatePoint(Point point, Area area, Dept dept, Map<String, Object> record) {
|
||||||
point.setArea(area);
|
point.setArea(area);
|
||||||
point.setDept(dept);
|
point.setDept(dept);
|
||||||
point.setDescription(readAll.get(i).get("存储类型") == null ? "" : readAll.get(i).get("存储类型").toString().trim());
|
point.setDescription(record.get("存储类型") == null ? "" : record.get("存储类型").toString().trim());
|
||||||
String types = readAll.get(i).get("类型").toString().trim();
|
String types = record.get("类型").toString().trim();
|
||||||
String lx = "";
|
String lx = "";
|
||||||
if (types.equals("缓存点")) {
|
if (types.equals("缓存点")) {
|
||||||
lx = BaseStatus.STORAGE;
|
lx = BaseStatus.STORAGE;
|
||||||
|
|
@ -182,19 +164,19 @@ public class PointController {
|
||||||
lx = BaseStatus.BOX;
|
lx = BaseStatus.BOX;
|
||||||
}
|
}
|
||||||
point.setType(lx);
|
point.setType(lx);
|
||||||
point.setPosX(readAll.get(i).get("坐标X") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标X").toString()));
|
point.setPosX(record.get("坐标X") == null ? 0 : Double.parseDouble(record.get("坐标X").toString()));
|
||||||
point.setPosY(readAll.get(i).get("坐标Y") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标Y").toString()));
|
point.setPosY(record.get("坐标Y") == null ? 0 : Double.parseDouble(record.get("坐标Y").toString()));
|
||||||
return point;
|
return point;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Point createPoint(Area area, Dept dept, List<Map<String, Object>> readAll, int i) {
|
private Point createPoint(Area area, Dept dept, Map<String, Object> record) {
|
||||||
Point point = new Point();
|
Point point = new Point();
|
||||||
point.setCode(readAll.get(i).get("编码").toString().trim());
|
point.setCode(record.get("编码").toString().trim());
|
||||||
point.setName(readAll.get(i).get("编码").toString().trim());
|
point.setName(record.get("编码").toString().trim());
|
||||||
point.setArea(area);
|
point.setArea(area);
|
||||||
point.setDept(dept);
|
point.setDept(dept);
|
||||||
point.setDescription(readAll.get(i).get("存储类型") == null ? "" : readAll.get(i).get("存储类型").toString().trim());
|
point.setDescription(record.get("存储类型") == null ? "" : record.get("存储类型").toString().trim());
|
||||||
String types = readAll.get(i).get("类型").toString().trim();
|
String types = record.get("类型").toString().trim();
|
||||||
String lx = "";
|
String lx = "";
|
||||||
if (types.equals("缓存点")) {
|
if (types.equals("缓存点")) {
|
||||||
lx = BaseStatus.STORAGE;
|
lx = BaseStatus.STORAGE;
|
||||||
|
|
@ -203,8 +185,8 @@ public class PointController {
|
||||||
lx = BaseStatus.BOX;
|
lx = BaseStatus.BOX;
|
||||||
}
|
}
|
||||||
point.setType(lx);
|
point.setType(lx);
|
||||||
point.setPosX(readAll.get(i).get("坐标X") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标X").toString()));
|
point.setPosX(record.get("坐标X") == null ? 0 : Double.parseDouble(record.get("坐标X").toString()));
|
||||||
point.setPosY(readAll.get(i).get("坐标Y") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标Y").toString()));
|
point.setPosY(record.get("坐标Y") == null ? 0 : Double.parseDouble(record.get("坐标Y").toString()));
|
||||||
point.setEnabled(true);
|
point.setEnabled(true);
|
||||||
point.setStatus(BaseStatus.FREE);
|
point.setStatus(BaseStatus.FREE);
|
||||||
return point;
|
return point;
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,9 @@ import com.youchain.basicdata.service.StockTypeService;
|
||||||
import com.youchain.basicdata.service.dto.StockQueryCriteria;
|
import com.youchain.basicdata.service.dto.StockQueryCriteria;
|
||||||
import com.youchain.config.FileProperties;
|
import com.youchain.config.FileProperties;
|
||||||
import com.youchain.exception.handler.ApiError;
|
import com.youchain.exception.handler.ApiError;
|
||||||
import com.youchain.utils.BaseStatus;
|
import com.youchain.exception.handler.ApiResult;
|
||||||
import com.youchain.utils.FileUtil;
|
import com.youchain.modules.system.domain.Dept;
|
||||||
import com.youchain.utils.UserUtils;
|
import com.youchain.utils.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
@ -44,15 +44,17 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||||
|
import static org.springframework.http.HttpStatus.OK;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @website https://eladmin.vip
|
|
||||||
* @author liuxue
|
* @author liuxue
|
||||||
|
* @website https://eladmin.vip
|
||||||
* @date 2023-07-28
|
* @date 2023-07-28
|
||||||
**/
|
**/
|
||||||
@RestController
|
@RestController
|
||||||
|
|
@ -67,6 +69,11 @@ public class StockController {
|
||||||
private final FileProperties properties;
|
private final FileProperties properties;
|
||||||
|
|
||||||
private final StockTypeService stockTypeService;
|
private final StockTypeService stockTypeService;
|
||||||
|
|
||||||
|
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||||
|
|
||||||
|
private final RedisObjectUtils redisObjectUtils;
|
||||||
|
|
||||||
@Log("导出数据")
|
@Log("导出数据")
|
||||||
@ApiOperation("导出数据")
|
@ApiOperation("导出数据")
|
||||||
@GetMapping(value = "/download")
|
@GetMapping(value = "/download")
|
||||||
|
|
@ -83,53 +90,76 @@ public class StockController {
|
||||||
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||||
String type = FileUtil.getFileType(suffix);
|
String type = FileUtil.getFileType(suffix);
|
||||||
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
||||||
//代码、库区
|
|
||||||
ExcelReader reader = ExcelUtil.getReader(file);
|
ExcelReader reader = ExcelUtil.getReader(file);
|
||||||
int i=0;
|
|
||||||
int edit_len=0;
|
|
||||||
int new_len=0;
|
|
||||||
try {
|
try {
|
||||||
List<Map<String, Object>> readAll = reader.readAll();
|
List<Map<String, Object>> readAll = reader.readAll();
|
||||||
for ( i = 0; i < readAll.size(); i++) {
|
Set<String> StockCodes = new HashSet<>();//容器集合
|
||||||
int j=0;
|
for (Map<String, Object> record : readAll) {
|
||||||
String code=readAll.get(i).get("代码").toString().trim();
|
String stockCode = record.get("代码").toString().trim();
|
||||||
String stockTypeCode=readAll.get(i).get("类型").toString().trim();
|
if (!StringUtils.isEmpty(stockCode)) {
|
||||||
StockType st=stockTypeService.findByCode(stockTypeCode);
|
StockCodes.add(stockCode);
|
||||||
if(code==null || code.length()<=0){
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if(st==null){
|
|
||||||
ApiError apiError = ApiError.errorJosn(BAD_REQUEST.value(),"找不到容器类型"+stockTypeCode);
|
|
||||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
|
||||||
}
|
}
|
||||||
Stock stock=stockService.findByCode(code,null);
|
|
||||||
if(stock==null){
|
//获取已存在的点位
|
||||||
stock=new Stock();
|
Map<String, Stock> existingStock = stockService.findByCodes(StockCodes);
|
||||||
stock.setCode(code);
|
|
||||||
stock.setName(code);
|
List<Stock> stocksToCreate = new ArrayList<>();//新增容器集合
|
||||||
|
List<Stock> stocksToUpdate = new ArrayList<>();//修改容器集合
|
||||||
|
|
||||||
|
for (Map<String, Object> record : readAll) {
|
||||||
|
String stockCode = record.get("代码").toString().trim();
|
||||||
|
String stockTypeCode = record.get("类型").toString().trim();
|
||||||
|
StockType stockType = redisObjectUtils.getObjectFromCache(stockTypeCode, () -> stockTypeService.findByCode(stockTypeCode), stockTypeCode + " 系统无此容器类型!");
|
||||||
|
|
||||||
|
//判断是否已存在容器
|
||||||
|
if (existingStock.containsKey(stockCode)) {
|
||||||
|
//修改容器
|
||||||
|
Stock stock = existingStock.get(stockCode);
|
||||||
|
stocksToUpdate.add(updateStock(stock, stockType));
|
||||||
|
} else {
|
||||||
|
//新增容器
|
||||||
|
stocksToCreate.add(createStock(stockType, UserUtils.getDept(), record));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CompletableFuture<Void> createFuture = CompletableFuture.runAsync(() -> {
|
||||||
|
//批量新增Mo票
|
||||||
|
if (!stocksToCreate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchCreate(stocksToCreate);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> {
|
||||||
|
//批量更新Mo票
|
||||||
|
if (!stocksToUpdate.isEmpty()) {
|
||||||
|
batchCreateOrUpdate.batchUpdate(stocksToUpdate);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 等待所有操作完成
|
||||||
|
CompletableFuture.allOf(createFuture, updateFuture).join();
|
||||||
|
return successRequest("导入成功:" + " 新增(" + stocksToCreate.size() + ")修改(" + stocksToUpdate.size() + ")", "");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
return badRequest("导入失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Stock createStock(StockType stockType, Dept dept, Map<String, Object> record) {
|
||||||
|
Stock stock = new Stock();
|
||||||
|
stock.setCode(record.get("代码").toString().trim());
|
||||||
|
stock.setName(record.get("代码").toString().trim());
|
||||||
|
stock.setStockType(stockType);
|
||||||
|
stock.setDept(dept);
|
||||||
stock.setStatus(BaseStatus.FREE);
|
stock.setStatus(BaseStatus.FREE);
|
||||||
stock.setEnabled(true);
|
stock.setEnabled(true);
|
||||||
stock.setStockType(st);
|
return stock;
|
||||||
stock.setDept(UserUtils.getDept());
|
|
||||||
stockService.create(stock);
|
|
||||||
}else{
|
|
||||||
stock.setCode(code);
|
|
||||||
stock.setName(code);
|
|
||||||
stock.setStockType(st);
|
|
||||||
stockService.update(stock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("导入"+(i+1)+"行");
|
private Stock updateStock(Stock stock, StockType stockType) {
|
||||||
}
|
stock.setStockType(stockType);
|
||||||
}catch (Exception e){
|
return stock;
|
||||||
ApiError apiError = ApiError.errorJosn(HttpStatus.BAD_REQUEST.value(), "导入异常---第"+(i+1)+"行:"+e.toString());
|
|
||||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ApiError apiError = ApiError.errorJosn(HttpStatus.OK.value(), "导入成功:"+(i)+"行 新增("+new_len+")修改("+edit_len+")");
|
|
||||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
|
|
@ -198,4 +228,12 @@ public class StockController {
|
||||||
return new ResponseEntity<>(stockService.getItemCode(stockCode), HttpStatus.OK);
|
return new ResponseEntity<>(stockService.getItemCode(stockCode), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Object> badRequest(String message) {
|
||||||
|
return new ResponseEntity<>(ApiResult.fail(BAD_REQUEST.value(), message, ""), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Object> successRequest(String message, Object object) {
|
||||||
|
return new ResponseEntity<>(ApiResult.fail(OK.value(), message, object), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import org.springframework.data.domain.Pageable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Set;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -187,4 +188,6 @@ public interface StockService {
|
||||||
*/
|
*/
|
||||||
void lineContainerReturn(String stockCode,String pointCode);
|
void lineContainerReturn(String stockCode,String pointCode);
|
||||||
|
|
||||||
|
Map<String,Stock> findByCodes(Set stockCodes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -343,6 +343,19 @@ public class StockServiceImpl implements StockService {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Stock> findByCodes(Set stockCodes) {
|
||||||
|
String hql = " from Stock where code in (:stockCodes)";
|
||||||
|
Query query = entityMapper.createQuery(hql);
|
||||||
|
query.setParameter("stockCodes", stockCodes);
|
||||||
|
List<Stock> stockList = query.getResultList();
|
||||||
|
Map<String, Stock> stockMap = new HashMap<>();
|
||||||
|
for (Stock stock : stockList) {
|
||||||
|
stockMap.put(stock.getCode(), stock);
|
||||||
|
}
|
||||||
|
return stockMap;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isValidFKArea(String areaName) {
|
private boolean isValidFKArea(String areaName) {
|
||||||
return AreaNameDic.TwoFFK.equals(areaName) || AreaNameDic.TwoFFK2.equals(areaName);
|
return AreaNameDic.TwoFFK.equals(areaName) || AreaNameDic.TwoFFK2.equals(areaName);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.youchain.utils;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存对象通用工具类
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class RedisObjectUtils {
|
||||||
|
|
||||||
|
private final RedisUtils redisUtils;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public RedisObjectUtils(RedisUtils redisUtils) {
|
||||||
|
this.redisUtils = redisUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public <T> T getObjectFromCache(String cacheKey, Supplier<T> serviceSupplier, String errorMessage) {
|
||||||
|
T object = null;
|
||||||
|
boolean flag = redisUtils.hasKey(cacheKey);
|
||||||
|
if (flag) {
|
||||||
|
object = (T) redisUtils.get(cacheKey);
|
||||||
|
} else {
|
||||||
|
object = getOrThrow(Optional.ofNullable(serviceSupplier.get()), errorMessage);
|
||||||
|
redisUtils.set(cacheKey, object);
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getOrThrow(Optional<T> optional, String errorMessage) {
|
||||||
|
return optional.orElseThrow(() -> new IllegalArgumentException(errorMessage));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue