录音统计
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
package com.rj.service.impl;
|
||||
|
||||
import com.rj.entity.AudioManagementStatistics;
|
||||
import com.rj.mapper.AudioManagementStatisticsMapper;
|
||||
import com.rj.service.IAudioManagementStatisticsService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 门店录音统计表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author 李中华 ,spllzh
|
||||
* @since 2025-08-07
|
||||
*/
|
||||
@Service
|
||||
public class AudioManagementStatisticsServiceImpl extends ServiceImpl<AudioManagementStatisticsMapper, AudioManagementStatistics> implements IAudioManagementStatisticsService {
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int generateStatisticsByDate(LocalDate date) {
|
||||
// 先删除该日期的统计数据(如果存在)
|
||||
baseMapper.deleteByStatisticsDate(date);
|
||||
|
||||
// 查询该日期的门店录音统计数据
|
||||
List<Map<String, Object>> dealershipStatsList = baseMapper.getDealershipStatisticsByDate(date);
|
||||
|
||||
// 查询该日期的销售人员录音统计数据
|
||||
List<Map<String, Object>> salesStatsList = baseMapper.getSalesStatisticsByDate(date);
|
||||
|
||||
// 查询该日期的项目录音统计数据
|
||||
List<Map<String, Object>> projectStatsList = baseMapper.getProjectStatisticsByDate(date);
|
||||
|
||||
int count = 0;
|
||||
|
||||
// 处理门店统计数据
|
||||
for (Map<String, Object> stat : dealershipStatsList) {
|
||||
AudioManagementStatistics statistics = new AudioManagementStatistics();
|
||||
|
||||
// 设置门店信息
|
||||
Object dealershipIdObj = stat.get("dealership_id");
|
||||
if (dealershipIdObj != null) {
|
||||
try {
|
||||
statistics.setDealershipId(Long.valueOf(dealershipIdObj.toString()));
|
||||
} catch (NumberFormatException e) {
|
||||
// 如果dealership_id是UUID格式,使用hashCode作为ID
|
||||
statistics.setDealershipId((long) dealershipIdObj.toString().hashCode());
|
||||
}
|
||||
} else {
|
||||
statistics.setDealershipId(0L);
|
||||
}
|
||||
|
||||
Object dealershipNameObj = stat.get("dealership_name");
|
||||
statistics.setDealershipName(dealershipNameObj != null ? dealershipNameObj.toString() : "未知门店");
|
||||
|
||||
// 设置门店录音统计数据
|
||||
Object totalDurationObj = stat.get("total_duration_seconds");
|
||||
if (totalDurationObj != null) {
|
||||
statistics.setDealershipTotalDuration(Long.valueOf(totalDurationObj.toString()));
|
||||
} else {
|
||||
statistics.setDealershipTotalDuration(0L);
|
||||
}
|
||||
|
||||
Object recordingCountObj = stat.get("recording_count");
|
||||
if (recordingCountObj != null) {
|
||||
statistics.setDealershipRecordingCount(Integer.valueOf(recordingCountObj.toString()));
|
||||
} else {
|
||||
statistics.setDealershipRecordingCount(0);
|
||||
}
|
||||
|
||||
// 计算门店平均时长
|
||||
if (statistics.getDealershipRecordingCount() > 0 && statistics.getDealershipTotalDuration() > 0) {
|
||||
BigDecimal avgDuration = BigDecimal.valueOf(statistics.getDealershipTotalDuration())
|
||||
.divide(BigDecimal.valueOf(statistics.getDealershipRecordingCount()), 2, RoundingMode.HALF_UP);
|
||||
statistics.setDealershipAvgDuration(avgDuration);
|
||||
} else {
|
||||
statistics.setDealershipAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
// 查找该门店对应的销售人员统计数据
|
||||
String dealershipId = dealershipIdObj != null ? dealershipIdObj.toString() : "";
|
||||
Map<String, Object> salesStat = findSalesStatByDealership(salesStatsList, dealershipId);
|
||||
|
||||
if (salesStat != null) {
|
||||
// 设置销售人员信息
|
||||
Object salesIdObj = salesStat.get("sales_id");
|
||||
statistics.setSalesId(salesIdObj != null ? salesIdObj.toString() : "");
|
||||
|
||||
Object salesNameObj = salesStat.get("sales_name");
|
||||
statistics.setSalesName(salesNameObj != null ? salesNameObj.toString() : "未知销售");
|
||||
|
||||
// 设置销售人员录音统计数据
|
||||
Object salesTotalDurationObj = salesStat.get("total_duration_seconds");
|
||||
if (salesTotalDurationObj != null) {
|
||||
statistics.setSalesTotalDuration(Long.valueOf(salesTotalDurationObj.toString()));
|
||||
} else {
|
||||
statistics.setSalesTotalDuration(0L);
|
||||
}
|
||||
|
||||
Object salesRecordingCountObj = salesStat.get("recording_count");
|
||||
if (salesRecordingCountObj != null) {
|
||||
statistics.setSalesRecordingCount(Integer.valueOf(salesRecordingCountObj.toString()));
|
||||
} else {
|
||||
statistics.setSalesRecordingCount(0);
|
||||
}
|
||||
|
||||
// 计算销售人员平均时长
|
||||
if (statistics.getSalesRecordingCount() > 0 && statistics.getSalesTotalDuration() > 0) {
|
||||
BigDecimal salesAvgDuration = BigDecimal.valueOf(statistics.getSalesTotalDuration())
|
||||
.divide(BigDecimal.valueOf(statistics.getSalesRecordingCount()), 2, RoundingMode.HALF_UP);
|
||||
statistics.setSalesAvgDuration(salesAvgDuration);
|
||||
} else {
|
||||
statistics.setSalesAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
} else {
|
||||
// 如果没有找到对应的销售人员数据,设置为空
|
||||
statistics.setSalesId("");
|
||||
statistics.setSalesName("");
|
||||
statistics.setSalesTotalDuration(0L);
|
||||
statistics.setSalesRecordingCount(0);
|
||||
statistics.setSalesAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
// 查找该门店对应的项目统计数据
|
||||
String dealershipIdForProject = dealershipIdObj != null ? dealershipIdObj.toString() : "";
|
||||
Map<String, Object> projectStat = findProjectStatByDealership(projectStatsList, dealershipIdForProject);
|
||||
|
||||
if (projectStat != null) {
|
||||
// 设置项目信息
|
||||
Object projectIdObj = projectStat.get("project_id");
|
||||
statistics.setProjectId(projectIdObj != null ? projectIdObj.toString() : "");
|
||||
|
||||
Object projectNameObj = projectStat.get("project_name");
|
||||
statistics.setProjectName(projectNameObj != null ? projectNameObj.toString() : "未知项目");
|
||||
|
||||
// 设置项目录音统计数据
|
||||
Object projectTotalDurationObj = projectStat.get("total_duration_seconds");
|
||||
if (projectTotalDurationObj != null) {
|
||||
statistics.setProjectTotalDuration(Long.valueOf(projectTotalDurationObj.toString()));
|
||||
} else {
|
||||
statistics.setProjectTotalDuration(0L);
|
||||
}
|
||||
|
||||
Object projectRecordingCountObj = projectStat.get("recording_count");
|
||||
if (projectRecordingCountObj != null) {
|
||||
statistics.setProjectRecordingCount(Integer.valueOf(projectRecordingCountObj.toString()));
|
||||
} else {
|
||||
statistics.setProjectRecordingCount(0);
|
||||
}
|
||||
|
||||
// 计算项目平均时长
|
||||
if (statistics.getProjectRecordingCount() > 0 && statistics.getProjectTotalDuration() > 0) {
|
||||
BigDecimal projectAvgDuration = BigDecimal.valueOf(statistics.getProjectTotalDuration())
|
||||
.divide(BigDecimal.valueOf(statistics.getProjectRecordingCount()), 2, RoundingMode.HALF_UP);
|
||||
statistics.setProjectAvgDuration(projectAvgDuration);
|
||||
} else {
|
||||
statistics.setProjectAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
} else {
|
||||
// 如果没有找到对应的项目数据,设置为空
|
||||
statistics.setProjectId("");
|
||||
statistics.setProjectName("");
|
||||
statistics.setProjectTotalDuration(0L);
|
||||
statistics.setProjectRecordingCount(0);
|
||||
statistics.setProjectAvgDuration(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
// 设置统计日期和时间
|
||||
statistics.setStatisticsDate(date);
|
||||
statistics.setCreatedAt(LocalDateTime.now());
|
||||
statistics.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
// 保存统计记录
|
||||
save(statistics);
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据门店ID查找对应的销售人员统计数据
|
||||
* @param salesStatsList 销售人员统计列表
|
||||
* @param dealershipId 门店ID
|
||||
* @return 销售人员统计数据
|
||||
*/
|
||||
private Map<String, Object> findSalesStatByDealership(List<Map<String, Object>> salesStatsList, String dealershipId) {
|
||||
// 这里需要根据业务逻辑来确定门店和销售人员的对应关系
|
||||
// 由于原始数据中可能没有直接的关联关系,这里返回第一个销售人员数据作为示例
|
||||
// 实际使用时需要根据具体的业务逻辑来调整
|
||||
if (!salesStatsList.isEmpty()) {
|
||||
return salesStatsList.get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据门店ID查找对应的项目统计数据
|
||||
* @param projectStatsList 项目统计列表
|
||||
* @param dealershipId 门店ID
|
||||
* @return 项目统计数据
|
||||
*/
|
||||
private Map<String, Object> findProjectStatByDealership(List<Map<String, Object>> projectStatsList, String dealershipId) {
|
||||
// 这里需要根据业务逻辑来确定门店和项目的对应关系
|
||||
// 由于原始数据中可能没有直接的关联关系,这里返回第一个项目数据作为示例
|
||||
// 实际使用时需要根据具体的业务逻辑来调整
|
||||
if (!projectStatsList.isEmpty()) {
|
||||
return projectStatsList.get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int generateYesterdayStatistics() {
|
||||
LocalDate yesterday = LocalDate.now().minusDays(1);
|
||||
return generateStatisticsByDate(yesterday);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsByDateRange(LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.between(AudioManagementStatistics::getStatisticsDate, startDate, endDate)
|
||||
.orderByAsc(AudioManagementStatistics::getStatisticsDate)
|
||||
.orderByAsc(AudioManagementStatistics::getDealershipId);
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsByDealership(Long dealershipId, LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(AudioManagementStatistics::getDealershipId, dealershipId)
|
||||
.between(AudioManagementStatistics::getStatisticsDate, startDate, endDate)
|
||||
.orderByAsc(AudioManagementStatistics::getStatisticsDate);
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsBySales(String salesId, LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(AudioManagementStatistics::getSalesId, salesId)
|
||||
.between(AudioManagementStatistics::getStatisticsDate, startDate, endDate)
|
||||
.orderByAsc(AudioManagementStatistics::getStatisticsDate);
|
||||
return list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AudioManagementStatistics> getStatisticsByProject(String projectId, LocalDate startDate, LocalDate endDate) {
|
||||
LambdaQueryWrapper<AudioManagementStatistics> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(AudioManagementStatistics::getProjectId, projectId)
|
||||
.between(AudioManagementStatistics::getStatisticsDate, startDate, endDate)
|
||||
.orderByAsc(AudioManagementStatistics::getStatisticsDate);
|
||||
return list(queryWrapper);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user