点位导入、容器导入
parent
654d97276a
commit
26e11156c5
|
|
@ -75,6 +75,8 @@ public class PointController {
|
|||
|
||||
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||
|
||||
private final RedisObjectUtils redisObjectUtils;
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
|
|
@ -97,36 +99,14 @@ public class PointController {
|
|||
ExcelReader reader = ExcelUtil.getReader(file);
|
||||
List<Map<String, Object>> readAll = reader.readAll();
|
||||
try {
|
||||
Set<String> areaCodes = new HashSet<>();//库区集合
|
||||
Set<String> pointCodes = new HashSet<>();//点位集合
|
||||
for (int i = 0; i < readAll.size(); i++) {
|
||||
String areaCode = readAll.get(i).get("库区").toString().trim();
|
||||
String pointCode = readAll.get(i).get("编码").toString().trim();
|
||||
if (!StringUtils.isEmpty(areaCode)) {
|
||||
areaCodes.add(areaCode);
|
||||
}
|
||||
for (Map<String, Object> record : readAll) {
|
||||
String pointCode = record.get("编码").toString().trim();
|
||||
if (!StringUtils.isEmpty(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);
|
||||
|
|
@ -134,34 +114,36 @@ public class PointController {
|
|||
|
||||
List<Point> pointsToCreate = new ArrayList<>();//新增点位集合
|
||||
List<Point> pointsToUpdate = new ArrayList<>();//修改点位集合
|
||||
for (int i = 0; i < readAll.size(); i++) {
|
||||
String pointCode = readAll.get(i).get("编码").toString().trim();
|
||||
String areaCode = readAll.get(i).get("库区").toString().trim();
|
||||
Area area = existingArea.get(areaCode);
|
||||
for (Map<String, Object> record : readAll) {
|
||||
String pointCode = record.get("编码").toString().trim();
|
||||
String areaCode = record.get("库区").toString().trim();
|
||||
Area area = redisObjectUtils.getObjectFromCache(areaCode, () -> areaService.findByCode(areaCode), areaCode + " 系统无此库区!");
|
||||
//判断是否已存在点位
|
||||
if (existingPoint.containsKey(pointCode)) {
|
||||
Point point = existingPoint.get(pointCode);
|
||||
//更新点位
|
||||
pointsToUpdate.add(updatePoint(point, area, dept, readAll, i));
|
||||
pointsToUpdate.add(updatePoint(point, area, dept, record));
|
||||
} else {
|
||||
//新增点位
|
||||
pointsToCreate.add(createPoint(area, dept, readAll, i));
|
||||
pointsToCreate.add(createPoint(area, dept, record));
|
||||
}
|
||||
}
|
||||
|
||||
CompletableFuture.runAsync(() -> {
|
||||
CompletableFuture<Void> createFuture = CompletableFuture.runAsync(() -> {
|
||||
//批量新增Mo票
|
||||
if (!pointsToCreate.isEmpty()) {
|
||||
batchCreateOrUpdate.batchCreate(pointsToCreate);
|
||||
}
|
||||
});
|
||||
|
||||
CompletableFuture.runAsync(() -> {
|
||||
CompletableFuture<Void> updateFuture = CompletableFuture.runAsync(() -> {
|
||||
//批量更新Mo票
|
||||
if (!pointsToUpdate.isEmpty()) {
|
||||
batchCreateOrUpdate.batchUpdate(pointsToUpdate);
|
||||
}
|
||||
});
|
||||
// 等待所有操作完成
|
||||
CompletableFuture.allOf(createFuture, updateFuture).join();
|
||||
return successRequest("导入成功:" + " 新增(" + pointsToCreate.size() + ")修改(" + pointsToUpdate.size() + ")", "");
|
||||
} catch (Exception e) {
|
||||
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.setDept(dept);
|
||||
point.setDescription(readAll.get(i).get("存储类型") == null ? "" : readAll.get(i).get("存储类型").toString().trim());
|
||||
String types = readAll.get(i).get("类型").toString().trim();
|
||||
point.setDescription(record.get("存储类型") == null ? "" : record.get("存储类型").toString().trim());
|
||||
String types = record.get("类型").toString().trim();
|
||||
String lx = "";
|
||||
if (types.equals("缓存点")) {
|
||||
lx = BaseStatus.STORAGE;
|
||||
|
|
@ -182,19 +164,19 @@ public class PointController {
|
|||
lx = BaseStatus.BOX;
|
||||
}
|
||||
point.setType(lx);
|
||||
point.setPosX(readAll.get(i).get("坐标X") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标X").toString()));
|
||||
point.setPosY(readAll.get(i).get("坐标Y") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标Y").toString()));
|
||||
point.setPosX(record.get("坐标X") == null ? 0 : Double.parseDouble(record.get("坐标X").toString()));
|
||||
point.setPosY(record.get("坐标Y") == null ? 0 : Double.parseDouble(record.get("坐标Y").toString()));
|
||||
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.setCode(readAll.get(i).get("编码").toString().trim());
|
||||
point.setName(readAll.get(i).get("编码").toString().trim());
|
||||
point.setCode(record.get("编码").toString().trim());
|
||||
point.setName(record.get("编码").toString().trim());
|
||||
point.setArea(area);
|
||||
point.setDept(dept);
|
||||
point.setDescription(readAll.get(i).get("存储类型") == null ? "" : readAll.get(i).get("存储类型").toString().trim());
|
||||
String types = readAll.get(i).get("类型").toString().trim();
|
||||
point.setDescription(record.get("存储类型") == null ? "" : record.get("存储类型").toString().trim());
|
||||
String types = record.get("类型").toString().trim();
|
||||
String lx = "";
|
||||
if (types.equals("缓存点")) {
|
||||
lx = BaseStatus.STORAGE;
|
||||
|
|
@ -203,8 +185,8 @@ public class PointController {
|
|||
lx = BaseStatus.BOX;
|
||||
}
|
||||
point.setType(lx);
|
||||
point.setPosX(readAll.get(i).get("坐标X") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标X").toString()));
|
||||
point.setPosY(readAll.get(i).get("坐标Y") == null ? 0 : Double.parseDouble(readAll.get(i).get("坐标Y").toString()));
|
||||
point.setPosX(record.get("坐标X") == null ? 0 : Double.parseDouble(record.get("坐标X").toString()));
|
||||
point.setPosY(record.get("坐标Y") == null ? 0 : Double.parseDouble(record.get("坐标Y").toString()));
|
||||
point.setEnabled(true);
|
||||
point.setStatus(BaseStatus.FREE);
|
||||
return point;
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.youchain.basicdata.rest;
|
||||
|
||||
import cn.hutool.poi.excel.ExcelReader;
|
||||
|
|
@ -28,9 +28,9 @@ import com.youchain.basicdata.service.StockTypeService;
|
|||
import com.youchain.basicdata.service.dto.StockQueryCriteria;
|
||||
import com.youchain.config.FileProperties;
|
||||
import com.youchain.exception.handler.ApiError;
|
||||
import com.youchain.utils.BaseStatus;
|
||||
import com.youchain.utils.FileUtil;
|
||||
import com.youchain.utils.UserUtils;
|
||||
import com.youchain.exception.handler.ApiResult;
|
||||
import com.youchain.modules.system.domain.Dept;
|
||||
import com.youchain.utils.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
|
@ -44,17 +44,19 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.OK;
|
||||
|
||||
/**
|
||||
* @website https://eladmin.vip
|
||||
* @author liuxue
|
||||
* @date 2023-07-28
|
||||
**/
|
||||
* @author liuxue
|
||||
* @website https://eladmin.vip
|
||||
* @date 2023-07-28
|
||||
**/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
|
|
@ -67,6 +69,11 @@ public class StockController {
|
|||
private final FileProperties properties;
|
||||
|
||||
private final StockTypeService stockTypeService;
|
||||
|
||||
private final BatchCreateOrUpdate batchCreateOrUpdate;
|
||||
|
||||
private final RedisObjectUtils redisObjectUtils;
|
||||
|
||||
@Log("导出数据")
|
||||
@ApiOperation("导出数据")
|
||||
@GetMapping(value = "/download")
|
||||
|
|
@ -78,87 +85,110 @@ public class StockController {
|
|||
@PostMapping(value = "/import_stock")
|
||||
@ApiOperation("容器导入")
|
||||
@AnonymousAccess
|
||||
public ResponseEntity<Object> importStock( @RequestParam("file") MultipartFile multipartFile) {
|
||||
public ResponseEntity<Object> importStock(@RequestParam("file") MultipartFile multipartFile) {
|
||||
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
|
||||
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||
String type = FileUtil.getFileType(suffix);
|
||||
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
||||
//代码、库区
|
||||
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();
|
||||
for ( i = 0; i < readAll.size(); i++) {
|
||||
int j=0;
|
||||
String code=readAll.get(i).get("代码").toString().trim();
|
||||
String stockTypeCode=readAll.get(i).get("类型").toString().trim();
|
||||
StockType st=stockTypeService.findByCode(stockTypeCode);
|
||||
if(code==null || code.length()<=0){
|
||||
break;
|
||||
Set<String> StockCodes = new HashSet<>();//容器集合
|
||||
for (Map<String, Object> record : readAll) {
|
||||
String stockCode = record.get("代码").toString().trim();
|
||||
if (!StringUtils.isEmpty(stockCode)) {
|
||||
StockCodes.add(stockCode);
|
||||
}
|
||||
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();
|
||||
stock.setCode(code);
|
||||
stock.setName(code);
|
||||
stock.setStatus(BaseStatus.FREE);
|
||||
stock.setEnabled(true);
|
||||
stock.setStockType(st);
|
||||
stock.setDept(UserUtils.getDept());
|
||||
stockService.create(stock);
|
||||
}else{
|
||||
stock.setCode(code);
|
||||
stock.setName(code);
|
||||
stock.setStockType(st);
|
||||
stockService.update(stock);
|
||||
}
|
||||
|
||||
log.info("导入"+(i+1)+"行");
|
||||
}
|
||||
}catch (Exception e){
|
||||
ApiError apiError = ApiError.errorJosn(HttpStatus.BAD_REQUEST.value(), "导入异常---第"+(i+1)+"行:"+e.toString());
|
||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
||||
|
||||
//获取已存在的点位
|
||||
Map<String, Stock> existingStock = stockService.findByCodes(StockCodes);
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ApiError apiError = ApiError.errorJosn(HttpStatus.OK.value(), "导入成功:"+(i)+"行 新增("+new_len+")修改("+edit_len+")");
|
||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
||||
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.setEnabled(true);
|
||||
return stock;
|
||||
}
|
||||
|
||||
private Stock updateStock(Stock stock, StockType stockType) {
|
||||
stock.setStockType(stockType);
|
||||
return stock;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation("查询stock")
|
||||
public ResponseEntity<Object> queryStock(StockQueryCriteria criteria, Pageable pageable){
|
||||
return new ResponseEntity<>(stockService.queryAll(criteria,pageable),HttpStatus.OK);
|
||||
public ResponseEntity<Object> queryStock(StockQueryCriteria criteria, Pageable pageable) {
|
||||
return new ResponseEntity<>(stockService.queryAll(criteria, pageable), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("查询可用空闲的容器stock")
|
||||
@GetMapping(value = "/queryKyStockList")
|
||||
public ResponseEntity<Object> queryKyStockList(StockQueryCriteria criteria, Pageable pageable){
|
||||
return new ResponseEntity<>(stockService.queryKyStockList(criteria.getCode()),HttpStatus.OK);
|
||||
public ResponseEntity<Object> queryKyStockList(StockQueryCriteria criteria, Pageable pageable) {
|
||||
return new ResponseEntity<>(stockService.queryKyStockList(criteria.getCode()), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Log("新增stock")
|
||||
@ApiOperation("新增stock")
|
||||
@PreAuthorize("@el.check('stock:add')")
|
||||
public ResponseEntity<Object> createStock(@Validated @RequestBody Stock resources){
|
||||
public ResponseEntity<Object> createStock(@Validated @RequestBody Stock resources) {
|
||||
resources.setDept(UserUtils.getDept());
|
||||
resources.setEnabled(true);
|
||||
return new ResponseEntity<>(stockService.create(resources),HttpStatus.CREATED);
|
||||
return new ResponseEntity<>(stockService.create(resources), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@Log("修改stock")
|
||||
@ApiOperation("修改stock")
|
||||
@PreAuthorize("@el.check('stock:edit')")
|
||||
public ResponseEntity<Object> updateStock(@Validated @RequestBody Stock resources){
|
||||
public ResponseEntity<Object> updateStock(@Validated @RequestBody Stock resources) {
|
||||
stockService.update(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
|
@ -167,7 +197,7 @@ public class StockController {
|
|||
@Log("修改stock")
|
||||
@ApiOperation("修改stock")
|
||||
@PreAuthorize("@el.check('stock:edit')")
|
||||
public ResponseEntity<Object> updateStockOK(@Validated @RequestBody Stock resources){
|
||||
public ResponseEntity<Object> updateStockOK(@Validated @RequestBody Stock resources) {
|
||||
stockService.update(resources);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
|
@ -176,7 +206,7 @@ public class StockController {
|
|||
@Log("批量修改stock的状态")
|
||||
@ApiOperation("批量修改stock的状态")
|
||||
@PreAuthorize("@el.check('stock:edit')")
|
||||
public ResponseEntity<Object> updateStockStatus(@Validated @RequestBody List<Stock> stockList){
|
||||
public ResponseEntity<Object> updateStockStatus(@Validated @RequestBody List<Stock> stockList) {
|
||||
for (Stock s : stockList) {
|
||||
stockService.update(s);
|
||||
}
|
||||
|
|
@ -194,8 +224,16 @@ public class StockController {
|
|||
|
||||
@PostMapping(value = "/getItemCode")
|
||||
@AnonymousAccess
|
||||
public ResponseEntity<Object> getItemCode(@Validated @RequestBody String stockCode){
|
||||
return new ResponseEntity<>(stockService.getItemCode(stockCode),HttpStatus.OK);
|
||||
public ResponseEntity<Object> getItemCode(@Validated @RequestBody String stockCode) {
|
||||
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.List;
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
|
|
@ -187,4 +188,6 @@ public interface StockService {
|
|||
*/
|
||||
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) {
|
||||
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