对时间复杂度进行了尝试,发现并没有任何懒用
This commit is contained in:
parent
2c242f4408
commit
37d0f4b33f
|
@ -1,92 +1,100 @@
|
|||
package org.jeecg.modules.demo.superlilu.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import org.jeecg.modules.demo.superlilu.entity.CetEnglish;
|
||||
import org.jeecg.modules.demo.superlilu.mapper.CetEnglishMapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.jeecg.modules.demo.superlilu.service.ICetEnglishService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.jeecg.modules.demo.superlilu.entity.CetEnglish;
|
||||
import org.jeecg.modules.demo.superlilu.mapper.CetEnglishMapper;
|
||||
import org.jeecg.common.api.vo.Result;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Description: 四六级英语
|
||||
* @Author: jeecg-boot
|
||||
* @Date: 2023-10-27
|
||||
* @Version: V1.0
|
||||
*/
|
||||
@Service
|
||||
public class CetEnglishServiceImpl extends ServiceImpl<CetEnglishMapper, CetEnglish> implements ICetEnglishService {
|
||||
|
||||
/**
|
||||
* {
|
||||
* "college": "xxx",//全部则留空
|
||||
* "batch": "2021-06-01",//全部则留空
|
||||
* "level": "英语四级",//全部则留空
|
||||
* "cultivationlevel": "本科",//全部则留空
|
||||
* "examgrade": "大一"//全部则留空
|
||||
* }
|
||||
*
|
||||
* @param webData webData
|
||||
* @return Result<?>
|
||||
*/
|
||||
@Autowired
|
||||
private CetEnglishMapper cetEnglishMapper;
|
||||
|
||||
@Override
|
||||
public Result<JSONObject> searchCetData(JSONObject webData) {
|
||||
QueryWrapper<CetEnglish> queryWrapper = new QueryWrapper<>();
|
||||
// 定义要匹配的字段
|
||||
String[] fields = {"college", "batch", "level", "cultivationlevel", "examgrade"};
|
||||
|
||||
for (String field : fields) {
|
||||
if (webData.containsKey(field)) {
|
||||
Object fieldValue = webData.get(field);
|
||||
if (fieldValue instanceof List) {
|
||||
if (((List<?>) fieldValue).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
// 如果值是列表,则使用in条件
|
||||
queryWrapper.in(field, (List<?>) fieldValue);
|
||||
} else {
|
||||
if (fieldValue == null || "".equals(fieldValue)) {
|
||||
continue;
|
||||
}
|
||||
queryWrapper.in(field, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
// 构建查询条件
|
||||
if (webData.containsKey("college") && webData.getJSONArray("college").size() > 0) {
|
||||
queryWrapper.in("college", webData.getJSONArray("college"));
|
||||
}
|
||||
// 如果queryWrapper为空,则查询全部
|
||||
List<CetEnglish> list = list(queryWrapper);
|
||||
//基于学院college分组
|
||||
Map<String, List<CetEnglish>> collegeMap = list.stream().collect(Collectors.groupingBy(CetEnglish::getCollege));
|
||||
// 基于学院college分组后的map再基于批次batch分组
|
||||
Map<String, Map<String, List<CetEnglish>>> collegeBatchMap = collegeMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().collect(Collectors.groupingBy(CetEnglish::getBatch))));
|
||||
//转为JSON,并计算每个学院的每个批次的报名人数和(isPassed=1)通过人数及通过率,存入JSONObject
|
||||
if (webData.containsKey("batch") && webData.getJSONArray("batch").size() > 0) {
|
||||
queryWrapper.in("batch", webData.getJSONArray("batch"));
|
||||
}
|
||||
if (webData.containsKey("level") && !webData.getString("level").isEmpty()) {
|
||||
queryWrapper.eq("level", webData.getString("level"));
|
||||
}
|
||||
if (webData.containsKey("cultivationlevel") && !webData.getString("cultivationlevel").isEmpty()) {
|
||||
queryWrapper.eq("cultivationlevel", webData.getString("cultivationlevel"));
|
||||
}
|
||||
if (webData.containsKey("examgrade") && !webData.getString("examgrade").isEmpty()) {
|
||||
queryWrapper.eq("examgrade", webData.getString("examgrade"));
|
||||
}
|
||||
|
||||
// 查询满足条件的数据
|
||||
List<CetEnglish> cetDataList = cetEnglishMapper.selectList(queryWrapper);
|
||||
|
||||
// 根据学院和考试日期进行分组统计报名人数、通过人数和通过率
|
||||
Map<String, Map<String, List<CetEnglish>>> groupedData = cetDataList.stream()
|
||||
.collect(Collectors.groupingBy(CetEnglish::getCollege,
|
||||
Collectors.groupingBy(CetEnglish::getBatch)));
|
||||
|
||||
// 构建结果数据
|
||||
JSONObject data = new JSONObject();
|
||||
for (Map.Entry<String, Map<String, List<CetEnglish>>> collegeBatchEntry : collegeBatchMap.entrySet()) {
|
||||
JSONObject collegeBatchData = new JSONObject();
|
||||
for (Map.Entry<String, List<CetEnglish>> batchEntry : collegeBatchEntry.getValue().entrySet()) {
|
||||
JSONObject batchData = new JSONObject();
|
||||
double total = batchEntry.getValue().size();
|
||||
double passed = 0;
|
||||
for (CetEnglish cetEnglish : batchEntry.getValue()) {
|
||||
if (cetEnglish.getIspassed() == 1) {
|
||||
passed++;
|
||||
}
|
||||
}
|
||||
batchData.put("total", total);
|
||||
batchData.put("passed", passed);
|
||||
batchData.put("passRate", Math.round(passed / total * 10000) / 100.0 + "%");
|
||||
collegeBatchData.put(batchEntry.getKey(), batchData);
|
||||
JSONArray resultList = new JSONArray();
|
||||
int totalEntries = 0;
|
||||
int totalPass = 0;
|
||||
|
||||
for (Map.Entry<String, Map<String, List<CetEnglish>>> collegeEntry : groupedData.entrySet()) {
|
||||
String college = collegeEntry.getKey();
|
||||
Map<String, List<CetEnglish>> gradeData = collegeEntry.getValue();
|
||||
|
||||
for (Map.Entry<String, List<CetEnglish>> gradeEntry : gradeData.entrySet()) {
|
||||
String batch = gradeEntry.getKey();
|
||||
List<CetEnglish> gradeCetData = gradeEntry.getValue();
|
||||
|
||||
int entries = gradeCetData.size();
|
||||
int passCount = (int) gradeCetData.stream().filter(cet -> cet.getIspassed() == 1).count();
|
||||
double passRate = (entries > 0) ? ((double) passCount / entries) * 100 : 0;
|
||||
|
||||
// 构建单条结果数据
|
||||
JSONObject resultEntry = new JSONObject();
|
||||
resultEntry.put("college", college);
|
||||
resultEntry.put("batch", batch);
|
||||
resultEntry.put("entries", entries);
|
||||
resultEntry.put("passCount", passCount);
|
||||
resultEntry.put("passRate", passRate);
|
||||
|
||||
resultList.add(resultEntry);
|
||||
|
||||
// 更新总报名人数和总通过人数
|
||||
totalEntries += entries;
|
||||
totalPass += passCount;
|
||||
}
|
||||
data.put(collegeBatchEntry.getKey(), collegeBatchData);
|
||||
}
|
||||
//返回结果
|
||||
|
||||
// 计算总通过率
|
||||
double passRate = (totalEntries > 0) ? ((double) totalPass / totalEntries) * 100 : 0;
|
||||
|
||||
// 构建返回结果
|
||||
data.put("resultList", resultList);
|
||||
data.put("totalEntries", totalEntries);
|
||||
data.put("totalPass", totalPass);
|
||||
data.put("passRate", passRate);
|
||||
|
||||
// 返回结果
|
||||
return Result.OK(data);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue