Commit bb6734c5 by shenjunjie

首页模块完成

parent ea21057a
......@@ -17,6 +17,24 @@ public class RedisKeyPrefix {
private static final String SEPARATOR = ":";
/**
* 首页相关缓存KEY
*/
public static final String INDEX_YUQING = "BRANDKBS:INDEX:YUQING:";
public static final String INDEX_REPUTATION = "BRANDKBS:INDEX:REPUTATION:";
public static final String INDEX_EVENT = "BRANDKBS:INDEX:EVENT:";
public static final String INDEX_PLATFORM = "BRANDKBS:INDEX:PLATFORM:";
public static final String INDEX_SPREAD = "BRANDKBS:INDEX:SPREAD:";
public static final String INDEX_COMPARE_SUMMARY_MOBILE = "BRANDKBS:INDEX:COMPARE_SUMMARY:MOBILE:";
/**
* 竞品库-获取竞品对比舆情解读数据(PC
*/
public static final String INDEX_COMPARE_SUMMARY_PC = "BRANDKBS:CONTEND:COMPARE_SUMMARY:PC:";
/**
* 竞品库-获取竞品对比传播分析数据
*/
public static final String INDEX_COMPARE_ANALYZE = "BRANDKBS:CONTEND:COMPARE_ANALYZE:";
public static final String MARK_ANALYZE_SUMMARY = "BRANDKBS:MARK:ANALYZE_SUMMARY:";
public static final String MARK_PLATFORM_PROPORTION = "BRANDKBS:MARK:PLATFORM_PROPORTION:";
public static final String MARK_SPREAD = "BRANDKBS:MARK:SPREAD:";
......@@ -60,16 +78,6 @@ public class RedisKeyPrefix {
*/
private static final String EVENT_ANALYZE_PROGRESS = "BRANDKBS:EVENT:ANALYZE:PROGRESS:";
/**
* 竞品库-获取竞品对比舆情解读数据(PC
*/
public static final String INDEX_COMPARE_SUMMARY_PC = "BRANDKBS:CONTEND:COMPARESUMMARY:PC:";
/**
* 竞品库-获取竞品对比传播分析数据
*/
public static final String INDEX_COMPARE_ANALYZE = "BRANDKBS:CONTEND:COMPAREANALYZE:";
public static String eventAnalysisProgress(String eventId, String projectId) {
return RedisKeyPrefix.generateRedisKey(RedisKeyPrefix.EVENT_ANALYZE_PROGRESS, projectId, eventId);
}
......
......@@ -19,6 +19,7 @@ public class Constant {
public static final String HOUR_PATTERN = "yyyy-MM-dd HH";
public static final String DAY_PATTERN = "yyyy-MM-dd";
public static final String MONTH_PATTERN = "yyyy-MM";
public static final String YEAR_PATTERN = "yyyy";
public static final FastDateFormat HOUR_FORMAT = FastDateFormat.getInstance(HOUR_PATTERN);
public static final FastDateFormat DAY_FORMAT = FastDateFormat.getInstance(DAY_PATTERN);
......@@ -56,5 +57,5 @@ public class Constant {
/**
* 主品牌默认竞品ID
*/
public static final String PRIMARY_CONTENDID = "0";
public static final String PRIMARY_CONTEND_ID = "0";
}
......@@ -125,14 +125,14 @@ public class AppHotController extends BaseController {
Date startDate = DateUtils.addHours(endDate, -24);
List<Map.Entry<String, Integer>> markTopTitleList =
markDataService.getMarkTopTitle(startDate.getTime(), endDate.getTime(), null, projectId, linkedGroupId, Constant.PRIMARY_CONTENDID, size);
markDataService.getMarkTopTitle(startDate.getTime(), endDate.getTime(), null, projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID, size);
List<JSONObject> resultList = markTopTitleList.stream().map(map -> {
JSONObject resultJsonObject = new JSONObject();
resultJsonObject.put("title", map.getKey());
resultJsonObject.put("num", map.getValue());
try {
BaseMap firstArticle = markDataService.getFirstArticle(startDate.getTime(), endDate.getTime(), map.getKey(), projectId, linkedGroupId, Constant.PRIMARY_CONTENDID);
BaseMap firstArticle = markDataService.getFirstArticle(startDate.getTime(), endDate.getTime(), map.getKey(), projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID);
resultJsonObject.put("content", firstArticle.getContent());
resultJsonObject.put("url", firstArticle.getUrl());
resultJsonObject.put("realSource", firstArticle.getRealSource());
......
package com.zhiwei.brandkbs2.controller.app;
import com.zhiwei.brandkbs2.auth.Auth;
import com.zhiwei.brandkbs2.controller.BaseController;
import com.zhiwei.brandkbs2.enmus.RoleEnum;
import com.zhiwei.brandkbs2.model.ResponseResult;
import com.zhiwei.brandkbs2.service.IndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.annotation.Resource;
/**
* @author lxj
* @version 1.0
* @description 前台首页展示接口
* @date 2019/9/11 14:38
*/
@RestController
@RequestMapping("/app/index")
@Api(tags = "前台首页展示接口", description = "提供前台首页相关信息展示")
@Auth(role = RoleEnum.CUSTOMER)
public class AppIndexController extends BaseController {
@Resource
private IndexService indexService;
@ApiOperation("首页-舆情总量")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "long")
})
@GetMapping("/yuqing-amount")
public ResponseResult getYuqingAmount(@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
return ResponseResult.success(indexService.getYuqingAmount(startTime, endTime, true));
}
@ApiOperation("首页-美誉度")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "long")
})
@GetMapping("/reputation")
public ResponseResult getReputation(@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
return ResponseResult.success(indexService.getReputation(startTime, endTime, true));
}
@ApiOperation("首页-事件数")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "long")
})
@GetMapping("/event-amount")
public ResponseResult getEvent(@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
return ResponseResult.success(indexService.getEventAmount(startTime, endTime, true));
}
@ApiOperation("首页-平台贡献信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "long")
})
@GetMapping("/platform-info")
public ResponseResult getPlatformInfo(@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
return ResponseResult.success(indexService.getPlatformInfo(startTime, endTime, true));
}
@ApiOperation("传播趋势")
@ApiImplicitParams({
@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "long"),
@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "long")
})
@GetMapping("/spreadingTend")
public ResponseResult getSpreadingTend(@RequestParam(value = "startTime", required = false) Long startTime,
@RequestParam(value = "endTime", required = false) Long endTime) {
return ResponseResult.success(indexService.getSpreadingTend(startTime, endTime, true));
}
}
......@@ -27,9 +27,9 @@ public interface EventDao extends BaseMongoDao<Event> {
/**
* 根据联合id查询事件
*
* @param yqEventId 舆情事件id
* @param projectId 项目id
* @param contendId 竞品id
* @param yqEventId 舆情事件id
* @param projectId 项目id
* @param contendId 竞品id
* @return 事件
*/
Event getEventByUniqueIds(String yqEventId, String projectId, String contendId);
......@@ -63,11 +63,35 @@ public interface EventDao extends BaseMongoDao<Event> {
/**
* 获取参与的反常事件数
*
* @param channelIndex 渠道标识
* @param eventEmotions 事件情感倾向
* @param channelIndex 渠道标识
* @param eventEmotions 事件情感倾向
* @param articleEmotion 文章情感倾向
* @return 参与的反常事件数
*/
long getEventCount(ChannelIndex channelIndex, List<String> eventEmotions, String articleEmotion);
/**
* 根据特征值获取事件数
*
* @param startTime
* @param endTime
* @param emotion
* @param projectId
* @param contendId
* @return
*/
long getEventCountByProjectIdAndContendId(Long startTime, Long endTime, String emotion, String projectId, String contendId);
/**
* 根据特征值获取事件
*
* @param startTime
* @param endTime
* @param emotion
* @param projectId
* @param contendId
* @return
*/
List<Event> getEventsByProjectIdAndContendId(Long startTime, Long endTime, String emotion, String projectId, String contendId, int limit);
}
......@@ -3,9 +3,11 @@ package com.zhiwei.brandkbs2.dao.impl;
import com.alibaba.fastjson.JSONObject;
import com.zhiwei.brandkbs2.config.Constant;
import com.zhiwei.brandkbs2.dao.EventDao;
import com.zhiwei.brandkbs2.enmus.EmotionEnum;
import com.zhiwei.brandkbs2.pojo.ChannelIndex;
import com.zhiwei.brandkbs2.pojo.Event;
import com.zhiwei.brandkbs2.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
......@@ -87,8 +89,41 @@ public class EventDaoImpl extends BaseMongoDaoImpl<Event> implements EventDao {
return res;
}
@Override
public long getEventCountByProjectIdAndContendId(Long startTime, Long endTime, String emotion, String projectId, String contendId) {
return mongoTemplate.count(Query.query(eventCountCriteria(startTime, endTime, emotion, projectId, contendId)), COLLECTION_NAME);
}
@Override
public List<Event> getEventsByProjectIdAndContendId(Long startTime, Long endTime, String emotion, String projectId, String contendId, int limit) {
Query query = new Query();
Criteria criteria = eventCountCriteria(startTime, endTime, emotion, projectId, contendId);
query.addCriteria(criteria);
query.limit(limit);
// 按影响力排序
addSort(query, "{\"influence\":\"descend\"}");
return mongoTemplate.find(query, clazz, COLLECTION_NAME);
}
private Criteria eventCountCriteria(Long startTime, Long endTime, String emotion, String projectId, String contendId) {
Criteria criteria = Criteria.where("projectId").is(projectId);
criteria.and("contendId").is(contendId);
Criteria startTimeCriteria = criteria.and("startTime");
if (null != startTime) {
startTimeCriteria.gte(startTime);
}
if (null != endTime) {
startTimeCriteria.lt(endTime);
}
if (StringUtils.isNotEmpty(emotion) && !EmotionEnum.ALL.getName().equals(emotion)) {
criteria.and("emotion").is(emotion);
}
return criteria;
}
/**
* mongo版本不支持toString/toObjectId 表达式
* 已被getEventCount 代替
*
* @param channelIndex
* @param eventEmotions
......
package com.zhiwei.brandkbs2.service;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
/**
* @ClassName: IndexService
* @Description 首页服务
* @author: sjj
* @date: 2022-08-18 15:15
*/
public interface IndexService {
/**
* 获取舆情总量信息
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param cache 是否读取缓存
* @return 舆情总量信息
*/
JSONObject getYuqingAmount(Long startTime, Long endTime, boolean cache);
/**
* 获取品牌美誉度信息
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param cache 是否读取缓存
* @return 品牌美誉度信息
*/
JSONObject getReputation(Long startTime, Long endTime, boolean cache);
/**
* 获取品牌平台贡献度信息
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param cache 是否读取缓存
* @return 品牌平台贡献度信息
*/
JSONObject getEventAmount(Long startTime, Long endTime, boolean cache);
/**
* 获取品牌平台贡献度信息
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param cache 是否读取缓存
* @return 品牌平台贡献度信息
*/
List<JSONObject> getPlatformInfo(Long startTime, Long endTime, boolean cache);
/**
* 获取主品牌传播趋势
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param cache 是否优先读取缓存
* @return 主品牌传播趋势
*/
JSONObject getSpreadingTend(Long startTime, Long endTime, boolean cache);
}
......@@ -8,10 +8,8 @@ import com.zhiwei.brandkbs2.pojo.dto.ExportAppYuqingDTO;
import com.zhiwei.brandkbs2.pojo.dto.MarkSearchDTO;
import com.zhiwei.brandkbs2.pojo.vo.PageVO;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
......@@ -167,6 +165,7 @@ public interface MarkDataService {
/**
* 竞品库-获取搜索条件
*
* @param linkedGroupId 关联组Id
* @return 搜索条件
*/
......@@ -174,7 +173,8 @@ public interface MarkDataService {
/**
* 竞品库-获取搜索结果列表
* @param markSearchDTO 标注数据搜索传输类
*
* @param markSearchDTO 标注数据搜索传输类
* @return 搜索结果列表
*/
......@@ -191,10 +191,60 @@ public interface MarkDataService {
/**
* 竞品库-获取竞品对比传播分析数据
*
* @param markSearchDTO 标注数据搜索传输类
* @param hotArticleSize 热门稿件数量
* @param cache 是否启用缓存
* @return
*/
JSONObject getContendSpreadAnalyze(MarkSearchDTO markSearchDTO, int hotArticleSize, boolean cache) throws IOException;
/**
* 时间段内稿件数
*
* @param startTime
* @param endTime
* @param emotion
* @param projectId
* @param contendId
* @return
*/
long getYuqingMarkCount(Long startTime, Long endTime, String emotion, String projectId, String contendId) throws IOException;
/**
* 时间段内稿件数
*
* @param startTime
* @param endTime
* @param emotion
* @param platformId
* @param projectId
* @param contendId
* @return
*/
long getYuqingMarkCount(Long startTime, Long endTime, String emotion, String platformId, String projectId, String contendId) throws IOException;
/**
* 项目最早稿件时间
*
* @param projectId
* @param contendId
* @return
* @throws IOException
*/
long getYuqingMarkFirstTime(String projectId, String contendId) throws IOException;
/**
* 根据情感获取时间段发布最多稿件的渠道
*
* @param startTime 开始时间
* @param endTime 结束时间
* @param emotion 情感倾向
* @param projectId 项目ID
* @param contendId 竞品ID
* @param size 大小
* @return 发布最多稿件的渠道
*/
List<Map<String, Object>> getEsTopSource(Long startTime, Long endTime, String projectId, String linkedGroupId, String contendId, String emotion, int size) throws IOException;
}
......@@ -99,7 +99,7 @@ public class CustomEventServiceImpl implements CustomEventService {
JSONObject result = new JSONObject();
String aggTitle = map.getKey();
try {
BaseMap firstArticle = markDataService.getFirstArticle(startTime, endTime, aggTitle, projectId, linkedGroupId, Constant.PRIMARY_CONTENDID);
BaseMap firstArticle = markDataService.getFirstArticle(startTime, endTime, aggTitle, projectId, linkedGroupId, Constant.PRIMARY_CONTEND_ID);
result.put("title", firstArticle.getTitle());
result.put("time", firstArticle.getTime());
result.put("url", firstArticle.getUrl());
......@@ -271,7 +271,8 @@ public class CustomEventServiceImpl implements CustomEventService {
result.put("platformPro", this.getPlatformPro(articleList));
result.put("hotKeyword", this.getHotKeyword(articleList));
FastDateFormat df = gtDay ? Constant.DAY_FORMAT : Constant.HOUR_FORMAT;
List<Map<String, String>> dateList = gtDay ? Tools.parseToDays(customEvent.getStartTime(), customEvent.getEndTime()) : Tools.parseToHours(customEvent.getStartTime(), customEvent.getEndTime());
List<Map<String, Long>> dateList = gtDay ? Tools.parseToDays(customEvent.getStartTime(), customEvent.getEndTime()) :
Tools.parseToHours(customEvent.getStartTime(), customEvent.getEndTime());
result.put("spreadType", gtDay ? "day" : "hour");
result.put("spread", this.getSpread(dateList, df, articleList));
result.put("hotArticles", this.getHotArticles(articleList));
......@@ -458,38 +459,34 @@ public class CustomEventServiceImpl implements CustomEventService {
* @param articleList 自定义事件稿件集合
* @return 自定义事件传播趋势
*/
private List<JSONObject> getSpread(List<Map<String, String>> dateList, FastDateFormat df, List<BaseMap> articleList) {
private List<JSONObject> getSpread(List<Map<String, Long>> dateList, FastDateFormat df, List<BaseMap> articleList) {
List<JSONObject> lineList = new ArrayList<>(dateList.size());
for (Map<String, String> map : dateList) {
try {
Long startTime = df.parse(map.get("startTime")).getTime();
Long endTime = df.parse(map.get("endTime")).getTime();
long count = 0;
long posCount = 0;
long neuCount = 0;
long negCount = 0;
for (BaseMap baseMap : articleList) {
if (baseMap.getTime() >= startTime && baseMap.getTime() < endTime) {
if (EmotionEnum.POSITIVE.getName().equals(baseMap.getEmotion())) {
posCount++;
} else if (EmotionEnum.NEGATIVE.getName().equals(baseMap.getEmotion())) {
negCount++;
} else {
neuCount++;
}
count++;
for (Map<String, Long> map : dateList) {
Long startTime = map.get("startTime");
Long endTime = map.get("endTime");
long count = 0;
long posCount = 0;
long neuCount = 0;
long negCount = 0;
for (BaseMap baseMap : articleList) {
if (baseMap.getTime() >= startTime && baseMap.getTime() < endTime) {
if (EmotionEnum.POSITIVE.getName().equals(baseMap.getEmotion())) {
posCount++;
} else if (EmotionEnum.NEGATIVE.getName().equals(baseMap.getEmotion())) {
negCount++;
} else {
neuCount++;
}
count++;
}
JSONObject line = new JSONObject();
line.put("count", count);
line.put("posCount", posCount);
line.put("neuCount", neuCount);
line.put("negCount", negCount);
line.put("time", startTime);
lineList.add(line);
} catch (ParseException pe) {
log.error("时间转换错误");
}
JSONObject line = new JSONObject();
line.put("count", count);
line.put("posCount", posCount);
line.put("neuCount", neuCount);
line.put("negCount", negCount);
line.put("time", startTime);
lineList.add(line);
}
return lineList;
}
......
......@@ -645,19 +645,18 @@ public class EventServiceImpl implements EventService {
String year = Tools.DF_YEAR.format(new Date());
int yearInt = Integer.parseInt(year);
List<Integer> years = Arrays.asList(yearInt, yearInt - 1, yearInt - 2);
List<JSONObject> collect = years.stream().map(yearInfo -> {
return years.stream().map(yearInfo -> {
JSONObject result = new JSONObject();
try {
String yearStr = String.valueOf(yearInfo);
result.put("name", yearStr);
result.put("startTime", Tools.DF_YEAR.parse(yearStr));
result.put("endTime", Tools.DF_YEAR.parse(yearStr));
result.put("startTime", Tools.DF_YEAR.parse(yearStr).getTime());
result.put("endTime", Tools.DF_YEAR.parse(yearStr).getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
return result;
}).collect(Collectors.toList());
return collect;
}
/**
......
......@@ -31,6 +31,26 @@ public class RedisUtil {
return RedisKeyPrefix.AGGREE_RESULT_CACHE + projectId + ":" + id;
}
public static String getIndexYuqingKey(String projectId, Long startTime, Long endTime) {
return RedisKeyPrefix.INDEX_YUQING + Tools.concat(projectId, startTime, endTime);
}
public static String getIndexReputationKey(String projectId, Long startTime, Long endTime) {
return RedisKeyPrefix.INDEX_REPUTATION + Tools.concat(projectId, startTime, endTime);
}
public static String getIndexEventKey(String projectId, Long startTime, Long endTime) {
return RedisKeyPrefix.INDEX_EVENT + Tools.concat(projectId, startTime, endTime);
}
public static String getIndexPlatformKey(String projectId, Long startTime, Long endTime) {
return RedisKeyPrefix.INDEX_PLATFORM + Tools.concat(projectId, startTime, endTime);
}
public static String getIndexSpread(String projectId, Long startTime, Long endTime) {
return RedisKeyPrefix.INDEX_SPREAD + Tools.concat(projectId, startTime, endTime);
}
public void setExpire(String key, String value, long timeout, TimeUnit unit) {
stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
}
......
......@@ -703,8 +703,7 @@ public class Tools {
* @param endTime 结束时间
* @return 按日分割的map集合
*/
public static List<Map<String, String>> parseToDays(Long startTime, Long endTime) {
FastDateFormat df = DAY_FORMAT;
public static List<Map<String, Long>> parseToDays(Long startTime, Long endTime) {
Date start = Tools.truncDate(new Date(startTime), Constant.DAY_PATTERN);
Date end = Tools.truncDate(new Date(endTime), Constant.DAY_PATTERN);
Period periodDays = new Period(start.getTime(), end.getTime(), PeriodType.days());
......@@ -712,26 +711,77 @@ public class Tools {
if (days < 0) {
return Collections.emptyList();
}
List<Map<String, String>> dayList = new ArrayList<>(days);
List<Map<String, Long>> dayList = new ArrayList<>(days);
for (int i = 0; i <= days; i++) {
Map<String, String> dayMap = new HashMap<>(4);
Map<String, Long> dayMap = new HashMap<>(4);
//开始时间
dayMap.put("startTime", df.format(DateUtils.addDays(start, i)));
dayMap.put("startTime", DateUtils.addDays(start, i).getTime());
//结束时间
dayMap.put("endTime", df.format(DateUtils.addDays(start, i + 1)));
dayMap.put("endTime", DateUtils.addDays(start, i + 1).getTime());
dayList.add(dayMap);
}
return dayList;
}
/**
* 解析时间转换成按年的集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 按年分割的map集合
*/
public static List<Map<String, Long>> parseToYears(Long startTime, Long endTime) {
Date start = Tools.truncDate(new Date(startTime), Constant.YEAR_PATTERN);
Date end = Tools.truncDate(new Date(endTime), Constant.YEAR_PATTERN);
Period periodYears = new Period(start.getTime(), end.getTime(), PeriodType.years());
int years = periodYears.getYears();
if (years < 0) {
return Collections.emptyList();
}
List<Map<String, Long>> yearList = new ArrayList<>(years);
for (int i = 0; i <= years; i++) {
Map<String, Long> yearMap = new HashMap<>(4);
yearMap.put("startTime", DateUtils.addYears(start, i).getTime());
yearMap.put("endTime", DateUtils.addYears(start, i + 1).getTime());
yearList.add(yearMap);
}
return yearList;
}
/**
* 解析时间转换成按月的集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 按月分割的map集合
*/
public static List<Map<String, Long>> parseToMonths(Long startTime, Long endTime) {
Date start = Tools.truncDate(new Date(startTime), Constant.MONTH_PATTERN);
Date end = Tools.truncDate(new Date(endTime), Constant.MONTH_PATTERN);
Period periodMonths = new Period(start.getTime(), end.getTime(), PeriodType.months());
int months = periodMonths.getMonths();
if (months < 0) {
return Collections.emptyList();
}
List<Map<String, Long>> monthList = new ArrayList<>(months);
for (int i = 0; i <= months; i++) {
Map<String, Long> monthMap = new HashMap<>(4);
monthMap.put("startTime", DateUtils.addMonths(start, i).getTime());
monthMap.put("endTime", DateUtils.addMonths(start, i + 1).getTime());
monthList.add(monthMap);
}
return monthList;
}
/**
* 解析时间转换成按小时的集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 按小时分割的map集合
*/
public static List<Map<String, String>> parseToHours(Long startTime, Long endTime) {
public static List<Map<String, Long>> parseToHours(Long startTime, Long endTime) {
FastDateFormat df = HOUR_FORMAT;
Date start = Tools.truncDate(new Date(startTime), Constant.HOUR_PATTERN);
Date end = Tools.truncDate(new Date(endTime), Constant.HOUR_PATTERN);
......@@ -740,19 +790,50 @@ public class Tools {
if (hours < 0) {
return Collections.emptyList();
}
List<Map<String, String>> hourList = new ArrayList<>(hours);
List<Map<String, Long>> hourList = new ArrayList<>(hours);
for (int i = 0; i <= hours; i++) {
Map<String, String> hourMap = new HashMap<>(4);
Map<String, Long> hourMap = new HashMap<>(4);
//开始时间
hourMap.put("startTime", df.format(DateUtils.addHours(start, i)));
hourMap.put("startTime", DateUtils.addHours(start, i).getTime());
//结束时间
hourMap.put("endTime", df.format(DateUtils.addHours(start, i + 1)));
hourMap.put("endTime", DateUtils.addHours(start, i + 1).getTime());
hourList.add(hourMap);
}
return hourList;
}
/**
* 解析时间转换成按月基数时间的集合
*
* @param startTime 开始时间
* @param endTime 结束时间
* @return 按月基数分割时间的集合
*/
public static List<Map<String, Long>> parseToDaysWithBase(Long startLong, Long endLong) {
Date start = Tools.truncDate(new Date(startLong), Constant.DAY_PATTERN);
Date end = Tools.truncDate(new Date(endLong - 1), Constant.DAY_PATTERN);
Period periodDays = new Period(start.getTime(), end.getTime(), PeriodType.days());
Period periodMonths = new Period(start.getTime(), end.getTime(), PeriodType.months());
int base = (periodMonths.getMonths() + 1);
int days = periodDays.getDays() / (periodMonths.getMonths() + 1);
if (days < 0) {
return Collections.emptyList();
}
List<Map<String, Long>> dayList = new ArrayList<>(days);
for (int i = 0; i <= days; i++) {
//开始时间
Long startTime = DateUtils.addDays(start, base * i).getTime();
//结束时间
Long endTime = i == days ? (DateUtils.addDays(end, 1)).getTime() : DateUtils.addDays(start, base * (i + 1)).getTime();
Map<String, Long> dayMap = new HashMap<>(4);
dayMap.put("startTime", startTime);
dayMap.put("endTime", endTime);
dayList.add(dayMap);
}
return dayList;
}
/**
* 是否包含关键字
*
* @param keywords 关键词数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment