|
|
@@ -26,6 +26,9 @@ import org.dromara.hq.service.IHqSatDetailService;
|
|
|
import org.dromara.hq.domain.bo.HqSatSurveyQuestionBo;
|
|
|
import org.dromara.hq.domain.vo.HqSatSurveyQuestionVo;
|
|
|
import org.dromara.hq.domain.vo.HqSatDetailVo;
|
|
|
+import org.dromara.hq.domain.bo.HqSatOptionBo;
|
|
|
+import org.dromara.hq.domain.vo.HqSatOptionVo;
|
|
|
+import org.dromara.hq.service.IHqSatOptionService;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -58,6 +61,7 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
private final IHqSatSurveyQuestionService surveyQuestionService;
|
|
|
private final IHqSatRecordService recordService;
|
|
|
private final IHqSatDetailService detailService;
|
|
|
+ private final IHqSatOptionService optionService;
|
|
|
|
|
|
@Autowired
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
@@ -858,6 +862,7 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
|
|
|
// 构建返回数据
|
|
|
statistics.put("surveyId", surveyId);
|
|
|
+ statistics.put("score", survey.getScore());
|
|
|
statistics.put("surveyTitle", survey.getSurveyTitle());
|
|
|
statistics.put("surveyCode", survey.getSurveyCode());
|
|
|
statistics.put("totalParticipants", totalParticipants != null ? totalParticipants : 0);
|
|
|
@@ -953,15 +958,15 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
String optionStatsSql = """
|
|
|
SELECT
|
|
|
o.id as option_id,
|
|
|
- o.option_value,
|
|
|
- o.option_text,
|
|
|
+ o.option_score,
|
|
|
+ o.option_content,
|
|
|
COUNT(DISTINCT a.record_id) as select_count
|
|
|
- FROM hq_sat_question_option o
|
|
|
+ FROM hq_sat_option o
|
|
|
LEFT JOIN hq_sat_answer a ON o.id = a.option_id AND a.deleted = 0
|
|
|
LEFT JOIN hq_sat_record r ON a.record_id = r.id AND r.survey_id = ?
|
|
|
WHERE o.question_id = ? AND o.deleted = 0
|
|
|
- GROUP BY o.id, o.option_value, o.option_text
|
|
|
- ORDER BY o.option_value
|
|
|
+ GROUP BY o.id, o.option_score, o.option_content
|
|
|
+ ORDER BY o.option_score DESC
|
|
|
""";
|
|
|
|
|
|
java.util.List<java.util.Map<String, Object>> optionStats = jdbcTemplate.queryForList(optionStatsSql, surveyId, questionId);
|
|
|
@@ -979,13 +984,13 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
String textAnswersSql = """
|
|
|
SELECT
|
|
|
a.id,
|
|
|
- a.answer_value as answer_text,
|
|
|
- LENGTH(a.answer_value) as text_length,
|
|
|
+ a.remark as answer_text,
|
|
|
+ LENGTH(a.remark) as text_length,
|
|
|
DATE_FORMAT(r.submit_time, '%Y-%m-%d %H:%i:%s') as answer_time
|
|
|
FROM hq_sat_answer a
|
|
|
JOIN hq_sat_record r ON a.record_id = r.id
|
|
|
WHERE a.question_id = ? AND r.survey_id = ? AND a.answer_value IS NOT NULL
|
|
|
- AND a.deleted = 0 AND LENGTH(a.answer_value) > 0
|
|
|
+ AND a.deleted = 0 AND LENGTH(a.remark) > 0
|
|
|
ORDER BY r.submit_time DESC
|
|
|
LIMIT 10
|
|
|
""";
|
|
|
@@ -1040,13 +1045,38 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
String status = (String) query.get("status");
|
|
|
String userName = (String) query.get("userName");
|
|
|
|
|
|
- // 构建查询SQL
|
|
|
+ // 查询问卷信息,获取目标群体类型
|
|
|
+ String targetGroupSql = "SELECT target_group FROM hq_sat_survey WHERE id = ? AND deleted = 0";
|
|
|
+ String targetGroup = jdbcTemplate.queryForObject(targetGroupSql, String.class, surveyId);
|
|
|
+
|
|
|
+ if (targetGroup == null) {
|
|
|
+ throw new ServiceException("问卷不存在或已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建查询SQL - 从用户表查询符合条件的用户,并关联答题记录表
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
- sql.append("SELECT r.*, u.nick_name as userName, d.dept_name as deptName ");
|
|
|
- sql.append("FROM hq_sat_record r ");
|
|
|
- sql.append("LEFT JOIN sys_user u ON r.user_id = u.user_id ");
|
|
|
+ sql.append("SELECT u.user_id, u.nick_name as userName, d.dept_name as deptName, ");
|
|
|
+ sql.append("rec.id, rec.survey_id, rec.submit_time, rec.total_score, rec.create_time ");
|
|
|
+ sql.append("FROM sys_user u ");
|
|
|
sql.append("LEFT JOIN sys_dept d ON u.dept_id = d.dept_id ");
|
|
|
- sql.append("WHERE r.survey_id = ? AND r.deleted = 0 ");
|
|
|
+ sql.append("LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id ");
|
|
|
+ sql.append("LEFT JOIN sys_role r ON ur.role_id = r.role_id ");
|
|
|
+ sql.append("LEFT JOIN hq_sat_record rec ON u.user_id = rec.user_id AND rec.survey_id = ? AND rec.deleted = 0 ");
|
|
|
+ sql.append("WHERE u.del_flag = '0' ");
|
|
|
+
|
|
|
+ // 根据目标群体过滤用户
|
|
|
+ sql.append("AND (");
|
|
|
+ if ("1".equals(targetGroup)) {
|
|
|
+ // 学生群体
|
|
|
+ sql.append("r.role_key = 'student'");
|
|
|
+ } else if ("2".equals(targetGroup)) {
|
|
|
+ // 教职工群体
|
|
|
+ sql.append("r.role_key = 'teacher'");
|
|
|
+ } else if ("3".equals(targetGroup)) {
|
|
|
+ // 全体群体(学生和教职工)
|
|
|
+ sql.append("(r.role_key = 'student' OR r.role_key = 'teacher')");
|
|
|
+ }
|
|
|
+ sql.append(") ");
|
|
|
|
|
|
java.util.List<Object> params = new java.util.ArrayList<>();
|
|
|
params.add(surveyId);
|
|
|
@@ -1054,9 +1084,9 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
// 添加状态过滤
|
|
|
if (status != null && !status.isEmpty()) {
|
|
|
if ("1".equals(status)) {
|
|
|
- sql.append("AND r.submit_time IS NOT NULL ");
|
|
|
+ sql.append("AND rec.submit_time IS NOT NULL ");
|
|
|
} else if ("0".equals(status)) {
|
|
|
- sql.append("AND r.submit_time IS NULL ");
|
|
|
+ sql.append("AND (rec.submit_time IS NULL OR rec.id IS NULL) ");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1066,30 +1096,40 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
params.add("%" + userName + "%");
|
|
|
}
|
|
|
|
|
|
- sql.append("ORDER BY r.create_time DESC ");
|
|
|
-
|
|
|
- // 分页查询
|
|
|
- int offset = (pageNum - 1) * pageSize;
|
|
|
- sql.append("LIMIT ? OFFSET ?");
|
|
|
- params.add(pageSize);
|
|
|
- params.add(offset);
|
|
|
-
|
|
|
- java.util.List<java.util.Map<String, Object>> records = jdbcTemplate.queryForList(sql.toString(), params.toArray());
|
|
|
+ sql.append("GROUP BY u.user_id, rec.id ");
|
|
|
+ sql.append("ORDER BY CASE WHEN rec.submit_time IS NOT NULL THEN 0 ELSE 1 END, rec.submit_time DESC, u.user_id ");
|
|
|
|
|
|
// 查询总数
|
|
|
StringBuilder countSql = new StringBuilder();
|
|
|
- countSql.append("SELECT COUNT(*) FROM hq_sat_record r ");
|
|
|
- countSql.append("LEFT JOIN sys_user u ON r.user_id = u.user_id ");
|
|
|
- countSql.append("WHERE r.survey_id = ? AND r.deleted = 0 ");
|
|
|
+ countSql.append("SELECT COUNT(DISTINCT u.user_id) ");
|
|
|
+ countSql.append("FROM sys_user u ");
|
|
|
+ countSql.append("LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id ");
|
|
|
+ countSql.append("LEFT JOIN sys_role r ON ur.role_id = r.role_id ");
|
|
|
+ countSql.append("LEFT JOIN hq_sat_record rec ON u.user_id = rec.user_id AND rec.survey_id = ? AND rec.deleted = 0 ");
|
|
|
+ countSql.append("WHERE u.del_flag = '0' ");
|
|
|
+
|
|
|
+ // 根据目标群体过滤用户
|
|
|
+ countSql.append("AND (");
|
|
|
+ if ("1".equals(targetGroup)) {
|
|
|
+ // 学生群体
|
|
|
+ countSql.append("r.role_key = 'student'");
|
|
|
+ } else if ("2".equals(targetGroup)) {
|
|
|
+ // 教职工群体
|
|
|
+ countSql.append("r.role_key = 'teacher'");
|
|
|
+ } else if ("3".equals(targetGroup)) {
|
|
|
+ // 全体群体(学生和教职工)
|
|
|
+ countSql.append("(r.role_key = 'student' OR r.role_key = 'teacher')");
|
|
|
+ }
|
|
|
+ countSql.append(") ");
|
|
|
|
|
|
java.util.List<Object> countParams = new java.util.ArrayList<>();
|
|
|
countParams.add(surveyId);
|
|
|
|
|
|
if (status != null && !status.isEmpty()) {
|
|
|
if ("1".equals(status)) {
|
|
|
- countSql.append("AND r.submit_time IS NOT NULL ");
|
|
|
+ countSql.append("AND rec.submit_time IS NOT NULL ");
|
|
|
} else if ("0".equals(status)) {
|
|
|
- countSql.append("AND r.submit_time IS NULL ");
|
|
|
+ countSql.append("AND (rec.submit_time IS NULL OR rec.id IS NULL) ");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1100,13 +1140,42 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
|
|
|
Integer total = jdbcTemplate.queryForObject(countSql.toString(), Integer.class, countParams.toArray());
|
|
|
|
|
|
- // 处理记录数据,添加状态和持续时间字段
|
|
|
+ // 分页查询
|
|
|
+ int offset = (pageNum - 1) * pageSize;
|
|
|
+ sql.append("LIMIT ? OFFSET ?");
|
|
|
+ params.add(pageSize);
|
|
|
+ params.add(offset);
|
|
|
+
|
|
|
+ java.util.List<java.util.Map<String, Object>> records = jdbcTemplate.queryForList(sql.toString(), params.toArray());
|
|
|
+
|
|
|
+ // 查询问卷题目信息,用于前端显示
|
|
|
+ String questionsSql = "SELECT q.id, q.question_title, q.question_type FROM hq_sat_question q " +
|
|
|
+ "JOIN hq_sat_survey_question sq ON q.id = sq.question_id " +
|
|
|
+ "WHERE sq.survey_id = ? AND q.deleted = 0 AND sq.deleted = 0 " +
|
|
|
+ "ORDER BY sq.sort_order";
|
|
|
+
|
|
|
+ List<Map<String, Object>> questionsList = jdbcTemplate.queryForList(questionsSql, surveyId);
|
|
|
+ Map<Long, Map<String, Object>> questionsMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 构建题目ID到题目信息的映射
|
|
|
+ for (Map<String, Object> question : questionsList) {
|
|
|
+ Long questionId = Long.valueOf(question.get("id").toString());
|
|
|
+ questionsMap.put(questionId, question);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理记录数据,添加状态、持续时间和答题详情字段
|
|
|
for (java.util.Map<String, Object> record : records) {
|
|
|
- // 设置状态
|
|
|
- record.put("status", record.get("submit_time") != null ? "1" : "0");
|
|
|
+ // 设置状态 - 已提交或未提交
|
|
|
+ boolean hasSubmitted = record.get("submit_time") != null;
|
|
|
+ record.put("status", hasSubmitted ? "1" : "0");
|
|
|
+
|
|
|
+ // 设置得分 - 已提交显示实际得分,未提交显示0
|
|
|
+ if (!hasSubmitted) {
|
|
|
+ record.put("total_score", 0);
|
|
|
+ }
|
|
|
|
|
|
- // 计算持续时间(暂时使用随机值,后续可以实现真实计算)
|
|
|
- record.put("duration", 300 + (int)(Math.random() * 600)); // 5-15分钟
|
|
|
+ // 计算持续时间(已提交的才计算,未提交显示0)
|
|
|
+ record.put("duration", hasSubmitted ? (300 + (int)(Math.random() * 600)) : 0); // 已提交的记录显示5-15分钟,未提交的显示0
|
|
|
|
|
|
// 格式化时间
|
|
|
if (record.get("create_time") != null) {
|
|
|
@@ -1115,18 +1184,114 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
if (record.get("submit_time") != null) {
|
|
|
record.put("endTime", record.get("submit_time").toString());
|
|
|
}
|
|
|
+
|
|
|
+ // 获取答题详情(仅对已提交的记录)
|
|
|
+ if (hasSubmitted && record.get("id") != null) {
|
|
|
+ Long recordId = Long.valueOf(record.get("id").toString());
|
|
|
+ List<Map<String, Object>> answerDetails = getAnswersFromSatAnswerTable(recordId);
|
|
|
+
|
|
|
+ // 将答案与题目信息关联,组织成前端需要的格式
|
|
|
+ List<Map<String, Object>> formattedAnswers = new ArrayList<>();
|
|
|
+ Map<Long, List<Map<String, Object>>> questionAnswers = new HashMap<>();
|
|
|
+
|
|
|
+ // 先按题目ID分组答案
|
|
|
+ for (Map<String, Object> answer : answerDetails) {
|
|
|
+ Long questionId = Long.valueOf(answer.get("questionId").toString());
|
|
|
+
|
|
|
+ if (!questionAnswers.containsKey(questionId)) {
|
|
|
+ questionAnswers.put(questionId, new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ questionAnswers.get(questionId).add(answer);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 为每个题目创建一个答案记录
|
|
|
+ for (Long questionId : questionAnswers.keySet()) {
|
|
|
+ List<Map<String, Object>> answers = questionAnswers.get(questionId);
|
|
|
+
|
|
|
+ // 获取题目基本信息
|
|
|
+ Map<String, Object> questionInfo = questionsMap.get(questionId);
|
|
|
+ if (questionInfo == null) continue;
|
|
|
+
|
|
|
+ Map<String, Object> formattedAnswer = new HashMap<>();
|
|
|
+ formattedAnswer.put("id", questionId);
|
|
|
+ formattedAnswer.put("questionId", questionId);
|
|
|
+ formattedAnswer.put("questionTitle", questionInfo.get("question_title"));
|
|
|
+ formattedAnswer.put("questionType", String.valueOf(questionInfo.get("question_type")));
|
|
|
+
|
|
|
+ // 设置分数(汇总所有选项的分数)
|
|
|
+ double totalScore = 0;
|
|
|
+ for (Map<String, Object> answer : answers) {
|
|
|
+ String scoreStr = (String)answer.get("answerScore");
|
|
|
+ if (scoreStr != null) {
|
|
|
+ try {
|
|
|
+ totalScore += Double.parseDouble(scoreStr);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.warn("解析答题分数失败: {}", scoreStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ formattedAnswer.put("score", totalScore);
|
|
|
+
|
|
|
+ String questionType = (String)formattedAnswer.get("questionType");
|
|
|
+
|
|
|
+ // 处理问答题
|
|
|
+ if ("3".equals(questionType)) {
|
|
|
+ // 取第一个答案的值作为问答题答案
|
|
|
+ if (!answers.isEmpty()) {
|
|
|
+ formattedAnswer.put("answerValue", answers.get(0).get("remark"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 处理单选题和多选题
|
|
|
+ else if ("1".equals(questionType) || "2".equals(questionType)) {
|
|
|
+ List<Map<String, Object>> options = new ArrayList<>();
|
|
|
+
|
|
|
+ // 获取所有选中的选项
|
|
|
+ for (Map<String, Object> answer : answers) {
|
|
|
+ // 只处理有选项ID的答案
|
|
|
+ if (answer.get("optionId") != null) {
|
|
|
+ Map<String, Object> option = new HashMap<>();
|
|
|
+ log.info("answer-zlwu12: {}", answer);
|
|
|
+ option.put("optionId", answer.get("optionId"));
|
|
|
+ option.put("optionValue", answer.get("optionValue"));
|
|
|
+ option.put("optionText", answer.get("optionText"));
|
|
|
+ // option.put("optionScore", answer.get("answerScore"));
|
|
|
+ option.put("isSelected", true);
|
|
|
+ options.add(option);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ formattedAnswer.put("options", options);
|
|
|
+
|
|
|
+ // 设置answerValue为所有选中选项值的拼接
|
|
|
+ StringBuilder answerValue = new StringBuilder();
|
|
|
+ for (Map<String, Object> option : options) {
|
|
|
+ if (answerValue.length() > 0) {
|
|
|
+ answerValue.append(",");
|
|
|
+ }
|
|
|
+ answerValue.append(option.get("optionValue"));
|
|
|
+ }
|
|
|
+ formattedAnswer.put("answerValue", answerValue.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ formattedAnswers.add(formattedAnswer);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将答题详情添加到记录中
|
|
|
+ record.put("answers", formattedAnswers);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
java.util.Map<String, Object> result = new java.util.HashMap<>();
|
|
|
result.put("rows", records);
|
|
|
result.put("total", total != null ? total : 0);
|
|
|
|
|
|
- log.info("获取答题记录完成,总数: {}, 当前页: {}", total, records.size());
|
|
|
+ log.info("获取符合问卷面向群体的所有用户信息完成,总数: {}, 当前页: {}", total, records.size());
|
|
|
return result;
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- log.error("获取答题记录失败,问卷ID: {}, 错误: {}", surveyId, e.getMessage(), e);
|
|
|
- throw new ServiceException("获取答题记录失败: " + e.getMessage());
|
|
|
+ log.error("获取问卷用户记录失败,问卷ID: {}, 错误: {}", surveyId, e.getMessage(), e);
|
|
|
+ throw new ServiceException("获取问卷用户记录失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1139,7 +1304,6 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
@Override
|
|
|
public java.util.Map<String, Object> getAnswerRecordDetail(Long recordId) {
|
|
|
log.info("获取答题记录详情,记录ID: {}", recordId);
|
|
|
-
|
|
|
try {
|
|
|
// 查询答题记录基本信息
|
|
|
String recordSql = "SELECT r.*, u.nick_name as userName, d.dept_name as deptName " +
|
|
|
@@ -1147,57 +1311,107 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
"LEFT JOIN sys_user u ON r.user_id = u.user_id " +
|
|
|
"LEFT JOIN sys_dept d ON u.dept_id = d.dept_id " +
|
|
|
"WHERE r.id = ? AND r.deleted = 0";
|
|
|
-
|
|
|
java.util.List<java.util.Map<String, Object>> recordList = jdbcTemplate.queryForList(recordSql, recordId);
|
|
|
if (recordList.isEmpty()) {
|
|
|
throw new ServiceException("答题记录不存在", 404);
|
|
|
}
|
|
|
-
|
|
|
java.util.Map<String, Object> record = recordList.get(0);
|
|
|
-
|
|
|
- // 查询答题详情
|
|
|
+ Long surveyId = record.get("survey_id") != null ? Long.valueOf(record.get("survey_id").toString()) : null;
|
|
|
+ // 1. 获取问卷题目顺序
|
|
|
+ List<HqSatSurveyQuestionVo> questionVos = surveyQuestionService.getSurveyQuestionsWithDetail(surveyId);
|
|
|
+ // 题目顺序Map
|
|
|
+ Map<Long, Integer> questionOrderMap = new HashMap<>();
|
|
|
+ for (int i = 0; i < questionVos.size(); i++) {
|
|
|
+ questionOrderMap.put(questionVos.get(i).getQuestionId(), i);
|
|
|
+ }
|
|
|
+ // 2. 查询所有选项顺序
|
|
|
+ Map<Long, List<HqSatOptionVo>> questionOptionsMap = new HashMap<>();
|
|
|
+ for (HqSatSurveyQuestionVo qvo : questionVos) {
|
|
|
+ HqSatOptionBo bo = new HqSatOptionBo();
|
|
|
+ bo.setQuestionId(qvo.getQuestionId());
|
|
|
+ bo.setDeleted("0");
|
|
|
+ List<HqSatOptionVo> options = optionService.queryList(bo);
|
|
|
+ options.sort((a, b) -> {
|
|
|
+ if (a.getOptionOrder() == null && b.getOptionOrder() == null) return 0;
|
|
|
+ if (a.getOptionOrder() == null) return 1;
|
|
|
+ if (b.getOptionOrder() == null) return -1;
|
|
|
+ return a.getOptionOrder().compareTo(b.getOptionOrder());
|
|
|
+ });
|
|
|
+ questionOptionsMap.put(qvo.getQuestionId(), options);
|
|
|
+ }
|
|
|
+ // 3. 查询答题详情
|
|
|
String answerSql = "SELECT a.question_id, a.question_type, a.answer_value, " +
|
|
|
- "a.option_id, a.option_value, a.option_text, a.answer_score, " +
|
|
|
+ "a.option_id, a.option_value, a.option_text, a.answer_score, a.remark, " +
|
|
|
"q.question_title " +
|
|
|
"FROM hq_sat_answer a " +
|
|
|
"LEFT JOIN hq_sat_question q ON a.question_id = q.id " +
|
|
|
- "WHERE a.record_id = ? AND a.deleted = 0 " +
|
|
|
- "ORDER BY a.question_id, a.option_id";
|
|
|
-
|
|
|
+ "WHERE a.record_id = ? AND a.deleted = 0 ";
|
|
|
java.util.List<java.util.Map<String, Object>> answers = jdbcTemplate.queryForList(answerSql, recordId);
|
|
|
-
|
|
|
- // 按题目分组答案
|
|
|
- java.util.Map<Long, java.util.Map<String, Object>> questionAnswerMap = new java.util.HashMap<>();
|
|
|
- for (java.util.Map<String, Object> answer : answers) {
|
|
|
- Long questionId = Long.valueOf(answer.get("question_id").toString());
|
|
|
-
|
|
|
- if (!questionAnswerMap.containsKey(questionId)) {
|
|
|
- java.util.Map<String, Object> questionAnswer = new java.util.HashMap<>();
|
|
|
- questionAnswer.put("id", questionId);
|
|
|
- questionAnswer.put("questionId", questionId);
|
|
|
- questionAnswer.put("questionTitle", answer.get("question_title"));
|
|
|
- questionAnswer.put("questionType", answer.get("question_type"));
|
|
|
- questionAnswer.put("answerValue", answer.get("answer_value"));
|
|
|
- questionAnswer.put("score", answer.get("answer_score"));
|
|
|
- questionAnswer.put("options", new java.util.ArrayList<java.util.Map<String, Object>>());
|
|
|
- questionAnswerMap.put(questionId, questionAnswer);
|
|
|
+ // 按题目分组
|
|
|
+ Map<Long, List<Map<String, Object>>> answerGroup = new HashMap<>();
|
|
|
+ for (Map<String, Object> answer : answers) {
|
|
|
+ Long qid = Long.valueOf(answer.get("question_id").toString());
|
|
|
+ answerGroup.computeIfAbsent(qid, k -> new ArrayList<>()).add(answer);
|
|
|
+ }
|
|
|
+ // 组装answers,按题目顺序
|
|
|
+ List<Map<String, Object>> resultAnswers = new ArrayList<>();
|
|
|
+ for (HqSatSurveyQuestionVo qvo : questionVos) {
|
|
|
+ Long qid = qvo.getQuestionId();
|
|
|
+ List<Map<String, Object>> answerList = answerGroup.getOrDefault(qid, new ArrayList<>());
|
|
|
+ Map<String, Object> answerObj = new HashMap<>();
|
|
|
+ answerObj.put("id", qid);
|
|
|
+ answerObj.put("questionId", qid);
|
|
|
+ answerObj.put("questionTitle", qvo.getQuestionTitle());
|
|
|
+ answerObj.put("questionType", qvo.getQuestionType());
|
|
|
+ double totalScore = 0;
|
|
|
+ for (Map<String, Object> a : answerList) {
|
|
|
+ Object scoreStr = a.get("answer_score");
|
|
|
+ if (scoreStr != null) {
|
|
|
+ try { totalScore += Double.parseDouble(scoreStr.toString()); } catch (Exception ignore) {}
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 如果是选择题,添加选项信息
|
|
|
- if (answer.get("option_id") != null) {
|
|
|
- java.util.Map<String, Object> option = new java.util.HashMap<>();
|
|
|
- option.put("optionId", answer.get("option_id"));
|
|
|
- option.put("optionValue", answer.get("option_value"));
|
|
|
- option.put("optionText", answer.get("option_text"));
|
|
|
- option.put("isSelected", true); // 查询出来的都是已选择的选项
|
|
|
-
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- java.util.List<java.util.Map<String, Object>> options =
|
|
|
- (java.util.List<java.util.Map<String, Object>>) questionAnswerMap.get(questionId).get("options");
|
|
|
- options.add(option);
|
|
|
+ answerObj.put("score", totalScore);
|
|
|
+ // 问答题
|
|
|
+ if ("3".equals(qvo.getQuestionType())) {
|
|
|
+ String remark = answerList.isEmpty() ? "" : (answerList.get(0).get("remark") != null ? answerList.get(0).get("remark").toString() : "");
|
|
|
+ answerObj.put("answerValue", remark);
|
|
|
+ answerObj.put("options", new ArrayList<>());
|
|
|
+ } else if ("1".equals(qvo.getQuestionType()) || "2".equals(qvo.getQuestionType())) {
|
|
|
+ // 选择题,组装选项,按optionOrder排序
|
|
|
+ List<HqSatOptionVo> allOptions = questionOptionsMap.getOrDefault(qid, new ArrayList<>());
|
|
|
+ List<Map<String, Object>> options = new ArrayList<>();
|
|
|
+ for (HqSatOptionVo opt : allOptions) {
|
|
|
+ Map<String, Object> optMap = new HashMap<>();
|
|
|
+ optMap.put("optionId", opt.getId());
|
|
|
+ optMap.put("optionValue", opt.getOptionContent());
|
|
|
+ optMap.put("optionText", opt.getOptionContent());
|
|
|
+ optMap.put("optionScore", opt.getOptionScore());
|
|
|
+ optMap.put("isSelected", false);
|
|
|
+ // 检查是否被选中
|
|
|
+ for (Map<String, Object> a : answerList) {
|
|
|
+ if (a.get("option_id") != null && opt.getId().toString().equals(a.get("option_id").toString())) {
|
|
|
+ optMap.put("isSelected", true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ options.add(optMap);
|
|
|
+ }
|
|
|
+ answerObj.put("options", options);
|
|
|
+ // answerValue为所有选中optionValue的拼接
|
|
|
+ StringBuilder answerValue = new StringBuilder();
|
|
|
+ for (Map<String, Object> opt : options) {
|
|
|
+ if (Boolean.TRUE.equals(opt.get("isSelected"))) {
|
|
|
+ if (answerValue.length() > 0) answerValue.append(",");
|
|
|
+ answerValue.append(opt.get("optionValue"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ answerObj.put("answerValue", answerValue.toString());
|
|
|
+ } else {
|
|
|
+ answerObj.put("options", new ArrayList<>());
|
|
|
+ answerObj.put("answerValue", "");
|
|
|
}
|
|
|
+ resultAnswers.add(answerObj);
|
|
|
}
|
|
|
-
|
|
|
// 构建返回结果
|
|
|
java.util.Map<String, Object> result = new java.util.HashMap<>();
|
|
|
result.put("id", record.get("id"));
|
|
|
@@ -1210,11 +1424,9 @@ public class HqSatSurveyServiceImpl implements IHqSatSurveyService {
|
|
|
result.put("duration", 300 + (int)(Math.random() * 600)); // 暂时使用随机值
|
|
|
result.put("totalScore", record.get("total_score"));
|
|
|
result.put("status", record.get("submit_time") != null ? "1" : "0");
|
|
|
- result.put("answers", new java.util.ArrayList<>(questionAnswerMap.values()));
|
|
|
-
|
|
|
- log.info("获取答题记录详情完成,记录ID: {}, 答案数量: {}", recordId, questionAnswerMap.size());
|
|
|
+ result.put("answers", resultAnswers);
|
|
|
+ log.info("获取答题记录详情完成,记录ID: {}, 答案数量: {}", recordId, resultAnswers.size());
|
|
|
return result;
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
log.error("获取答题记录详情失败,记录ID: {}, 错误: {}", recordId, e.getMessage(), e);
|
|
|
throw new ServiceException("获取答题记录详情失败: " + e.getMessage());
|