forked from Big-Data-Lab/CET-cmd-2.0
修改查询接口,并对数据进行排序
This commit is contained in:
parent
7417bfac04
commit
8d695e80b2
|
@ -16,6 +16,8 @@ import org.springframework.cache.annotation.Cacheable;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
|
@ -64,15 +66,22 @@ public class CenterController {
|
|||
}
|
||||
|
||||
@GetMapping("/getRateByBatch")
|
||||
public Result<JSONObject> getRateByBatch(@RequestParam("batch") Date batch, @RequestParam("level") String level) {
|
||||
public Result<JSONObject> getRateByBatch(@RequestParam("batch") String batch, @RequestParam("level") String level) {
|
||||
//四六级分开查询
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date date;
|
||||
try {
|
||||
date= dateFormat.parse(batch);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (level.equals("cet4")) {
|
||||
Cet_4 cet = new Cet_4();
|
||||
cet.setBatch(batch);
|
||||
cet.setBatch(date);
|
||||
return cet4Service.getRate(cet);
|
||||
} else if (level.equals("cet6")) {
|
||||
Cet_6 cet = new Cet_6();
|
||||
cet.setBatch(batch);
|
||||
cet.setBatch(date);
|
||||
return cet6Service.getRate(cet);
|
||||
} else {
|
||||
return null;
|
||||
|
@ -80,7 +89,7 @@ public class CenterController {
|
|||
}
|
||||
//根据batch/college是否为空判断两种查询方式
|
||||
|
||||
@GetMapping("/getRateByCollege")
|
||||
@PostMapping("/getRateByCollege")
|
||||
public Result<JSONObject> getRateByCollege(@RequestParam("college") String college, @RequestParam("level") String level) {
|
||||
if (level.equals("cet4")) {
|
||||
Cet_4 cet = new Cet_4();
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
package org.jeecg.modules.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
||||
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
|
@ -14,9 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
@ -31,7 +29,7 @@ public class Cet_4ServiceImpl extends ServiceImpl<Cet_4Mapper, Cet_4> implements
|
|||
Cet_4Mapper cet4Mapper;
|
||||
|
||||
public Result<JSONObject> getRate(Cet_4 cet) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
|
||||
//select count(*) from cet_4 where college ='?' and result>=425
|
||||
LambdaQueryWrapper<Cet_4> queryWrapper = new LambdaQueryWrapper<>();
|
||||
//构造查询条件
|
||||
|
@ -39,13 +37,12 @@ public class Cet_4ServiceImpl extends ServiceImpl<Cet_4Mapper, Cet_4> implements
|
|||
//根据学院查人数
|
||||
queryWrapper.eq(Cet_4::getCollege, cet.getCollege()).ge(Cet_4::getResult, 425);
|
||||
List<Cet_4> result = cet4Mapper.selectList(queryWrapper);
|
||||
Map<String, List<Cet_4>> collect = result.stream().collect(Collectors.groupingBy(Cet_4::getCollege));
|
||||
for (Map.Entry<String, List<Cet_4>> entry : collect.entrySet()) {
|
||||
double sum = (double) getNum(cet);
|
||||
Map<Date, List<Cet_4>> collect = result.stream().collect(Collectors.groupingBy(Cet_4::getBatch));
|
||||
for (Map.Entry<Date, List<Cet_4>> entry : collect.entrySet()) {
|
||||
double sum = (double) getNumByCollege(cet, entry.getKey());
|
||||
double size = entry.getValue().size();
|
||||
double passRate = sum / size;
|
||||
jsonObject.put(String.valueOf(entry.getKey()),
|
||||
passRate);
|
||||
double passRate = size / sum;
|
||||
jsonObject.put(String.valueOf(entry.getKey()), passRate);
|
||||
}
|
||||
|
||||
return Result.OK(jsonObject);
|
||||
|
@ -54,31 +51,53 @@ public class Cet_4ServiceImpl extends ServiceImpl<Cet_4Mapper, Cet_4> implements
|
|||
//根据批次查人数
|
||||
queryWrapper.eq(Cet_4::getBatch, cet.getBatch()).ge(Cet_4::getResult, 425);
|
||||
List<Cet_4> result = cet4Mapper.selectList(queryWrapper);
|
||||
Map<Date, List<Cet_4>> collect = result.stream().collect(Collectors.groupingBy(Cet_4::getBatch));
|
||||
for (Map.Entry<Date, List<Cet_4>> entry : collect.entrySet()) {
|
||||
double sum = (double) getNum(cet);
|
||||
//通过批次
|
||||
Map<String, List<Cet_4>> collect = result.stream().collect(Collectors.groupingBy(Cet_4::getCollege));
|
||||
for (Map.Entry<String, List<Cet_4>> entry : collect.entrySet()) {
|
||||
double sum = (double) getNumByBatch(cet, entry.getKey());
|
||||
|
||||
double size = entry.getValue().size();
|
||||
double passRate = sum / size;
|
||||
jsonObject.put(String.valueOf(entry.getKey()),
|
||||
passRate);
|
||||
|
||||
double passRate = size / sum;
|
||||
// 定义格式化规则,保留四位小数
|
||||
DecimalFormat decimalFormat = new DecimalFormat("#.####");
|
||||
String rate = decimalFormat.format(passRate);
|
||||
double res = Double.parseDouble(rate);
|
||||
|
||||
jsonObject.put(String.valueOf(entry.getKey()), res);
|
||||
|
||||
|
||||
}
|
||||
//对jsonObject的值进行排序
|
||||
|
||||
List<Map.Entry<String, Object>> list = new ArrayList<>(jsonObject.entrySet());
|
||||
|
||||
Collections.sort(list, (o1, o2) -> (double) o1.getValue() > (double) o2.getValue() ? -1 : 1);
|
||||
|
||||
jsonObject.clear();
|
||||
|
||||
for (Map.Entry<String, Object> stringObjectEntry : list) {
|
||||
jsonObject.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
|
||||
|
||||
}
|
||||
|
||||
|
||||
return Result.OK(jsonObject);
|
||||
}
|
||||
else return null;
|
||||
} else return null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
private long getNum(Cet_4 cet) {
|
||||
private long getNumByCollege(Cet_4 cet, Date batch) {
|
||||
LambdaQueryWrapper<Cet_4> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (cet.getCollege() == null) {
|
||||
queryWrapper.eq(Cet_4::getBatch, cet.getBatch());
|
||||
}
|
||||
if (cet.getBatch() == null) {
|
||||
queryWrapper.eq(Cet_4::getCollege, cet.getCollege());
|
||||
}
|
||||
queryWrapper.eq(Cet_4::getCollege, cet.getCollege()).eq(Cet_4::getBatch, batch);
|
||||
return cet4Mapper.selectCount(queryWrapper);
|
||||
}
|
||||
|
||||
private long getNumByBatch(Cet_4 cet, String college) {
|
||||
LambdaQueryWrapper<Cet_4> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(Cet_4::getBatch, cet.getBatch()).eq(Cet_4::getCollege, college);
|
||||
return cet4Mapper.selectCount(queryWrapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue