|
@@ -23,6 +23,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -83,17 +84,17 @@ public class IndexController {
|
|
|
|
|
|
logger.info("用户:{}首页概览数据计算中---ing",userId);
|
|
|
// 所有公众号昨日数据
|
|
|
- int totalNum = ydStatData.stream().mapToInt(AccountFansStat::getTotalFansNum).sum();
|
|
|
- int addNum = ydStatData.stream().mapToInt(AccountFansStat::getAddNum).sum();
|
|
|
- int cancelNum = ydStatData.stream().mapToInt(AccountFansStat::getCancelNum).sum();
|
|
|
- int newNum = ydStatData.stream().mapToInt(AccountFansStat::getNewNum).sum();
|
|
|
- int inactiveNum = ydStatData.stream().mapToInt(AccountFansStat::getInactiveNum).sum();
|
|
|
+ long totalNum = ydStatData.stream().mapToLong(AccountFansStat::getTotalFansNum).sum();
|
|
|
+ long addNum = ydStatData.stream().mapToLong(AccountFansStat::getAddNum).sum();
|
|
|
+ long cancelNum = ydStatData.stream().mapToLong(AccountFansStat::getCancelNum).sum();
|
|
|
+ long newNum = ydStatData.stream().mapToLong(AccountFansStat::getNewNum).sum();
|
|
|
+ long inactiveNum = ydStatData.stream().mapToLong(AccountFansStat::getInactiveNum).sum();
|
|
|
// 所有公众号前日数据
|
|
|
- int bfCancelNum = beforeYdStat.stream().mapToInt(AccountFansStat::getCancelNum).sum();
|
|
|
- int bfAddNum = beforeYdStat.stream().mapToInt(AccountFansStat::getAddNum).sum();
|
|
|
- int bfNewNum = beforeYdStat.stream().mapToInt(AccountFansStat::getNewNum).sum();
|
|
|
- int bfInactiveNum = beforeYdStat.stream().mapToInt(AccountFansStat::getInactiveNum).sum();
|
|
|
- int bfTotalNum = beforeYdStat.stream().mapToInt(AccountFansStat::getTotalFansNum).sum();
|
|
|
+ long bfCancelNum = beforeYdStat.stream().mapToLong(AccountFansStat::getCancelNum).sum();
|
|
|
+ long bfAddNum = beforeYdStat.stream().mapToLong(AccountFansStat::getAddNum).sum();
|
|
|
+ long bfNewNum = beforeYdStat.stream().mapToLong(AccountFansStat::getNewNum).sum();
|
|
|
+ long bfInactiveNum = beforeYdStat.stream().mapToLong(AccountFansStat::getInactiveNum).sum();
|
|
|
+ long bfTotalNum = beforeYdStat.stream().mapToLong(AccountFansStat::getTotalFansNum).sum();
|
|
|
|
|
|
AccountFansStat result = new AccountFansStat();
|
|
|
result.setAddNum(addNum);
|
|
@@ -131,11 +132,39 @@ public class IndexController {
|
|
|
@RequestParam(required = false) String startDate,
|
|
|
@RequestParam(required = false) String endDate) {
|
|
|
String userId = JwtTokenUtil.getUserId(token);
|
|
|
- Map<String,Object> result = new HashMap<>(2);
|
|
|
- logger.info("用户:{}查询首页单个公众号:{}粉丝增长---start",userId,accountId);
|
|
|
- List<StatData> statData = queryFansGrowthStatData(accountId, type, startDate, endDate);
|
|
|
- result.put("tableData",statData);
|
|
|
- logger.info("用户:{}查询首页单个公众号:{}粉丝增长---end",userId,accountId);
|
|
|
+ if (!checkDate(startDate,endDate)){
|
|
|
+ throw new CustomException(500,"请选择开始和结束时间!");
|
|
|
+ }
|
|
|
+ Map<String,Object> result = new HashMap<>(8);
|
|
|
+ logger.info("用户:{}查询首页单个公众号:{}粉丝增长,type:{},startDate:{},endDate:{} --->start",
|
|
|
+ userId,accountId,type,startDate,endDate);
|
|
|
+ LocalDate start = LocalDate.parse(startDate);
|
|
|
+ LocalDate end= LocalDate.parse(endDate);
|
|
|
+ long days = end.toEpochDay() - start.toEpochDay();
|
|
|
+ List<StatData> statDataList = queryFansGrowthStatData(accountId, type, start, end);
|
|
|
+ long newNum =0;
|
|
|
+ long addNum =0;
|
|
|
+ long inactiveNum =0;
|
|
|
+ long cancelNum =0;
|
|
|
+ BigDecimal ave =BigDecimal.ZERO;
|
|
|
+ if (CollectionUtils.isEmpty(statDataList)) {
|
|
|
+ newNum = statDataList.parallelStream().mapToLong(StatData::getNewNum).sum();
|
|
|
+ addNum = statDataList.parallelStream().mapToLong(StatData::getAddNum).sum();
|
|
|
+ inactiveNum = statDataList.parallelStream().mapToLong(StatData::getInactiveNum).sum();
|
|
|
+ cancelNum = statDataList.parallelStream().mapToLong(StatData::getCancelNum).sum();
|
|
|
+ if (days == 0){
|
|
|
+ days = 1;
|
|
|
+ }
|
|
|
+ ave = new BigDecimal(newNum/days).setScale(0);
|
|
|
+ }
|
|
|
+ result.put("newNum",newNum);
|
|
|
+ result.put("addNum",addNum);
|
|
|
+ result.put("inactiveNum",inactiveNum);
|
|
|
+ result.put("cancelNum",cancelNum);
|
|
|
+ result.put("aveNum",ave);
|
|
|
+ result.put("tableData",statDataList);
|
|
|
+ logger.info("用户:{}查询首页单个公众号:{}粉丝增长,type:{},startDate:{},endDate:{} --->end",
|
|
|
+ userId,accountId,type,startDate,endDate);
|
|
|
return ResponseEntity.ok(JsonResult.success(result));
|
|
|
}
|
|
|
|
|
@@ -149,42 +178,45 @@ public class IndexController {
|
|
|
* @param endDate
|
|
|
* @return java.util.List<com.idiot.operationbackend.vo.StatData>
|
|
|
*/
|
|
|
- private List<StatData> queryFansGrowthStatData(String accountId, String type, String startDate, String endDate) {
|
|
|
+ private List<StatData> queryFansGrowthStatData(String accountId, String type, LocalDate startDate, LocalDate endDate) {
|
|
|
|
|
|
- int totalNum = fansService.countFansByAccountId(accountId);
|
|
|
- if (HOUR.equals(type)) {
|
|
|
- // 小时数据 需要在粉丝 动作中统计
|
|
|
- if (!checkDate(startDate,endDate)){
|
|
|
- throw new CustomException(500,"请选择开始和结束时间");
|
|
|
- }
|
|
|
|
|
|
- long start = LocalDate.parse(startDate).atTime(LocalTime.of(0,0,0))
|
|
|
- .toInstant(Constants.DEFAULT_ZONE).getEpochSecond();
|
|
|
- long end = LocalDate.parse(endDate).atTime(LocalTime.of(23,59,59))
|
|
|
- .toInstant(Constants.DEFAULT_ZONE).getEpochSecond();
|
|
|
- // 查询时间段内的值
|
|
|
- List<FansActionStat> fansActionStats = actionStatService.queryFansActionStat(accountId,start,end);
|
|
|
- if (CollectionUtils.isEmpty(fansActionStats)) {
|
|
|
- throw new CustomException(500,"暂无数据");
|
|
|
- }
|
|
|
- List<StatData> statDataList = new ArrayList<>();
|
|
|
- StatData statData = null;
|
|
|
- List<FansActionStat> tempStatList = null;
|
|
|
- while (start<=end){
|
|
|
- long finalStart = start;
|
|
|
- // 过滤两小时内
|
|
|
- tempStatList = fansActionStats.stream()
|
|
|
- .filter(e->e.getCreateTime() >= finalStart && e.getCreateTime()<= finalStart +7200)
|
|
|
- .sorted(Comparator.comparingLong(FansActionStat::getCreateTime))
|
|
|
- .collect(Collectors.toList());
|
|
|
- statData = countStatData(tempStatList,finalStart,finalStart+7200);
|
|
|
- statDataList.add(statData);
|
|
|
- start += 7200;
|
|
|
- }
|
|
|
- return statDataList;
|
|
|
+ long disValue = 0;
|
|
|
+ if (HOUR.equals(type)) {
|
|
|
+ disValue = 7200;
|
|
|
+ }else if (DAY.equals(type)){
|
|
|
+ disValue = 86400;
|
|
|
+ }else if (WEEK.equals(type)){
|
|
|
+ disValue = 604800;
|
|
|
+ }else if (MONTH.equals(type)){
|
|
|
+ // 默认 30 天
|
|
|
+ disValue = 2592000;
|
|
|
+ }else {
|
|
|
+ throw new CustomException(500,"请你选择正确的时间统计类型!");
|
|
|
}
|
|
|
- return null;
|
|
|
-
|
|
|
+ LocalDateTime startLocalDate = startDate.atTime(LocalTime.of(0,0,0));
|
|
|
+ LocalDateTime endLocalDate = endDate.plusDays(1).atTime(LocalTime.of(0,0,0));
|
|
|
+ List<FansActionStat> fansActionStats = actionStatService.queryFansActionStat(accountId,startLocalDate,endLocalDate);
|
|
|
+ if (CollectionUtils.isEmpty(fansActionStats)) {
|
|
|
+ throw new CustomException(500,"暂无数据!");
|
|
|
+ }
|
|
|
+ long start = startLocalDate.toEpochSecond(Constants.DEFAULT_ZONE);
|
|
|
+ long end = endLocalDate.toEpochSecond(Constants.DEFAULT_ZONE);
|
|
|
+ List<StatData> statDataList = new ArrayList<>();
|
|
|
+ StatData statData = null;
|
|
|
+ List<FansActionStat> tempStatList = null;
|
|
|
+ while (start<=end){
|
|
|
+ long finalStart = start;
|
|
|
+ long finalDisValue = disValue;
|
|
|
+ tempStatList = fansActionStats.stream()
|
|
|
+ .filter(e->e.getCreateTime() >= finalStart && e.getCreateTime()<= finalStart + finalDisValue)
|
|
|
+ .sorted(Comparator.comparingLong(FansActionStat::getCreateTime))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ statData = countStatData(tempStatList,finalStart,finalStart+finalDisValue,type);
|
|
|
+ statDataList.add(statData);
|
|
|
+ start += finalDisValue;
|
|
|
+ }
|
|
|
+ return statDataList;
|
|
|
}
|
|
|
|
|
|
private boolean checkDate (String start, String end) {
|
|
@@ -192,42 +224,40 @@ public class IndexController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ * 统计
|
|
|
* @author wangxiao
|
|
|
- * @date 16:03 2020/9/16
|
|
|
+ * @date 16:45 2020/9/16
|
|
|
* @param fansActionStats
|
|
|
* @param start
|
|
|
* @param end
|
|
|
+ * @param type
|
|
|
* @return com.idiot.operationbackend.vo.StatData
|
|
|
*/
|
|
|
- private StatData countStatData (List<FansActionStat> fansActionStats,long start,long end) {
|
|
|
+ private StatData countStatData (List<FansActionStat> fansActionStats,long start,long end,String type) {
|
|
|
|
|
|
- LocalDateTime startTime =LocalDateTime.ofInstant(Instant.ofEpochSecond(start),Constants.DEFAULT_ZONE);
|
|
|
- LocalDateTime endTime =LocalDateTime.ofInstant(Instant.ofEpochSecond(end),Constants.DEFAULT_ZONE);
|
|
|
+ LocalDateTime startDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(start),Constants.DEFAULT_ZONE);
|
|
|
+ LocalDateTime endDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(end),Constants.DEFAULT_ZONE);
|
|
|
StatData statData = new StatData();
|
|
|
- int newNum = 0;
|
|
|
- int cancelNum = 0;
|
|
|
- int inactiveNum = 0;
|
|
|
+ long newNum = 0;
|
|
|
+ long cancelNum = 0;
|
|
|
+ long inactiveNum = 0;
|
|
|
if (!CollectionUtils.isEmpty(fansActionStats)) {
|
|
|
- int size = fansActionStats.size();
|
|
|
- FansActionStat temp = null;
|
|
|
- for (int i = 0; i < size; i++) {
|
|
|
- temp = fansActionStats.get(i);
|
|
|
- switch (temp.getAction()){
|
|
|
- case Constants.NEW:
|
|
|
- newNum += 1;
|
|
|
- break;
|
|
|
- case Constants.CANCEL:
|
|
|
- cancelNum += 1;
|
|
|
- break;
|
|
|
- default:
|
|
|
- inactiveNum += 1;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ newNum = fansActionStats.parallelStream().filter(e->Constants.NEW == e.getAction()).count();
|
|
|
+ cancelNum = fansActionStats.parallelStream().filter(e->Constants.CANCEL == e.getAction()).count();
|
|
|
+ inactiveNum = fansActionStats.parallelStream().filter(e->Constants.NEW != e.getAction() && Constants.CANCEL != e.getAction()).count();
|
|
|
+ }
|
|
|
+ String label = "";
|
|
|
+
|
|
|
+ if (HOUR.equals(type)) {
|
|
|
+ label = String.format("%s %s:%s-%s:%s",startDateTime.toLocalDate(),startDateTime.getHour(),"00",endDateTime.getHour(),"00");
|
|
|
+ }else if (DAY.equals(type)){
|
|
|
+ label = String.format("%s",startDateTime.toLocalDate());
|
|
|
+ }else if (WEEK.equals(type)){
|
|
|
+ label = String.format("%s/%s-%s/%s",startDateTime.getMonthValue(),startDateTime.getDayOfMonth()
|
|
|
+ ,endDateTime.getMonthValue(),endDateTime.getDayOfMonth());
|
|
|
+ }else if (MONTH.equals(type)){
|
|
|
+ label = String.format("%s-%s",startDateTime.getYear(),startDateTime.getMonthValue());
|
|
|
}
|
|
|
- String label = String.format("%s %s:%s-%s:%s",
|
|
|
- startTime.toLocalDate(),startTime.getHour(),"00",endTime.getHour(),"00");
|
|
|
statData.setDateLabel(label);
|
|
|
statData.setAddNum(newNum-cancelNum);
|
|
|
statData.setCancelNum(cancelNum);
|
|
@@ -239,4 +269,5 @@ public class IndexController {
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
}
|