You've already forked FrameTour-BE
feat(scenic): 优化景区信息获取与缓存机制
-移除景区信息查询相关冗余代码 - 增加缓存逻辑,提高景区信息获取效率 - 更新 ScenicRepository 中的 getScenic 和 getScenicConfig 方法 - 重构 ScenicServiceImpl 中的 list 方法 - 删除 ScenicService 接口中未使用的多个方法
This commit is contained in:
@@ -23,51 +23,9 @@ public class ScenicReqQuery extends BaseQueryParameterReq {
|
||||
*/
|
||||
// 景区名称
|
||||
private String name;
|
||||
/**
|
||||
* 景区介绍
|
||||
*/
|
||||
// 景区介绍
|
||||
private String introduction;
|
||||
/**
|
||||
* 经度
|
||||
*/
|
||||
// 经度
|
||||
private BigDecimal longitude;
|
||||
/***
|
||||
* 纬度
|
||||
*/
|
||||
// 纬度
|
||||
private BigDecimal latitude;
|
||||
/**
|
||||
* 半径(km)
|
||||
*/
|
||||
// 半径(km)
|
||||
private BigDecimal radius;
|
||||
/**
|
||||
* 省份
|
||||
*/
|
||||
// 省份
|
||||
private String province;
|
||||
/**
|
||||
* 城市
|
||||
*/
|
||||
// 城市
|
||||
private String city;
|
||||
/**
|
||||
* 区
|
||||
*/
|
||||
// 区
|
||||
private String area;
|
||||
/**
|
||||
* 详细地址
|
||||
*/
|
||||
// 详细地址
|
||||
private String address;
|
||||
/**
|
||||
* 状态 1启用0关闭
|
||||
*/
|
||||
// 状态 1启用0关闭
|
||||
private String status;
|
||||
private Date startTime;
|
||||
private Date endTime;
|
||||
}
|
||||
|
@@ -3,7 +3,6 @@ package com.ycwl.basic.repository;
|
||||
import com.ycwl.basic.utils.JacksonUtil;
|
||||
import com.ycwl.basic.mapper.MpConfigMapper;
|
||||
import com.ycwl.basic.mapper.MpNotifyConfigMapper;
|
||||
import com.ycwl.basic.mapper.ScenicMapper;
|
||||
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
|
||||
import com.ycwl.basic.model.pc.mp.MpNotifyConfigEntity;
|
||||
import com.ycwl.basic.model.pc.mp.ScenicMpNotifyVO;
|
||||
@@ -24,8 +23,6 @@ import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ScenicRepository {
|
||||
@Autowired
|
||||
private ScenicMapper scenicMapper;
|
||||
@Autowired
|
||||
private MpConfigMapper mpConfigMapper;
|
||||
@Autowired
|
||||
@@ -44,18 +41,48 @@ public class ScenicRepository {
|
||||
public ScenicEntity getScenic(Long id) {
|
||||
try {
|
||||
ScenicV2DTO scenicDTO = scenicIntegrationService.getScenic(id);
|
||||
return convertToScenicEntity(scenicDTO);
|
||||
ScenicEntity scenicEntity = convertToScenicEntity(scenicDTO);
|
||||
|
||||
// 请求成功,写入缓存
|
||||
if (scenicEntity != null) {
|
||||
redisTemplate.opsForValue().set(
|
||||
String.format(SCENIC_CACHE_KEY, id),
|
||||
JacksonUtil.toJSONString(scenicEntity)
|
||||
);
|
||||
}
|
||||
return scenicEntity;
|
||||
} catch (Exception e) {
|
||||
return scenicMapper.get(id);
|
||||
// 请求失败,尝试从缓存获取历史成功数据
|
||||
String cacheKey = String.format(SCENIC_CACHE_KEY, id);
|
||||
if (redisTemplate.hasKey(cacheKey)) {
|
||||
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(cacheKey), ScenicEntity.class);
|
||||
}
|
||||
// 缓存也没有,返回null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ScenicConfigEntity getScenicConfig(Long scenicId) {
|
||||
try {
|
||||
ScenicV2WithConfigDTO scenicWithConfigDTO = scenicIntegrationService.getScenicWithConfig(scenicId);
|
||||
return convertToScenicConfigEntity(scenicWithConfigDTO, scenicId);
|
||||
ScenicConfigEntity configEntity = convertToScenicConfigEntity(scenicWithConfigDTO, scenicId);
|
||||
|
||||
// 请求成功,写入缓存
|
||||
if (configEntity != null) {
|
||||
redisTemplate.opsForValue().set(
|
||||
String.format(SCENIC_CONFIG_CACHE_KEY, scenicId),
|
||||
JacksonUtil.toJSONString(configEntity)
|
||||
);
|
||||
}
|
||||
return configEntity;
|
||||
} catch (Exception e) {
|
||||
return scenicMapper.getConfig(scenicId);
|
||||
// 请求失败,尝试从缓存获取历史成功数据
|
||||
String cacheKey = String.format(SCENIC_CONFIG_CACHE_KEY, scenicId);
|
||||
if (redisTemplate.hasKey(cacheKey)) {
|
||||
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(cacheKey), ScenicConfigEntity.class);
|
||||
}
|
||||
// 缓存也没有,返回null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -17,23 +17,7 @@ import java.util.List;
|
||||
* @Date:2024/12/3 15:22
|
||||
*/
|
||||
public interface ScenicService {
|
||||
ApiResponse<PageInfo<ScenicRespVO>> pageQuery(ScenicReqQuery scenicReqQuery);
|
||||
ApiResponse<List<ScenicRespVO>> list(ScenicReqQuery scenicReqQuery);
|
||||
ApiResponse<ScenicRespVO> getById(Long id);
|
||||
ApiResponse<Boolean> add(ScenicAddOrUpdateReq scenicAddOrUpdateReq);
|
||||
ApiResponse<Boolean> deleteById(Long id);
|
||||
ApiResponse<Boolean> update(ScenicAddOrUpdateReq scenicAddOrUpdateReq);
|
||||
ApiResponse<Boolean> updateStatus(Long id);
|
||||
ApiResponse<Boolean> addConfig(ScenicConfigEntity scenicConfig);
|
||||
/**
|
||||
* 修改景区配置
|
||||
* @param scenicConfig
|
||||
* @return
|
||||
*/
|
||||
ApiResponse<Boolean> updateConfigById(ScenicConfigEntity scenicConfig);
|
||||
|
||||
ScenicConfigEntity getConfig(Long id);
|
||||
void saveConfig(Long configId, ScenicConfigEntity config);
|
||||
|
||||
IStorageAdapter getScenicStorageAdapter(Long scenicId);
|
||||
|
||||
|
@@ -44,186 +44,14 @@ public class ScenicServiceImpl implements ScenicService {
|
||||
@Autowired
|
||||
private ScenicMapper scenicMapper;
|
||||
@Autowired
|
||||
private ScenicAccountMapper scenicAccountMapper;
|
||||
@Autowired
|
||||
private TaskFaceService taskFaceService;
|
||||
@Autowired
|
||||
private ScenicRepository scenicRepository;
|
||||
|
||||
@Override
|
||||
public ApiResponse<PageInfo<ScenicRespVO>> pageQuery(ScenicReqQuery scenicReqQuery) {
|
||||
PageHelper.startPage(scenicReqQuery.getPageNum(), scenicReqQuery.getPageSize());
|
||||
List<ScenicRespVO> list = scenicMapper.list(scenicReqQuery);
|
||||
PageInfo<ScenicRespVO> pageInfo = new PageInfo<>(list);
|
||||
return ApiResponse.success(pageInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public ApiResponse<List<ScenicRespVO>> list(ScenicReqQuery scenicReqQuery) {
|
||||
return ApiResponse.success(scenicMapper.list(scenicReqQuery));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<ScenicRespVO> getById(Long id) {
|
||||
return ApiResponse.success(scenicMapper.getById(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResponse<Boolean> add(ScenicAddOrUpdateReq scenicAddReq) {
|
||||
Long scenicId = SnowFlakeUtil.getLongId();
|
||||
scenicAddReq.setId(scenicId);
|
||||
int add = scenicMapper.add(scenicAddReq);
|
||||
ScenicAccountEntity scenicAccount = scenicAccountMapper.getByAccount(scenicAddReq.getAccount());
|
||||
if (scenicAccount == null) {
|
||||
scenicAccount = new ScenicAccountEntity();
|
||||
scenicAccount.setId(SnowFlakeUtil.getLongId());
|
||||
scenicAccount.setName(scenicAddReq.getName() + "管理员");
|
||||
scenicAccount.setAccount(scenicAddReq.getAccount());
|
||||
scenicAccount.setPassword(scenicAddReq.getPassword());
|
||||
scenicAccount.setIsSuper(1);
|
||||
scenicAccountMapper.add(scenicAccount);
|
||||
}
|
||||
scenicAccountMapper.addAccountScenicRelation(scenicAccount.getId(), scenicId, 1);
|
||||
if (add > 0) {
|
||||
return ApiResponse.success(true);
|
||||
} else {
|
||||
return ApiResponse.fail("景区添加失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResponse<Boolean> deleteById(Long id) {
|
||||
int i = scenicMapper.deleteById(id);
|
||||
if (i > 0) {
|
||||
scenicAccountMapper.deleteRelationByScenicId(id);
|
||||
IFaceBodyAdapter adapter = getScenicFaceBodyAdapter(id);
|
||||
Thread.ofVirtual().start(() -> {
|
||||
adapter.deleteFaceDb(id.toString());
|
||||
adapter.deleteFaceDb(USER_FACE_DB_NAME + id);
|
||||
});
|
||||
scenicMapper.deleteConfigByScenicId(id);
|
||||
scenicRepository.clearCache(id);
|
||||
scenicFaceBodyAdapterMap.remove(id);
|
||||
scenicStorageAdapterMap.remove(id);
|
||||
scenicPayAdapterMap.remove(id);
|
||||
return ApiResponse.success(true);
|
||||
}else {
|
||||
return ApiResponse.fail("景区删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<Boolean> update(ScenicAddOrUpdateReq scenicUpdateReq) {
|
||||
if (scenicUpdateReq.getId() == null) {
|
||||
return ApiResponse.fail("参数错误");
|
||||
}
|
||||
if (StringUtils.isNotBlank(scenicUpdateReq.getAccount()) && StringUtils.isNotBlank(scenicUpdateReq.getPassword())) {
|
||||
ScenicAccountEntity scenicAccount = scenicAccountMapper.getByAccount(scenicUpdateReq.getAccount());
|
||||
if (scenicAccount != null) {
|
||||
if (!scenicAccount.getScenicId().equals(scenicUpdateReq.getId())) {
|
||||
return ApiResponse.fail("账号已存在");
|
||||
}
|
||||
}
|
||||
ScenicAccountEntity account = scenicAccountMapper.getSuperAccountOfScenic(scenicUpdateReq.getId());
|
||||
if (account != null) {
|
||||
account.setAccount(scenicUpdateReq.getAccount());
|
||||
account.setPassword(scenicUpdateReq.getPassword());
|
||||
scenicAccountMapper.update(account);
|
||||
} else {
|
||||
account = new ScenicAccountEntity();
|
||||
account.setId(SnowFlakeUtil.getLongId());
|
||||
account.setName(scenicUpdateReq.getName() + "管理员");
|
||||
account.setAccount(scenicUpdateReq.getAccount());
|
||||
account.setPassword(scenicUpdateReq.getPassword());
|
||||
account.setIsSuper(1);
|
||||
scenicAccountMapper.add(account);
|
||||
}
|
||||
scenicAccountMapper.addAccountScenicRelation(account.getId(), scenicUpdateReq.getId(), 1);
|
||||
}
|
||||
int i = scenicMapper.update(scenicUpdateReq);
|
||||
if (i > 0) {
|
||||
scenicRepository.clearCache(scenicUpdateReq.getId());
|
||||
scenicFaceBodyAdapterMap.remove(scenicUpdateReq.getId());
|
||||
scenicStorageAdapterMap.remove(scenicUpdateReq.getId());
|
||||
scenicTmpStorageAdapterMap.remove(scenicUpdateReq.getId());
|
||||
scenicLocalStorageAdapterMap.remove(scenicUpdateReq.getId());
|
||||
scenicPayAdapterMap.remove(scenicUpdateReq.getId());
|
||||
return ApiResponse.success(true);
|
||||
}else {
|
||||
return ApiResponse.fail("景区修改失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<Boolean> updateStatus(Long id) {
|
||||
int i = scenicMapper.updateStatus(id);
|
||||
if (i > 0) {
|
||||
scenicRepository.clearCache(id);
|
||||
scenicFaceBodyAdapterMap.remove(id);
|
||||
scenicStorageAdapterMap.remove(id);
|
||||
scenicTmpStorageAdapterMap.remove(id);
|
||||
scenicLocalStorageAdapterMap.remove(id);
|
||||
scenicPayAdapterMap.remove(id);
|
||||
return ApiResponse.success(true);
|
||||
}else {
|
||||
return ApiResponse.fail("景区状态修改失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<Boolean> addConfig(ScenicConfigEntity scenicConfig) {
|
||||
if (scenicConfig.getId() == null) {
|
||||
scenicConfig.setId(SnowFlakeUtil.getLongId());
|
||||
}
|
||||
int i = scenicMapper.addConfig(scenicConfig);
|
||||
if (i > 0) {
|
||||
scenicRepository.clearCache(scenicConfig.getScenicId());
|
||||
return ApiResponse.success(true);
|
||||
}else {
|
||||
return ApiResponse.fail("景区配置添加失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiResponse<Boolean> updateConfigById(ScenicConfigEntity scenicConfig) {
|
||||
int i = scenicMapper.updateConfigById(scenicConfig);
|
||||
if (i > 0) {
|
||||
scenicRepository.clearCache(scenicConfig.getScenicId());
|
||||
scenicFaceBodyAdapterMap.remove(scenicConfig.getScenicId());
|
||||
scenicStorageAdapterMap.remove(scenicConfig.getScenicId());
|
||||
scenicTmpStorageAdapterMap.remove(scenicConfig.getScenicId());
|
||||
scenicLocalStorageAdapterMap.remove(scenicConfig.getScenicId());
|
||||
scenicPayAdapterMap.remove(scenicConfig.getScenicId());
|
||||
return ApiResponse.success(true);
|
||||
}else {
|
||||
return ApiResponse.fail("景区配置修改失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScenicConfigEntity getConfig(Long id) {
|
||||
return scenicRepository.getScenicConfig(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveConfig(Long configId, ScenicConfigEntity config) {
|
||||
config.setId(configId);
|
||||
if (config.getScenicId() == null) {
|
||||
throw new RuntimeException("景区ID不能为空");
|
||||
}
|
||||
scenicMapper.updateConfigById(config);
|
||||
scenicRepository.clearCache(config.getScenicId());
|
||||
scenicFaceBodyAdapterMap.remove(config.getScenicId());
|
||||
scenicStorageAdapterMap.remove(config.getScenicId());
|
||||
scenicTmpStorageAdapterMap.remove(config.getScenicId());
|
||||
scenicLocalStorageAdapterMap.remove(config.getScenicId());
|
||||
scenicPayAdapterMap.remove(config.getScenicId());
|
||||
}
|
||||
|
||||
|
||||
private static final Map<Long, IStorageAdapter> scenicStorageAdapterMap = new ConcurrentHashMap<>();
|
||||
@Override
|
||||
public IStorageAdapter getScenicStorageAdapter(Long scenicId) {
|
||||
|
Reference in New Issue
Block a user