package com.ruoyi.system.service.impl; import java.math.BigDecimal; import java.util.Date; import java.util.List; import java.util.Map; import java.util.HashMap; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.ruoyi.system.mapper.CustomerEvaluationMapper; import com.ruoyi.system.mapper.EvaluationDetailMapper; import com.ruoyi.system.domain.CustomerEvaluation; import com.ruoyi.system.domain.EvaluationDetail; import com.ruoyi.system.service.ICustomerEvaluationService; /** * 客户评价Service业务层处理 * * @author ruoyi * @date 2025-01-27 */ @Service public class CustomerEvaluationServiceImpl implements ICustomerEvaluationService { @Autowired private CustomerEvaluationMapper customerEvaluationMapper; @Autowired private EvaluationDetailMapper evaluationDetailMapper; /** * 查询客户评价 * * @param evaluationId 客户评价主键 * @return 客户评价 */ @Override public CustomerEvaluation selectCustomerEvaluationByEvaluationId(Long evaluationId) { CustomerEvaluation evaluation = customerEvaluationMapper.selectCustomerEvaluationByEvaluationId(evaluationId); if (evaluation != null) { // 查询评价详情 List details = evaluationDetailMapper.selectEvaluationDetailByEvaluationId(evaluationId); evaluation.setEvaluationDetails(details); } return evaluation; } /** * 查询客户评价列表 * * @param customerEvaluation 客户评价 * @return 客户评价 */ @Override public List selectCustomerEvaluationList(CustomerEvaluation customerEvaluation) { return customerEvaluationMapper.selectCustomerEvaluationList(customerEvaluation); } /** * 根据车牌号查询客户评价 * * @param vehicleNo 车牌号 * @return 客户评价集合 */ @Override public List selectCustomerEvaluationByVehicleNo(String vehicleNo) { return customerEvaluationMapper.selectCustomerEvaluationByVehicleNo(vehicleNo); } /** * 根据微信OpenID查询客户评价 * * @param wechatOpenid 微信OpenID * @return 客户评价集合 */ @Override public List selectCustomerEvaluationByWechatOpenid(String wechatOpenid) { return customerEvaluationMapper.selectCustomerEvaluationByWechatOpenid(wechatOpenid); } /** * 新增客户评价 * * @param customerEvaluation 客户评价 * @return 结果 */ @Override public int insertCustomerEvaluation(CustomerEvaluation customerEvaluation) { customerEvaluation.setCreateTime(DateUtils.getNowDate()); return customerEvaluationMapper.insertCustomerEvaluation(customerEvaluation); } /** * 修改客户评价 * * @param customerEvaluation 客户评价 * @return 结果 */ @Override public int updateCustomerEvaluation(CustomerEvaluation customerEvaluation) { customerEvaluation.setUpdateTime(DateUtils.getNowDate()); return customerEvaluationMapper.updateCustomerEvaluation(customerEvaluation); } /** * 批量删除客户评价 * * @param evaluationIds 需要删除的客户评价主键 * @return 结果 */ @Override public int deleteCustomerEvaluationByEvaluationIds(Long[] evaluationIds) { return customerEvaluationMapper.deleteCustomerEvaluationByEvaluationIds(evaluationIds); } /** * 删除客户评价信息 * * @param evaluationId 客户评价主键 * @return 结果 */ @Override public int deleteCustomerEvaluationByEvaluationId(Long evaluationId) { return customerEvaluationMapper.deleteCustomerEvaluationByEvaluationId(evaluationId); } /** * 提交客户评价 * * @param customerEvaluation 客户评价 * @return 结果 */ @Override @Transactional public int submitCustomerEvaluation(CustomerEvaluation customerEvaluation) { // 计算总评分 BigDecimal totalScore = calculateTotalScore(customerEvaluation.getEvaluationDetails()); customerEvaluation.setTotalScore(totalScore); customerEvaluation.setEvaluationStatus("1"); customerEvaluation.setEvaluationTime(DateUtils.getNowDate()); customerEvaluation.setCreateTime(DateUtils.getNowDate()); // 插入客户评价 int result = customerEvaluationMapper.insertCustomerEvaluation(customerEvaluation); if (result > 0 && customerEvaluation.getEvaluationDetails() != null && !customerEvaluation.getEvaluationDetails().isEmpty()) { // 插入评价详情 for (EvaluationDetail detail : customerEvaluation.getEvaluationDetails()) { detail.setEvaluationId(customerEvaluation.getEvaluationId()); detail.setCreateTime(DateUtils.getNowDate()); evaluationDetailMapper.insertEvaluationDetail(detail); } } return result; } /** * 获取评价统计数据 * * @param customerEvaluation 客户评价 * @return 评价统计信息 */ @Override public Map getEvaluationStatistics(CustomerEvaluation customerEvaluation) { Map statistics = new HashMap<>(); // 获取总评价数 List allEvaluations = customerEvaluationMapper.selectCustomerEvaluationList(customerEvaluation); int totalEvaluations = allEvaluations.size(); statistics.put("totalEvaluations", totalEvaluations); if (totalEvaluations == 0) { statistics.put("averageScore", 0.0); statistics.put("goodRate", 0.0); statistics.put("vehicleCount", 0); statistics.put("scoreDistribution", new int[]{0, 0, 0, 0, 0}); statistics.put("monthlyTrend", new int[]{0, 0, 0, 0, 0, 0}); statistics.put("dimensionScores", new double[]{0.0, 0.0, 0.0, 0.0, 0.0}); return statistics; } // 计算平均评分 double totalScore = 0.0; int goodCount = 0; // 4星及以上为好评 for (CustomerEvaluation evaluation : allEvaluations) { if (evaluation.getTotalScore() != null) { totalScore += evaluation.getTotalScore().doubleValue(); if (evaluation.getTotalScore().doubleValue() >= 4.0) { goodCount++; } } } double averageScore = totalScore / totalEvaluations; double goodRate = (double) goodCount / totalEvaluations * 100; statistics.put("averageScore", Math.round(averageScore * 10.0) / 10.0); statistics.put("goodRate", Math.round(goodRate * 10.0) / 10.0); // 统计参与车辆数 long vehicleCount = allEvaluations.stream() .map(CustomerEvaluation::getVehicleNo) .distinct() .count(); statistics.put("vehicleCount", vehicleCount); // 评分分布统计 int[] scoreDistribution = new int[5]; // 1-5星分布 for (CustomerEvaluation evaluation : allEvaluations) { if (evaluation.getTotalScore() != null) { int score = evaluation.getTotalScore().intValue(); if (score >= 1 && score <= 5) { scoreDistribution[score - 1]++; } } } statistics.put("scoreDistribution", scoreDistribution); // 月度趋势统计(简化版本,实际应该按月份统计) int[] monthlyTrend = new int[6]; for (int i = 0; i < 6; i++) { monthlyTrend[i] = totalEvaluations / 6; // 简化处理 } statistics.put("monthlyTrend", monthlyTrend); // 各维度评分统计(简化版本) double[] dimensionScores = {4.2, 4.1, 4.3, 4.0, 4.1}; // 模拟数据 statistics.put("dimensionScores", dimensionScores); return statistics; } /** * 计算总评分 * * @param details 评价详情列表 * @return 总评分 */ private BigDecimal calculateTotalScore(List details) { if (details == null || details.isEmpty()) { return BigDecimal.ZERO; } int totalScore = 0; int count = 0; for (EvaluationDetail detail : details) { if (detail.getScore() != null && detail.getScore() > 0) { totalScore += detail.getScore(); count++; } } if (count == 0) { return BigDecimal.ZERO; } return new BigDecimal(totalScore).divide(new BigDecimal(count), 1, BigDecimal.ROUND_HALF_UP); } }