|
@@ -0,0 +1,242 @@
|
|
|
+package com.ynfy.buss.exam.chouqian.service.impl;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.ynfy.buss.exam.chouqian.entity.*;
|
|
|
+import com.ynfy.buss.exam.chouqian.mapper.*;
|
|
|
+import com.ynfy.buss.exam.chouqian.service.ICqXxxqService;
|
|
|
+import org.jeecg.modules.system.entity.SysDepart;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: exam
|
|
|
+ * @Author: jeecg-boot
|
|
|
+ * @Date: 2023-02-23
|
|
|
+ * @Version: V1.0
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CqXxxqServiceImpl extends ServiceImpl<CqXxxqMapper, CqXxxq> implements ICqXxxqService {
|
|
|
+ @Autowired
|
|
|
+ private CqXxxqMapper cqXxxqMapper ;
|
|
|
+ @Autowired
|
|
|
+ private CqYcqxxMapper cqYcqxxMapper ;
|
|
|
+ @Autowired
|
|
|
+ private CqKsxxMapper cqKsxxMapper ;
|
|
|
+ @Autowired
|
|
|
+ private CqCqksMapper cqCqksMapper ;
|
|
|
+ @Autowired
|
|
|
+ private CqCqbjxxMapper cqCqbjxxMapper ;
|
|
|
+ @Autowired
|
|
|
+
|
|
|
+ //指定区间内抽取随机数
|
|
|
+ public static int getRandomNumber(int min, int max) {
|
|
|
+ if (min > max) {
|
|
|
+ throw new IllegalArgumentException("最小值不能大于最大值");
|
|
|
+ }
|
|
|
+ Random random = new Random();
|
|
|
+ return random.nextInt((max - min) + 1) + min;
|
|
|
+ }
|
|
|
+ // 抽学校
|
|
|
+ public static List<CqXxxq> chouschool(List<CqXxxq> candidates, int numberOfWinners) {
|
|
|
+ if (candidates == null || candidates.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("候选学校列表不能为空");
|
|
|
+ }
|
|
|
+ if (numberOfWinners <= 0 || numberOfWinners > candidates.size()) {
|
|
|
+ throw new IllegalArgumentException("中签学校必须大于0且不超过候选学校数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ Random random = new Random();
|
|
|
+ Set<Integer> selectedIndices = new HashSet<>(); // 用于存储已选中的索引
|
|
|
+ List<CqXxxq> winners = new ArrayList<>(); // 存储中签者
|
|
|
+
|
|
|
+ while (winners.size() < numberOfWinners) {
|
|
|
+ int randomIndex = random.nextInt(candidates.size()); // 生成随机索引
|
|
|
+ if (selectedIndices.add(randomIndex)) { // 如果索引未被选中过
|
|
|
+ winners.add(candidates.get(randomIndex)); // 添加到中签者列表
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return winners;
|
|
|
+ }// 抽学生
|
|
|
+ public static List<CqKsxx> choustudent(List<CqKsxx> candidates, int numberOfWinners) {
|
|
|
+ if (candidates == null || candidates.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("候选学校列表不能为空");
|
|
|
+ }
|
|
|
+ if (numberOfWinners <= 0 || numberOfWinners > candidates.size()) {
|
|
|
+ throw new IllegalArgumentException("中签学校必须大于0且不超过候选学校数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ Random random = new Random();
|
|
|
+ Set<Integer> selectedIndices = new HashSet<>(); // 用于存储已选中的索引
|
|
|
+ List<CqKsxx> winners = new ArrayList<>(); // 存储中签者
|
|
|
+
|
|
|
+ while (winners.size() < numberOfWinners) {
|
|
|
+ int randomIndex = random.nextInt(candidates.size()); // 生成随机索引
|
|
|
+ if (selectedIndices.add(randomIndex)) { // 如果索引未被选中过
|
|
|
+ winners.add(candidates.get(randomIndex)); // 添加到中签者列表
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return winners;
|
|
|
+ }
|
|
|
+ // 从集合中抽取多个学校
|
|
|
+ public static List<String> chouclass(List<String> candidates, int numberOfWinners) {
|
|
|
+ if (candidates == null || candidates.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("候选列表不能为空");
|
|
|
+ }
|
|
|
+ if (numberOfWinners <= 0 || numberOfWinners > candidates.size()) {
|
|
|
+ throw new IllegalArgumentException("中签人数必须大于0且不超过候选人数");
|
|
|
+ }
|
|
|
+
|
|
|
+ Random random = new Random();
|
|
|
+ Set<Integer> selectedIndices = new HashSet<>(); // 用于存储已选中的索引
|
|
|
+ List<String> winners = new ArrayList<>(); // 存储中签者
|
|
|
+
|
|
|
+ while (winners.size() < numberOfWinners) {
|
|
|
+ int randomIndex = random.nextInt(candidates.size()); // 生成随机索引
|
|
|
+ if (selectedIndices.add(randomIndex)) { // 如果索引未被选中过
|
|
|
+ winners.add(candidates.get(randomIndex)); // 添加到中签者列表
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return winners;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public List<CqXxxq> cqxx(CqXxxq cqXxxq) {
|
|
|
+ int studentamount=Integer.parseInt(cqXxxq.getStudentamount());
|
|
|
+ //根据县区名称、年级、层次获取符合的学校
|
|
|
+ List<CqXxxq> cqXxxqList=cqXxxqMapper.cqxxxq(cqXxxq);
|
|
|
+ System.out.println("显示要抽签的学校。");
|
|
|
+ System.out.println(cqXxxqList);
|
|
|
+ System.out.println(cqXxxqList.size());
|
|
|
+ List<CqXxxq> cqXxList=new ArrayList<>();//被抽签到到学校
|
|
|
+ cqXxList=chouschool(cqXxxqList, Integer.parseInt(cqXxxq.getSchooamount()));
|
|
|
+ System.out.println("抽取到的学校"+cqXxList);
|
|
|
+ CqYcqxx cqYcqxx=new CqYcqxx();//添加抽取记录的对象
|
|
|
+
|
|
|
+ //检查当前区是否已经抽签
|
|
|
+ LambdaQueryWrapper<CqYcqxx> query = new LambdaQueryWrapper<>();
|
|
|
+ query.eq(CqYcqxx::getDistrictname, cqXxxq.getDistrictname());
|
|
|
+ Long count = cqYcqxxMapper.selectCount(query);
|
|
|
+ System.out.println("显示count:"+count);
|
|
|
+ if (Integer.parseInt(count+"") >0) {
|
|
|
+ System.out.println("该区已经有抽签记录无法重复抽签");
|
|
|
+ }else if(Integer.parseInt(count+"") ==0){
|
|
|
+ //保存被抽签的学校
|
|
|
+ for (CqXxxq xxxq : cqXxList) {
|
|
|
+ cqYcqxx.setId(null);
|
|
|
+ cqYcqxx.setDistrictname(xxxq.getDistrictname());
|
|
|
+ cqYcqxx.setSchoolname(xxxq.getSchoolname());
|
|
|
+ cqYcqxx.setNianji(xxxq.getNianji());
|
|
|
+ cqYcqxx.setEstate(xxxq.getEstate());
|
|
|
+ //获取抽签学校的考生数
|
|
|
+ LambdaQueryWrapper<CqKsxx> query1 = new LambdaQueryWrapper<>();
|
|
|
+ query1.eq(CqKsxx::getDistrictname, xxxq.getDistrictname())
|
|
|
+ .eq(CqKsxx::getSchoolname, xxxq.getSchoolname());
|
|
|
+ cqYcqxx.setSchoolpopulation(String.valueOf(cqKsxxMapper.selectCount(query1)));
|
|
|
+ cqYcqxxMapper.insert(cqYcqxx);
|
|
|
+ //抽签班级
|
|
|
+ this.cqbanji(cqYcqxx,xxxq,studentamount);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ System.out.println("异常");
|
|
|
+ }
|
|
|
+ //正常情况抽取完成,开始抽取学校不足指定人数的规则
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean cqbanji(CqYcqxx cqYcqxx,CqXxxq xxxq,int studentamount) {
|
|
|
+ //学校抽签完毕抽取学校班级
|
|
|
+ //查询当前学校有多少个班级
|
|
|
+ List<String> bjlist=cqKsxxMapper.chaclassname(xxxq.getSchoolname());
|
|
|
+ //抽签班级
|
|
|
+ System.out.println(bjlist);
|
|
|
+ List<String> chouqianclass=chouclass(bjlist,1);
|
|
|
+ CqCqbjxx cqCqbjxx= new CqCqbjxx();
|
|
|
+ cqCqbjxx.setYcqxxId(cqYcqxx.getId());
|
|
|
+ cqCqbjxx.setClassname(chouqianclass.get(0));
|
|
|
+ cqCqbjxxMapper.insert(cqCqbjxx);
|
|
|
+ //班级抽取完毕开始抽取学生
|
|
|
+ //判断抽到的学校人数是否大于指定人数
|
|
|
+ if(Integer.parseInt(cqYcqxx.getSchoolpopulation())>studentamount){
|
|
|
+ //获取班级考试人数
|
|
|
+ System.out.println("开始抽人数");
|
|
|
+ //获取考生详情
|
|
|
+ List<CqKsxx> cqKsxxList =cqKsxxMapper.chacqksxx(xxxq.getSchoolname(),cqCqbjxx.getClassname());
|
|
|
+ System.out.println("抽签班级学生详情"+cqKsxxList);
|
|
|
+ System.out.println("抽签班级学生数量"+cqKsxxList.size());
|
|
|
+ List<CqCqks> cqCqksList=new ArrayList<>();//抽中学生保存集合
|
|
|
+ List<CqKsxx> cqKsxxListcz=new ArrayList<>();//抽中学生集合
|
|
|
+ //判断班级是否抽签
|
|
|
+ LambdaQueryWrapper<CqCqks> query = new LambdaQueryWrapper<>();
|
|
|
+ query.eq(CqCqks::getSchoolname, xxxq.getSchoolname())
|
|
|
+ .eq(CqCqks::getClassname, cqCqbjxx.getClassname());
|
|
|
+ Long count = cqCqksMapper.selectCount(query);
|
|
|
+ if (Integer.parseInt(count+"")==0){
|
|
|
+ //判断班级人数是否符合抽签人数
|
|
|
+ if(cqKsxxList.size()>studentamount){
|
|
|
+ System.out.println("大于45,随机仇人");
|
|
|
+ //班级人数大于抽签人数时随机抽取考试人员
|
|
|
+ cqKsxxListcz= choustudent(cqKsxxList,studentamount);
|
|
|
+ System.out.println(cqKsxxListcz);
|
|
|
+ System.out.println(cqKsxxListcz.size());
|
|
|
+ cqCqksList=this.cqbanji(cqKsxxListcz);
|
|
|
+ for (CqCqks cqCqks : cqCqksList) {
|
|
|
+ cqCqks.setId(null);
|
|
|
+ cqCqksMapper.insert(cqCqks);
|
|
|
+ }
|
|
|
+ }else if(cqKsxxList.size()==studentamount){
|
|
|
+ System.out.println("=45");
|
|
|
+ cqCqksList=this.cqbanji(cqKsxxList);
|
|
|
+ for (CqCqks cqCqks : cqCqksList) {
|
|
|
+ cqCqks.setId(null);
|
|
|
+ cqCqksMapper.insert(cqCqks);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ System.out.println("小于45");
|
|
|
+ //若抽中班级的学生数量少于当前学段需要参加考试的学生数量,当前班级人员全部参加
|
|
|
+ cqCqksList=this.cqbanji(cqKsxxList);
|
|
|
+ for (CqCqks cqCqks : cqCqksList) {
|
|
|
+ cqCqks.setId(null);
|
|
|
+ cqCqksMapper.insert(cqCqks);
|
|
|
+ }
|
|
|
+ //在本校中重新抽班级补人
|
|
|
+
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ System.out.println("该班级以抽签");
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ System.out.println("学校人数不足抽签人数全部参加,都抽签完毕后,去别的学校里补");
|
|
|
+ }
|
|
|
+ //
|
|
|
+ //
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ //循环赋值
|
|
|
+ public List<CqCqks> cqbanji(List<CqKsxx> cqKsxxList) {
|
|
|
+ List<CqCqks> cqCqksList=new ArrayList<>();
|
|
|
+ CqCqks cqCqks=new CqCqks();
|
|
|
+ for (CqKsxx cqKsxx : cqKsxxList) {
|
|
|
+ cqCqks.setUsername(cqKsxx.getUsername());
|
|
|
+ cqCqks.setRealname(cqKsxx.getRealname());
|
|
|
+ cqCqks.setDistrictname(cqKsxx.getDistrictname());
|
|
|
+ cqCqks.setSchoolname(cqKsxx.getSchoolname());
|
|
|
+ cqCqks.setNianji(cqKsxx.getNianji());
|
|
|
+ cqCqks.setClassname(cqKsxx.getClassname());
|
|
|
+ cqCqksList.add(cqCqks);
|
|
|
+ cqCqks=new CqCqks();
|
|
|
+ }
|
|
|
+ return cqCqksList;
|
|
|
+
|
|
|
+ }
|
|
|
+}
|