Authored by mali

价格走势,每个维度都多增加前一个节点给APP

package com.yohoufo.product.cache;
import java.util.concurrent.TimeUnit;
/**
* Created by li.ma on 2019/3/6.
*/
public enum UfoProductCacheKeyEnum {
SELLER_LIMIT_KEY("ufo:product:limitInfo:", "出售限制", 5, TimeUnit.MINUTES);
private String cacheKey;
private String desc;
private int defaultExpireTime;
private TimeUnit timeUnit; // 缓存时间单位
UfoProductCacheKeyEnum(String cacheKey, String desc, int defaultExpireTime, TimeUnit timeUnit) {
this.cacheKey = cacheKey;
this.desc = desc;
this.defaultExpireTime = defaultExpireTime;
this.timeUnit = timeUnit;
}
public int getDefaultExpireTime() {
return defaultExpireTime;
}
public String getCacheKey() {
return cacheKey;
}
public TimeUnit getTimeUnit() {
return timeUnit;
}
public long getExpireSecondTime() {
return this.timeUnit.toSeconds(defaultExpireTime);
}
}
... ...
package com.yohoufo.product.cache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.cache.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
/**
* Created by li.ma on 2019/3/6.
*/
@Service
public class UfoProductCacheService {
@Autowired
private CacheClient cacheClient;
/**
* 单个设置字符串缓存
* @param cacheKeyEnum 缓存的枚举类
* @param value 缓存的值
* @param keyParams 缓存key可变的参数列表 可空
*/
public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime());
}
/**
* 批量设置字符串缓存 仅适合同一场场景的缓存批量设置
* @param cacheKeyEnum 缓存的枚举类
* @param value 缓存的值
* @param keyParams 缓存key可变的参数列表 可空
*/
public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
Map<RedisKeyBuilder, Object> map = null;
cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime());
}
/**
* 获取缓存
* @param cacheKeyEnum 缓存的枚举类
* @param clazz 缓存的值对象的类型
* @param keyParams 缓存key可变的参数列表 可空
* @param <T>
* @return
*/
public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, String... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
return cacheClient.get(redisKeyBuilder, clazz);
}
/**
* 删除缓存
* @param cacheKeyEnum 缓存的枚举类
* @param keyParams 缓存key可变的参数列表 可空
*/
public void deleteCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, String... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
cacheClient.delete(redisKeyBuilder.getKey());
}
private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, String[] keyParams) {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams);
return redisKeyBuilder;
}
}
... ...
... ... @@ -22,13 +22,14 @@ public class PriceTrendDayService implements PriceTrendServiceInf{
@Autowired
private PriceTrendDayMapper priceTrendDayMapper;
// 两个小时颗粒度
public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) {
if (null != productId) {
int currentSecond = DateUtil.getCurrentTimeSecond();
LOG.info("method priceTrendDayMapper.selectByProductId in, productId:{},sizeId:{},currentSecond:{}", productId, sizeId, currentSecond);
List<PriceTrendModel> priceTrendModels = priceTrendDayMapper.selectByProductId(productId, sizeId, currentSecond - 86400, currentSecond);
List<PriceTrendModel> priceTrendModels = priceTrendDayMapper.selectByProductId(productId, sizeId, currentSecond - 86400 - 7200, currentSecond);
priceTrendModels.stream().forEach(item -> {
item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime());
... ...
... ... @@ -30,7 +30,7 @@ public class PriceTrendHalfYearService implements PriceTrendServiceInf{
// 180天(最小颗粒度:3天);
public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) {
if (null != productId) {
int timeSecondOfHalfYear = DateUtil.getTimeSecondOfDay(-179);
int timeSecondOfHalfYear = DateUtil.getTimeSecondOfDay(-182); // 多放一个节点给APP,APP首节点好展示
List<PriceTrendModel> priceTrendModels = priceTrendHalfYearMapper.selectByProductId(productId, sizeId, timeSecondOfHalfYear, DateUtil.getCurrentTimeSecond());
//List<PriceTrendModel> completeResult = compeleteZeroPrice(priceTrendModels, timeSecondOfHalfYear, productId, sizeId);
... ...
... ... @@ -19,9 +19,11 @@ public class PriceTrendMonthService implements PriceTrendServiceInf{
@Autowired
private PriceTrendMonthMapper priceTrendMonthMapper;
// 12小时一个颗粒
public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) {
if (null != productId) {
List<PriceTrendModel> priceTrendModels = priceTrendMonthMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-29), DateUtil.getCurrentTimeSecond());
// 多一个节点给APP,好展示
List<PriceTrendModel> priceTrendModels = priceTrendMonthMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-29) - 43200, DateUtil.getCurrentTimeSecond());
priceTrendModels.stream().forEach(item -> {
item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime());
... ...
... ... @@ -17,9 +17,11 @@ public class PriceTrendSixtyDayService implements PriceTrendServiceInf{
@Autowired
private PriceTrendSixtyDayMapper priceTrendSixtyDayMapper;
// 颗粒度一天一个节点
public List<PriceTrendModel> queryProductPriceTrend(Integer productId, Integer sizeId) {
if (null != productId) {
List<PriceTrendModel> priceTrendModels = priceTrendSixtyDayMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-59), DateUtil.getCurrentTimeSecond());
// 多放一个节点给APP,APP首节点好展示
List<PriceTrendModel> priceTrendModels = priceTrendSixtyDayMapper.selectByProductId(productId, sizeId, DateUtil.getTimeSecondOfDay(-60), DateUtil.getCurrentTimeSecond());
priceTrendModels.stream().forEach(item -> {
item.setEndTime(null == item.getCreateTime() ? null : item.getCreateTime());
item.setStartTime(null == item.getCreateTime() ? null : item.getCreateTime() - 86400);
... ...