|
@@ -1,7 +1,9 @@
|
|
|
package com.gis.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.gis.common.util.Result;
|
|
|
+import com.gis.domain.dto.AnswerDto;
|
|
|
import com.gis.domain.dto.CountAnswerDto;
|
|
|
import com.gis.domain.po.CountAnswerEntity;
|
|
|
import com.gis.domain.po.QuestionGroupEntity;
|
|
@@ -16,9 +18,7 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.DecimalFormat;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -42,15 +42,25 @@ public class CountAnswerServiceImpl extends IBaseServiceImpl<CountAnswerEntity,
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public Result<CountAnswerEntity> saveEntity(List<CountAnswerDto> param) {
|
|
|
+ public Result<CountAnswerEntity> saveEntity(CountAnswerDto param) {
|
|
|
|
|
|
- for (CountAnswerDto countAnswerDto : param) {
|
|
|
+ String phone = param.getPhone();
|
|
|
+ Long questionGroupId = param.getQuestionGroupId();
|
|
|
+ String realName = param.getRealName();
|
|
|
+
|
|
|
+ List<AnswerDto> answer = param.getAnswer();
|
|
|
+
|
|
|
+ for (AnswerDto dto: answer) {
|
|
|
CountAnswerEntity entity = new CountAnswerEntity();
|
|
|
- BeanUtils.copyProperties(countAnswerDto, entity);
|
|
|
- this.save(entity);
|
|
|
- }
|
|
|
+ entity.setNum(dto.getNum());
|
|
|
+ entity.setAnswer(dto.getAnswer().toUpperCase());
|
|
|
|
|
|
+ entity.setQuestionGroupId(questionGroupId);
|
|
|
+ entity.setPhone(phone);
|
|
|
+ entity.setRealName(realName);
|
|
|
|
|
|
+ this.save(entity);
|
|
|
+ }
|
|
|
return Result.success();
|
|
|
|
|
|
}
|
|
@@ -60,86 +70,73 @@ public class CountAnswerServiceImpl extends IBaseServiceImpl<CountAnswerEntity,
|
|
|
* @param type 0:百分比, 1:计数
|
|
|
* @return
|
|
|
*/
|
|
|
- @Override
|
|
|
- public Result findByQuestionGroupId(Long questionGroupId, Integer type) {
|
|
|
- QuestionGroupEntity groupEntity = questionGroupService.findById(questionGroupId);
|
|
|
-
|
|
|
- if (groupEntity == null) {
|
|
|
- return Result.failure(2001, "没有数据");
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public Result findByQuestionGroupId(Long questionGroupId, Integer type){
|
|
|
+ QuestionGroupEntity groupEntity = questionGroupService.findById(questionGroupId);
|
|
|
|
|
|
- List<CountAnswerVo> answers = entityMapper.findByQuestionGroupId(questionGroupId);
|
|
|
+ if (groupEntity == null) {
|
|
|
+ return Result.failure(2001, "没有数据");
|
|
|
+ }
|
|
|
|
|
|
- if (answers.size() == 0) {
|
|
|
- return Result.failure(2001, "没有数据");
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- CountAnswerVo vo = answers.get(0);
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("name", groupEntity.getName());
|
|
|
- result.put("createTime", vo.getCreateTime());
|
|
|
-
|
|
|
-
|
|
|
- if (type == 1) {
|
|
|
- result.put("answer", answers);
|
|
|
- } else {
|
|
|
- // 返回百分比
|
|
|
- answers.forEach(p -> {
|
|
|
+ List<CountAnswerVo> answers = entityMapper.findByQuestionGroupId(questionGroupId);
|
|
|
|
|
|
- // 分母
|
|
|
- Double a = p.getA()== null ? 0 : p.getA();
|
|
|
- Double b = p.getB()== null ? 0 : p.getB();
|
|
|
- Double c = p.getC()== null ? 0 : p.getC();
|
|
|
- Double d = p.getD()== null ? 0 : p.getD();
|
|
|
- Double e = p.getE()== null ? 0 : p.getE();
|
|
|
- Double f = p.getF()== null ? 0 : p.getF();
|
|
|
- Double g = p.getG()== null ? 0 : p.getG();
|
|
|
- Double sum = a + b + c + d + e + f + g;
|
|
|
+ if (answers.size() == 0) {
|
|
|
+ return Result.failure(2001, "没有数据");
|
|
|
+ }
|
|
|
|
|
|
-// a = NumberUtil.div(a, sum, 4);
|
|
|
-// b = NumberUtil.div(b, sum, 4);
|
|
|
-// c = NumberUtil.div(c, sum, 4);
|
|
|
-// d = NumberUtil.div(d, sum, 4);
|
|
|
-// e = NumberUtil.div(e, sum, 4);
|
|
|
-// f = NumberUtil.div(f, sum, 4);
|
|
|
-// g = NumberUtil.div(g, sum, 4);
|
|
|
|
|
|
-// p.setA(a == 0 ? null : a);
|
|
|
-// p.setA(b == 0 ? null : b);
|
|
|
-// p.setA(c == 0 ? null : c);
|
|
|
-// p.setA(d == 0 ? null : d);
|
|
|
-// p.setA(e == 0 ? null : e);
|
|
|
-// p.setA(f == 0 ? null : f);
|
|
|
-// p.setA(g == 0 ? null : g);
|
|
|
+ CountAnswerVo vo = answers.get(0);
|
|
|
+ Map<Object, Object> result = new HashMap<>();
|
|
|
+ result.put("name", groupEntity.getName());
|
|
|
+ result.put("createTime", vo.getCreateTime());
|
|
|
|
|
|
-// p.setA(a);
|
|
|
-// p.setB(b);
|
|
|
-// p.setC(c);
|
|
|
-// p.setD(d);
|
|
|
-// p.setE(e);
|
|
|
-// p.setF(f);
|
|
|
-// p.setG(g);
|
|
|
+ Map<String, List<CountAnswerVo> > resCount = new HashMap<>();
|
|
|
|
|
|
+ // 去重
|
|
|
+ Set<Integer> nums = answers.stream().map(CountAnswerVo::getNum).collect(Collectors.toSet());
|
|
|
|
|
|
+ for (Integer num : nums){
|
|
|
+ List<CountAnswerVo> list = new ArrayList<>();
|
|
|
+ for (CountAnswerVo answer : answers) {
|
|
|
+ if (num.equals(answer.getNum())){
|
|
|
+ CountAnswerVo vo1 = new CountAnswerVo();
|
|
|
+ BeanUtils.copyProperties(answer, vo1);
|
|
|
+ list.add(vo1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resCount.put(num.toString(), list);
|
|
|
+ }
|
|
|
|
|
|
- p.setA(NumberUtil.div(a, sum, 4));
|
|
|
- p.setB(NumberUtil.div(b, sum, 4));
|
|
|
- p.setC(NumberUtil.div(c, sum, 4));
|
|
|
- p.setD(NumberUtil.div(d, sum, 4));
|
|
|
- p.setE(NumberUtil.div(e, sum, 4));
|
|
|
- p.setF(NumberUtil.div(f, sum, 4));
|
|
|
- p.setG(NumberUtil.div(g, sum, 4));
|
|
|
|
|
|
- });
|
|
|
-
|
|
|
- result.put("answer", answers);
|
|
|
+ if (type == 1) {
|
|
|
+ result.put("answer", resCount);
|
|
|
|
|
|
+ } else {
|
|
|
+ Map<String, Object> percent = percent(resCount);
|
|
|
+ result.put("answer", percent);
|
|
|
+ }
|
|
|
|
|
|
+ return Result.success(result);
|
|
|
}
|
|
|
|
|
|
- return Result.success(result);
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
+ private Map<String , Object> percent(Map<String, List<CountAnswerVo> > resCount){
|
|
|
+
|
|
|
+ Map<String, Object > res = new HashMap<>();
|
|
|
+ for (Map.Entry<String, List<CountAnswerVo>> m : resCount.entrySet()){
|
|
|
+ List<CountAnswerVo> value = m.getValue();
|
|
|
+ // 每个题号回答的总数
|
|
|
+ List<JSONObject> list = new ArrayList<>();
|
|
|
+ long sum = value.stream().mapToInt(CountAnswerVo::getCount).summaryStatistics().getSum();
|
|
|
+ for (CountAnswerVo vo : value) {
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("answer", vo.getAnswer());
|
|
|
+ Integer count = vo.getCount();
|
|
|
+ json.put("count", NumberUtil.div(count+"", sum+"", 4));
|
|
|
+ list.add(json);
|
|
|
+ }
|
|
|
+ res.put(m.getKey(), list);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
}
|