fix (farm): 首页,基地/地块
- 首页加载地块信息慢bug,使用redis缓存解决 - 地块户主信息,增加租赁费字段 ,包含导出/入
This commit is contained in:
@@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Schema(description = "管理后台 - 地块户主 - 导出")
|
||||
@@ -59,6 +60,10 @@ public class PlotLesseeForExportExcelRespVO {
|
||||
@ExcelProperty("租赁剩余天数")
|
||||
private Long expirationDays;
|
||||
|
||||
@Schema(description = "租赁费")
|
||||
@ExcelProperty("租赁费")
|
||||
private BigDecimal leaseRental;
|
||||
|
||||
@Schema(description = "是否修改 (是/否)")
|
||||
@ExcelProperty("是否修改 (是/否))")
|
||||
private String isModified;
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Schema(description = "管理后台 - 地块户主 - 导入")
|
||||
@@ -60,6 +61,10 @@ public class PlotLesseeForImportExcelRespVO {
|
||||
@ExcelProperty("租赁剩余天数)")
|
||||
private Long expirationDays;
|
||||
|
||||
@Schema(description = "租赁费")
|
||||
@ExcelProperty( "租赁费")
|
||||
private BigDecimal leaseRental;
|
||||
|
||||
@Schema(description = "是否修改 (是/否)")
|
||||
@ExcelProperty("是否修改 (是/否))")
|
||||
private String isModified;
|
||||
|
||||
@@ -5,6 +5,8 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @description: 租赁人信息
|
||||
* @author: binda
|
||||
@@ -27,7 +29,11 @@ public class PlotLesseeSaveReqVO {
|
||||
private String leaseStartDate;
|
||||
@Schema(description = "租赁接口日期")
|
||||
private String leaseEndDate;
|
||||
@Schema(description = "租赁费")
|
||||
private BigDecimal leaseRental;
|
||||
@Schema(description = "租赁状态 (1: 当前租赁人; 2: 历史租赁人)")
|
||||
private String leaseStatus;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
@@ -29,6 +30,7 @@ public class PlotLesseeDO extends BaseDO {
|
||||
private String leasePhone;
|
||||
private LocalDate leaseStartDate;
|
||||
private LocalDate leaseEndDate;
|
||||
private BigDecimal leaseRental;
|
||||
private Integer leaseStatus;
|
||||
|
||||
private String baseId;
|
||||
|
||||
@@ -0,0 +1,536 @@
|
||||
package cn.iocoder.yudao.module.farm.service.plotinfo;
|
||||
|
||||
import cn.iocoder.yudao.module.farm.dal.dataobject.plotinfo.PlotInfoDO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands;
|
||||
import org.springframework.data.redis.core.RedisCallback;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @description: 地块缓存
|
||||
* @author: binda
|
||||
* @date: 2025/11/12 15:35
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class PlotInfoCacheService {
|
||||
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
// 缓存前缀
|
||||
private static final String CACHE_PREFIX = "plots:";
|
||||
// 缓存过期时间
|
||||
private static final Duration CACHE_TTL = Duration.ofHours(2);
|
||||
|
||||
// Hash 字段常量
|
||||
private static final String FIELD_ID = "id";
|
||||
private static final String FIELD_BASE_ID = "baseId";
|
||||
private static final String FIELD_BASE_NAME = "baseName";
|
||||
private static final String FIELD_PLOT_ID = "plotId";
|
||||
private static final String FIELD_PLOT_NAME = "plotName";
|
||||
private static final String FIELD_PLOT_AREA = "plotArea";
|
||||
private static final String FIELD_PLOT_COLOR = "plotColor";
|
||||
private static final String FIELD_POINT = "point";
|
||||
private static final String FIELD_EMPLOYEE_ID = "employeeId";
|
||||
private static final String FIELD_EMPLOYEE_NAME = "employeeName";
|
||||
private static final String FIELD_MANAGER = "manager";
|
||||
private static final String FIELD_TYPE = "type";
|
||||
private static final String FIELD_PLOT_LEASE_STATUS = "plotLeaseStatus";
|
||||
private static final String FIELD_GRID_NUMBER = "gridNumber";
|
||||
private static final String FIELD_TENANT_ID = "tenantId";
|
||||
|
||||
public PlotInfoCacheService(StringRedisTemplate stringRedisTemplate) {
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
// ==================== 创建/更新缓存 ====================
|
||||
|
||||
/**
|
||||
* 创建或更新地块信息缓存
|
||||
*/
|
||||
public void createOrUpdatePlotInfo(PlotInfoDO plotInfo) {
|
||||
if (plotInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String plotKey = buildPlotKey(plotInfo.getId());
|
||||
|
||||
// 使用 Hash 存储
|
||||
Map<String, String> hashData = toHashData(plotInfo);
|
||||
stringRedisTemplate.opsForHash().putAll(plotKey, hashData);
|
||||
stringRedisTemplate.expire(plotKey, CACHE_TTL);
|
||||
|
||||
// 更新索引集合
|
||||
updateIndexes(plotInfo);
|
||||
|
||||
log.debug("成功缓存地块信息(Hash),地块ID: {}", plotInfo.getId());
|
||||
} catch (Exception e) {
|
||||
log.error("缓存地块信息失败,地块ID: {}", plotInfo.getId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量创建或更新地块信息缓存
|
||||
*/
|
||||
public void batchCreateOrUpdatePlotInfo(List<PlotInfoDO> plotInfoList) {
|
||||
if (CollectionUtils.isEmpty(plotInfoList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 使用管道批量操作
|
||||
stringRedisTemplate.executePipelined((RedisCallback<Object>) connection -> {
|
||||
for (PlotInfoDO plotInfo : plotInfoList) {
|
||||
if (plotInfo != null) {
|
||||
String plotKey = buildPlotKey(plotInfo.getId());
|
||||
Map<String, String> hashData = toHashData(plotInfo);
|
||||
|
||||
// 批量设置 Hash 字段
|
||||
connection.hashCommands().hMSet(
|
||||
plotKey.getBytes(StandardCharsets.UTF_8),
|
||||
hashData.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
e -> e.getKey().getBytes(StandardCharsets.UTF_8),
|
||||
e -> e.getValue().getBytes(StandardCharsets.UTF_8)
|
||||
))
|
||||
);
|
||||
|
||||
// 设置过期时间
|
||||
connection.keyCommands().expire(
|
||||
plotKey.getBytes(StandardCharsets.UTF_8),
|
||||
CACHE_TTL.getSeconds()
|
||||
);
|
||||
|
||||
// 更新索引
|
||||
updateIndexInPipeline(connection, plotInfo);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
log.debug("批量缓存地块信息(Hash)成功,数量: {}", plotInfoList.size());
|
||||
} catch (Exception e) {
|
||||
log.error("批量缓存地块信息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分更新地块信息
|
||||
*/
|
||||
public void partialUpdatePlotInfo(String plotId, Map<String, Object> updateFields) {
|
||||
if (!StringUtils.hasText(plotId) || CollectionUtils.isEmpty(updateFields)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String plotKey = buildPlotKey(plotId);
|
||||
Map<String, String> hashUpdates = new HashMap<>();
|
||||
|
||||
// 转换更新字段
|
||||
for (Map.Entry<String, Object> entry : updateFields.entrySet()) {
|
||||
if (entry.getValue() != null) {
|
||||
hashUpdates.put(entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
if (!hashUpdates.isEmpty()) {
|
||||
stringRedisTemplate.opsForHash().putAll(plotKey, hashUpdates);
|
||||
stringRedisTemplate.expire(plotKey, CACHE_TTL);
|
||||
|
||||
// 如果更新了索引字段,需要重新构建索引
|
||||
if (updateFields.containsKey(FIELD_TENANT_ID) || updateFields.containsKey(FIELD_BASE_ID)) {
|
||||
// 重新构建索引(这里可以优化,先删除旧索引再添加新索引)
|
||||
PlotInfoDO fullPlotInfo = getPlotInfoById(plotId);
|
||||
if (fullPlotInfo != null) {
|
||||
updateIndexes(fullPlotInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("部分更新地块缓存失败,地块ID: {}", plotId, e);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 查询缓存 ====================
|
||||
|
||||
/**
|
||||
* 通过ID获取地块信息
|
||||
*/
|
||||
public PlotInfoDO getPlotInfoById(String id) {
|
||||
if (!StringUtils.hasText(id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
String plotKey = buildPlotKey(id);
|
||||
Map<Object, Object> hashData = stringRedisTemplate.opsForHash().entries(plotKey);
|
||||
|
||||
if (!CollectionUtils.isEmpty(hashData)) {
|
||||
return fromHashData(hashData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取地块缓存信息失败,地块ID: {}", id, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过plotId获取地块信息
|
||||
*/
|
||||
public PlotInfoDO getPlotInfoByPlotId(String plotId) {
|
||||
if (!StringUtils.hasText(plotId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 由于 plotId 不是主键,需要先找到对应的主键ID
|
||||
String plotIdIndexKey = buildPlotIdIndexKey(plotId);
|
||||
String mainId = stringRedisTemplate.opsForValue().get(plotIdIndexKey);
|
||||
|
||||
if (StringUtils.hasText(mainId)) {
|
||||
return getPlotInfoById(mainId);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("通过plotId获取地块缓存信息失败,plotId: {}", plotId, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过租户ID获取地块集合
|
||||
*/
|
||||
public List<PlotInfoDO> getPlotsByTenantId(String tenantId) {
|
||||
return getPlotsByIndexKey(buildTenantPlotsKey(tenantId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过基地ID获取地块集合
|
||||
*/
|
||||
public List<PlotInfoDO> getPlotsByBaseId(String baseId) {
|
||||
return getPlotsByIndexKey(buildBasePlotsKey(baseId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量获取地块信息
|
||||
*/
|
||||
public List<PlotInfoDO> batchGetPlotInfo(List<String> plotIds) {
|
||||
if (CollectionUtils.isEmpty(plotIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<PlotInfoDO> result = new ArrayList<>();
|
||||
try {
|
||||
// 使用管道批量获取
|
||||
List<Object> results = stringRedisTemplate.executePipelined((RedisCallback<Object>) connection -> {
|
||||
for (String plotId : plotIds) {
|
||||
String plotKey = buildPlotKey(plotId);
|
||||
connection.hashCommands().hGetAll(plotKey.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
for (Object hashDataObj : results) {
|
||||
if (hashDataObj instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<Object, Object> hashData = (Map<Object, Object>) hashDataObj;
|
||||
if (!hashData.isEmpty()) {
|
||||
PlotInfoDO plotInfo = fromHashData(hashData);
|
||||
if (plotInfo != null) {
|
||||
result.add(plotInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("批量获取地块缓存信息失败", e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// ==================== 删除缓存 ====================
|
||||
|
||||
/**
|
||||
* 删除地块信息缓存
|
||||
*/
|
||||
public void deletePlotInfo(String id, String tenantId, String baseId) {
|
||||
if (!StringUtils.hasText(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String plotKey = buildPlotKey(id);
|
||||
|
||||
// 1. 获取地块信息(用于清理索引)
|
||||
PlotInfoDO plotInfo = getPlotInfoById(id);
|
||||
|
||||
// 2. 删除主数据
|
||||
stringRedisTemplate.delete(plotKey);
|
||||
|
||||
// 3. 清理索引
|
||||
cleanupIndexes(plotInfo);
|
||||
|
||||
log.debug("删除地块缓存成功,地块ID: {}", id);
|
||||
} catch (Exception e) {
|
||||
log.error("删除地块缓存失败,地块ID: {}", id, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除地块信息缓存
|
||||
*/
|
||||
public void batchDeletePlotInfo(List<PlotInfoDO> plotInfoList) {
|
||||
if (CollectionUtils.isEmpty(plotInfoList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 使用管道批量操作
|
||||
stringRedisTemplate.executePipelined((RedisCallback<Object>) connection -> {
|
||||
for (PlotInfoDO plotInfo : plotInfoList) {
|
||||
if (plotInfo != null) {
|
||||
String plotKey = buildPlotKey(plotInfo.getId());
|
||||
// 删除主数据
|
||||
connection.del(plotKey.getBytes(StandardCharsets.UTF_8));
|
||||
// 清理索引
|
||||
cleanupIndexesInPipeline(connection, plotInfo);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
log.debug("批量删除地块缓存成功,数量: {}", plotInfoList.size());
|
||||
} catch (Exception e) {
|
||||
log.error("批量删除地块缓存失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 私有方法 ====================
|
||||
|
||||
/**
|
||||
* PlotInfoDO 转 Hash 数据
|
||||
*/
|
||||
private Map<String, String> toHashData(PlotInfoDO plotInfo) {
|
||||
Map<String, String> hashData = new HashMap<>();
|
||||
|
||||
hashData.put(FIELD_ID, plotInfo.getId());
|
||||
hashData.put(FIELD_BASE_ID, plotInfo.getBaseId());
|
||||
hashData.put(FIELD_BASE_NAME, plotInfo.getBaseName());
|
||||
hashData.put(FIELD_PLOT_ID, plotInfo.getPlotId());
|
||||
hashData.put(FIELD_PLOT_NAME, plotInfo.getPlotName());
|
||||
hashData.put(FIELD_PLOT_AREA, plotInfo.getPlotArea() != null ? plotInfo.getPlotArea().toString() : null);
|
||||
hashData.put(FIELD_PLOT_COLOR, plotInfo.getPlotColor());
|
||||
hashData.put(FIELD_POINT, plotInfo.getPoint());
|
||||
hashData.put(FIELD_EMPLOYEE_ID, plotInfo.getEmployeeId());
|
||||
hashData.put(FIELD_EMPLOYEE_NAME, plotInfo.getEmployeeName());
|
||||
hashData.put(FIELD_MANAGER, plotInfo.getManager());
|
||||
hashData.put(FIELD_TYPE, plotInfo.getType());
|
||||
hashData.put(FIELD_PLOT_LEASE_STATUS, plotInfo.getPlotLeaseStatus() != null ? plotInfo.getPlotLeaseStatus().toString() : null);
|
||||
hashData.put(FIELD_GRID_NUMBER, plotInfo.getGridNumber() != null ? plotInfo.getGridNumber().toString() : null);
|
||||
hashData.put(FIELD_TENANT_ID, plotInfo.getTenantId());
|
||||
|
||||
// 移除 null 值
|
||||
hashData.entrySet().removeIf(entry -> entry.getValue() == null);
|
||||
|
||||
return hashData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash 数据转 PlotInfoDO
|
||||
*/
|
||||
private PlotInfoDO fromHashData(Map<Object, Object> hashData) {
|
||||
if (CollectionUtils.isEmpty(hashData)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
PlotInfoDO plotInfo = new PlotInfoDO();
|
||||
|
||||
plotInfo.setId(getStringValue(hashData, FIELD_ID));
|
||||
plotInfo.setBaseId(getStringValue(hashData, FIELD_BASE_ID));
|
||||
plotInfo.setBaseName(getStringValue(hashData, FIELD_BASE_NAME));
|
||||
plotInfo.setPlotId(getStringValue(hashData, FIELD_PLOT_ID));
|
||||
plotInfo.setPlotName(getStringValue(hashData, FIELD_PLOT_NAME));
|
||||
plotInfo.setPlotArea(getBigDecimalValue(hashData, FIELD_PLOT_AREA));
|
||||
plotInfo.setPlotColor(getStringValue(hashData, FIELD_PLOT_COLOR));
|
||||
plotInfo.setPoint(getStringValue(hashData, FIELD_POINT));
|
||||
plotInfo.setEmployeeId(getStringValue(hashData, FIELD_EMPLOYEE_ID));
|
||||
plotInfo.setEmployeeName(getStringValue(hashData, FIELD_EMPLOYEE_NAME));
|
||||
plotInfo.setManager(getStringValue(hashData, FIELD_MANAGER));
|
||||
plotInfo.setType(getStringValue(hashData, FIELD_TYPE));
|
||||
plotInfo.setPlotLeaseStatus(getIntegerValue(hashData, FIELD_PLOT_LEASE_STATUS));
|
||||
plotInfo.setGridNumber(getIntegerValue(hashData, FIELD_GRID_NUMBER));
|
||||
plotInfo.setTenantId(getStringValue(hashData, FIELD_TENANT_ID));
|
||||
|
||||
return plotInfo;
|
||||
} catch (Exception e) {
|
||||
log.error("转换Hash数据到PlotInfoDO失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新索引
|
||||
*/
|
||||
private void updateIndexes(PlotInfoDO plotInfo) {
|
||||
// 1. 添加到租户索引
|
||||
if (StringUtils.hasText(plotInfo.getTenantId())) {
|
||||
String tenantKey = buildTenantPlotsKey(plotInfo.getTenantId());
|
||||
stringRedisTemplate.opsForSet().add(tenantKey, plotInfo.getId());
|
||||
stringRedisTemplate.expire(tenantKey, CACHE_TTL);
|
||||
}
|
||||
|
||||
// 2. 添加到基地索引
|
||||
if (StringUtils.hasText(plotInfo.getBaseId())) {
|
||||
String baseKey = buildBasePlotsKey(plotInfo.getBaseId());
|
||||
stringRedisTemplate.opsForSet().add(baseKey, plotInfo.getId());
|
||||
stringRedisTemplate.expire(baseKey, CACHE_TTL);
|
||||
}
|
||||
|
||||
// 3. 创建 plotId 到主键的映射
|
||||
if (StringUtils.hasText(plotInfo.getPlotId())) {
|
||||
String plotIdIndexKey = buildPlotIdIndexKey(plotInfo.getPlotId());
|
||||
stringRedisTemplate.opsForValue().set(plotIdIndexKey, plotInfo.getId(), CACHE_TTL);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理索引
|
||||
*/
|
||||
private void cleanupIndexes(PlotInfoDO plotInfo) {
|
||||
if (plotInfo == null) return;
|
||||
|
||||
// 从租户索引中移除
|
||||
if (StringUtils.hasText(plotInfo.getTenantId())) {
|
||||
String tenantKey = buildTenantPlotsKey(plotInfo.getTenantId());
|
||||
stringRedisTemplate.opsForSet().remove(tenantKey, plotInfo.getId());
|
||||
}
|
||||
|
||||
// 从基地索引中移除
|
||||
if (StringUtils.hasText(plotInfo.getBaseId())) {
|
||||
String baseKey = buildBasePlotsKey(plotInfo.getBaseId());
|
||||
stringRedisTemplate.opsForSet().remove(baseKey, plotInfo.getId());
|
||||
}
|
||||
|
||||
// 删除 plotId 索引
|
||||
if (StringUtils.hasText(plotInfo.getPlotId())) {
|
||||
String plotIdIndexKey = buildPlotIdIndexKey(plotInfo.getPlotId());
|
||||
stringRedisTemplate.delete(plotIdIndexKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过索引键获取地块列表
|
||||
*/
|
||||
private List<PlotInfoDO> getPlotsByIndexKey(String indexKey) {
|
||||
if (!StringUtils.hasText(indexKey)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
Set<String> plotIds = stringRedisTemplate.opsForSet().members(indexKey);
|
||||
if (CollectionUtils.isEmpty(plotIds)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return batchGetPlotInfo(new ArrayList<>(plotIds));
|
||||
} catch (Exception e) {
|
||||
log.error("通过索引键获取地块列表失败,键: {}", indexKey, e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 管道操作辅助方法 ====================
|
||||
|
||||
private void updateIndexInPipeline(RedisConnection connection, PlotInfoDO plotInfo) {
|
||||
if (StringUtils.hasText(plotInfo.getTenantId())) {
|
||||
String tenantKey = buildTenantPlotsKey(plotInfo.getTenantId());
|
||||
connection.setCommands().sAdd(tenantKey.getBytes(StandardCharsets.UTF_8),
|
||||
plotInfo.getId().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(plotInfo.getBaseId())) {
|
||||
String baseKey = buildBasePlotsKey(plotInfo.getBaseId());
|
||||
connection.setCommands().sAdd(baseKey.getBytes(StandardCharsets.UTF_8),
|
||||
plotInfo.getId().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(plotInfo.getPlotId())) {
|
||||
String plotIdIndexKey = buildPlotIdIndexKey(plotInfo.getPlotId());
|
||||
connection.stringCommands().set(
|
||||
plotIdIndexKey.getBytes(StandardCharsets.UTF_8),
|
||||
plotInfo.getId().getBytes(StandardCharsets.UTF_8),
|
||||
Expiration.from(CACHE_TTL),
|
||||
RedisStringCommands.SetOption.UPSERT
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanupIndexesInPipeline(RedisConnection connection, PlotInfoDO plotInfo) {
|
||||
if (StringUtils.hasText(plotInfo.getTenantId())) {
|
||||
String tenantKey = buildTenantPlotsKey(plotInfo.getTenantId());
|
||||
connection.setCommands().sRem(tenantKey.getBytes(StandardCharsets.UTF_8),
|
||||
plotInfo.getId().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(plotInfo.getBaseId())) {
|
||||
String baseKey = buildBasePlotsKey(plotInfo.getBaseId());
|
||||
connection.setCommands().sRem(baseKey.getBytes(StandardCharsets.UTF_8),
|
||||
plotInfo.getId().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(plotInfo.getPlotId())) {
|
||||
String plotIdIndexKey = buildPlotIdIndexKey(plotInfo.getPlotId());
|
||||
connection.del(plotIdIndexKey.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 工具方法 ====================
|
||||
|
||||
private String getStringValue(Map<Object, Object> hashData, String field) {
|
||||
Object value = hashData.get(field);
|
||||
return value != null ? value.toString() : null;
|
||||
}
|
||||
|
||||
private Integer getIntegerValue(Map<Object, Object> hashData, String field) {
|
||||
String value = getStringValue(hashData, field);
|
||||
return value != null ? Integer.parseInt(value) : null;
|
||||
}
|
||||
|
||||
private BigDecimal getBigDecimalValue(Map<Object, Object> hashData, String field) {
|
||||
String value = getStringValue(hashData, field);
|
||||
return value != null ? new BigDecimal(value) : null;
|
||||
}
|
||||
|
||||
// ==================== 键构建方法 ====================
|
||||
|
||||
private String buildPlotKey(String id) {
|
||||
return CACHE_PREFIX + "info:" + id;
|
||||
}
|
||||
|
||||
private String buildTenantPlotsKey(String tenantId) {
|
||||
return CACHE_PREFIX + "tenant:" + tenantId;
|
||||
}
|
||||
|
||||
private String buildBasePlotsKey(String baseId) {
|
||||
return CACHE_PREFIX + "base:" + baseId;
|
||||
}
|
||||
|
||||
private String buildPlotIdIndexKey(String plotId) {
|
||||
return CACHE_PREFIX + "index:plotId:" + plotId;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
@@ -48,14 +49,20 @@ public class PlotInfoServiceImpl implements PlotInfoService {
|
||||
private final PlotLesseeMapper plotLesseeMapper;
|
||||
private final ProductionPlanService productionPlanService;
|
||||
private final PersonnelManagementService personnelManagementService;
|
||||
private final PlotInfoCacheService pCatchService;
|
||||
|
||||
|
||||
public PlotInfoServiceImpl(BaseInfoMapper baseInfoMapper, PlotInfoMapper plotInfoMapper, PlotLesseeMapper plotLesseeMapper, ProductionPlanService productionPlanService, PersonnelManagementService personnelManagementService) {
|
||||
public PlotInfoServiceImpl(
|
||||
BaseInfoMapper baseInfoMapper,
|
||||
PlotInfoMapper plotInfoMapper,
|
||||
PlotLesseeMapper plotLesseeMapper,
|
||||
ProductionPlanService productionPlanService,
|
||||
PersonnelManagementService personnelManagementService, PlotInfoCacheService pCatchService) {
|
||||
this.baseInfoMapper = baseInfoMapper;
|
||||
this.plotInfoMapper = plotInfoMapper;
|
||||
this.plotLesseeMapper = plotLesseeMapper;
|
||||
this.productionPlanService = productionPlanService;
|
||||
this.personnelManagementService = personnelManagementService;
|
||||
this.pCatchService = pCatchService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -77,6 +84,9 @@ public class PlotInfoServiceImpl implements PlotInfoService {
|
||||
insertLessee(plotLesseeDO, createReqVO.getBaseId(), plotInfo.getId(), plotInfo.getPlotName());
|
||||
}
|
||||
|
||||
// 缓存处理
|
||||
pCatchService.createOrUpdatePlotInfo(plotInfo);
|
||||
|
||||
// 返回
|
||||
return plotInfo.getId();
|
||||
}
|
||||
@@ -228,6 +238,7 @@ public class PlotInfoServiceImpl implements PlotInfoService {
|
||||
target.setLeasePhone(l.getLeasePhone());
|
||||
target.setLeaseStartDate(l.getLeaseStartDate());
|
||||
target.setLeaseEndDate(l.getLeaseEndDate());
|
||||
target.setLeaseRental(l.getLeaseRental().setScale(2, RoundingMode.HALF_UP));
|
||||
target.setExpirationDays(getDaysUntilLeaseEnd(l.getLeaseEndDate()));
|
||||
}
|
||||
return target;
|
||||
|
||||
@@ -17,3 +17,4 @@ public class PlotLesseeServiceImpl implements PlotLesseeService {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user