feat(foundations):完善服务项和服务详情查询缓存的功能

This commit is contained in:
JIAN 2024-08-25 17:28:47 +08:00
parent 179bc9560f
commit ba389a4b80
5 changed files with 79 additions and 21 deletions

View File

@ -6,10 +6,7 @@ import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.foundations.service.IServeTypeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@ -46,6 +43,14 @@ public class ServeController {
return serveService.getHotServeList(regionId);
}
/**
* 获取指定id的服务详情
*/
@GetMapping("/{id}")
public ServeAggregationSimpleResDTO getServeById(@PathVariable("id") Long serveId) {
return serveService.getServeById(serveId);
}
@Resource
private IServeService serveService;
@Resource

View File

@ -20,6 +20,11 @@ import java.util.List;
* @since 2023-07-03
*/
public interface IServeItemService extends IService<ServeItem> {
/**
* 查询指定id的服务项并缓存
*/
ServeItem selectByIdCache(Long id);
/**
* 服务项新增
*
@ -97,4 +102,4 @@ public interface IServeItemService extends IService<ServeItem> {
* @return 服务项目录
*/
List<ServeTypeCategoryResDTO> queryActiveServeItemCategory();
}
}

View File

@ -17,6 +17,11 @@ import java.util.List;
* @author JIAN
*/
public interface IServeService extends IService<Serve> {
/**
* 查询指定id的服务并缓存
*/
Serve selectByIdCache(Long id);
/**
* 区域服务分页查询
* @param servePageQueryReqDTO 分页参数
@ -42,7 +47,7 @@ public interface IServeService extends IService<Serve> {
/**
* 设置指定的服务下架
*/
Serve updateOffSale(Long id);
void updateOffSale(Long id);
/**
* 删除指定的服务
@ -52,12 +57,12 @@ public interface IServeService extends IService<Serve> {
/**
* 设置指定服务热门
*/
Serve updateOnHot(Long id);
void updateOnHot(Long id);
/**
* 取消指定服务热门
*/
Serve updateOffHot(Long id);
void updateOffHot(Long id);
/**
* 获取首页服务类型及服务项
@ -70,4 +75,9 @@ public interface IServeService extends IService<Serve> {
* @param regionId 地区id
*/
List<ServeAggregationSimpleResDTO> getHotServeList(Long regionId);
/**
* 获取指定id的服务
*/
ServeAggregationSimpleResDTO getServeById(Long serveId);
}

View File

@ -28,6 +28,7 @@ import com.jzo2o.foundations.service.IServeSyncService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -53,6 +54,12 @@ public class ServeItemServiceImpl extends ServiceImpl<ServeItemMapper, ServeItem
@Resource
private ServeMapper serveMapper;
@Override
@Cacheable(value = RedisConstants.CacheName.SERVE_ITEM, key = "#id", cacheManager = RedisConstants.CacheManager.ONE_DAY)
public ServeItem selectByIdCache(Long id) {
return baseMapper.selectById(id);
}
/**
* 服务项新增
*

View File

@ -11,7 +11,6 @@ import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.enums.HotStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
@ -22,8 +21,11 @@ import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
@ -41,6 +43,12 @@ import java.util.List;
*/
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
@Override
@Cacheable(value = RedisConstants.CacheName.SERVE, key = "#id", cacheManager = RedisConstants.CacheManager.ONE_DAY)
public Serve selectByIdCache(Long id) {
return baseMapper.selectById(id);
}
@Override
public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
return PageHelperUtils.selectPage(servePageQueryReqDTO,
@ -53,7 +61,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
for (ServeUpsertReqDTO serveItemDTO : serveItems) {
// 服务项校验
Long serveItemId = serveItemDTO.getServeItemId();
ServeItem serveItem = serveItemMapper.selectById(serveItemId);
ServeItem serveItem = serveItemService.getById(serveItemId);
if (ObjectUtils.isEmpty(serveItem)) {
throw new ForbiddenOperationException("服务项不存在");
}
@ -91,6 +99,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
@Override
@Transactional
@CachePut(value = RedisConstants.CacheName.SERVE, key = "#id", unless = "#result.saleStatus != 2", cacheManager = RedisConstants.CacheManager.ONE_DAY)
public Serve updatePrice(Long id, BigDecimal price) {
// 区域服务检验
Serve serve = getServeByIdIfExist(id);
@ -108,6 +117,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
@Override
@Transactional
@CachePut(value = RedisConstants.CacheName.SERVE, key = "#id", cacheManager = RedisConstants.CacheManager.ONE_DAY)
public Serve updateOnSale(Long id) {
// 区域服务检验
Serve serve = getServeByIdIfExist(id);
@ -118,7 +128,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
throw new ForbiddenOperationException("草稿或下架状态才能上架");
}
Integer activeStatus = serveItemMapper.selectById(serve.getServeItemId()).getActiveStatus();
Integer activeStatus = serveItemService.getById(serve.getServeItemId()).getActiveStatus();
if (!ObjectUtils.equal(activeStatus, FoundationStatusEnum.ENABLE.getStatus())) {
throw new ForbiddenOperationException("对应的服务项未启用不能上架");
}
@ -137,7 +147,8 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
@Override
@Transactional
public Serve updateOffSale(Long id) {
@CacheEvict(value = RedisConstants.CacheName.SERVE, key = "#id", cacheManager = RedisConstants.CacheManager.ONE_DAY)
public void updateOffSale(Long id) {
// 区域服务检验
Serve serve = getServeByIdIfExist(id);
@ -153,9 +164,6 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
.update()) {
throw new CommonException("状态更新失败");
}
serve.setSaleStatus(disableStatus);
return serve;
}
@Override
@ -173,7 +181,7 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
@Override
@Transactional
public Serve updateOnHot(Long id) {
public void updateOnHot(Long id) {
Serve serve = getServeByIdIfExist(id);
if (serve.getIsHot() != HotStatusEnum.OFF_HOT.getStatus()) {
@ -189,12 +197,11 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
}
serve.setIsHot(hotStatus);
return serve;
}
@Override
@Transactional
public Serve updateOffHot(Long id) {
public void updateOffHot(Long id) {
Serve serve = getServeByIdIfExist(id);
if (serve.getIsHot() != HotStatusEnum.ON_HOT.getStatus()) {
@ -210,7 +217,6 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
}
serve.setIsHot(hotStatus);
return serve;
}
@Override
@ -264,9 +270,34 @@ public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements
return hotServeList;
}
@Resource
private ServeItemMapper serveItemMapper;
@Override
public ServeAggregationSimpleResDTO getServeById(Long serveId) {
Serve serve = serveService.selectByIdCache(serveId);
if (ObjectUtils.isEmpty(serve)) {
return new ServeAggregationSimpleResDTO();
}
ServeItem serveItem = serveItemService.selectByIdCache(serve.getServeItemId());
if (ObjectUtils.isEmpty(serveItem)) {
return new ServeAggregationSimpleResDTO();
}
return ServeAggregationSimpleResDTO.builder()
.id(serve.getId())
.serveItemId(serveItem.getId())
.serveItemName(serveItem.getName())
.serveItemImg(serveItem.getImg())
.detailImg(serveItem.getDetailImg())
.unit(serveItem.getUnit())
.price(serve.getPrice())
.cityCode(serve.getCityCode())
.build();
}
@Resource
private IServeItemService serveItemService;
@Resource
private RegionMapper regionMapper;
@Resource
private IServeService serveService;
}