fix (farm): 首页,基地/地块 (补充提交)

- 优先从缓存中获取地块信息
- 项目启动,预加载地块
This commit is contained in:
达富斌
2025-11-13 14:29:47 +08:00
parent 297fd4edb2
commit 888681752b
5 changed files with 148 additions and 66 deletions

View File

@@ -81,7 +81,7 @@ public class PlotInfoController {
@Parameter(name = "id", description = "编号", required = true)
public CommonResult<PlotLesseeInfoRespVO> getPlotInfo(@RequestParam("id") String id) {
PlotInfoDO plotInfo = plotInfoService.getPlotInfo(id);
return success(plotLesseeService.exportPlotLesseeInfoExcel(id,plotInfo));
return success(plotLesseeService.getPlotLessee(id,plotInfo));
}
@GetMapping("/page")

View File

@@ -332,6 +332,26 @@ public class PlotInfoCacheService {
}
}
public void cleanupOldIndexes(String plotId, String oldBaseId, String oldTenantId) {
try {
// 从基地索引集合中移除
if (StringUtils.hasText(oldBaseId)) {
String oldBaseKey = "plots:base:" + oldBaseId;
stringRedisTemplate.opsForSet().remove(oldBaseKey, plotId);
log.debug("清理旧基地索引,基地ID: {}, 地块ID: {}", oldBaseId, plotId);
}
// 从租户索引集合中移除
if (StringUtils.hasText(oldTenantId)) {
String oldTenantKey = "plots:tenant:" + oldTenantId;
stringRedisTemplate.opsForSet().remove(oldTenantKey, plotId);
log.debug("清理旧租户索引,租户ID: {}, 地块ID: {}", oldTenantId, plotId);
}
} catch (Exception e) {
log.error("清理旧索引失败,地块ID: {}", plotId, e);
}
}
// ==================== 私有方法 ====================
/**

View File

@@ -75,7 +75,7 @@ public class PlotInfoServiceImpl implements PlotInfoService {
// 插入户主信息
plotLesseeService.insertPlotLessee(createReqVO, createReqVO.getBaseId(), plotInfo.getId(), plotInfo.getPlotName());
// 缓存处理
// 添加缓存
pCatchService.createOrUpdatePlotInfo(plotInfo);
// 返回
@@ -92,10 +92,18 @@ public class PlotInfoServiceImpl implements PlotInfoService {
// 校验地块面积
validatePlotArea(updateReqVO.getBaseId(), updateReqVO.getId(), updateReqVO.getPlotArea());
// 更新
// 先获取旧数据,用于清理旧索引
PlotInfoDO oldPlotInfo = plotInfoMapper.selectById(updateReqVO.getId());
String oldBaseId = oldPlotInfo != null ? oldPlotInfo.getBaseId() : null;
String oldTenantId = oldPlotInfo != null ? oldPlotInfo.getTenantId() : null;
// 更新数据库
PlotInfoDO plotInfo = BeanUtils.toBean(updateReqVO, PlotInfoDO.class);
plotInfoMapper.updateById(plotInfo);
// 更新缓存 - 从数据库重新查询完整数据
updatePlotInfoCache(updateReqVO.getId(), oldBaseId, oldTenantId);
// 更新租户信息
plotLesseeService.updatePlotLessee(updateReqVO);
}
@@ -114,27 +122,17 @@ public class PlotInfoServiceImpl implements PlotInfoService {
}
// 删除
plotInfoMapper.deleteById(id);
// 删除缓存
this.deletePlotInfoCache(obj);
}
@Override
public void deletePlotInfoListByIds(List<String> ids) {
// 删除
// 批量删除
plotInfoMapper.deleteByIds(ids);
}
private void validatePlotInfoExists(String id) {
if (plotInfoMapper.selectById(id) == null) {
throw exception(PLOT_INFO_NOT_EXISTS);
}
}
private void validatePlotArea(String baseId, String plotId, BigDecimal updatePlotArea) {
boolean flag = this.validateCurrentPlotArea(baseId, plotId, updatePlotArea);
if (flag) {
throw exception(PLOT_AREA_ERROR);
}
}
@Override
public PlotInfoDO getPlotInfo(String id) {
LambdaQueryWrapperX<PlotInfoDO> wrapper = new LambdaQueryWrapperX<>();
@@ -225,18 +223,6 @@ public class PlotInfoServiceImpl implements PlotInfoService {
}
}
/**
* 降级方案:直接从数据库查询
*/
private List<PlotInfoDO> fallbackToDatabase(String baseId) {
try {
List<PlotInfoDO> result = plotInfoMapper.selectList("base_id", baseId);
return result != null ? result : Collections.emptyList();
} catch (Exception e) {
log.error("降级查询数据库也失败,基地ID: {}", baseId, e);
return Collections.emptyList();
}
}
@Override
public List<PlotInfoDO> getPlotInfos() {
@@ -280,29 +266,6 @@ public class PlotInfoServiceImpl implements PlotInfoService {
return count > 0;
}
private boolean validateCurrentPlotArea(String baseId, String plotId, BigDecimal currentPlotArea) {
List<PlotInfoDO> plotInfoDOS = plotInfoMapper.selectList("base_id", baseId);
BigDecimal currentTotalArea = null;
if (plotInfoDOS == null || plotInfoDOS.isEmpty()) {
currentTotalArea = currentPlotArea;
}else{
BigDecimal plotTotalArea = plotInfoDOS.stream().map(PlotInfoDO::getPlotArea).filter(area -> area != null).reduce(BigDecimal.ZERO, BigDecimal::add);
if (null == plotId) {
currentTotalArea = plotTotalArea.add(currentPlotArea);
}else{
PlotInfoDO one = plotInfoMapper.selectOne("id", plotId);
currentTotalArea = plotTotalArea.subtract(one.getPlotArea()).add(currentPlotArea);
}
}
// 计算基地面积
BaseInfoDO baseObj = baseInfoMapper.selectOne("id", baseId);
BigDecimal baseArea = baseObj.getArea();
// 比对
return currentTotalArea.compareTo(baseArea) > 0;
}
public boolean validateTotalPlotArea(String baseId, BigDecimal baseArea) {
List<PlotInfoDO> plotInfoDOS = plotInfoMapper.selectList("base_id", baseId);
if (plotInfoDOS == null || plotInfoDOS.isEmpty()) {
@@ -340,4 +303,107 @@ public class PlotInfoServiceImpl implements PlotInfoService {
return plotInfoMapper.selectCount(wrapperX);
}
// ==================== 私有方法 ====================
/**
* 更新地块信息缓存
*/
private void updatePlotInfoCache(String plotId, String oldBaseId, String oldTenantId) {
try {
// 1. 从数据库重新查询完整的最新数据
PlotInfoDO updatedPlotInfo = plotInfoMapper.selectById(plotId);
if (updatedPlotInfo == null) {
log.warn("更新缓存时未找到地块信息,地块ID: {}", plotId);
return;
}
// 2. 检查基地ID是否发生变化,如果变化需要清理旧索引
boolean baseIdChanged = oldBaseId != null && !oldBaseId.equals(updatedPlotInfo.getBaseId());
boolean tenantIdChanged = oldTenantId != null && !oldTenantId.equals(updatedPlotInfo.getTenantId());
if (baseIdChanged || tenantIdChanged) {
// 清理旧的索引
pCatchService.cleanupOldIndexes(plotId, oldBaseId, oldTenantId);
}
// 3. 更新缓存数据
pCatchService.createOrUpdatePlotInfo(updatedPlotInfo);
log.debug("成功更新地块缓存,地块ID: {}", plotId);
} catch (Exception e) {
log.error("更新地块缓存失败,地块ID: {}", plotId, e);
// 缓存更新失败不应该影响主业务流程
}
}
/**
* 降级方案:直接从数据库查询
*/
private List<PlotInfoDO> fallbackToDatabase(String baseId) {
try {
List<PlotInfoDO> result = plotInfoMapper.selectList("base_id", baseId);
return result != null ? result : Collections.emptyList();
} catch (Exception e) {
log.error("降级查询数据库也失败,基地ID: {}", baseId, e);
return Collections.emptyList();
}
}
private void validatePlotInfoExists(String id) {
if (plotInfoMapper.selectById(id) == null) {
throw exception(PLOT_INFO_NOT_EXISTS);
}
}
private void validatePlotArea(String baseId, String plotId, BigDecimal updatePlotArea) {
boolean flag = this.validateCurrentPlotArea(baseId, plotId, updatePlotArea);
if (flag) {
throw exception(PLOT_AREA_ERROR);
}
}
private boolean validateCurrentPlotArea(String baseId, String plotId, BigDecimal currentPlotArea) {
List<PlotInfoDO> plotInfoDOS = plotInfoMapper.selectList("base_id", baseId);
BigDecimal currentTotalArea = null;
if (plotInfoDOS == null || plotInfoDOS.isEmpty()) {
currentTotalArea = currentPlotArea;
}else{
BigDecimal plotTotalArea = plotInfoDOS.stream().map(PlotInfoDO::getPlotArea).filter(area -> area != null).reduce(BigDecimal.ZERO, BigDecimal::add);
if (null == plotId) {
currentTotalArea = plotTotalArea.add(currentPlotArea);
}else{
PlotInfoDO one = plotInfoMapper.selectOne("id", plotId);
currentTotalArea = plotTotalArea.subtract(one.getPlotArea()).add(currentPlotArea);
}
}
// 计算基地面积
BaseInfoDO baseObj = baseInfoMapper.selectOne("id", baseId);
BigDecimal baseArea = baseObj.getArea();
// 比对
return currentTotalArea.compareTo(baseArea) > 0;
}
/**
* 删除地块信息缓存
*/
private void deletePlotInfoCache(PlotInfoDO plotInfo) {
try {
// 使用缓存服务删除缓存(包括所有索引)
pCatchService.deletePlotInfo(
plotInfo.getId(),
plotInfo.getTenantId(),
plotInfo.getBaseId()
);
log.debug("成功删除地块缓存,地块ID: {}, 地块名称: {}", plotInfo.getId(), plotInfo.getPlotName());
} catch (Exception e) {
log.error("删除地块缓存失败,地块ID: {}", plotInfo.getId(), e);
// 缓存删除失败不应该影响主业务流程,只记录日志
}
}
}

View File

@@ -14,8 +14,6 @@ public interface PlotLesseeService {
/**
* 插入数据
*
* @param createReqVO
*/
void insertPlotLessee(PlotInfoSaveReqVO createReqVO, String baseId, String plotId, String plotName);
@@ -24,31 +22,29 @@ public interface PlotLesseeService {
*/
void updatePlotLessee(PlotInfoSaveReqVO updateReqVO);
/**
* 地块&租户信息
*/
PlotLesseeInfoRespVO getPlotLessee(String id, PlotInfoDO plotInfo);
/**
* 导入
* @param req
* @return
*/
PlotLesseeImportRespVO importPlotLesseeInfoExcel(List<PlotLesseeForImportExcelRespVO> req);
/**
* 导出
* @return
*/
List<PlotLesseeForExportExcelRespVO> exportPlotLesseeInfoExcel();
/**
* 获取租户集合
* @param plotId
* @return
* 获取当前租户
*/
List<PlotLesseeDetailsRespVO> getPlotLesseeList(String plotId);
List<PlotLesseeDetailsRespVO> getPlotLesseeHistoryList(String plotId);
/**
* 地块&租户信息
* @param id
* @return
* 获取历史租户
*/
PlotLesseeInfoRespVO exportPlotLesseeInfoExcel(String id, PlotInfoDO plotInfo);
List<PlotLesseeDetailsRespVO> getPlotLesseeHistoryList(String plotId);
}

View File

@@ -208,7 +208,7 @@ public class PlotLesseeServiceImpl implements PlotLesseeService {
}
@Override
public PlotLesseeInfoRespVO exportPlotLesseeInfoExcel(String id, PlotInfoDO plotInfo) {
public PlotLesseeInfoRespVO getPlotLessee(String id, PlotInfoDO plotInfo) {
LambdaQueryWrapperX<PlotLesseeDO> x = new LambdaQueryWrapperX<>();
x.eq(PlotLesseeDO::getTenantId, getLoginUser().getTenantId().toString());
x.eq(PlotLesseeDO::getDeleted, CommonConstants.NO_CODE);