录音时长统计相关代码
This commit is contained in:
@@ -94,17 +94,45 @@ public class AudioStatisticsController {
|
||||
/**
|
||||
* 查询指定销售人员的统计数据
|
||||
*/
|
||||
@Operation(summary = "查询指定销售人员的统计数据", description = "查询指定销售人员在指定日期范围内的录音统计数据")
|
||||
@GetMapping("/sales/{salesId}")
|
||||
@Operation(summary = "查询指定销售人员的统计数据", description = "根据销售人员ID或电话号码查询指定日期范围内的录音统计数据")
|
||||
@GetMapping("/sales")
|
||||
public Result<?> getStatisticsBySales(
|
||||
@Parameter(description = "销售人员ID")
|
||||
@PathVariable String salesId,
|
||||
@Parameter(description = "开始日期,格式:yyyy-MM-dd")
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
|
||||
@Parameter(description = "结束日期,格式:yyyy-MM-dd")
|
||||
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
|
||||
@RequestParam(required = false) String salesId,
|
||||
@Parameter(description = "销售人员电话号码")
|
||||
@RequestParam(required = false) String salesPhone,
|
||||
@Parameter(description = "日期范围类型:1=1个月,2=3个月,3=6个月")
|
||||
@RequestParam Integer dateRangeType) {
|
||||
try {
|
||||
List<AudioManagementStatistics> statistics = audioStatisticsService.getStatisticsBySales(salesId, startDate, endDate);
|
||||
// 验证参数:至少提供一个查询条件
|
||||
if ((salesId == null || salesId.trim().isEmpty()) &&
|
||||
(salesPhone == null || salesPhone.trim().isEmpty())) {
|
||||
return Result.error("销售人员ID和电话号码至少需要提供一个");
|
||||
}
|
||||
|
||||
// 验证日期范围类型
|
||||
if (dateRangeType == null || (dateRangeType != 1 && dateRangeType != 2 && dateRangeType != 3)) {
|
||||
return Result.error("日期范围类型必须为1(1个月)、2(3个月)或3(6个月)");
|
||||
}
|
||||
|
||||
// 根据日期范围类型计算开始和结束日期
|
||||
LocalDate endDate = LocalDate.now().minusDays(1); // 昨天
|
||||
LocalDate startDate;
|
||||
switch (dateRangeType) {
|
||||
case 1:
|
||||
startDate = endDate.minusMonths(1); // 1个月前
|
||||
break;
|
||||
case 2:
|
||||
startDate = endDate.minusMonths(3); // 3个月前
|
||||
break;
|
||||
case 3:
|
||||
startDate = endDate.minusMonths(6); // 6个月前
|
||||
break;
|
||||
default:
|
||||
return Result.error("日期范围类型无效");
|
||||
}
|
||||
|
||||
List<AudioManagementStatistics> statistics = audioStatisticsService.getStatisticsBySales(salesId, salesPhone, startDate, endDate);
|
||||
Result<?> result = Result.ok(statistics);
|
||||
result.setMsg("查询成功");
|
||||
return result;
|
||||
|
||||
@@ -45,7 +45,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("dealership_name")
|
||||
private String dealershipName;
|
||||
|
||||
@Schema(description = "门店录音总时长(秒)")
|
||||
@Schema(description = "门店录音总时长(分钟)")
|
||||
@TableField("dealership_total_duration")
|
||||
private Long dealershipTotalDuration;
|
||||
|
||||
@@ -53,7 +53,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("dealership_recording_count")
|
||||
private Integer dealershipRecordingCount;
|
||||
|
||||
@Schema(description = "门店录音平均时长(秒)")
|
||||
@Schema(description = "门店录音平均时长(分钟)")
|
||||
@TableField("dealership_avg_duration")
|
||||
private BigDecimal dealershipAvgDuration;
|
||||
|
||||
@@ -65,7 +65,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("sales_name")
|
||||
private String salesName;
|
||||
|
||||
@Schema(description = "销售人员录音总时长(秒)")
|
||||
@Schema(description = "销售人员录音总时长(分钟)")
|
||||
@TableField("sales_total_duration")
|
||||
private Long salesTotalDuration;
|
||||
|
||||
@@ -73,7 +73,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("sales_recording_count")
|
||||
private Integer salesRecordingCount;
|
||||
|
||||
@Schema(description = "销售人员录音平均时长(秒)")
|
||||
@Schema(description = "销售人员录音平均时长(分钟)")
|
||||
@TableField("sales_avg_duration")
|
||||
private BigDecimal salesAvgDuration;
|
||||
|
||||
@@ -85,7 +85,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("project_name")
|
||||
private String projectName;
|
||||
|
||||
@Schema(description = "项目录音总时长(秒)")
|
||||
@Schema(description = "项目录音总时长(分钟)")
|
||||
@TableField("project_total_duration")
|
||||
private Long projectTotalDuration;
|
||||
|
||||
@@ -93,7 +93,7 @@ public class AudioManagementStatistics implements Serializable {
|
||||
@TableField("project_recording_count")
|
||||
private Integer projectRecordingCount;
|
||||
|
||||
@Schema(description = "项目录音平均时长(秒)")
|
||||
@Schema(description = "项目录音平均时长(分钟)")
|
||||
@TableField("project_avg_duration")
|
||||
private BigDecimal projectAvgDuration;
|
||||
|
||||
|
||||
@@ -89,4 +89,32 @@ public interface AudioManagementStatisticsMapper extends BaseMapper<AudioManagem
|
||||
* @return 删除的记录数
|
||||
*/
|
||||
int deleteByStatisticsDate(@Param("date") LocalDate date);
|
||||
|
||||
/**
|
||||
* 根据销售人员和日期范围聚合查询总录音时长和平均录音时长
|
||||
* @param salesId 销售人员ID
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return 聚合统计结果(包含总录音时长、总录音数量、平均录音时长、销售人员姓名)
|
||||
*/
|
||||
@Select("SELECT " +
|
||||
"sales_id, " +
|
||||
"MAX(sales_name) as sales_name, " +
|
||||
"SUM(sales_total_duration) as total_duration, " +
|
||||
"SUM(sales_recording_count) as total_count, " +
|
||||
"CASE " +
|
||||
" WHEN SUM(sales_recording_count) > 0 THEN " +
|
||||
" ROUND(SUM(sales_total_duration) / SUM(sales_recording_count), 2) " +
|
||||
" ELSE 0 " +
|
||||
"END as avg_duration " +
|
||||
"FROM audio_management_statistics " +
|
||||
"WHERE sales_id = #{salesId} " +
|
||||
"AND statistics_date >= #{startDate} " +
|
||||
"AND statistics_date <= #{endDate} " +
|
||||
"AND sales_id IS NOT NULL " +
|
||||
"AND sales_id != '' " +
|
||||
"GROUP BY sales_id")
|
||||
Map<String, Object> getSalesAggregateStatistics(@Param("salesId") String salesId,
|
||||
@Param("startDate") LocalDate startDate,
|
||||
@Param("endDate") LocalDate endDate);
|
||||
}
|
||||
|
||||
@@ -55,6 +55,16 @@ public interface IAudioManagementStatisticsService extends IService<AudioManagem
|
||||
*/
|
||||
List<AudioManagementStatistics> getStatisticsBySales(String salesId, LocalDate startDate, LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 查询指定销售人员的统计数据(支持根据ID或电话号码查询)
|
||||
* @param salesId 销售人员ID(可选)
|
||||
* @param salesPhone 销售人员电话号码(可选)
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return 统计记录列表
|
||||
*/
|
||||
List<AudioManagementStatistics> getStatisticsBySales(String salesId, String salesPhone, LocalDate startDate, LocalDate endDate);
|
||||
|
||||
/**
|
||||
* 查询指定项目的统计数据
|
||||
* @param projectId 项目ID
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.rj.service.impl;
|
||||
|
||||
import com.rj.entity.AudioManagementStatistics;
|
||||
import com.rj.entity.SalesManagement;
|
||||
import com.rj.mapper.AudioManagementStatisticsMapper;
|
||||
import com.rj.service.IAudioManagementStatisticsService;
|
||||
import com.rj.service.ISalesManagementService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -26,6 +29,9 @@ import java.util.Map;
|
||||
@Service
|
||||
public class AudioManagementStatisticsServiceImpl extends ServiceImpl<AudioManagementStatisticsMapper, AudioManagementStatistics> implements IAudioManagementStatisticsService {
|
||||
|
||||
@Autowired
|
||||
private ISalesManagementService salesManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int generateStatisticsByDate(LocalDate date) {
|
||||
@@ -253,6 +259,104 @@ public class AudioManagementStatisticsServiceImpl extends ServiceImpl<AudioManag
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsBySales(String salesId, String salesPhone, LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 如果提供了电话号码,先通过电话号码查询到salesId
|
||||
String finalSalesId = salesId;
|
||||
if ((finalSalesId == null || finalSalesId.trim().isEmpty()) &&
|
||||
salesPhone != null && !salesPhone.trim().isEmpty()) {
|
||||
// 通过电话号码查询销售人员
|
||||
LambdaQueryWrapper<SalesManagement> salesQueryWrapper = new LambdaQueryWrapper<>();
|
||||
salesQueryWrapper.eq(SalesManagement::getLoginAccount, salesPhone);
|
||||
List<SalesManagement> salesList = salesManagementService.list(salesQueryWrapper);
|
||||
|
||||
if (salesList == null || salesList.isEmpty()) {
|
||||
// 如果没有找到对应的销售人员,返回空列表
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 如果找到多个销售人员,使用第一个的ID
|
||||
// 或者可以根据业务需求处理多个销售人员的情况
|
||||
finalSalesId = salesList.get(0).getId();
|
||||
}
|
||||
|
||||
// 如果最终没有salesId,返回空列表
|
||||
if (finalSalesId == null || finalSalesId.trim().isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 根据salesId查询统计数据
|
||||
queryWrapper.eq(AudioManagementStatistics::getSalesId, finalSalesId)
|
||||
.between(AudioManagementStatistics::getStatisticsDate, startDate, endDate)
|
||||
.orderByAsc(AudioManagementStatistics::getStatisticsDate);
|
||||
List<AudioManagementStatistics> statisticsList = list(queryWrapper);
|
||||
|
||||
// 在数据库层面聚合查询总录音时长和平均录音时长
|
||||
Map<String, Object> aggregateResult = baseMapper.getSalesAggregateStatistics(finalSalesId, startDate, endDate);
|
||||
|
||||
// 如果查询到聚合结果,将汇总统计信息设置到每条记录上
|
||||
if (aggregateResult != null && !aggregateResult.isEmpty() && !statisticsList.isEmpty()) {
|
||||
// 提取聚合结果
|
||||
Long totalDuration = null;
|
||||
Integer totalCount = null;
|
||||
BigDecimal avgDuration = null;
|
||||
String salesName = null;
|
||||
|
||||
// 获取总录音时长
|
||||
Object totalDurationObj = aggregateResult.get("total_duration");
|
||||
if (totalDurationObj != null) {
|
||||
totalDuration = Long.valueOf(totalDurationObj.toString());
|
||||
}
|
||||
|
||||
// 获取总录音数量
|
||||
Object totalCountObj = aggregateResult.get("total_count");
|
||||
if (totalCountObj != null) {
|
||||
totalCount = Integer.valueOf(totalCountObj.toString());
|
||||
}
|
||||
|
||||
// 获取平均录音时长
|
||||
Object avgDurationObj = aggregateResult.get("avg_duration");
|
||||
if (avgDurationObj != null) {
|
||||
avgDuration = new BigDecimal(avgDurationObj.toString());
|
||||
}
|
||||
|
||||
// 获取销售人员姓名
|
||||
Object salesNameObj = aggregateResult.get("sales_name");
|
||||
if (salesNameObj != null) {
|
||||
salesName = salesNameObj.toString();
|
||||
}
|
||||
|
||||
// 将聚合统计结果设置到每条记录上
|
||||
for (AudioManagementStatistics stat : statisticsList) {
|
||||
// 设置总录音时长(汇总)
|
||||
if (totalDuration != null) {
|
||||
stat.setSalesTotalDuration(totalDuration);
|
||||
}
|
||||
|
||||
// 设置总录音数量(汇总)
|
||||
if (totalCount != null) {
|
||||
stat.setSalesRecordingCount(totalCount);
|
||||
}
|
||||
|
||||
// 设置平均录音时长(汇总)
|
||||
if (avgDuration != null) {
|
||||
stat.setSalesAvgDuration(avgDuration);
|
||||
} else {
|
||||
stat.setSalesAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
// 如果聚合结果中有销售人员姓名,且当前记录没有,则设置
|
||||
if (salesName != null && (stat.getSalesName() == null || stat.getSalesName().isEmpty())) {
|
||||
stat.setSalesName(salesName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statisticsList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsByProject(String projectId, LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
Reference in New Issue
Block a user