|
@@ -1,22 +1,47 @@
|
|
|
package com.ichaoj.ams.controller;
|
|
|
|
|
|
-import com.ichaoj.ams.service.IAmsAddressAccountService;
|
|
|
-import com.ichaoj.ams.service.IAmsTradeRecordService;
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelBase;
|
|
|
+import cn.hutool.poi.excel.ExcelUtil;
|
|
|
+import cn.hutool.poi.excel.ExcelWriter;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.ichaoj.ams.common.util.FileUtils;
|
|
|
+import com.ichaoj.ams.common.util.PoiUtils;
|
|
|
+import com.ichaoj.ams.entity.AmsAirdropProject;
|
|
|
+import com.ichaoj.ams.entity.AmsAirdropTask;
|
|
|
+import com.ichaoj.ams.entity.AmsExecuteRecord;
|
|
|
+import com.ichaoj.ams.entity.AmsTradeRecord;
|
|
|
+import com.ichaoj.ams.request.statistics.DailyCostRequest;
|
|
|
+import com.ichaoj.ams.response.address.CountAddressResponse;
|
|
|
+import com.ichaoj.ams.response.statistics.*;
|
|
|
+import com.ichaoj.ams.response.task.TaskProgressResponse;
|
|
|
+import com.ichaoj.ams.service.*;
|
|
|
import com.ichaoj.common.annotation.AuthResource;
|
|
|
+import com.ichaoj.common.exception.ErrorServiceException;
|
|
|
import com.ichaoj.common.model.PublicResult;
|
|
|
import com.ichaoj.common.model.PublicUserInfo;
|
|
|
import com.ichaoj.web.context.SuperWhaleContext;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author : cjwen
|
|
@@ -30,35 +55,174 @@ public class StatisticsController {
|
|
|
|
|
|
@Resource
|
|
|
private IAmsTradeRecordService tradeService;
|
|
|
+ @Resource
|
|
|
+ private IAmsAirdropProjectService projectService;
|
|
|
|
|
|
@Resource
|
|
|
private IAmsAddressAccountService accountService;
|
|
|
+ @Resource
|
|
|
+ private IAmsAirdropTaskService taskService;
|
|
|
+ @Resource
|
|
|
+ private IAmsExecuteRecordService executeRecordService;
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("address-group")
|
|
|
+ @Operation(summary = "地址统计")
|
|
|
+ @AuthResource
|
|
|
+ private PublicResult<List<CountAddressResponse>> getGroupAndWalletNum() {
|
|
|
+ List<String> list = accountService.queryGroupList(0);
|
|
|
+ List<CountAddressResponse> responseList = new ArrayList<>();
|
|
|
+ CountAddressResponse preCount = new CountAddressResponse();
|
|
|
+ preCount.setGroupName("精品号");
|
|
|
+ preCount.setAddressCount(list.size());
|
|
|
+ responseList.add(0, preCount);
|
|
|
+ String userId = SuperWhaleContext.getContext(PublicUserInfo.class).getUserId();
|
|
|
+ List<CountAddressResponse> batchList = accountService.countBatchCount(userId);
|
|
|
+ responseList.addAll(batchList);
|
|
|
+ return PublicResult.success(responseList);
|
|
|
+ }
|
|
|
|
|
|
@GetMapping("daily-cost")
|
|
|
@Operation(summary = "每日消耗gas和本金")
|
|
|
@AuthResource
|
|
|
- public PublicResult<Map<String, Object>> dailyCostStatistics() {
|
|
|
- Map<String, Object> map = new HashMap<>(5);
|
|
|
- String userId = SuperWhaleContext.getContext(PublicUserInfo.class).getUserId();
|
|
|
- List<String> batchAddresses = accountService.queryGroupList(1);
|
|
|
+ public PublicResult<List<DailyCostResponse>> dailyCostStatistics() {
|
|
|
+ List<DailyCostResponse> list = tradeService.dailyCostStatistics();
|
|
|
+ return PublicResult.success(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("predict-cost")
|
|
|
+ @Operation(summary = "统计预计投入")
|
|
|
+ @AuthResource
|
|
|
+ public PublicResult<PredictCostResponse> predictCostStatistics() {
|
|
|
+ PredictCostResponse response = projectService.predictCostStatistics();
|
|
|
+ return PublicResult.success(response);
|
|
|
+ }
|
|
|
|
|
|
+ @GetMapping("count-fee")
|
|
|
+ @Operation(summary = "费用统计")
|
|
|
+ @AuthResource
|
|
|
+ public PublicResult<Map<String, Object>> feeCostStatistics() {
|
|
|
+ Map<String, Object> map = new HashMap<>(5);
|
|
|
+ List<String> batchAddresses = accountService.queryBatchAddress(1);
|
|
|
// 从链上获取当日gas费用
|
|
|
- BigDecimal dailyGasCost = getDailyGasCost(userId);
|
|
|
+ BigDecimal dailyGasCost = getGasCost(batchAddresses);
|
|
|
+ map.put("expendGas", dailyGasCost);
|
|
|
// 从链上获取当日本金消耗量
|
|
|
-// BigDecimal dailyPrincipalCost = getDailyPrincipalCost(userId);
|
|
|
- // 总消耗 = gas费用 + 本金消耗
|
|
|
-// BigDecimal totalCost = dailyGasCost.add(dailyPrincipalCost);
|
|
|
+ BigDecimal dailyPrincipalCost = getPrincipalCost(batchAddresses);
|
|
|
+ // 本金余额
|
|
|
+ map.put("principalBalance", dailyPrincipalCost);
|
|
|
+
|
|
|
+ map.put("predictTotalCost", projectService.list().stream().mapToDouble(p -> Double.parseDouble(p.getEstimatedCost())).sum());
|
|
|
return PublicResult.success(map);
|
|
|
}
|
|
|
|
|
|
- private void getGroupAndWalletNum(Map<String, Object> map) {
|
|
|
- List<String> list = accountService.queryGroupList(0);
|
|
|
- map.put("premiumCount", list.size());
|
|
|
+ @GetMapping("task-progress")
|
|
|
+ @Operation(summary = "统计任务进度")
|
|
|
+ @AuthResource
|
|
|
+ public PublicResult<List<TaskProgressResponse>> countTaskProgress() {
|
|
|
+ List<TaskProgressResponse> responseList = new ArrayList<>();
|
|
|
+ List<AmsAirdropTask> list = taskService.list();
|
|
|
+ for (AmsAirdropTask task : list) {
|
|
|
+ TaskProgressResponse response = new TaskProgressResponse();
|
|
|
+ Integer planTimes = task.getPlanTimes();
|
|
|
+ response.setTotalCount(planTimes);
|
|
|
+ long count = executeRecordService.count(
|
|
|
+ new LambdaQueryWrapper<AmsExecuteRecord>()
|
|
|
+ .eq(AmsExecuteRecord::getTaskId, task.getAmsTaskId())
|
|
|
+ .eq(AmsExecuteRecord::getExecuteStatus, 1)
|
|
|
+ );
|
|
|
+ response.setFinishCount((int) count);
|
|
|
+ response.setTaskName(task.getTaskName());
|
|
|
+ responseList.add(response);
|
|
|
+ }
|
|
|
+ return PublicResult.success(responseList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @GetMapping("/export")
|
|
|
+ @Operation(summary = "导出项目报表")
|
|
|
+ @AuthResource
|
|
|
+// public void createExcelFile(@RequestBody DailyCostRequest dailyCostRequest, HttpServletResponse res) {
|
|
|
+ public void createExcelFile(HttpServletResponse res) {
|
|
|
+ try {
|
|
|
+ // 支出趋势
|
|
|
+ List<DailyCostResponse> trendList = tradeService.dailyCostStatistics();
|
|
|
+ CostResponse trendCost = new CostResponse();
|
|
|
+ trendCost.setActualCost(BigDecimal.valueOf(trendList.stream().mapToDouble(t -> t.getGas().doubleValue()).sum()));
|
|
|
+
|
|
|
+ // 支付详情
|
|
|
+ List<ExportResponse> detailData = tradeService.getExportData();
|
|
|
+ List<String> projectIds = detailData.stream().map(ExportResponse::getProjectId).collect(Collectors.toList());
|
|
|
+ List<AmsAirdropProject> projects = projectService.listByIds(projectIds);
|
|
|
+ CostResponse detailCost = new CostResponse();
|
|
|
+ detailCost.setActualCost(BigDecimal.valueOf(trendList.stream().mapToDouble(t -> t.getGas().doubleValue()).sum()));
|
|
|
+ detailCost.setPredictCost(BigDecimal.valueOf(projects.stream().mapToDouble(d -> Double.parseDouble(d.getEstimatedCost())).sum()));
|
|
|
+
|
|
|
+ Map<String, String> trendMap = new LinkedHashMap<>();
|
|
|
+ trendMap.put("date", "时间");
|
|
|
+ trendMap.put("gas", "消耗gas");
|
|
|
+ trendMap.put("principal", "消耗本金");
|
|
|
+ trendMap.put("actualCost", "实际支出");
|
|
|
+ trendMap.put("predictCost", "预计支出");
|
|
|
+
|
|
|
+ Map<String, String> detailMap = new LinkedHashMap<>();
|
|
|
+ detailMap.put("projectName", "项目名称");
|
|
|
+ detailMap.put("taskName", "任务名称");
|
|
|
+ detailMap.put("address", "钱包");
|
|
|
+ detailMap.put("amount", "支出金额");
|
|
|
+ detailMap.put("gas", "消耗gas");
|
|
|
+ detailMap.put("createTime", "时间");
|
|
|
+ detailMap.put("txId", "txid");
|
|
|
+ detailMap.put("predictCost", "预计支出");
|
|
|
+ detailMap.put("actualCost", "实际支出");
|
|
|
+
|
|
|
+ List<SheetDTO> arrayList = new ArrayList<>();
|
|
|
+ arrayList.add(new SheetDTO("支出趋势", trendMap, trendList));
|
|
|
+ arrayList.add(new SheetDTO("支付详情", detailMap, detailData));
|
|
|
+ PoiUtils.exportExcel(res, arrayList, DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN) + "财务报表");
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ErrorServiceException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private File createProjectExcelFile() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private File createProjectExcelFile(DailyCostRequest dailyCostRequest) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal getPrincipalCost(List<String> batchAddresses) {
|
|
|
+ List<AmsTradeRecord> list = getTradeRecordList(batchAddresses);
|
|
|
+ double sumAmount = list.stream().mapToDouble(t -> Double.parseDouble(t.getCurrentBalance())).sum();
|
|
|
+ return BigDecimal.valueOf(sumAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal getGasCost(List<String> batchAddresses) {
|
|
|
+ List<AmsTradeRecord> list = getTradeRecordList(batchAddresses);
|
|
|
+ double sumGas = list.stream().mapToDouble(t -> Double.parseDouble(t.getGas())).sum();
|
|
|
+ return BigDecimal.valueOf(sumGas);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ private List<AmsTradeRecord> getTradeRecordList(List<String> batchAddresses) {
|
|
|
+ return tradeService.list(new LambdaQueryWrapper<AmsTradeRecord>()
|
|
|
+ .in(AmsTradeRecord::getAddress, batchAddresses)
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- private BigDecimal getDailyGasCost(String userId) {
|
|
|
- return BigDecimal.ZERO;
|
|
|
+ private static LocalDateTime getTodayStartTime() {
|
|
|
+ // 获取Calendar实例
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ // 获取今天凌晨的日期时间
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date todayStart = cal.getTime();
|
|
|
+ return LocalDateTimeUtil.of(todayStart);
|
|
|
}
|
|
|
|
|
|
}
|