|
|
@@ -1,5 +1,6 @@
|
|
|
package com.cyksj.web.controller.manage.form;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
@@ -16,6 +17,9 @@ import com.cyksj.model.entity.FormFieldsDefault;
|
|
|
import com.cyksj.model.entity.FormTemplate;
|
|
|
import com.cyksj.model.entity.QuestionnairePromotion;
|
|
|
import com.cyksj.model.views.FormFieldsDataView;
|
|
|
+import com.cyksj.model.views.FormQuestionnaireDataHeadView;
|
|
|
+import com.cyksj.model.views.FormQuestionnaireDataListView;
|
|
|
+import com.cyksj.model.views.FormSubmitDataView;
|
|
|
import com.cyksj.service.mange.form.FormService;
|
|
|
import com.cyksj.service.mange.form.QuestionnairePromotionService;
|
|
|
import com.ejlchina.searcher.BeanSearcher;
|
|
|
@@ -27,7 +31,13 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/*
|
|
|
*项目名: netflix
|
|
|
@@ -167,6 +177,109 @@ public class FormController {
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 用户问卷回答横向列表
|
|
|
+ */
|
|
|
+ @GetMapping("/get/question/resp/data/list")
|
|
|
+ public Result<FormQuestionnaireDataListView> getQuestionRespDataList(Long promotionId, Long startTime, Long endTime,
|
|
|
+ @RequestParam(required = false, defaultValue = "1") Integer start,
|
|
|
+ @RequestParam(required = false, defaultValue = "10") Integer limit) {
|
|
|
+ DateTime startDate = null;
|
|
|
+ DateTime endDate = null;
|
|
|
+ if (startTime != null) {
|
|
|
+ startDate = DateUtil.date(startTime);
|
|
|
+ }
|
|
|
+ if (endTime != null) {
|
|
|
+ endDate = DateUtil.date(endTime);
|
|
|
+ }
|
|
|
+ QuestionnairePromotion promotion = promotionService.getById(promotionId);
|
|
|
+ if (promotion == null) throw BusinessRuntimeException.getInstance("问卷不存在");
|
|
|
+ List<FormFields> formFields = formFieldsMapper.selectList(Wrappers.lambdaQuery(FormFields.class)
|
|
|
+ .eq(FormFields::getTemplateId, promotion.getTemplateId())
|
|
|
+ .eq(FormFields::getDeleted, true)
|
|
|
+ .eq(FormFields::getShowTable, 1)
|
|
|
+ .orderByAsc(FormFields::getSorted)
|
|
|
+ .orderByAsc(FormFields::getId));
|
|
|
+
|
|
|
+ String timeSql = StrUtil.EMPTY;
|
|
|
+ if (startDate != null) {
|
|
|
+ timeSql += String.format(" and fd.created_time >= '%s'", startDate);
|
|
|
+ }
|
|
|
+ if (endDate != null) {
|
|
|
+ timeSql += String.format(" and fd.created_time < '%s'", endDate);
|
|
|
+ }
|
|
|
+ MapBuilder builder = MapUtils.builder()
|
|
|
+ .field(FormSubmitDataView::getPromotionId, promotionId)
|
|
|
+ .orderBy(FormSubmitDataView::getSorted).asc()
|
|
|
+ .orderBy(FormSubmitDataView::getFieldsId).asc();
|
|
|
+ if (StrUtil.isNotBlank(timeSql)) {
|
|
|
+ builder.put("time_sql", timeSql);
|
|
|
+ }
|
|
|
+ List<FormSubmitDataView> formSubmitDataViews = beanSearcher.searchAll(FormSubmitDataView.class, builder.build());
|
|
|
+
|
|
|
+ FormQuestionnaireDataListView view = new FormQuestionnaireDataListView();
|
|
|
+ List<FormQuestionnaireDataHeadView> heads = formFields.stream().map(field -> {
|
|
|
+ FormQuestionnaireDataHeadView headView = new FormQuestionnaireDataHeadView();
|
|
|
+ headView.setFieldsId(field.getId());
|
|
|
+ headView.setFieldsName(field.getFieldsName());
|
|
|
+ headView.setTitle(field.getFieldsName());
|
|
|
+ headView.setDataIndex(getFieldDataIndex(field.getId()));
|
|
|
+ headView.setKey(getFieldDataIndex(field.getId()));
|
|
|
+ return headView;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ heads.add(buildHead("用户id", "userId"));
|
|
|
+ heads.add(buildHead("用户名", "nickname"));
|
|
|
+ heads.add(buildHead("提交时间", "createdTime"));
|
|
|
+ view.setHeads(heads);
|
|
|
+ view.setFields(heads.stream().filter(head -> head.getFieldsId() != null).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ Map<Long, List<FormSubmitDataView>> groupMap = formSubmitDataViews.stream()
|
|
|
+ .collect(Collectors.groupingBy(this::getSubmitGroupId, LinkedHashMap::new, Collectors.toList()));
|
|
|
+ List<List<FormSubmitDataView>> answerGroups = new ArrayList<>(groupMap.values());
|
|
|
+ List<List<FormSubmitDataView>> pageGroups = answerGroups.stream()
|
|
|
+ .sorted(Comparator.comparing(this::getSubmitTime, Comparator.nullsFirst(Date::compareTo)).reversed())
|
|
|
+ .skip((long) (Math.max(start, 1) - 1) * Math.max(limit, 1))
|
|
|
+ .limit(Math.max(limit, 1))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ pageGroups
|
|
|
+ .stream()
|
|
|
+ .forEach(answers -> {
|
|
|
+ Map<Long, List<FormSubmitDataView>> fieldsValueMap = answers.stream()
|
|
|
+ .filter(answer -> answer.getFieldsId() != null)
|
|
|
+ .collect(Collectors.groupingBy(FormSubmitDataView::getFieldsId));
|
|
|
+ Map<String, Object> row = new LinkedHashMap<>();
|
|
|
+ row.put("rowKey", getSubmitGroupId(answers.get(0)));
|
|
|
+ formFields.forEach(fields -> {
|
|
|
+ List<FormSubmitDataView> values = fieldsValueMap.get(fields.getId());
|
|
|
+ row.put(getFieldDataIndex(fields.getId()), CollUtil.isEmpty(values) ? StrUtil.EMPTY : values.get(0).getContent());
|
|
|
+ });
|
|
|
+ FormSubmitDataView submitInfo = answers.get(0);
|
|
|
+ Long userId = submitInfo.getUserId();
|
|
|
+ row.put("userId", userId == null ? "0" : userId.toString());
|
|
|
+ row.put("nickname", StrUtil.blankToDefault(submitInfo.getNickname(), "未知"));
|
|
|
+ Date submitTime = getSubmitTime(answers);
|
|
|
+ row.put("createdTime", submitTime == null ? StrUtil.EMPTY : DateUtil.format(submitTime, "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ rows.add(row);
|
|
|
+ });
|
|
|
+ view.setTotalCount(answerGroups.size());
|
|
|
+ view.setRows(rows);
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(view);
|
|
|
+ }
|
|
|
+
|
|
|
+ private FormQuestionnaireDataHeadView buildHead(String title, String dataIndex) {
|
|
|
+ FormQuestionnaireDataHeadView headView = new FormQuestionnaireDataHeadView();
|
|
|
+ headView.setFieldsName(title);
|
|
|
+ headView.setTitle(title);
|
|
|
+ headView.setDataIndex(dataIndex);
|
|
|
+ headView.setKey(dataIndex);
|
|
|
+ return headView;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getFieldDataIndex(Long fieldsId) {
|
|
|
+ return String.format("field_%s", fieldsId);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @param promotionId
|
|
|
*/
|
|
|
@@ -182,4 +295,19 @@ public class FormController {
|
|
|
}
|
|
|
formService.exportQuestionInfo(promotionId, startDate, endDate, response);
|
|
|
}
|
|
|
+
|
|
|
+ private Long getSubmitGroupId(FormSubmitDataView dataView) {
|
|
|
+ if (dataView.getGroupsId() != null) {
|
|
|
+ return dataView.getGroupsId();
|
|
|
+ }
|
|
|
+ return dataView.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Date getSubmitTime(List<FormSubmitDataView> answers) {
|
|
|
+ return answers.stream()
|
|
|
+ .map(FormSubmitDataView::getCreatedTime)
|
|
|
+ .filter(date -> date != null)
|
|
|
+ .max(Date::compareTo)
|
|
|
+ .orElse(null);
|
|
|
+ }
|
|
|
}
|