|
@@ -2,9 +2,13 @@ package com.idiot.operationbackend.controller;
|
|
|
|
|
|
import com.idiot.operationbackend.entity.Account;
|
|
import com.idiot.operationbackend.entity.Account;
|
|
import com.idiot.operationbackend.entity.AccountFansStat;
|
|
import com.idiot.operationbackend.entity.AccountFansStat;
|
|
|
|
+import com.idiot.operationbackend.entity.FansActionStat;
|
|
|
|
+import com.idiot.operationbackend.service.facade.AccountFansService;
|
|
import com.idiot.operationbackend.service.facade.AccountFansStatService;
|
|
import com.idiot.operationbackend.service.facade.AccountFansStatService;
|
|
import com.idiot.operationbackend.service.facade.AccountService;
|
|
import com.idiot.operationbackend.service.facade.AccountService;
|
|
|
|
+import com.idiot.operationbackend.service.facade.FansActionStatService;
|
|
import com.idiot.operationbackend.support.Constants;
|
|
import com.idiot.operationbackend.support.Constants;
|
|
|
|
+import com.idiot.operationbackend.support.CustomException;
|
|
import com.idiot.operationbackend.support.JsonResult;
|
|
import com.idiot.operationbackend.support.JsonResult;
|
|
import com.idiot.operationbackend.util.JwtTokenUtil;
|
|
import com.idiot.operationbackend.util.JwtTokenUtil;
|
|
import com.idiot.operationbackend.vo.StatData;
|
|
import com.idiot.operationbackend.vo.StatData;
|
|
@@ -15,12 +19,15 @@ import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
|
|
|
+import java.time.Instant;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.LocalTime;
|
|
|
|
+import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.idiot.operationbackend.support.Constants.calcRate;
|
|
import static com.idiot.operationbackend.support.Constants.calcRate;
|
|
@@ -38,12 +45,28 @@ public class IndexController {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(IndexController.class);
|
|
private final Logger logger = LoggerFactory.getLogger(IndexController.class);
|
|
|
|
|
|
|
|
+ private final String HOUR = "H";
|
|
|
|
+
|
|
|
|
+ private final String WEEK = "W";
|
|
|
|
+
|
|
|
|
+ private final String DAY = "D";
|
|
|
|
+
|
|
|
|
+ private final String MONTH = "M";
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private AccountService accountService;
|
|
private AccountService accountService;
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private AccountFansStatService fansStatService;
|
|
private AccountFansStatService fansStatService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private AccountFansService fansService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private FansActionStatService actionStatService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
@GetMapping("/summary")
|
|
@GetMapping("/summary")
|
|
@ApiOperation(value = "查询公众号昨日统计数据概览")
|
|
@ApiOperation(value = "查询公众号昨日统计数据概览")
|
|
@@ -102,13 +125,118 @@ public class IndexController {
|
|
|
|
|
|
@GetMapping("/fansGrowth/{accountId}")
|
|
@GetMapping("/fansGrowth/{accountId}")
|
|
@ApiOperation(value = "查询单个公众号--粉丝增长")
|
|
@ApiOperation(value = "查询单个公众号--粉丝增长")
|
|
- public ResponseEntity<JsonResult<List<StatData>>> getFansGrowthStat (@PathVariable String accountId,
|
|
|
|
- @RequestHeader String token,
|
|
|
|
- @RequestParam String type,
|
|
|
|
- @RequestParam(required = false) String startDate,
|
|
|
|
- @RequestParam(required = false) String endDate) {
|
|
|
|
|
|
+ public ResponseEntity<JsonResult<Map<String,Object>>> getFansGrowthStat (@PathVariable String accountId,
|
|
|
|
+ @RequestHeader String token,
|
|
|
|
+ @RequestParam String type,
|
|
|
|
+ @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);
|
|
|
|
+ return ResponseEntity.ok(JsonResult.success(result));
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 统计粉丝数据 好难受写不出完美的代码
|
|
|
|
+ * @author wangxiao
|
|
|
|
+ * @date 16:00 2020/9/16
|
|
|
|
+ * @param accountId
|
|
|
|
+ * @param type
|
|
|
|
+ * @param startDate
|
|
|
|
+ * @param endDate
|
|
|
|
+ * @return java.util.List<com.idiot.operationbackend.vo.StatData>
|
|
|
|
+ */
|
|
|
|
+ private List<StatData> queryFansGrowthStatData(String accountId, String type, String startDate, String 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;
|
|
|
|
+ }
|
|
return null;
|
|
return null;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean checkDate (String start, String end) {
|
|
|
|
+ return !(StringUtils.isEmpty(start) | StringUtils.isEmpty(end));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * @author wangxiao
|
|
|
|
+ * @date 16:03 2020/9/16
|
|
|
|
+ * @param fansActionStats
|
|
|
|
+ * @param start
|
|
|
|
+ * @param end
|
|
|
|
+ * @return com.idiot.operationbackend.vo.StatData
|
|
|
|
+ */
|
|
|
|
+ private StatData countStatData (List<FansActionStat> fansActionStats,long start,long end) {
|
|
|
|
+
|
|
|
|
+ LocalDateTime startTime =LocalDateTime.ofInstant(Instant.ofEpochSecond(start),Constants.DEFAULT_ZONE);
|
|
|
|
+ LocalDateTime endTime =LocalDateTime.ofInstant(Instant.ofEpochSecond(end),Constants.DEFAULT_ZONE);
|
|
|
|
+ StatData statData = new StatData();
|
|
|
|
+ int newNum = 0;
|
|
|
|
+ int cancelNum = 0;
|
|
|
|
+ int 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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ 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);
|
|
|
|
+ statData.setNewNum(newNum);
|
|
|
|
+ statData.setInactiveNum(inactiveNum);
|
|
|
|
+ return statData;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|