fix(分账统计):新增分账统计后端逻辑
This commit is contained in:
+102
@@ -0,0 +1,102 @@
|
||||
package com.central.order.controller;
|
||||
|
||||
import com.central.common.model.Result;
|
||||
import com.central.order.service.IOrderStatisticsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/statistics")
|
||||
@Tag(name = "订单统计", description = "订单多维度统计接口")
|
||||
@RequiredArgsConstructor
|
||||
public class OrderStatisticsController {
|
||||
|
||||
private final IOrderStatisticsService orderStatisticsService;
|
||||
|
||||
@Operation(summary = "生成统计数据", description = "根据时间范围生成订单统计数据")
|
||||
@Parameters({
|
||||
@Parameter(name = "startTime", description = "开始时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true),
|
||||
@Parameter(name = "endTime", description = "结束时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true)
|
||||
})
|
||||
@PostMapping("/generate")
|
||||
public Result<String> generateStatistics(
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime) {
|
||||
try {
|
||||
boolean result = orderStatisticsService.generateStatistics(startTime, endTime);
|
||||
return result ? Result.succeed("统计数据生成成功") : Result.failed("统计数据生成失败");
|
||||
} catch (Exception e) {
|
||||
log.error("生成统计数据失败", e);
|
||||
return Result.failed("生成统计数据时发生错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "按时间维度统计", description = "统计指定二维码在时间范围内的订单数据")
|
||||
@Parameters({
|
||||
@Parameter(name = "qrcodeId", description = "二维码ID", required = true),
|
||||
@Parameter(name = "startTime", description = "开始时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true),
|
||||
@Parameter(name = "endTime", description = "结束时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true),
|
||||
@Parameter(name = "timeType", description = "时间维度类型(hour/day/month)", required = false)
|
||||
})
|
||||
@GetMapping("/time")
|
||||
public Result<List<Map<String, Object>>> statisticsByTime(
|
||||
@RequestParam String qrcodeId,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime,
|
||||
@RequestParam(defaultValue = "day") String timeType) {
|
||||
try {
|
||||
List<Map<String, Object>> result = orderStatisticsService.statisticsByTime(
|
||||
qrcodeId, startTime, endTime, timeType);
|
||||
return Result.succeed(result, "获取时间维度统计数据成功");
|
||||
} catch (Exception e) {
|
||||
log.error("获取时间维度统计数据失败", e);
|
||||
return Result.failed("获取统计数据时发生错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "按景区维度统计", description = "统计时间范围内各景区的订单数据")
|
||||
@Parameters({
|
||||
@Parameter(name = "startTime", description = "开始时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true),
|
||||
@Parameter(name = "endTime", description = "结束时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true)
|
||||
})
|
||||
@GetMapping("/tourism")
|
||||
public Result<List<Map<String, Object>>> statisticsByTourism(
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime) {
|
||||
try {
|
||||
List<Map<String, Object>> result = orderStatisticsService.statisticsByTourism(startTime, endTime);
|
||||
return Result.succeed(result, "获取景区维度统计数据成功");
|
||||
} catch (Exception e) {
|
||||
log.error("获取景区维度统计数据失败", e);
|
||||
return Result.failed("获取统计数据时发生错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "按设备维度统计", description = "统计时间范围内各设备的订单数据")
|
||||
@Parameters({
|
||||
@Parameter(name = "startTime", description = "开始时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true),
|
||||
@Parameter(name = "endTime", description = "结束时间 (格式:yyyy-MM-dd HH:mm:ss)", required = true)
|
||||
})
|
||||
@GetMapping("/device")
|
||||
public Result<List<Map<String, Object>>> statisticsByDevice(
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String startTime,
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String endTime) {
|
||||
try {
|
||||
List<Map<String, Object>> result = orderStatisticsService.statisticsByDevice(startTime, endTime);
|
||||
return Result.succeed(result, "获取设备维度统计数据成功");
|
||||
} catch (Exception e) {
|
||||
log.error("获取设备维度统计数据失败", e);
|
||||
return Result.failed("获取统计数据时发生错误:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.central.order.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.central.order.model.OrderStatistics;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Mapper
|
||||
public interface OrderStatisticsMapper extends BaseMapper<OrderStatistics> {
|
||||
|
||||
@Select({
|
||||
"SELECT",
|
||||
" o.code_id as qrcodeId,",
|
||||
" o.tourism_id as tourismId,",
|
||||
" t.tourism_name as tourismName,",
|
||||
" o.device_sn as deviceSn,",
|
||||
" COUNT(o.id) as totalOrders,",
|
||||
" SUM(CASE WHEN o.order_status IN (1,2,3,4,5,6) THEN 1 ELSE 0 END) as paidOrders,",
|
||||
" SUM(CASE WHEN o.order_status IN (3,6) THEN 1 ELSE 0 END) as completedOrders,",
|
||||
" SUM(CASE WHEN o.order_status = 2 THEN 1 ELSE 0 END) as canceledOrders,",
|
||||
" SUM(CAST(o.order_amount AS SIGNED)) as totalAmount,",
|
||||
" SUM(CASE WHEN o.order_status IN (1,2,3,4,5,6) THEN CAST(o.order_amount AS SIGNED) ELSE 0 END) as paidAmount,",
|
||||
" SUM(CASE WHEN o.order_status IN (7,8) THEN CAST(o.order_amount AS SIGNED) ELSE 0 END) as refundAmount",
|
||||
"FROM orders o",
|
||||
"LEFT JOIN tourisms t ON o.tourism_id = t.id",
|
||||
"WHERE o.create_time BETWEEN #{startTime} AND #{endTime}",
|
||||
" AND o.is_del = 0",
|
||||
"GROUP BY o.code_id, o.tourism_id, t.tourism_name, o.device_sn"
|
||||
})
|
||||
List<Map<String, Object>> statisticsByQrcode(
|
||||
@Param("startTime") String startTime,
|
||||
@Param("endTime") String endTime
|
||||
);
|
||||
|
||||
@Select({
|
||||
"SELECT DATE_FORMAT(o.create_time, #{dateFormat}) as timeGroup,",
|
||||
" COUNT(o.id) as totalOrders,",
|
||||
" SUM(CAST(o.order_amount AS SIGNED)) as totalAmount",
|
||||
"FROM orders o",
|
||||
"WHERE o.code_id = #{qrcodeId}",
|
||||
" AND o.create_time BETWEEN #{startTime} AND #{endTime}",
|
||||
" AND o.is_del = 0",
|
||||
"GROUP BY timeGroup",
|
||||
"ORDER BY timeGroup"
|
||||
})
|
||||
List<Map<String, Object>> statisticsByTime(
|
||||
@Param("qrcodeId") String qrcodeId,
|
||||
@Param("startTime") String startTime,
|
||||
@Param("endTime") String endTime,
|
||||
@Param("dateFormat") String dateFormat
|
||||
);
|
||||
|
||||
@Select({
|
||||
"SELECT",
|
||||
" t.id as tourismId,",
|
||||
" t.tourism_name as tourismName,",
|
||||
" COUNT(o.id) as totalOrders,",
|
||||
" SUM(CASE WHEN o.order_status IN (3,6) THEN 1 ELSE 0 END) as completedOrders,",
|
||||
" SUM(CAST(o.order_amount AS SIGNED)) as totalAmount,",
|
||||
" SUM(CASE WHEN o.order_status IN (1,2,3,4,5,6) THEN CAST(o.order_amount AS SIGNED) ELSE 0 END) as paidAmount,",
|
||||
" SUM(CASE WHEN o.order_status IN (7,8) THEN CAST(o.order_amount AS SIGNED) ELSE 0 END) as refundAmount",
|
||||
"FROM orders o",
|
||||
"LEFT JOIN tourisms t ON o.tourism_id = t.id",
|
||||
"WHERE o.create_time BETWEEN #{startTime} AND #{endTime}",
|
||||
" AND o.is_del = 0",
|
||||
"GROUP BY t.id, t.tourism_name",
|
||||
"ORDER BY totalOrders DESC"
|
||||
})
|
||||
List<Map<String, Object>> statisticsByTourism(
|
||||
@Param("startTime") String startTime,
|
||||
@Param("endTime") String endTime
|
||||
);
|
||||
|
||||
@Select({
|
||||
"SELECT",
|
||||
" o.device_sn as deviceSn,",
|
||||
" COUNT(o.id) as totalOrders,",
|
||||
" SUM(CAST(o.order_amount AS SIGNED)) as totalAmount,",
|
||||
" COUNT(DISTINCT DATE(o.create_time)) * 24 as totalHours,",
|
||||
" COUNT(DISTINCT HOUR(o.create_time)) as usedHours",
|
||||
"FROM orders o",
|
||||
"WHERE o.create_time BETWEEN #{startTime} AND #{endTime}",
|
||||
" AND o.is_del = 0",
|
||||
"GROUP BY o.device_sn",
|
||||
"ORDER BY totalOrders DESC"
|
||||
})
|
||||
List<Map<String, Object>> statisticsByDevice(
|
||||
@Param("startTime") String startTime,
|
||||
@Param("endTime") String endTime
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.central.order.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@TableName("order_statistics")
|
||||
public class OrderStatistics {
|
||||
private Long id;
|
||||
|
||||
// 统计维度
|
||||
private String qrcodeId; // 二维码ID
|
||||
private Long tourismId; // 景区ID
|
||||
private String tourismName; // 景区名称
|
||||
private String deviceSn; // 设备编号
|
||||
private String tenantId; // 租户ID
|
||||
|
||||
// 订单统计
|
||||
private Integer totalOrders; // 总订单数
|
||||
private Integer paidOrders; // 已支付订单数
|
||||
private Integer completedOrders;// 已完成订单数
|
||||
private Integer canceledOrders; // 已取消订单数
|
||||
|
||||
// 金额统计(单位:分)
|
||||
private Long totalAmount; // 总金额
|
||||
private Long paidAmount; // 已支付金额
|
||||
private Long refundAmount; // 退款金额
|
||||
|
||||
// 分账金额
|
||||
private BigDecimal platformRate = BigDecimal.ZERO;
|
||||
private BigDecimal tourismRate = BigDecimal.ZERO;
|
||||
private Long platformAmount = 0L;
|
||||
private Long tourismAmount = 0L;
|
||||
|
||||
// 时间维度
|
||||
private Date startTime; // 统计开始时间
|
||||
private Date endTime; // 统计结束时间
|
||||
private String statisticsDate; // 统计日期(yyyy-MM-dd)
|
||||
private Integer statisticsHour; // 统计小时(0-23)
|
||||
private Date createTime; // 创建时间
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.central.order.service;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.service.ISuperService;
|
||||
import com.central.order.model.OrderStatistics;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface IOrderStatisticsService extends ISuperService<OrderStatistics> {
|
||||
// 基础统计
|
||||
PageResult<OrderStatistics> findList(Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 生成统计数据
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 是否生成成功
|
||||
*/
|
||||
boolean generateStatistics(String startTime, String endTime);
|
||||
|
||||
/**
|
||||
* 按时间维度统计
|
||||
* @param qrcodeId 二维码ID
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @param timeType 时间类型(hour/day/month)
|
||||
* @return 统计数据列表,包含:
|
||||
* - timeGroup: 时间分组
|
||||
* - formatTime: 格式化后的时间
|
||||
* - totalOrders: 订单总数
|
||||
* - totalAmount: 总金额(分)
|
||||
* - totalAmountYuan: 总金额(元)
|
||||
*/
|
||||
List<Map<String, Object>> statisticsByTime(String qrcodeId, String startTime, String endTime, String timeType);
|
||||
|
||||
/**
|
||||
* 按景区维度统计
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 统计数据列表,包含:
|
||||
* - tourismId: 景区ID
|
||||
* - tourismName: 景区名称
|
||||
* - totalOrders: 订单总数
|
||||
* - completedOrders: 已完成订单数
|
||||
* - completionRate: 完成率
|
||||
* - totalAmountYuan: 总金额(元)
|
||||
* - paidAmountYuan: 已支付金额(元)
|
||||
* - refundAmountYuan: 退款金额(元)
|
||||
*/
|
||||
List<Map<String, Object>> statisticsByTourism(String startTime, String endTime);
|
||||
|
||||
/**
|
||||
* 按设备维度统计
|
||||
* @param startTime 开始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 统计数据列表,包含:
|
||||
* - deviceSn: 设备编号
|
||||
* - totalOrders: 订单总数
|
||||
* - totalAmountYuan: 总金额(元)
|
||||
* - usageRate: 设备使用率
|
||||
*/
|
||||
List<Map<String, Object>> statisticsByDevice(String startTime, String endTime);
|
||||
}
|
||||
+304
@@ -0,0 +1,304 @@
|
||||
package com.central.order.service.impl;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.order.mapper.OrderStatisticsMapper;
|
||||
import com.central.order.model.OrderStatistics;
|
||||
import com.central.order.model.QrCode;
|
||||
import com.central.order.service.IOrderStatisticsService;
|
||||
import com.central.order.service.IQrCodeService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrderStatisticsServiceImpl extends SuperServiceImpl<OrderStatisticsMapper, OrderStatistics> implements IOrderStatisticsService {
|
||||
|
||||
@Autowired
|
||||
private OrderStatisticsMapper orderStatisticsMapper;
|
||||
|
||||
@Autowired
|
||||
private IQrCodeService qrCodeService;
|
||||
|
||||
@Override
|
||||
public PageResult<OrderStatistics> findList(Map<String, Object> params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean generateStatistics(String startTime, String endTime) {
|
||||
try {
|
||||
List<Map<String, Object>> statisticsList = orderStatisticsMapper.statisticsByQrcode(startTime, endTime);
|
||||
|
||||
for (Map<String, Object> statistics : statisticsList) {
|
||||
OrderStatistics orderStatistics = new OrderStatistics();
|
||||
|
||||
// 设置基础信息
|
||||
orderStatistics.setQrcodeId(String.valueOf(statistics.get("qrcodeId")));
|
||||
orderStatistics.setTourismId(getLongValue(statistics.get("tourismId")));
|
||||
orderStatistics.setTourismName(String.valueOf(statistics.get("tourismName")));
|
||||
orderStatistics.setDeviceSn(String.valueOf(statistics.get("deviceSn")));
|
||||
|
||||
// 设置订单统计
|
||||
orderStatistics.setTotalOrders(getIntegerValue(statistics.get("totalOrders")));
|
||||
orderStatistics.setPaidOrders(getIntegerValue(statistics.get("paidOrders")));
|
||||
orderStatistics.setCompletedOrders(getIntegerValue(statistics.get("completedOrders")));
|
||||
orderStatistics.setCanceledOrders(getIntegerValue(statistics.get("canceledOrders")));
|
||||
|
||||
// 设置金额统计
|
||||
orderStatistics.setTotalAmount(getLongValue(statistics.get("totalAmount")));
|
||||
orderStatistics.setPaidAmount(getLongValue(statistics.get("paidAmount")));
|
||||
orderStatistics.setRefundAmount(getLongValue(statistics.get("refundAmount")));
|
||||
|
||||
// 设置默认分账比例和金额
|
||||
BigDecimal platformRate = new BigDecimal("0.30");
|
||||
BigDecimal tourismRate = new BigDecimal("0.70");
|
||||
|
||||
orderStatistics.setPlatformRate(platformRate);
|
||||
orderStatistics.setTourismRate(tourismRate);
|
||||
|
||||
// 计算分账金额
|
||||
Long paidAmount = orderStatistics.getPaidAmount();
|
||||
if (paidAmount == null) {
|
||||
paidAmount = 0L;
|
||||
}
|
||||
|
||||
// 计算平台分成金额
|
||||
Long platformAmount = platformRate
|
||||
.multiply(new BigDecimal(paidAmount))
|
||||
.divide(new BigDecimal("100"), 0, RoundingMode.HALF_UP)
|
||||
.longValue();
|
||||
orderStatistics.setPlatformAmount(platformAmount);
|
||||
|
||||
// 计算景区分成金额
|
||||
Long tourismAmount = tourismRate
|
||||
.multiply(new BigDecimal(paidAmount))
|
||||
.divide(new BigDecimal("100"), 0, RoundingMode.HALF_UP)
|
||||
.longValue();
|
||||
orderStatistics.setTourismAmount(tourismAmount);
|
||||
|
||||
// 设置时间
|
||||
orderStatistics.setStartTime(parseDateTime(startTime));
|
||||
orderStatistics.setEndTime(parseDateTime(endTime));
|
||||
orderStatistics.setCreateTime(new Date());
|
||||
|
||||
// 设置统计日期
|
||||
SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
orderStatistics.setStatisticsDate(dateSdf.format(new Date()));
|
||||
|
||||
// 设置统计小时
|
||||
Calendar cal = Calendar.getInstance();
|
||||
orderStatistics.setStatisticsHour(cal.get(Calendar.HOUR_OF_DAY));
|
||||
|
||||
// 保存统计数据
|
||||
orderStatisticsMapper.insert(orderStatistics);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("生成统计数据失败", e);
|
||||
throw new RuntimeException("生成统计数据失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 安全的Integer转换方法
|
||||
private Integer getIntegerValue(Object value) {
|
||||
if (value == null) {
|
||||
return 0;
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return ((Long) value).intValue();
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(value.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 安全的Long转换方法
|
||||
private Long getLongValue(Object value) {
|
||||
if (value == null) {
|
||||
return 0L;
|
||||
}
|
||||
if (value instanceof Long) {
|
||||
return (Long) value;
|
||||
}
|
||||
if (value instanceof Integer) {
|
||||
return ((Integer) value).longValue();
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(value.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
// 日期转换方法
|
||||
private Date parseDateTime(String dateTimeStr) {
|
||||
try {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return sdf.parse(dateTimeStr);
|
||||
} catch (ParseException e) {
|
||||
log.error("日期解析失败: {}", dateTimeStr, e);
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> statisticsByTime(String qrcodeId, String startTime, String endTime, String timeType) {
|
||||
try {
|
||||
String dateFormat;
|
||||
switch (timeType) {
|
||||
case "hour":
|
||||
dateFormat = "%Y-%m-%d %H:00:00";
|
||||
break;
|
||||
case "day":
|
||||
dateFormat = "%Y-%m-%d";
|
||||
break;
|
||||
case "month":
|
||||
dateFormat = "%Y-%m";
|
||||
break;
|
||||
default:
|
||||
dateFormat = "%Y-%m-%d";
|
||||
}
|
||||
|
||||
List<Map<String, Object>> statistics = orderStatisticsMapper.statisticsByTime(
|
||||
qrcodeId, startTime, endTime, dateFormat);
|
||||
|
||||
// 处理统计数据
|
||||
for (Map<String, Object> stat : statistics) {
|
||||
// 添加金额格式化(转换为元)
|
||||
Long totalAmount = getLongValue(stat.get("totalAmount"));
|
||||
stat.put("totalAmountYuan", formatAmount(totalAmount));
|
||||
|
||||
// 添加时间格式化
|
||||
String timeGroup = (String) stat.get("timeGroup");
|
||||
stat.put("formatTime", formatTimeGroup(timeGroup, timeType));
|
||||
}
|
||||
|
||||
return statistics;
|
||||
} catch (Exception e) {
|
||||
log.error("按时间维度统计失败", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> statisticsByTourism(String startTime, String endTime) {
|
||||
try {
|
||||
List<Map<String, Object>> statistics = orderStatisticsMapper.statisticsByTourism(startTime, endTime);
|
||||
|
||||
// 处理统计数据
|
||||
for (Map<String, Object> stat : statistics) {
|
||||
// 添加金额格式化
|
||||
Long totalAmount = getLongValue(stat.get("totalAmount"));
|
||||
Long paidAmount = getLongValue(stat.get("paidAmount"));
|
||||
Long refundAmount = getLongValue(stat.get("refundAmount"));
|
||||
|
||||
stat.put("totalAmountYuan", formatAmount(totalAmount));
|
||||
stat.put("paidAmountYuan", formatAmount(paidAmount));
|
||||
stat.put("refundAmountYuan", formatAmount(refundAmount));
|
||||
|
||||
// 计算完成率
|
||||
Integer totalOrders = getIntegerValue(stat.get("totalOrders"));
|
||||
Integer completedOrders = getIntegerValue(stat.get("completedOrders"));
|
||||
if (totalOrders > 0) {
|
||||
double completionRate = (completedOrders * 100.0) / totalOrders;
|
||||
stat.put("completionRate", String.format("%.2f%%", completionRate));
|
||||
} else {
|
||||
stat.put("completionRate", "0.00%");
|
||||
}
|
||||
}
|
||||
|
||||
return statistics;
|
||||
} catch (Exception e) {
|
||||
log.error("按景区维度统计失败", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<String, Object>> statisticsByDevice(String startTime, String endTime) {
|
||||
try {
|
||||
List<Map<String, Object>> statistics = orderStatisticsMapper.statisticsByDevice(startTime, endTime);
|
||||
|
||||
// 处理统计数据
|
||||
for (Map<String, Object> stat : statistics) {
|
||||
// 添加金额格式化
|
||||
Long totalAmount = getLongValue(stat.get("totalAmount"));
|
||||
stat.put("totalAmountYuan", formatAmount(totalAmount));
|
||||
|
||||
// 计算设备使用率
|
||||
Integer totalHours = getIntegerValue(stat.get("totalHours"));
|
||||
Integer usedHours = getIntegerValue(stat.get("usedHours"));
|
||||
if (totalHours > 0) {
|
||||
double usageRate = (usedHours * 100.0) / totalHours;
|
||||
stat.put("usageRate", String.format("%.2f%%", usageRate));
|
||||
} else {
|
||||
stat.put("usageRate", "0.00%");
|
||||
}
|
||||
}
|
||||
|
||||
return statistics;
|
||||
} catch (Exception e) {
|
||||
log.error("按设备维度统计失败", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化金额(分转元)
|
||||
private String formatAmount(Long amount) {
|
||||
if (amount == null) {
|
||||
return "0.00";
|
||||
}
|
||||
return String.format("%.2f", amount / 100.0);
|
||||
}
|
||||
|
||||
// 格式化时间组
|
||||
private String formatTimeGroup(String timeGroup, String timeType) {
|
||||
if (timeGroup == null) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
SimpleDateFormat inputFormat;
|
||||
SimpleDateFormat outputFormat;
|
||||
switch (timeType) {
|
||||
case "hour":
|
||||
inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
|
||||
outputFormat = new SimpleDateFormat("MM-dd HH:00");
|
||||
break;
|
||||
case "day":
|
||||
inputFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
outputFormat = new SimpleDateFormat("MM-dd");
|
||||
break;
|
||||
case "month":
|
||||
inputFormat = new SimpleDateFormat("yyyy-MM");
|
||||
outputFormat = new SimpleDateFormat("yyyy-MM");
|
||||
break;
|
||||
default:
|
||||
return timeGroup;
|
||||
}
|
||||
Date date = inputFormat.parse(timeGroup);
|
||||
return outputFormat.format(date);
|
||||
} catch (ParseException e) {
|
||||
return timeGroup;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -99,6 +99,10 @@
|
||||
{
|
||||
path: '/orders/refund.html',
|
||||
component: './orders/Refund',
|
||||
},
|
||||
{
|
||||
path: '/orders/statistics.html',
|
||||
component: './orders/statistics',
|
||||
}
|
||||
],
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user